Latency controlling PWM/Servo FeatherWing with RC Radio

Hello, I am very new to Arduino, since Christmas. The last coding I did was in Fortran about thirty years ago.

I purchased an Adafruit Feather Basic Proto M0 and an accompanying 8 channel PWM wing. I plan on dropping it im an RC tank project I am building.

Problem: I have quite a bit of latency between the RC input and the servo output. Maybe 500 ms if I had to guess.

Any thoughts or advice would be greatly appreciated.

Here is the code I wrote:

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 110 // this is the ‘minimum’ pulse length count (out of 4096.
#define SERVOMAX 650 // this is the ‘maximum’ pulse length count (out of 4096.

int leftTrack = 0; //The servo channel on the WING output to the Left Track ESC
int rightTrack = 1; //The servo channel on the WING output to the Right Track ESC
int transMotor = 2; //The servo channel on the wing output to the turret transverse motor

int pinChan01 = 5; // Check what pin to use
int pinChan02 = 6; // Check what pin to use
int pinChan03 = 9; // Check what pin to use

void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60); //Analog servos run at ~60 Hz updates

pinMode(pinChan01, INPUT); // PWM input pin
pinMode(pinChan02, INPUT); // PWM input pin
pinMode(pinChan03, INPUT); // PWM input pin
delay(10);
}

void loop() {

int servoPWM = 1500;
int duration;

//Left Track ESC
duration = pulseIn(pinChan01, HIGH, 22000);
servoPWM = pwmConverter (duration);
pwm.setPWM(leftTrack, 0, servoPWM);
Serial.print ("Left Track ESC = ");
Serial.print (servoPWM);

//Right Track ESC
duration = pulseIn(pinChan02, HIGH, 22000);
servoPWM = pwmConverter (duration);
pwm.setPWM(rightTrack, 0, servoPWM);
Serial.print ("Right Track ESC = ");
Serial.print (servoPWM);

//Transverse motor ESC
duration = pulseIn(pinChan03, HIGH, 22000); //Fixed, thanks to Steve
servoPWM = pwmConverter (duration);
pwm.setPWM(transMotor, 0, servoPWM);
Serial.print ("Transverse ESC = ");
Serial.println (servoPWM);
}

int pwmConverter(int x){
int result;
result = map(x, 980, 2020, 110, 650);
return result;

}

Presumably you shouldn’t be using pinChan01 for both Left and Transverse and what you have posted won’t compile, being missing the odd ) and ; in places.

But I would guess most latency is caused by all those slow Serial.prints. Try increasing the Serial rate to something like Serial.begin(115200) and see what difference that makes.

If that’s not enough another trick would be to only write to the ESCs and print anything if ‘duration’ has changed since you last read it but that’s a rather larger change.

Steve

slipstick:
Presumably you shouldn't be using pinChan01 for both Left and Transverse and what you have posted won't compile, being missing the odd ) and ; in places.

But I would guess most latency is caused by all those slow Serial.prints. Try increasing the Serial rate to something like Serial.begin(115200) and see what difference that makes.

If that's not enough another trick would be to only write to the ESCs and print anything if 'duration' has changed since you last read it but that's a rather larger change.

Steve

I did, indeed, post the wrong version of my code, which had that error. Thanks for the help. I actually don't need the prints, so I will remove them.

Thanks again!