Let's Make Robots!

Servo time/speed and position control (using an Arduino)

Is there a way to control servo position and speed at the same time? ex: you want to build a biped but you don't want the movements to be jerky.

thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I've played with this a bit with multiple servo operated joints.

I found the best way to get smooth movement is to use nextmove assigned times.

This is just an example of what I'm talking about. (not actual code)

 

long NextMove;

int MoveDelay; ' the number of milliseconds between each step, smaller steps = faster movement

int Step; ' the amount the servo moves with each loop, keep this number small

int TargetPosition; ' this controls where the joint is moving to, you just change the targetposition to make it move

loop(){

   if millis() >= NextMove and JointPosition <> TargetPosition then MoveJoint;

}

MoveJoint() {

   Int NewPosition = CurrentPosition + Step;

   rem the next two lines make sure you don't overshoot the target

   If CurrentPosition < TargetPosition and NewPosition > TargetPosition then NewPosition = TargetPosition;

   if CurrentPosition > TargetPosition and NewPosition < TargetPosition then NewPosition = TargetPosition;

   Joint.move(NewPosition);

   CurrentPosition = NewPosition;

   NextMove = millis() + MoveDelay;

}

There are servos available that can have their speed programmed into them. That is what they usually use with humanoid robots and it makes them more expensive.

In my code I do something similar to what Patrick suggest except I use a polling method. Instead of just using a delay and having nothing else being done I write my code so the servo is incremented then continue through my program loop checking sensors etc.

You need to use more variables and sometimes arrays as you need to keep track of the start position, stop position, speed (change your increment value), direction.

some command such as pulsin have a timeout variable. If you are using pulsin to read an IR signal from a TV remote then you can set it's timeout value to work like a delay function except that it can also look for incomming commands.

i have tried what you said and seems i can get it.. here is the code and would like to remove that delay function please help

 

#include <Servo.h> //include Servo library

 

const int irPin = A1;  //Sharp infrared sensor pin

const int dangerThresh = 276; //threshold for obstacles

const int ledPinstop = 3;       // the pin that the LED is attached to

const int ledPingo = 12;       // the pin that the LED is attached to

 

int rightDistance; //distance away from obstacles on the right

int centerDistance; //distance away from obstacles in the center

int leftDistance;  //distance away from obstacles on the left

int averaging;                // Stores average of distance

int val = 0;

int dist = 0;

 

//declare motors

 

Servo myservo;  

Servo servoleft;     //Left wheel 

Servo servoright;    //Right wheel 

 

 

void setup()

{

 

  pinMode(ledPingo, OUTPUT);  

  pinMode(ledPinstop, OUTPUT);  

  myservo.attach(9);          // head to pin D9

  myservo.write(93);          // set IR sensor pan to center

  servoleft.attach(10);       // Left servo to pin D10

  servoright.attach(11);      // Right servo to pin D11

  servoright.write(90); 

  servoleft.write(90); 

 

}

 

void forward()

 

{

    servoleft.write(0);

    servoright.write(180); //move forward

}

 

void scan_area()

 

{

    servoleft.write(90);

    servoright.write(90);

    delay(100);

    myservo.write(125); 

    delay(300);

    rightDistance = analogRead(irPin); //scan to the left

    delay(100);

    myservo.write(93); //return to center

    delay(300);

    centerDistance = analogRead(irPin); //scan center

    delay(100);

    myservo.write(50);

    delay(300);

    leftDistance = analogRead(irPin); //scan to the right

    delay(100);

    myservo.write(93); //return to center

    delay(300);

    centerDistance = analogRead(irPin); //scan center

    delay(100);

}

int get_avg(int IRpin) //Gets 3 readings from the sensors and averages them to get rid of spikes

{                     

  int total = 0;                

  for(int x = 0; x <=3; x++)    

 

 

  {  

 

    total+=analogRead(IRpin); 

 

  }  

 

  return total/3;

 

}

 

void loop()

 

{

 

  int val = analogRead(irPin); //read IR sensor

  if (val<dangerThresh) //if path is clear

  {

    digitalWrite(ledPingo, HIGH);

    forward();

  }

  else //if path is blocked

  {

    digitalWrite(ledPingo, LOW);

    digitalWrite(ledPinstop, HIGH);

    scan_area();

    compareDistance();

    digitalWrite(ledPinstop, LOW);

  }

}

 

void compareDistance()

{

  if (leftDistance<rightDistance && leftDistance<centerDistance) //if left is less obstructed 

  {

    servoleft.write(180);

    servoright.write(180);  //turn right

    delay(300); 

  }

  else if (rightDistance<leftDistance && rightDistance<centerDistance) //if right is less obstructed

  {

    servoleft.write(0);

    servoright.write(0); //turn left

    delay(300);

  }

  else //if they are equally obstructed

  {

    servoleft.write(180);

    servoright.write(0); //move back

    delay(300);

    servoleft.write(0);

    servoright.write(0);    //turn left

    delay(950);

    scan_area();

    compareDistance();

 

  }

}

i'm new in arduino,so can u teach me about servo control direction and speed ??
just one simple example....because i no so good in english.....thanks a lot...

To ask, 'please teach me' is very challenging. Everyone has their own life to live and limited time to spend on the things they want and have to do. Many people on this site have contributed knowledge. There are also many sources on the Internet to learn about Arduino.

Take a look at some of the links under Programming on the LMR Primer page. The examples and knowledge you are looking for are there. You just need to use them. After you have studied and tried on your own, come back with specific questions or help to clarify something you didn't understand.

Post  your questions as their own forum topic when you are ready.

 

i'm new in arduino,so can u teach me about servo control direction and speed ??
just one simple example....because i no so good in english.....thanks a lot...

Hi, ggkc.

Look through the LetsMakeRobots pages. There are hundreds or maybe thousands of examples.

There are also a couple Malaysians on here who may be able to help more, since they speak your native language.

Aaronsuper1 is a 13-year-old in Puchong. He may be able to help some, at least with the language problem.

 

Anyway, there are a LOT of examples here at letsmakerobots.com with programming, designing, and building all sorts of different robots. To search for things, Click on "Search page" above the Shout Box ===>>>

 

Good luck.

 

Ok thanks

Increment or decrement the servo value with a small delay between each iteration. 

So instead of going from 0 to 90

you go from 0 to 1 to 2.... 90 with small delays in between each. Use a for loop.