Pages: [1] 2   Go Down
Author Topic: Wrong signal generated by using Servo library[Solved, but keep discussion]  (Read 2780 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 12, 2013, 11:05:43 pm by littleming » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You attach 11 servos and then write to 12 servos.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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");
}
« Last Edit: February 14, 2013, 09:09:52 pm by littleming » Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 159
Posts: 2916
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That's what he means, I believe. Also, you should bracket your code using the # icon,
Code:
void setup()
{
  Serial.begin(9600);
  for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
  {
    myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
  }
  Serial.println("Completed Setup");
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 159
Posts: 2916
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 15, 2013, 03:00:44 pm by oric_dan » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-lol
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 19, 2013, 12:20:06 pm by littleming » Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* 20130313_115314.jpg (2681.42 KB, 3264x2448 - viewed 22 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One more picture


* 20130313_115339.jpg (2443.9 KB, 3264x2448 - viewed 26 times.)
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged


Pages: [1] 2   Go Up
Jump to: