Hi,
Below is the program I have written to control two DC motors using a RC joystick
//------------------------------------------------------------ Code for RC inputs ---------------------------------------------------------
void RCMode() {
rcMotor.init();
int Speed = int(pulseIn(RCThrottlePin, HIGH, 30000)); // read throttle/left stick
int Steer = int(pulseIn(RCSteerPin, HIGH, 30000)); // read steering/right stick
if (Speed == 0) Speed = 1500; // if pulseIn times out (25mS) then set speed to stop
if (Steer == 0) Steer = 1500; // if pulseIn times out (25mS) then set steer to centre
if (abs(Speed - 1500) < RCdeadband) Speed = 1500; // if Speed input is within deadband set to 1500 (1500uS=center position for most servos)
if (abs(Steer - 1500) < RCdeadband) Steer = 1500; // if Steer input is within deadband set to 1500 (1500uS=center position for most servos)
Steer = Steer - 1500;
int m0Value = (Speed - Steer - 1500) * 8 / 10;
int m1Value = (Speed + Steer - 1500) * 8 / 10;
m0Value = m0Value < -255 ? -255 : m0Value; // joystick values sometimes reach -256 and 256
m0Value = m0Value > 255 ? 255 : m0Value;
m1Value = m1Value < -255 ? -255 : m1Value;
m1Value = m1Value > 255 ? 255 : m1Value;
rcMotor.setSpeeds(m0Value, m1Value); // move motors accordingly
}
Kindly ignore the pin mapping as it is connected appropriately and working. The issue is with the logic.
- There are two motors connect to a motor driver. Motors rotate at maximum speed at 255.
- When I move the joystick front left, front right, back left and back right, the values go upto 255 and motors rotate at full speed. However, when I move front, back, left and right, the maximum PWM values are 162,162 (+/- depending on the direction) instead of 255,255 (+/- depending on the direction).
Anybody has a correction in the above logic so that it reaches its full speed when driving forward, backward, left or right?