Arduino Forum

Topics => Robotics => Topic started by: Ghostbusters on Sep 06, 2013, 03:54 am

Title: servo code
Post by: Ghostbusters on Sep 06, 2013, 03:54 am
with the Sparkfun inventor kit there is a circuit that controls a single servo with arduino. based on this code i want to build a robot, But i am having trouble understanding the underlined part of the code  at the very bottom

/*
SparkFun Inventor's Kit
Example sketch 08


Servo servo1;  // servo control object


void setup()
{
  // We'll now "attach" the servo1 object to digital pin 9.
  // If you want to control more than one servo, attach more
  // servo objects to the desired pins (must be digital).

  // Attach tells the Arduino to begin sending control signals
  // to the servo. Servos require a continuous stream of control
  // signals, even if you're not currently moving them.
  // While the servo is being controlled, it will hold its
  // current position with some force. If you ever want to
  // release the servo (allowing it to be turned by hand),
  // you can call servo1.detach().

  servo1.attach(9);
}


void loop()
{
  int position;
 
  // To control a servo, you give it the angle you'd like it
  // to turn to. Servos cannot turn a full 360 degrees, but you
  // can tell it to move anywhere between 0 and 180 degrees.

  // Change position at full speed:

  servo1.write(90);    // Tell servo to go to 90 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(180);   // Tell servo to go to 180 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(0);     // Tell servo to go to 0 degrees

  delay(1000);         // Pause to get it time to move
 
  // Change position at a slower speed:

// To slow down the servo's motion, we'll use a for() loop
  // to give it a bunch of intermediate positions, with 20ms
  // delays between them. You can change the step size to make
  // the servo slow down or speed up. Note that the servo can't
  // move faster than its full speed, and you won't be able
  // to update it any faster than every 20ms.

  // Tell servo to go to 180 degrees, stepping by two degrees

  for(position = 0; position < 180; position += 2)
  {
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }

  // Tell servo to go to 0 degrees, stepping by one degree

  for(position = 180; position >= 0; position -= 1)
  {                               
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }
}
Title: Re: servo code
Post by: Jantje on Sep 06, 2013, 04:14 am
The sparkfun kit comes with lots of documentation and this is extremely well documented code. So I really wouldn't know what to add.
Title: Re: servo code
Post by: zoomkat on Sep 06, 2013, 04:19 am
#7 below would be a good place to start.

http://forum.arduino.cc/index.php/topic,148850.0.html
Title: Re: servo code
Post by: Ghostbusters on Sep 06, 2013, 01:09 pm
I THINK I NEED TO REPRASE THIS QUESTION. MY NEW QUESTION IS, WITH THE UNDERLINED CODE I DONT UNDERSTAND THE CODE AND WHAT MEANS WHAT TO ARDUINO. I AM MAKING A ROBOT AND I WANT TO USE SERVOS, HOWEVER THAT UNDERLINED CODE IS WHAT CONTROLS SPEED BUT I DONT UNDERSTAND HOW TO CONTROL THE SPEED WITH THAT CODE FORMAT. I HOPE YOU CAN UNDERSTAND MY QUESTION THANKS IN ADVANCE
Title: Re: servo code
Post by: AWOL on Sep 06, 2013, 04:23 pm
Quote
I THINK I NEED TO REPRASE rewrite THIS QUESTION without all the shoutiness.
Title: Re: servo code
Post by: Ghostbusters on Sep 06, 2013, 05:26 pm
sorry i didnt realize caps lock was on :smiley-red:
Title: Re: servo code
Post by: zoomkat on Sep 07, 2013, 05:44 am
Below is the servo sweep example from the arduino IDE. There is a delay between the small servo moves so the overall movement of the servo is slowed down.

Code: [Select]

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


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

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


void loop()
{
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // 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-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
Title: Re: servo code
Post by: JimboZA on Sep 07, 2013, 10:54 am
What your undelined code is doing is going from 0 to 180 in steps of 2 degrees... Each servo1.write(position) is 2 degrees further than the last write, since the for's position += 2 is incrementing in 2's. (Zoomcat's is similar, but in 1's). BUT, each pass through the for also includes a delay(20) (or 15 in zoomcat's example) which causes a pause of 20ms between each 2 degree movement step.

See here (http://arduino.cc/en/Reference/For) to read up on how "for" works.

Without that loop and delay, if the servo was at say 0 degrees and you sent it a servo1.write(180) it would have gone from 0 to 180 as fast as it was able. Servo speed is quoted by the makers as the time to move through 60 degrees, such as 0.16 seconds for this one (http://www.hobbyking.com/hobbyking/store/__3743__HXT_6_9kg_39_2g_16sec_Twin_bearing_servo.html). (That's the speed with no load, obviously slower when it's trying to move something.)

HTH?
Title: Re: servo code
Post by: Ghostbusters on Sep 08, 2013, 01:01 am
thanks guys you are awsome