Pages: [1] 2   Go Down
Author Topic: Noob Question about Servo Controls  (Read 1846 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I am just a beginner in C++.

I have learnt to control Servos using the Arduino Uno

But i bought a Servo Shield from Renbotics and the codes are pretty different.

Code:
#include <ServoShield.h>

ServoShield servos;                       //Create a ServoShield object to control up to 16 servos

void setup()
{
  for (int servo = 0; servo < 16; servo++)//Initialize all 16 servos
  {
    servos.setbounds(servo, 1000, 2000);  //Set the minimum and maximum pulse duration of the servo
    servos.setposition(servo, 1500);      //Set the initial position of the servo
  }
 
  servos.start();                         //Start the servo shield
}

void loop()
{
  for(int pos = 1000; pos < 2000; pos++)  //Move the servos from 0 degrees to 180 degrees
  {                                       //in steps of 1 degree
    for (int i = 0; i < 16; i++)          //for all 16 servos
      servos.setposition(i, pos);        //Tell servo to go to position in variable 'pos'
     delay(1);                           //waits 15ms for the servos to reach the position
  }
 
  for(int pos = 2000; pos >= 1000; pos--) //Move the servos from 180 degrees to 0 degrees
  {                               
    for (int i = 0; i < 16; i++)          //all 16 servos
      servos.setposition(i, pos);        //Tell servo to go to position in variable 'pos'
    delay(1);                            //waits 15ms for the servos to reach the position
  }
}

This is a Demo Code to control 16 Servos at once.

This board has 16 pins.

But since i am building a Hexapod, I want to be able to control 1 servo at a time. Can someone enlighten me on how do i go about changing this code to control on servo and a way to add another servo?

Please guide me.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the manual on the Renbotics Servo Shield page...

Functions

int setposition(int servo, int position);
Sets the position of the specified servo. Returns 0 if successfully set; returns 1 if
instruction failed.

int setbounds(int servo, int minposition, int maxposition);
Sets the valid maximum and minimum bounds of the specified servo; returns 1 if
instruction failed.
Defaults are 1000 and 2000

int getposition(int Servo);
Returns the current position of the specified servo.

int start();
Starts the servo controller; returns 1 if instruction failed.

int stop();
Stops the servo controller; returns 1 if instruction failed.

So you start() the shield, then use setposition() to move the servos you want to use. The servo connections are numbered on the shield.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the manual on the Renbotics Servo Shield page...

Functions

int setposition(int servo, int position);
Sets the position of the specified servo. Returns 0 if successfully set; returns 1 if
instruction failed.

int setbounds(int servo, int minposition, int maxposition);
Sets the valid maximum and minimum bounds of the specified servo; returns 1 if
instruction failed.
Defaults are 1000 and 2000

int getposition(int Servo);
Returns the current position of the specified servo.

int start();
Starts the servo controller; returns 1 if instruction failed.

int stop();
Stops the servo controller; returns 1 if instruction failed.

So you start() the shield, then use setposition() to move the servos you want to use. The servo connections are numbered on the shield.


Sorry i am too dumb to understand this. Dont mind can you show me a sample code of moving one servo?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <ServoShield.h>

ServoShield servos;                       //Create a ServoShield object to control up to 16 servos

void setup()
{
    servos.setbounds(1, 1000, 2000);  //Set the minimum and maximum pulse duration of the servo
    servos.setposition(1, 1500);      //Set the initial position of the servo
  }
 
  servos.start();                         //Start the servo shield
}

void loop()
{
  for(int pos = 1000; pos < 2000; pos++)  //Move servo 1 from 0 degrees to 180 degrees
  {                                       //in steps of 1 degree
    servos.setposition(1, pos);        //Tell servo to go to position in variable 'pos'
   delay(1);                           //waits 15ms for the servos to reach the position
  }
 
  for(int pos = 2000; pos >= 1000; pos--) //Move servo 2 from 180 degrees to 0 degrees
  {                               
    servos.setposition(1, pos);        //Tell servo to go to position in variable 'pos'
    delay(1);                            //waits 15ms for the servos to reach the position
  }
}

This is untested, as I don't have that servo shield.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <ServoShield.h>

ServoShield servos;                       //Create a ServoShield object to control up to 16 servos

void setup()
{
    servos.setbounds(1, 1000, 2000);  //Set the minimum and maximum pulse duration of the servo
    servos.setposition(1, 1500);      //Set the initial position of the servo
  }
 
  servos.start();                         //Start the servo shield
}

void loop()
{
  for(int pos = 1000; pos < 2000; pos++)  //Move servo 1 from 0 degrees to 180 degrees
  {                                       //in steps of 1 degree
    servos.setposition(1, pos);        //Tell servo to go to position in variable 'pos'
   delay(1);                           //waits 15ms for the servos to reach the position
  }
 
  for(int pos = 2000; pos >= 1000; pos--) //Move servo 2 from 180 degrees to 0 degrees
  {                               
    servos.setposition(1, pos);        //Tell servo to go to position in variable 'pos'
    delay(1);                            //waits 15ms for the servos to reach the position
  }
}

This is untested, as I don't have that servo shield.

Apparently it does nothing:(
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did the original code work?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26338
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd operate on servo zero, not servo one.
Maybe the OP's servo is plugged into the wrong output for the code given.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good spot. The shield has the servo connections numbered 1-16, so I made the mistake of thinking they would make their library match up.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd operate on servo zero, not servo one.
Maybe the OP's servo is plugged into the wrong output for the code given.

Hey so does that mean i have to change to the following line?

Code:
#include <ServoShield.h>

ServoShield servos;                       //Create a ServoShield object to control up to 16 servos

void setup()
{
    servos.setbounds(0, 1000, 2000);  //Set the minimum and maximum pulse duration of the servo
    servos.setposition(0, 1500);      //Set the initial position of the servo
  }
 
  servos.start();                         //Start the servo shield
}

void loop()
{
  for(int pos = 1000; pos < 2000; pos++)  //Move servo 1 from 0 degrees to 180 degrees
  {                                       //in steps of 1 degree
    servos.setposition(1, pos);        //Tell servo to go to position in variable 'pos'
   delay(1);                           //waits 15ms for the servos to reach the position
  }
 
  for(int pos = 2000; pos >= 1000; pos--) //Move servo 2 from 180 degrees to 0 degrees
  {                               
    servos.setposition(1, pos);        //Tell servo to go to position in variable 'pos'
    delay(1);                            //waits 15ms for the servos to reach the position
  }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49396
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hey so does that mean i have to change to the following line?
What happens when you do that? You are the one with the hardware.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26338
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd change the setposition calls too, but, as has already been pointed out, you're the one with the hardware.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK i got it to work.

Code:
#include <ServoShield.h>

ServoShield servo;                       //Create a ServoShield object to control up to 1 Servo

void setup()
{
    servo.setbounds(0, 1000, 2000);  //I still have no idea how this works, but putting this enable all servo to maintain this bounds.
    servo.setposition(0, 1500);      //Enables all servos to maintain pos of 1500, essential for the Hexapod to stand.
  }
 
  servos.start();                         //Start the servo shield
}

void loop()
{
  for(int pos = 1500; pos < 3000; pos++)  //This is the top left foot code, Top left foot is No. 0.
  {                                       //in steps of 1 degree
    servos.setposition(0, pos);        //Tell servo no.0  to raise its feet 90 Deg left
   delay(1);                           //waits 1 sec for the servos to reach the position
  }

  for(int pos = 1500; pos < 2000; pos++)  //This is the top left thighs. Top left thigh is No.1
  {                                       //in steps of 1 degree
    servos.setposition(1, pos);        //Tell servo no. 1 to move the thigh fowards
   delay(1);                           //waits 1 sec for the servos to reach the position
  }
 
  for(int pos = 2000; pos >= 1000; pos--) //This is back to Servo no.0, the top left foot.
  {                               
    servos.setposition(0, pos);        //Tell servo no. 0 to put down the left foot.
    delay(1);                            //waits 1 secs for the servos to reach the position
  }
}

This code has successfully moved the Top left leg of the 6 legged Hexapod (2DOF) to raise its foot, move forward and put down its foot.

The question is, i want to control the other 2 legs ( Bottom Left leg and Middle Right leg) simultaniously to achieve the gait walking sequence.

Can someone enlighten me how can i control 3 servos at the same time?

Thanks a Million!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK i got it to work.

Code:
#include <ServoShield.h>

ServoShield servo;                       //Create a ServoShield object to control up to 1 Servo

void setup()
{
    servo.setbounds(0, 1000, 2000);  //I still have no idea how this works, but putting this enable all servo to maintain this bounds.
    servo.setposition(0, 1500);      //Enables all servos to maintain pos of 1500, essential for the Hexapod to stand.
  }
 
  servos.start();                         //Start the servo shield
}

void loop()
{
  for(int pos = 1500; pos < 3000; pos++)  //This is the top left foot code, Top left foot is No. 0.
  {                                       //in steps of 1 degree
    servos.setposition(0, pos);        //Tell servo no.0  to raise its feet 90 Deg left
   delay(1);                           //waits 1 sec for the servos to reach the position
  }

  for(int pos = 1500; pos < 2000; pos++)  //This is the top left thighs. Top left thigh is No.1
  {                                       //in steps of 1 degree
    servos.setposition(1, pos);        //Tell servo no. 1 to move the thigh fowards
   delay(1);                           //waits 1 sec for the servos to reach the position
  }
 
  for(int pos = 2000; pos >= 1000; pos--) //This is back to Servo no.0, the top left foot.
  {                               
    servos.setposition(0, pos);        //Tell servo no. 0 to put down the left foot.
    delay(1);                            //waits 1 secs for the servos to reach the position
  }
}

This code has successfully moved the Top left leg of the 6 legged Hexapod (2DOF) to raise its foot, move forward and put down its foot.

The question is, i want to control the other 2 legs ( Bottom Left leg and Middle Right leg) simultaniously to achieve the gait walking sequence.

Can someone enlighten me how can i control 3 servos at the same time?

Thanks a Million!


Ok guys guess what, I actually used the search function in this forum and gotten an answer.

This piece of code has allowed my 6legged gait walk for the first time!! (wooohooo!, I am as excited as a new father!)

After 2 hours of studying the forum, i came up with this code.

Code:
#include <ServoShield.h>

ServoShield L0;                       //Creates a ServoShield object to control every single Servo one by one
ServoShield L1;
ServoShield L2;
ServoShield L3;
ServoShield L4;
ServoShield L5;
ServoShield R16;
ServoShield R15;
ServoShield R14;
ServoShield R13;
ServoShield R12;
ServoShield R11;

int pos1;                              // Creates a integer to tell Different movements.
int pos2;
int pos3;
int pos4;
int pos5;
int pos16;
int pos15;
int pos14;
int pos13;
int pos12;
int pos11;

void setup()
{
 
  {
    L0.setbounds(0, 1000, 2000);     // Left Top Foot                              //sets the Max and Min bounds of the each servos.
    L1.setbounds(1, 1000, 2000);     // Left Top Thigh
    L2.setbounds(2, 1000, 2000);     // Left Mid Foot
    L3.setbounds(3, 1000, 2000);     // Left Mid Thigh
    L4.setbounds(4, 1000, 2000);     // Left Bom Foot
    L5.setbounds(5, 1000, 2000);     // Left Bom Thigh
    R16.setbounds(16, 1000, 2000);   // Right Top Foot
    R15.setbounds(15, 1000, 2000);   // Right Top Thigh
    R14.setbounds(14, 1000, 2000);   // Right Mid Foot
    R13.setbounds(13, 1000, 2000);   // Right Mid Thigh
    R12.setbounds(12, 1000, 2000);   // Right Bom Foot
    R11.setbounds(11, 1000, 2000);   // Right Bom Thigh
   
   
    L0.setposition(0, 1500);         //Left top foot                                                               //Sets the Starting position of each Servos
    L1.setposition(1, 1500);         //Left top thigh
    L2.setposition(2, 1500);         //Left Middle foot
    L3.setposition(3, 1500);         //Left Middle thigh
    L4.setposition(4, 1500);         //Left Bottom Foot
    L5.setposition(5, 1500);         //Left Bottom thigh
    R16.setposition(16, 1500);       //Right Top Foot
    R15.setposition(15, 1500);       //Right Bottom Thigh
    R14.setposition(14, 1500);       //Right Middle Foot
    R13.setposition(13, 1500);       //Right Middle thigh
    R12.setposition(12, 1500);       //Right Bottom Foot
    R11.setposition(11, 1500);       //Right Bottom Thigh           
   
 }
 
  L0.start();       //Starts the servo shield plus the pin 1-16
  L1.start();
  L2.start();
  L3.start();
  L4.start();
  L5.start();
  R16.start();
  R15.start();
  R14.start();
  R13.start();
  R12.start();
  R11.start(); 
 
}

void loop()
//Start of Gait Step one, Brings Left Top, left Bottom and Right middle Foot up and Moves thier thighs forwards
  {                                 
    for (int i = 0; i < 1500; i += 1)
      {
      pos1 = i + 500;
         L0.setposition(0,pos1);                 //This moves the top left foot up'
         L1.setposition(1,pos1);                 //This moves the top left thigh forwartd
         L4.setposition(4,pos1);                 //This moves the Bottom left foot up
         L5.setposition(5,pos1);                 //This moves the Bottom left thigh Forward
         R14.setposition(14,pos1);               //This moves the middle Right foot up
         R13.setposition(13,pos1);               //This moves the middle right thigh Forward
         delay(1);                           //waits 1sec for the servos to reach the position
  }
// End of gait step one 
//Start of gait step two, This brings all the foot Down
                               
    for (int i = 0; i < 1500; i += 1)
      {
      pos2 = i - 500;
         L0.setposition(0,pos2);                 //This moves the top left foot down'
         L4.setposition(4,pos2);                 //This moves the Bottom left foot down
         R14.setposition(14,pos2);               //This moves the middle Right foot down
       
         delay(1);                           //waits 1sec for the servos to reach the position
  }
//End of gait step two. All foots are down on the floor.

//start of Gait step Three, Brings back the thighs to original Position at the same time brings the other 3 Legs into movement position.
                                 
    for (int i = 0; i < 1500; i += 1)
      {
      pos3 = i - 500;
      pos4 = i + 500;

         L1.setposition(1,pos3);                 //This moves the top left thigh backwards
         L5.setposition(5,pos3);                 //This moves the Bottom left thigh backwards
         R13.setposition(13,pos3);               //This moves the middle right thigh backwards
         R16.setposition(16,pos4);               //This moves the Right top foot to raise
         R15.setposition(15,pos4);               //This moves the Right top thigh forward
         R12.setposition(12,pos4);               //This moves the Right bottom foot to raise
         R11.setposition(11,pos4);               //This moves the Right bottom thigh forward
         L2.setposition(2,pos4);                 //This moves the Left middle foot to raise
         L3.setposition(3,pos4);                 //This moves the left middle thigh forward
         delay(1);                           //waits 1sec for the servos to reach the position
      }
//end of gait step three
//Start of gait step four, Puts all foot down on the floor

                                 
    for (int i = 0; i < 1500; i += 1)
      {
      pos5 = i - 500;
         R16.setposition(16,pos5);               //This moves the Right top foot to the floor
         R12.setposition(12,pos5);               //This moves the Right bottom foot to the floor
         L2.setposition(2,pos5);                 //This moves the Left middle foot to the floor
         delay(1);                           //waits 1sec for the servos to reach the position
  }
 
 
  //rinse and repeat



}

I wrote down all the comments so that i can refer to them as an when i need to compile.

This code works and my Hexapod moved.

The only problem i have is, how do i loop the whole thingy? It runs fine but after the first 2 steps, its starts jerking as if a limping dog.

I guess is the arrangement of my codes which needs to connect the loop somwhere.

can anyone advice where do i begin?

Also. i read somewhere that the function "int pos1" actually tells the code to store informations on "pos1"

Do i need many "int"s to make Arduino memorise all my steps or can i just use back the "int"


Please advice.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26338
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can anyone advice where do i begin?
Start to learn about arrays and for loops.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <ServoShield.h>

ServoShield L0;                       //Creates a ServoShield object to control every single Servo one by one
ServoShield L1;
ServoShield L2;
ServoShield L3;
ServoShield L4;
ServoShield L5;
ServoShield R16;
ServoShield R15;
ServoShield R14;
ServoShield R13;
ServoShield R12;
ServoShield R11;


You only have one servoshield, why are you making 12 objects representing it? Each object (L0, L3, R14, etc are all objects) can control all of the 16 servos via the first input to the functions. So, to move each servo to a different angle, you'd say

Code:
ServoShield servos;

void setup() {
  servos.setposition(0, 45);
  servos.setposition(1, 90);
  servos.setposition(2, 30);
  servos.setposition(3, 60);
  servos.setposition(4, 120);
}
void loop(){}

To do it with arrays:
Code:
const int servoNums[] = {2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15}; //Each number is the port of a servo in some order. Let's say that it's Left 0, 1, 2, 3, 4, 5 and Right 0, 1, 2, 3, 4, 5
// Const means they can't change
ServoShield servos;

int servoPositions[] = {45, 90, 30, 60, 120, 150, 180, 210, 240, 0, 135, 215}; // Positions of each of the servos in the same order as before. These can change.

void setup() {
 
  for (int i=0; i<12;i++) { //There are 12 items to loop through, so go until i is >= 12
     servos.setposition(servoNums[i], servoPositions[i]);
  }
}
void loop() {}
Logged

Pages: [1] 2   Go Up
Jump to: