Wrong signal generated by using Servo library[Solved, but keep discussion]

Dear all,

I am modified and tested the program for controlling 12 servo motors today. My program is used "Servo" library. The board is Mega1280. But I found that the PWM mark space ratio are wrong from pin 3 and pin 11. The pulse wave period should be 20ms, but more than one pulse is shown inside a period.

My program is shown below:
#include <Servo.h>

Servo myservo[12];

#define SERVO_TO_PIN(x) (x+2)

int pos = 5; //Beginning Degree
int RotateDirection = 0;

void setup()
{
Serial.begin(9600);
for(int ServoNum = 0; ServoNum < 11; ServoNum++)
{
myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
}
Serial.println("Completed Setup");
}

void loop()
{
pos += RotateDirection;

if(pos >= 175)
{
RotateDirection = -10;
}

if(pos <=5)
{
RotateDirection = 10;
}

for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
{
myservo[ServoNum].write(pos);// tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.print("The angle of the servo");
Serial.print(ServoNum);
Serial.print(" is ");
Serial.print(myservo[ServoNum].read());
Serial.print('\n');
delay(500);
}
}

Please help, thank you very much.

You attach 11 servos and then write to 12 servos.

Do you mean I should modify to

void setup()
{
Serial.begin(9600);
for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
{
myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
}
Serial.println("Completed Setup");
}

That's what he means, I believe. Also, you should bracket your code using the # icon,

void setup()
{
  Serial.begin(9600);
  for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
  {
    myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
  }
  Serial.println("Completed Setup");
}

Sorry I am a beginner of the Arduino. Where should I put the bracket in my code by using #?

Just play with it. During a post, click on the # and quote [page thingie] and typewriter
icons, and insert some text, and then click "Preview", and see what they do.

Also, if you accidentally post, or want to edit or delete a msg, you can go to the msg
in the thread, and use the "Modify" and "Remove" buttons.

Okay. Thank you very much. I know how to use the "post function" next time.

For the program, I will test on the next Monday because I have not CRO at home. I hope everything is going be alright. XD

Hey guys, I found that the servo library cannot control more than 5 motors at the same time although I use Arduino Mega. It is because I tested the program for servo days, but the program cannot work. The problem is the generated PWM cannot fix 20ms for a period. When the pulse width increase the period also would increase if I control more than 5 servo motors. For less than 5 motors, everything is fine.

The solution is that I modify the library for changing each timer just control 6 motors only. I use 2 timer to control 12 servo motors. I guess the original library is control all motors by using one timer only, so the process time is not fast enough. After I modify the library, it is Okay.

I have 2 hexapods that use the standard Arduino servo library. One has 12 servos (2DOF legs) and uses an ATmega328P, the other has 18 (3DOF legs) servos and uses an ATmega2560. I have no problems controlling the servos with the Arduino servo library.

Bajdi:
I have 2 hexapods that use the standard Arduino servo library. One has 12 servos (2DOF legs) and uses an ATmega328P, the other has 18 (3DOF legs) servos and uses an ATmega2560. I have no problems controlling the servos with the Arduino servo library.

When I use the standard Arduino servo library, the result is shown in attach picture. You will see that if the mark is increasing, the period cannot hold in 20 ms.

One more picture

The problem is the generated PWM cannot fix 20ms for a period. When the pulse width increase the period also would increase if I control more than 5 servo motors. For less than 5 motors, everything is fine.

And why is that a problem? A servo is only concerned about and responds to the actual width of the pulse which is nominally in the range of 1 to 2 millisec. The fact that the period of the servo frame might vary somewhat from a nominal 20 millisec should not cause the servo any effect.

So what problem are you really trying to solve?

Lefty

retrolefty:
A servo is only concerned about and responds to the actual width of the pulse which is nominally in the range of 1 to 2 millisec. The fact that the period of the servo frame might vary somewhat from a nominal 20 millisec should not cause the servo any effect.

Really?? the servo motor only concern the mark range? The space ratio is increasing 2.5 ms. The whole period is 22.5 ms. May be I try it tomorrow and check the servo is it out of control or not.

So what problem are you really trying to solve?

My origin problem is solved previously. Now I just curious that what is the reason for Bajdi can control so many servo motors by using servo library. It is because I read the datasheet point out that the servo motor would out of control if the period is not approximately 20 ms

littleming:

retrolefty:
A servo is only concerned about and responds to the actual width of the pulse which is nominally in the range of 1 to 2 millisec. The fact that the period of the servo frame might vary somewhat from a nominal 20 millisec should not cause the servo any effect.

Really?? the servo motor only concern the mark range? The space ratio is increasing 2.5 ms. The whole period is 22.5 ms. May be I try it tomorrow and check the servo is it out of control or not.

Really. Servo frame rate is typically nominally in the 20-25 msec range. It's the pulse width (mark width) that carries the information that the servo responds to. Some servos can work with much shorter frame rates (I've read of 400Hz for some?) but that just means the servo can react faster to command changes, but still based on the nominal 1-2 millisec pulse width value.
Lefty

So what problem are you really trying to solve?

My origin problem is solved previously. Now I just curious that what is the reason for Bajdi can control so many servo motors by using servo library. It is because I read the datasheet point out that the servo motor would out of control if the period is not approximately 20 ms

This is my small hexapod with 2 DOF legs, it uses an ATmega328 to control the 12 servos of the legs. Another ATmega328 handles the wireless control (nRF24L01) and controls 1 servo (to sweep the ultrasonic sensor). The first ATmega measures the current drawn from the lipo battery via an analog sensor and measures the lipo voltage through a voltage divider. The second ATmega328 controls 3 RGB leds via transistors, measures the temperature of an LM2576 voltage regulator that powers the servos. It runs a PID loop that pwms a mosfet that powers a small fan to keep the regulator cool. Can't remember to which ATmega the ultrasonic sensor is hooked up :slight_smile: The 2 ATmegas talk to each other over hardware serial. I've used almost all the pins on both ATmegas. I now very little about programming and used the standard servo library. It uses a very simple walking gait, no fancy coding.

I'm currently working on a bigger beast with 18 standard sized metal gear servos (MG996R). The servos are controlled by an Arduino Mega 2560. I've spent the last weeks understanding inverse kinematics and managed to write a walking gait only recently. The Mega has no problems controlling 18 servos. And its calculating a lot of floating point math for the inverse kinematics. I made the following video yesterday:

Cool walking action, that is quite an accomplishment.

LOL, so it can't carry it's own battery and so has to drag it along? Good thing it's not an aircraft.

Lefty

retrolefty:
Cool walking action, that is quite an accomplishment.

LOL, so it can't carry it's own battery and so has to drag it along? Good thing it's not an aircraft.

Lefty

The servos have enough torque to carry the battery. But the chassis is to small to fit my big 3S 3000mAh Lipo battery and all the electronics. I'm planning to use a slightly bigger chassis so the electronics and battery all fit inside/on top of the chassis.

Thank you guys for help.

But one more question for Bajdi's robot.

I'm currently working on a bigger beast with 18 standard sized metal gear servos (MG996R).

MG996R is a power hungry servo motor. 18 servos should be need 18A for motion if all of the motors are moving at the same time. And I just found 1 DC to DC converter to provide power in your video. The standard output current for that converter is 8A . So, I think the hexapod may have some problem when it walking. But I am not sure. Please make a correction if I am wrong.

I'm using 3 Turnigy 8A UBECs. I first tried to power the 18 servos with 2 8A UBECs and blew them up...