Servo Library for More than 2 Servos

I really like the Servo library included with 0012 but wonder how hard it would be to have a library for more than 2 servos? I looked at the code in playground and I think it is possible, but then again, I'm a software guy by day. :wink:

I wonder the same thing. On the playground there is a Servo library (Arduino Playground - Servo) that supports up to 20 servos. I'd like to use something that is officially sanctioned, but my current project requires 6 servos.

Mikal

The Servo library on the playground has some limitations due to the fact that the Servo's are actively controlled by software. This means that you need to call a refresh function at a certain interval, so, for example, you can't really use long calls to delay(). Also, I think the servo movement can be jerky. The official library uses hardware PWM to control the motors, so the movement is cleaner and it doesn't place any limitations on the rest of the sketch. In this case, it seemed more in keeping with the Arduino philosophy to start with something easy to use, even if it's a bit limited in functionality. We may add a SoftwareServo library, which would allow for the use of more servos.

I like that it is using the hardware PWM. Is there a limit on being able to use the other PWM outputs for servo control?

Timer 1 (pins 9 and 10) that we're using for the Servo library is 16 bit. The others are only 8 bit, so the servo control wouldn't be as fine.

I posted code a while ago for a library that supports up to eight servos driven by timer2. It uses interrupts rather than PWM so it uses a few CPU cycles approximately 8 times a millisecond to service servos in the background. The timer interval is 0.5 microseconds so the precision is pretty good.

The library is called servoTimer2 and the source code is in a few posts starting here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1204020386/74

The code works by using the timer interrupt to turn on and off each servo in sequence, based on values written to an array of pulse widths. It would have been easier to implement that code using timer1 but in the application in that thread, timer1 was needed to decode the incoming data from a radio control transmitter.

I havent had an opportunity to write up a tutorial on its use but if you want to give it a try I will be happy to help get you going.

Whoops, I must have missed that library. It could be a good replacement for the current Servo library in later Arduino versions.

Thanks both of you for the information. I wondered about an internal "limitation" since so many libraries use pin 9 and 10.

I probably will try out the other library to see how it works, too. It is nice to have options.

I must have missed that library. It could be a good replacement for the current Servo library in later Arduino versions.

The implementations are different but I think the only major difference in usage (other than being able to create up to eight instances) is that my library expects milliseconds instead of degrees. When I get a chance I will look at adding angle support by treating values less than 200 as degrees, values greater than 500 as microseconds. I like to work in microseconds but I guess degrees are preferred by some.

My library is more efficient if it uses timer1 so perhaps I will make that change as well. Lets see if there is enough interest to dust this off.

I'd be willing to give it a try. My plan is to use servos for robots that could have 3-4 servos (or more.) I would like to avoid using extra hardware if possible.

I'd have to look over the code but couldn't you add a SetAngle method instead of looking at values less than 200?

On current Servo lib. (hardwareServo) is name conflict and override old Servo.h (softwareServo) , me a bit confuse on my old codes..

I think old Servo lib. may be named to SoftwareServo and included on next build for backward 0015 Servo lib compatible..

... couldn't you add a SetAngle method instead of looking at values less than

I was intending to keep the underlying infrastructure storing values in milliseconds because those are the units used by the timer interrupt handler. So I would add code that converted from angles to milliseconds and was thinking of doing this implicitly if an argument had to be an angle (i.e. less than 200 ).

I would probably add macros called writeAngle() and readAngle() that looked like methods and explicitly did the conversions to and from millisconds using the existing methods. Would that work for you?

On current Servo lib. (hardwareServo) is name conflict and override old Servo.h (softwareServo) , me a bit confuse on my old codes..
I think old Servo lib. may be named to SoftwareServo and included on next build for backward 0015 Servo lib compatible

If you are getting errors under 0012 with this servo library: Arduino Playground - Servo

you could try removing #include <wiring.h> from the servo.h file

You may need to add that include into the servo.cpp file.

I haven't tried it but that should get you closer.

I would probably add macros called writeAngle() and readAngle() that looked like methods and explicitly did the conversions to and from millisconds using the existing methods. Would that work for you?

That would work for me.

I'd finished edit old Servo library and named "SoftwareServo" with examples included

and I'd add function millisSet(unsigned long) to wiring.c wiring.h
for direct millisSet() value set or clear millis() counter purpose ( i.e. millisSet(0); )

download here
http://www.avride.com/pop/store/SoftwareServo_millisSet_addon.zip

unpack file to current Arduino folder, overwrite old wiring.* files

If test are okay, I'll upload this lib. to Playground. :slight_smile:

I'd finished testing library http://www.avride.com/pop/store/SoftwareServo_millisSet_addon.zip

and put an example for new named SoftwareServo library on Servo page , Playground here
http://www.arduino.cc/playground/ComponentLib/Servo

on library .zip file, I'd modify "wiring.h" and "wiring.c" for add millisSet(unsigned long) function which I use on Sweep.pde example

hava fun