Go Down

Topic: Problem with RC "corruption" when using Servo.h library (Read 261 times) previous topic - next topic

alextrep

I am currently working on an Omni-wheeled robot and I am trying to control it with a Futaba T6EX remote and a R617FS receiver. I had already made a lot of progress and I was able to control everything without any difficulties until I added a servo on my robot...

I am using a futaba s3001 servo and controlling it is very easy with the Servo.h library but when I try to use it with the rest of my robot, it looks like it's messing up the RC readings. When I move the joystick, the wheels move but not like they should and the robot is just not controllable at all. Some wheels turn when they shouldn't and some stay inactive when they should turn.

I spent a lot of time on troubleshooting but I still have no clue on what is going on. Here are the things I know from troubleshooting:

-It is not a power issue, I completely disconnected the servo and ran the same code and that didn't fix it.

-It is probably not an error in my code since the problem only happens when I add the servo lines(which are very basic and simple) to my code.

-Including the library and creating the servo object seems fine(but I am not a 100% sure), it's when I add the line for setting the servo pin(myservo.attach) that the problem seems to appear.

-I tried pins 5, 9 and 10 (almost all other pins where already used, I use a UNO)

-I have isolated the problem down to only a few lines for the RC readings(I removed all the motor control lines and only kept one input from my RC receiver since they all work the same) here is the stripped down code :

Code: [Select]
#include <Servo.h>
Servo myservo;
int ch1;
int x;

void setup()
{
 Serial.begin(9600);
 pinMode(18, INPUT);
 myservo.attach(10);
}

void loop()
{
 ch1 = pulseIn(18, HIGH, 25000); // Read the pulse width
 x = map(ch1, 1320, 1650, -255, 255);
 
 Serial.println(x);
 Serial.println();
}


When my joystick(I'm using the right one) is in the zero position(when I am not touching it) and I read the Serial monitor, I get values of x ranging from 0 to -11 when I am not using the attach function. When I am using it, I get similar values but I sometimes get some drops ranging from -20 to -35... the mapping still seems to be working for other joystick positions but the values have some drops too.

I know it doesn't seem like a big difference in values and since the mapped values of other joystick positions still make sense, the robot shouldn't behave abnormally like it is and that is why I have no clue on what the problem is.

EDIT : I tried using the ServoTimer2 library instead but all it did was kind of shift the readings down so I get values of around -120 at the zero position and adjusting the mapping brings it back close to 0 but still with the -20/30 drops.

That is pretty much everything I know.

Alex

PaulS

I'd give the ServoTimer2 library a try. Servos are driven pulse pulse position modulation, which requires a timer to trigger the pulses at the proper time.

The Servo library's use to timer 1 may be interfering with pulseIn().
The art of getting good answers lies in asking good questions.

alextrep

That would make sense. I'll give it a try.

Thank you!

EDIT: It's even worse. Values when at zero now drop to around -140... It seems to have just shifted my RC values. When I adjust the mapping, I get similar results to the Servo.h ones.

Here is the code with the ServoTimer2 library:
Code: [Select]
#include <ServoTimer2.h>
ServoTimer2 myservo;
int ch1;
int x;

void setup()
{
Serial.begin(9600);
pinMode(18, INPUT);
myservo.attach(10);
}

void loop()
{
ch1 = pulseIn(18, HIGH, 25000); // Read the pulse width
x = map(ch1, 1320, 1650, -255, 255);

Serial.println(x);
Serial.println();
}

Go Up