Using Servos in Arduino 0018: twitch when attached

Hi,

I just got into Arduino for my RC planes and have to say wow! Amazing.

But after starting to work with it a major question comes up: when the sketch starts and the servos are attached thy twitch (i.e. move undavertedly).

Is there a way from preventing servos from moving when beeing attached.

In my case the servo has already the position that I give for the first time (i.e. 1000us). So normally the servo shouldn't move.

Thank you!

Cheers Alexander

I think I've read that when started up, the servo are sent to the equivilant of the 90 deg servo position. This would probably be when using the servo library.

What pin do you have it on? I got mine on 7, after I tried another, where it was twitching when I uploaded to the arduino and when it started up.

I think it would be pretty hard to design a hardware/software solution that prevents any twitching on first power up or attachment of the software. It's normal operation on standard radio control for the servos to twitch when first power is applied. Remember that the Arduino has no way of knowing the existing physical position of a servo when first attaching to them and starting the PPM ouput signal to the servo.

Lefty

Twitchless startup should be possible as I have an ssc-32 servo controller and the attached servos don’t move when it is powered up. I’ve got a servo attached to my arduino and using the below code (from another discussion) it appears that if the servo I have connected is in a ~93 deg position, there is no twitch when the serial monitor is started/stopped causing the arduino to reset. If the servo is in any other position, it moves to the ~93 deg position on an arduino reset. There appears to be a default position signal sent to the servo on startup, in my case ~93 deg. A study of the underlying code files might produce some info.

// type servo position (0-180) followed by a period (.)
// in serial monitor, then click send or hit enter.
//

#define MAX_LINE 20
boolean eol = false;

#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup()
{
      Serial.begin(9600);
        myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}

void loop()
{
        char line[MAX_LINE + 1]; // must be at least 1 char > MAXLINE

        //do something other than waiting for serial transfer
      if (lineAvailable(MAX_LINE,line))
      {
        Serial.write(line);       // echo back the line we just read
        Serial.write("\r\n");

          int     n;
          n = atoi(line);  //apparently converts string into number

         myservo.write(n);   // tell servo to go to position in variable 'pos'
        
         eol = false;               // get ready for another line
                
      }
      //do something other than waiting for serial transfer
}

boolean lineAvailable(int max_line,char *line)
{
      int c;
      static int line_idx = 0;
      if (max_line <= 0)    // handle bad values for max_line
      {
        eol = true;
        if (max_line == 0)
          line[0] = '\0';
      }
      else                // valid max_line
      {
        if (Serial.available() > 0)
        {
          c = Serial.read();
          if (c != -1)  // got a char -- should always be true
          {
            //if (c == '\r') //use with terminal programs and such
                if (c == '.')  //use with the serial monitor
              eol = true;
            else
              line[line_idx++] = c;
            if (line_idx >= max_line)
              eol = true;
            line[line_idx] = '\0';     // always terminate line, even if unfinished
            if (eol)
              line_idx = 0;           // reset for next line
            }
          }
        }
        return eol;
}

This came up recently, IIRC, and the suggested cure was a pull-down resistor. Have a look around the forum - I'll do the same, see if I can find it.

The below line in the servo.h file probably is of significance. Not sure if this can either be deleted or changed to something like zero to to avoid sending the default 1500us position on attachment.

define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached

I have the same issue with my servos twitching when I apply power to them. I tried adding a pulldown, but that didn't do anything.

I did a test just now to see what would happen if I applied power to my servos without the Arduino supplying position data though, and the result was interesting.

If I disconnect and reconnect the power, the servos twitch. In fact, not only do they twitch... one of them will actually rotate a significant amount if I apply and remove power over and over. The other will remain in place and simply twitch. (Same brand, same type of servo.)

It may be when starting up the twitch is due to there being no signal for the servos. I'm not sure of any good fix for this though. One would have to switch the power to the servos on after one's sketch is running I suppose.

But... I just did another test with the sketch running and I applied power to the servos, and they still twitch. Though they don't lose position. And they seem to twitch less.

Anyway... it seems possible there may be no software solution for servo twitching on power up. And there may be no easy hardware fix either.

Anyway... it seems possible there may be no software solution for servo twitching on power up. And there may be no easy hardware fix either.

That's my opinion. 8-)

Cant remember who suggested it, but it was suggested to put a wire on the pot inside the servo, and run it back to the arduino, that would allow the arduino to send the right signal to the servo, so signal and position would match.

Sorry for replying so late. I wasn't aware that I didn't activate the notification.

Thank you all for your effort.

In the meantime I was able to get rid of it. After searching a while here in the forum I was able to get the solution.

There will be no twichting when a) I make sure that I get a reliable RX reading. Thus I delay(100) in the setup routine first. This gives the RX some time to boot. Then I read the RX switch to get the default positions (in which the servos are supposed to be).

b) The next statement after servo.attach() is servo.writemicroseconds(). In this case the servo has no time at all to process the 1500us which is the default.

And here we go: no even subtle servo movement at all!

In my first sketch I did some (float) calculations between attaching the servo and setting the default. That was time enough to let the servos twitch (ie move towards 1500us).

BTW: here is my gear-door-sequencer-mockup in action :)

http://www.youtube.com/watch?v=VtJAJNm8W8k

Cheers Alexander