Go Down

Topic: Wrong signal generated by using Servo library[Solved, but keep discussion] (Read 3050 times) previous topic - next topic

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.

johnwasser

You attach 11 servos and then write to 12 servos.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#2
Feb 15, 2013, 02:00 am Last Edit: Feb 15, 2013, 03:09 am by littleming Reason: 1
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");
}

oric_dan

That's what he means, I believe. Also, you should bracket your code using the # icon,
Code: [Select]
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 #?

oric_dan

#5
Feb 15, 2013, 08:56 pm Last Edit: Feb 15, 2013, 09:00 pm by oric_dan Reason: 1
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

#7
Feb 19, 2013, 06:16 pm Last Edit: Feb 19, 2013, 06:20 pm by littleming Reason: 1
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.
www.bajdi.com


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.


retrolefty

Quote
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


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.

Quote
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

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


Quote
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

https://www.youtube.com/watch?v=3kSbl25b5KY

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 :) 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:

http://www.youtube.com/watch?v=VT15Y_FbyhE
www.bajdi.com

Go Up