RC mode algorithm help required

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.

  1. There are two motors connect to a motor driver. Motors rotate at maximum speed at 255.
  2. 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?

You should print the values for Speed and Steer as you get them from pulseIn() so you know whether the problem is outside the Arduino or inside it.

If this was my project I would use different variables to hold the processed values so I could print them alongside the originals.

...R

Yes. I have them printed and it is the problem with the logic. Kindly check the below and let me know if there is a better way to implement this.

  Steer = Steer - 1500;
  int m0Value = (Speed - Steer - 1500) * 8 / 10;
  int m1Value = (Speed + Steer - 1500) * 8 / 10;

praveen_khm:
Yes. I have them printed and it is the problem with the logic. Kindly check the below and let me know if there is a better way to implement this.

Then post some examples of the inputs you get from pulseIn() and the corresponding calculated values that are incorrect.

Also post your complete program so we can see all the details. Usually the problem is in the piece that has not been posted (which is likely why you have not yet found it :slight_smile: )

...R