SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By Tsaven
#146719
Hey guys, I've searched google for this, and poked around these boards but haven't found a solution. Most of the googled solutions I found were blamed on bad R/C equipment, which doesn't really apply to me.

tl;dr - 24 servos are twitching in a repeating pattern when they should be stationary. Hooked up to an Arduino Mega 2560

I'm building my own copy of the Chopsticks bot that I saw on Make's website, using a bunch of Polymorph, some chopsticks, and 24 mini-servos. I've got it all built and have done some of the basic programing to tweak the leg positions, but for some reason even though all the servos should be stationary, they all twitch in the same repeating pattern. Given the spindly nature of the robot, this eventually builds up a resonance and the thing flings itself about.

Video of the problem, you can see the twitchy pattern repeates every two seconds or so: http://youtu.be/BTn4Uj-rhs0

Can anyone give me some guidance on where the problem might be? I should clarify that the servos are being powered via an external 5v wall-wart, not from the Arduino's 5v line.

Code:
Code: Select all
#include <Servo.h>

int svt[24];               // used for calculations for servo positions

int svp[24];               // actual servo positions

int svc[72]={              //storing servo settings for different positions
  90,90,90,90,
  110,110,110,100,
  130,130,130,130,
  55,50,50,50,
  70,75,75,70,
  80,100,100,90,
  
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
  90,90,90,90,
};

Servo sv[24];                                       //create servo array

/*
All lables are front to back

26 27 28 29 - righ hip
0  1  2  3

30 31 32 33 - right thigh
4  5  6  7

34 35 36 37 - right knees
8  9  10 11

53 52 51 50 - left knee
12 13 14 15

49 48 47 46 - left thigh
16 17 18 19

45 44 43 42 - left hip
20 21 22 23
*/

void setup()
{
  for(int i=0;i<24;i++)                          //Loading center positions
  {
    svp[i]=svc[i];
  }
  
  for(int i=0; i<13; i++)                       //attaching right side servos
  {    
    sv[i].attach(26+i);
  }
  for(int i=12; i<24; i++)                     //attaching left side servos
  {
    sv[i].attach(30+i);
  }
  Serial.begin(9600);
}

void loop()
{
  for(int i=0;i<24;i++)                              //writing positions to servos
  {
    sv[i].write(svp[i]);
  }
 
}
By fll-freak
#146730
Do you have a scope to see if the pulse train from your processor is twitching? I suspect it is, but it would be nice to know for sure.

Guesses:
1) Servo library has a defect (bug) that is not updating the servos at a regular rate.
2) Interrupts on your processor preventing the Servo library from updating properly
3) Glitch in Servo library that occurs when you write the same value to the servo. If it was in the middle of updating that servo, you may have generated a runt pulse that causes the jitter. What if you write the value once and never update it again? If this solves the problem, then just keep track of the previous value you wrote to a servo and skip the new write if the old value is the same as what you want to write.
By Mee_n_Mac
#146741
falingtrea wrote:With 24 servos, maybe you are not updating them fast enough. I bet you don't have 24 hardware PWM circuits so you have to generate the pulses in software.
If he's using the Arduino servo library then that's a real good bet ! IIRC there was even a SFE tutorial that ran into the problem where too many servos using the library functionality were ;
- not updating at a constant 50 Hz rate
- the PWs were not as commanded due to the software lag(s)

I believe the Mega is like the Uno and has a single level of interrupt priority. Processing one interrupt means others occurring during that time pend and so run late, and perhaps with variable latency. Varying transport lag is not conducive to good feedback loop behavior.
By Tsaven
#146809
Thanks for the replys, everyone. To answer the questions, no I don't have a scope, I'm actually really new at electronics. Given the nature of it, I'm quite sure that it's something coming from the controllor. I'll writing the value once, instead of the loop continuously writing the value, and see if that works. Of course now I need to figure out how to write a loop that the execution stays in until I send it some other signal to command it to do something else.

As far as power, it's a wall-wart rated at 2a, I would be surprised if all the servos were drawing that much power when they're mostly still. And if it was bad power, would it cause the same repeating twitches? I would think insufficient power would be completely random and erratic.

The controller is the Ardy Mega, and no none of these channels are PWM, they're all the standard digital outs using the Ardy's servo library. Is there a workaround for this? Pretty disappointed if there's no way around it, I bought this board and shield specifically for this project :(
By Mee_n_Mac
#146813
Why don't you post the "twitchy" code so we can see if there's anything obviously goofy with it ? The problem with software timing (which is what you get using the library functionality) is that when you have multiple servos getting differing commands, the PWs (= to the commanded position) may vary depending on the other servos position commands.

Think of it this way, there's 1 timer that generates interrupts at some fixed rate. When that interrupt happens the software asks if it's time to turn on or off the digital output to servo #1. It does that. Then it does the same for servo #2, servo #3 ... etc. The execution time for each of these commands may vary, on to off. Thus the time from interrupt to the actual digital output will vary, moreso for the last servo to be updated. This means the PW and the commanded position will vary. The faster the MCU doing this, the less it matters. And if the code worked hard to equalize the execution times, it wouldn't matter. The "fix" is to either code up equalized times yourself or use a separate hardware IC that has multiple PWM channels, all independant of each other.

That's not to say this would cure your twitching, there may be other underlying causes. Hobby servos aren't exactly military grade hardware. The backlash in the gears and any slop in the pot will cause "twitch". Noise on the power supply can cause the feedback loop in the servo to twitch. No doubt there are other reasons that escape me ATM. You can try to combat the latter items by using a good power source, more amps and higher quality than you think you need ... at least to debug this problem. Perhaps bypass the power leads at each servo with a BFC.
By Tsaven
#146814
I did post the code, it's in the original post :) Thanks for the other advice, these are very cheap servos and it I give them a flick, they actually will flail back and forth endlessly in a very fast shudder as they try and re-center themselves. However, I'm pretty sure this isn't that, as even when they're all still, the pattern they twitch in is very consistent and repeats every two seconds or so (you can see it in the video)

I will try and wire in another power supply, the wall-wart was for testing purpouses. I've got a pair of 7.4v 800MHa RC aircraft batteries that I'm planning on using to power the thing long-term, they're each rated at 17a continuous draw.
By Mee_n_Mac
#146816
Tsaven wrote:I did post the code, it's in the original post :)

Wow, I guess I need to look back B4 posting after a double shift ! Eeeeks, my bad. Let me (and even better, others) look at what you did code-wise. My other comments re: servo problems stand. (like a willow tree)
By jremington
#146822
You might take a look at the Pololu "Maestro" servo controllers, specifically designed for precision and stability of response. They are not expensive and a 24 channel version is available. Polulo's product support is also far better than most. http://www.pololu.com/catalog/category/12

If even a few of the servos are moving at once against a load, they will bring a 2 amp power supply to its knees.
See this discussion: http://www.rctech.net/forum/radio-elect ... -draw.html
By Tsaven
#146840
Wow, that is WAY more current than I thought a servo could ever draw. And to be fair this is a cheap POS wall-wart that came from my old router, so who knows how much power it can actually put out. I'll work on getting these batteries hooked up, they're each rated for 20a continuous.

If anyone is seeing me doing something particularly retarded in my code, I'd love the feedback!
By Joeisi
#146864
Tsaven wrote:Wow, that is WAY more current than I thought a servo could ever draw. And to be fair this is a cheap POS wall-wart that came from my old router, so who knows how much power it can actually put out. I'll work on getting these batteries hooked up, they're each rated for 20a continuous.

If anyone is seeing me doing something particularly retarded in my code, I'd love the feedback!
Yeah, these suckers can drain current. Quite annoying sometimes. Make sure your power supply isn't drooping in voltage(due to massive current). I know this isn't the reason for the twitching, but if it is too hot to touch, then it is probably drawing too much current.(Solution: Better power supply. Like those batteries.)
By Tsaven
#146872
Well, I hooked up a large 2400mAh 7.4v LiPoly rated at 40a continuous draw, and I still get the exact same behavior, the same cyclical twitching pattern. To me, the code seems like it should be really straightforward, am I overlooking something really silly?
By fll-freak
#146874
I do not think you are overlooking anything silly. I think that the Arduino is just not up to the job of generating servo signals for 24 servos without some glitching.

Did you ever run the program to write the 24 servo values once and never again? Did it still twitch? Could you impose on a friend with a scope to look to see if the glitch is in the signal? If you happened to be near New Hampshire, I would offer you my lab for the test. If not, you might see if you have an old TV repair shop in the area and ask the tech for a quick peak.

You could also slowly increase the number of servos you are going to drive and see at what number the twitch shows up. Not sure what that would prove, but it might be an interesting exercise.

I think a servo controller like the Pololu is going to be in your future.
By Tsaven
#146957
I tweaked the code to write the values once and never again, still getting the twitches.

Then, just for kicks, I commented out half the servos, so it's only attaching the right side, 12 servos, to see if the controller could at least handle that. Still no luck, the attached side is twitching like it's got Parkinson. I then reduced it to attaching just 6 servos, and it's STILL twitchy.

Can the Ardy Mega really not handle just 6 servos? Their documentation explicitly states that it can handle up to 48 servo objects.

I'm not near NH, but I can probably find someone with a scope to check out the signal. What am I looking for?