VarSpeedServo - boolean "wait" command

Hello All,

I am trying out a simple sketch to make a servo move back and forth between it’s extents whilst controlling its speed with a pot. The sketch mostly works, however I am having a few problems with the “wait” function in the write command. If you are not familiar the library’s API (GitHub - thorikawa/VarSpeedServoSam: Another Arduino servo library with the ability to change the speed for boards with SAM processors.) describes it so:

write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete

Here is my sketch:

#include <VarSpeedServo.h>

VarSpeedServo myServo1;
const int potPin = A0;
int potVal;
int servoSpeed;

void setup() {
  myServo1.attach(9);
  Serial.begin(9600);
}

void loop() {
  potVal = analogRead(potPin);
  servoSpeed = map(potVal, 0, 1023, 20, 255);
  //value of 0 on the PWM freezes the servo & sketch - something to do with the library

  myServo1.write(1, servoSpeed, true);
  //true command only allows full 180° rotation at low speeds <200

  potVal = analogRead(potPin);
  servoSpeed = map(potVal, 0, 1023, 20, 255);
  //find a way of not having to repeat this...

  myServo1.write(180, servoSpeed, true);
  //true command only allows full 180° rotation at low speeds <200

  Serial.print("potVal: ");
  Serial.print(potVal);
  Serial.print(", servoSpeed: ");
  Serial.println(servoSpeed);
}

My issue is with the boolean “wait” function:

A) I don’t understand how this works at different speeds as there is no feedback loop from the servo (3pin). Does it take the speed and guess how long 180° is going to take and then move onto the next command?

B) At high speeds, anything above 200, it does not allow for the servo to complete the 180° before changing direction - why is this?

The boolean function also stalls the sketch like a delay would. I was hoping to incorporate a schedule function like this http://forum.arduino.cc/index.php?topic=54866.0 (similar to the blink without delay sketch). But I am not sure if I am able to use this with the ‘wait’ function - I’m guessing not.

Any ideas, thoughts and solutions are welcome - I am still a bit of a beginner and tried to research this particular problem but was unable to find anything.

Cheers

edit: I see here that at full speed the wait command goes to the next one before the operation is complete - note the servos returning at full speed to 10°.

The sketch mostly works, however I am having a few problems with the "wait" function in the write command.

There is NO wait function. There is a wait OPTION.

A) I don't understand how this works at different speeds as there is no feedback loop from the servo (3pin). Does it take the speed and guess how long 180° is going to take and then move onto the next command?

That's what it appears to do. That assumption is only valid with an unloaded servo powered properly, of course.

B) At high speeds, anything above 200, it does not allow for the servo to complete the 180° before changing direction - why is this?

Because the write() function returns prematurely, based on an invalid assumption about how long a move will take.

Any ideas, thoughts and solutions are welcome

The ONLY proper way to assure that a servo has moved to a specific position is to provide feedback, either by using a limit switch or be using an encoder.

Okay - an option. Good to know the terminology.

Fair enough if this is the case, I guess I will just do it with the for() function and millis()

experimenting further, if the library was using code similar to this:

      if ((millis() - lastUpdate) >  updateInterval) // time to update
      { lastUpdate = millis();
        pos += increment; // pos = pos + increment
        servo.write(pos); // tell the servo to move to pos
        if ((pos >= 180) || (pos <= 0)) //end of sweep.  "||" means OR
        {
          // reverse direction
          increment = -increment; // assign negative value to variable increment
        }

and if a value <3ms were given to the updateInterval variable then the servo does not have time to catch up with it’s own commands (for my servo anyway), as you get the same result if you apply max speed to the VarSpeedServo option.

I don’t know how to look at a library’s source code so I can’t check.

I don't know how to look at a library's source code so I can't check.

The library source code is the .cpp and .h files in the library folder. You can open them with any reasonable text editor.