can i make it so my raspberry pi differential drive mobile robot doesn't stop moving when sending digital signals to its motors in a for loop ?

1 view (last 30 days)
I have a differential drive mobile robot that is based on a raspberry pi, i control the right speed and left speed, i have converted those to linear and angular speed for better model for my control objective, i designed a controller for it and it generates a vector containing the optimal linear velocities and angular velocities.
after that, i designed a function called "move" to drive the robot, here it is;
function move(speed,turn,ti)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
EnaA = 12; In1A = 23; In2A = 24; EnaB = 18; In1B = 25; In2B = 8;
IR1 = 20; IR2 = 26;
rpi = evalin('base','rpi');%rpi = raspi;
configurePin(rpi,20,'DigitalInput');
configurePin(rpi,26,'DigitalInput');
% if readDigitalPin(rpi,20)
% writePWMDutyCycle(rpi,EnaA,0);
% writePWMDutyCycle(rpi,EnaB,0);
% return
% end
% while (readDigitalPin(rpi,20) || readDigitalPin(rpi,20)) == true
% leftSpeed = (1/0.066)*(speed - 0.0500*turn)
% rightSpeed = (1/0.066)*(speed + 0.0500*turn)
leftSpeed = speed - turn
rightSpeed = speed + turn
% leftSpeed = ((leftSpeed+ 15.1515)/(2*15.1515))*(2)- 1
% rightSpeed = ((rightSpeed+ 15.1515)/(2*15.1515))*(2)- 1
% turn = (2*(turn)/(2*0.05))-1
% leftSpeed = 2*(leftSpeed +15.1515)/(2*15.1515) - 1
% rightSpeed = 2*(rightSpeed +15.1515)/(2*15.1515) - 1
% rightSpeed = normalize(rightSpeed,'center','mean')
if leftSpeed > 1
leftSpeed = 1;
elseif leftSpeed < -1
leftSpeed = -1;
end
if rightSpeed > 1
rightSpeed = 1;
elseif rightSpeed < -1
rightSpeed = -1;
end
if leftSpeed > 0 && leftSpeed < 0.5
leftSpeed = 0.5;
elseif leftSpeed < 0 && leftSpeed > -0.5
leftSpeed = -0.5;
end
if rightSpeed > 0 && rightSpeed < 0.5
rightSpeed = 0.5;
elseif rightSpeed < 0 && rightSpeed > -0.5
rightSpeed = -0.5;
end
Stop(rpi ,In1A, In2A, In1B ,In2B)
writePWMDutyCycle(rpi,EnaA,abs(leftSpeed));
writePWMDutyCycle(rpi,EnaB,abs(rightSpeed));
if leftSpeed > 0
writeDigitalPin(rpi,In1A,1);
writeDigitalPin(rpi,In2A,0);
else
writeDigitalPin(rpi,In1A,0);
writeDigitalPin(rpi,In2A,1);
end
if rightSpeed > 0
writeDigitalPin(rpi,In1B,1);
writeDigitalPin(rpi,In2B,0);
else
writeDigitalPin(rpi,In1B,0);
writeDigitalPin(rpi,In2B,1);
end
% if nargin == 2
% continue
% else
pause(ti)
% N = 10*ti;
% if (readDigitalPin(rpi,20) || readDigitalPin(rpi,20)) == true
% pause(ti)
% else
% writePWMDutyCycle(rpi,EnaA,0);
% writePWMDutyCycle(rpi,EnaB,0);
% end
writePWMDutyCycle(rpi,EnaA,0);
writePWMDutyCycle(rpi,EnaB,0);
% end
the signal generated by the controller is discrete and containted between 1 and - 1, but when i use it like this
for i = 1: length(v_opt)
move(v_opt,w_opt,0.01) % v_opt and w_opt are the generated signals from the controller
end
i suspect that the problem is in pause function, but after many many attempts i still dont have a solution to this, please help.

Answers (0)

Categories

Find more on Raspberry Pi Hardware in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!