VarSpeedServo - a modified Servo library with speed control

Ah never mind
Typo somewhere. Testing it now and it works fine! Thanks so much!

I have just come across this, nice job Korman.

Do you think this is a capability I should add to the core Servo library?

BTW, I posted an example sketch using this library here: Simultaneous but different servo sweeps with four servos - #12 by mem - Robotics - Arduino Forum

Hi Mem,

it's nice to know, that you like it. Whether to add it to the main Servo-library should be considered carefully. From my point of view, you're welcome to add the code or any modification from it. However, please consider a few things first:

  • The speed control adds more memory to the servo structure. It's just a few bytes, but in large setup with many servos, you should check if this causes problems
  • The speed parameter doesn't relate to any real time measure. It's just a number and you can set some speeds but others not. For many applications, like moving something at a pleasant speed to look at, this is good enough. If some specific speeds are necessary, the model I used might not offer enough granularity.
  • I didn't really check if the interaction of the position update and attach/detach works in a reasonable manner. What happens if one detaches the servo while on a slow move and then reattaches it?
  • The method-name slowMove is clumsy and not really in line with the rest of the rest of the methods
  • The spectrum of the available speeds isn't well used. Out of the speed byte, values above 128 are usually to fast to make visible differences because of the limits of the motor speed itself. I would like it improve this, but then I'd need more bits for the ticks parameter. If the memory isn't a problem, adding a byte to the tick counter will give more slower speeds. I think, it should also be considered what kind of speeds are going to be used most.
  • Some proper documentation and samples are also necessary.

In short, it was mostly a quick hack to get landing gears on model aircraft extend and retract at a more more realistic pace. Going by the general feedback, it seems to be of use for some applications. If you think part of my work is useful to be added to the main library, feel free to go ahead.

Korman

Korman:
Hi Mem,

it's nice to know, that you like it. Whether to add it to the main Servo-library should be considered carefully. From my point of view, you're welcome to add the code or any modification from it. However, please consider a few things first:

  • The speed control adds more memory to the servo structure. It's just a few bytes, but in large setup with many servos, you should check if this causes problems
  • The speed parameter doesn't relate to any real time measure. It's just a number and you can set some speeds but others not. For many applications, like moving something at a pleasant speed to look at, this is good enough. If some specific speeds are necessary, the model I used might not offer enough granularity.
  • I didn't really check if the interaction of the position update and attach/detach works in a reasonable manner. What happens if one detaches the servo while on a slow move and then reattaches it?
  • The method-name slowMove is clumsy and not really in line with the rest of the rest of the methods
  • Some proper documentation and samples are also necessary.

In short, it was mostly a quick hack to get landing gears on model aircraft extend and retract at a more more realistic pace. Going by the general feedback, it seems to be of use for some applications. If you think part of my work is useful to be added to the main library, feel free to go ahead.

Korman

Hi Korman,

I agree with all your points. But this thread and the other one I linked below got me thinking about possibly adding a sweep function with user setable speed that I may be able to do using just one more byte of RAM per servo. I would be interested to hear if this is something that would be generally useful and will start a new thread to discuss this if enough people are interested.

To answer your question: I needed this kind of function, so I wrote the extension. From time to time, questions about moving things slowly come up, mostly for visual reasons. I think having such a function in the servo library would be a good idea if the library doesn't suffer from it.

NB: I added one more point about the speed control to my previous message.

Korman

I am interested to know if adding variable speed sweep would be good enough for most applications? Most of the additional RAM needed in VarSpeedServo is for the target position, but this is not needed if the sweep is between the min and max positions set in the attach method because only the speed and current direction need to be stored and these can be held in a single byte if the max speed is limited to 127.

Would something like that be useful or will most people want the capability to control speed when moving the servo to any given?

Would something like that be useful or will most people want the capability

I am not "most people", despite my inflated ego :slight_smile: But as you see from my reply earlier, I would have found that usefull.

My sweep used a millis() timer construct. For me - not a big hassle to program, but I know from many of the threads here, that it will be too much for many people to "do more than one thing" (ie let the sweep progress without disturbing other processing.) As I do not (yet) have any experience in accessing the hardware timers I do not know how much hassle it is to add that in the timer/interrupt code - or if it would limit the max number of servos.

(My sweep code has an advantage that I can do something synchronized with two servos. In an built in function one would need to change the sweep rate for each "ratio".)

The alternative is to let the sweep be "blocking", ie calling servo.write with asweep value will take the time it takes. I would not like a servo.updatesweep() to be called "often" for the sweep to work.

Msquare:
(My sweep code has an advantage that I can do something synchronized with two servos. In an built in function one would need to change the sweep rate for each "ratio".)

What do you mean by the ratio? Perhaps you can say more about the functionality.

I don't want to hijack this thread so I have created a new thread here: http://arduino.cc/forum/index.php/topic,68474.0.html

Gday all

just a quick question, just recently started with arduino and i am having trouble woth the VarSpeedServo library and slowmove.

Is there anywhere where it has been incorporated with a sweeep sketch to provide sweep at various speeds?

Thanks for the time, I could not find anything anywhere else

Cheers

Rick

There might be one, you'd have to check. Here's the test-code I use for this library. It should be fairly simple to extend.

/*
	ServoFollowPot
 
	This little program controls a servo with a potentiometer.
	The button moave the servo from one end to the other, the potentiometer
	sets the speed. 0 = full speed, 1 slowest, 255 fastest.
	When pressing the button, the led is turned on to exercise it a little.
	Every few seconds the current postion of the servos is reported on the
	serial line and whenever the button is pressed.
 
 The circuit:
	* LED attached from pin 13 to ground: Button led
	* Pushbutton attached to pin 2 from GND
	* Linear Potentiometer attached to analog pin 2 from GND and +5V acting
	  as a voltage splitter. Any value from 10 kOhm to 100 kOhm will work.
	* Servo control line attached to pin 12.
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
*/

#include <VarSpeedServo.h>

const int buttonPin = 2;	// the number of the pushbutton pin
const int ledPin =  13;	// the number of the LED pin
const int potPin =  16;	// the number of the potentiometer pin (Analog 0)
const int mainPin = 12;	// Main servo control line
const int ServoMin = 1000;	// Minimum pulse width for servos in ms.
const int ServoMax = 2000;	// Maximum pulse width for servos in ms.
const int PotiMin = 0;	// Minimum value read from potentiometer
const int PotiMax = 1023;	// Maximum value read from potentiometer
const int ButtonDelay = 100;	// Time in ms to press button to activate tuning mode


VarSpeedServo myServo;	// Servo

void setup() {
	// initialize the LED pin as an output:
	pinMode(ledPin, OUTPUT);      

	// initialize the pot and button pin as an input:
	pinMode(potPin, INPUT);
	pinMode(buttonPin, INPUT);

	// Activate internal pull up resistor for tuning button
	digitalWrite(buttonPin, HIGH);

	// Initialise Servos
	myServo.attach (mainPin, ServoMin, ServoMax);

	// Initialise Serial communication
	Serial.begin (9600);
}

void loop() {
	static unsigned long debounce = 0;         // variable for reading the pushbutton status
	static int buttonstate = HIGH;         // variable for reading the pushbutton status
	static unsigned long lasttick;

	// Report position;
	if (millis () - lasttick > 5000) {
		lasttick = millis ();
		Serial.print ("Position=");
		Serial.println (myServo.readMicroseconds());
	}

	// Read button and debounce.
	int buttonnow = digitalRead(buttonPin);
	if (millis() - debounce >= ButtonDelay
		&& buttonnow != buttonstate) {
		buttonstate = buttonnow; 
		debounce = millis();
		switch (buttonstate) {
			case LOW:
				// Button has just been pressed
				digitalWrite(ledPin, HIGH);
				
				// Set the servos
				{
				int potlast = analogRead (potPin);	// Read potentiometer
				int speed = map (potlast, PotiMin, PotiMax, 1, 256) & 0xff;
				myServo.slowmove ((myServo.readMicroseconds() > ServoMin + 400
					? ServoMin + 100 : ServoMax - 100), speed);
				// Report pot position
				Serial.print ("Speed=");
				Serial.println (speed);
				}

				break;
			case HIGH:
				// Button has just been released
				digitalWrite(ledPin, LOW);
				break;
		}
	} 
		
	// Slow down
	// delay (50);
}

Korman

Hi, Great work on this!

Question: would this work as acceleration control on continuous-rotation servos?? Newpos sets top speed, speed sets acceleration?

terryking228:
Question: would this work as acceleration control on continuous-rotation servos?? Newpos sets top speed, speed sets acceleration?

Terry, it well could be, but to be honest I don't know. The library just increases continuously the position of the target until the set target value is reached. The consequence is that the PWM pulse increases or decreases in length over time. If the speed on continuous rotation servos is the same as the position for a regular servo, the speed parameter would be the acceleration.

Perhaps someone with access to such a servo could try that out?

Korman

Hi Korman,

I have a small 2-wheel robot that I will try it on. Maybe be a day+ as I'm busy with The Work Thing..

If it's dynamically changing the POS value, that should work. Within a reasonable range, Continuous-Rotation servos speed is proportional to the POS offset from 90 in both directions...

Thanks Terry, that'll be interesting to know. If I understand it right, if you use slowmove with different speed parameters but the same position for each wheel, the robot should accelerate in a spiral.

Korman

the robot should accelerate in a spiral.

Cool! Gotta try that....

Hi Korman, great work!
Im going to give it a go over thru my ECU code.
one advantage I love is the easy way I can slowly raise the speed the motor for my gas turbine starter.
One question I have as you seem the guru in all things servoish....

what happens in situations when there is alot of other code processing going on, and the servo gets "noisy".
I used a simple for command to slowly raise the servo output.
to test it, I plug in a standard hobby servo, but my intended use is for a brushless motor controller.
I have a project that has an interupt watching a input, measuring RPM.
also a large LCD display and several other things going on, temperature measurment, button menu inputs, etc etc goes on and on (using a Mega 2560 and a 240x320 LCD).
Im using the Servo output to feed a Brushless motor controller input drive to slowly increase the speed of the motor to start a gas turbine.
but the servo output im getting is jittery, noisy. Is this because the processor isnt fast enough to keep up with the PWM pulses the motor requires?
On the oscilliscope, i see alot of "topping off" between the pulses. is there any practices I can apply to my code that will help avoid this?
I stripped away bits of my code untill the LCD touchscreen code was removed before the servo drive got clean, but it was still not perfect.
I did try to break up my code into sections so it always goes back to the servo drive command without too much delay.

What I am guessing, is maybe this is an application where an arduino such as a standalone arduino needs to be setup as a dedicated controller?

keen to hear your thoughts.
cheers.

Stimps:
One question I have as you seem the guru in all things servoish....

Sorry to disappoint you here, we have no guru here. The last thing that made guru-guru ended after a short sting in the oven on the table at thanksgiving.

Stimps:
what happens in situations when there is alot of other code processing going on, and the servo gets "noisy".
I used a simple for command to slowly raise the servo output.

Hi John,

as long as you have just one single interrupt going on the the servo is pretty stable, no matter what you do in your main loop. To test that out, create a program that leaves the servo in a fixed position and does other processing meanwhile. The servo should only have occasional minimal movements. Check that out with various servo positions, not only the extremes.

In my experience, the servos start to get jittery once you have multiple interrupt functions or you have functions that disable interrupts periodically. Then the pulse length isn't constant in the situations where another interrupt (or cli / sei instructions) are delaying the servo interrupt and thus lengthening the PWM impulse. This can be a hairy problem.

If the rpm-counter is bothering your servo, you might want to look into using your rpm-signal as an external clock reference so that you can use timer2 as a counter. (This is all from memory, I don't have the Atmel datasheet on me at the moment). For very high frequencies, consider adding an external counter chip you then can read at your leisure. As usual, the problem lies in the details.

When I'm back at home, I will have a closer look at the matter.

Korman

Hello, I was trying to make the servo go back and fourth 180 degrees with speed at 1 but it doesn't work, it just goes to one direction and is stuck there, what is wrong in the code?

#include <VarSpeedServo.h>

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.



 
int pos = 0;    // variable to store the servo position 
VarSpeedServo myServo; 
void setup() 
{ 
myServo.attach (9);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 180)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
  myServo.slowmove (pos,1);             // tell servo to go to position in variable 'pos' 
    
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=180)     // goes from 180 degrees to 0 degrees 
  {                                
    myServo.slowmove (pos,1);
  
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

Looks like you're not giving the servo enough time to reach 180 at the slow pace before you order it back. Put a substantial delay between the two for loops. Why you're using those loops at all given the 180 degree increments is a bit of a mystery too.

I just installed the library and referenced it. Upon build I get the following compile error. I use the latest Arduino 1.0 compiler running on Windows 2008 server with Arduino MEGA2560. Any thoughts?

C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:56:23: error: WProgram.h: No such file or directory
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void handle_interrupts(timer16_Sequence_t, volatile uint16_t*, volatile uint16_t*)':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:91: error: 'LOW' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:91: error: 'digitalWrite' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:122: error: 'HIGH' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:122: error: 'digitalWrite' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:126: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: At global scope:
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:266: error: 'boolean' does not name a type
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In constructor 'VarSpeedServo::VarSpeedServo()':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:283: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'uint8_t VarSpeedServo::attach(int, int, int)':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:297: error: 'OUTPUT' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:297: error: 'pinMode' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:304: error: 'isTimerActive' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::detach()':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:315: error: 'isTimerActive' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::write(int)':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:326: error: 'map' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::writeMicroseconds(int)':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:334: error: 'byte' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:334: error: expected `;' before 'channel'
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:335: error: 'channel' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:343: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::slowmove(int, uint8_t)':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:377: error: 'map' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:380: error: 'byte' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:380: error: expected `;' before 'channel'
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:381: error: 'channel' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:388: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'int VarSpeedServo::read()':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:407: error: 'map' was not declared in this scope
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'int VarSpeedServo::readMicroseconds()':
C:\Program Files (x86)\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:414: error: 'clockCyclesPerMicrosecond' was not declared in this scope