Arduino Forum

Topics => Robotics => Topic started by: oldPGMguy on Jun 21, 2011, 06:34 pm

Title: I can't get sevo to work right
Post by: oldPGMguy on Jun 21, 2011, 06:34 pm
building my first simple robot....the problem is , I can't get the many versions of code i tried to work right...I am using continous rotation servo's...if i run code, it just keeps on running and won't stop or change its direction...i googled and tried code from everywhere...how do i stop,reverse and forward with these continous rotation servos, without buying a external shield....or is it that a arduino can't do it?
Title: Re: I can't get sevo to work right
Post by: robtillaart on Jun 21, 2011, 07:09 pm
put all your code in setup() and it will stop after executing it once.

If you need a more elaborate advice you could post your code...
Title: Re: I can't get sevo to work right
Post by: retrolefty on Jun 21, 2011, 08:02 pm
Quote
how do i stop,reverse and forward with these continous rotation servos, without buying a external shield....or is it that a arduino can't do it?


By using the servo commands correctly. You will have to post your code for us to help you with this.

Lefty
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 12:26 am
ok, here's the code.....its supposed to rotate for 1 360 degree turn , then pause for 10 seconds and start the loop over

Code: [Select]

#include <Servo.h>

Servo servo1;
int pos;
int x;

void setup()
{
   servo1.attach(9);

}

void loop() {
  pos = 0;  // use 0 for forward,90 for stop,180 for reverse
  for(x=0; x<361; x++)
  {
  servo1.writeMicroseconds(pos);  // servo forward
delay(15);
  }
  delay(10000);
}

Title: Re: I can't get sevo to work right
Post by: retrolefty on Jun 22, 2011, 01:26 am
Also your program will have no way to know when the servo has completed turning a single 360 degrees as there is no feedback signal to test. A modified servo can only go forward/reverse/stop, there is no way to say go 360 degrees and then stop. You might be able to manually time the speed of the servo (learn it's RPM) and use a delay equal to that measurement, but as it's also a variable speed modified servo it may well be difficult to get any kind of accuracy using that method.

Servos modified for continous and bidirection variable speed is kind of a limited device to work with. I'm always amazed that so many newcomers buy them first rather then working with standard unmodified servos first. I suspect that far too many of them pick them because they think they are getting an 'extra' feature without realizing that they are giving up a equally or even more important feature, position control.

Lefty

Title: Re: I can't get sevo to work right
Post by: zoomkat on Jun 22, 2011, 01:41 am
Some simple servo test code you can use to see how your servo works.

Code: [Select]

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray);
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  }
}

Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 01:42 am
x is just a do nothing for loop.......if the servo is at 0, the motor should turn over and over again....but it turns then stops after a while.....i already know how to control a stardard sevo......i can run the code just to turn the servo at 0 position , but if i use the 180 position it should reverse and if i use 90 it should stop....but none of this works....in PBASIC for the basic stamp a simple PULSOUT 9,0 does the trick....maybe the arduino can't do it
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 01:47 am
Zoomcat your code rotated the servo for a while then stopped...just like my code
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 01:50 am
guess i'll have to dust off my old basic stamp2 and use it for a robot...don't think the arduino can handle continous rotation on a servo...or just use a DC motor with a h-bridge with the arduino board
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 01:54 am
@KE7GKP...it works with a basic stamp....i want it to run continous until i tell it to stop or backup if a object is detected with infrared detector.....the servo is fine, it works....its just the arduino can't do a pulsout command like the stamp can
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 01:59 am
look ..all i want to do is, run the servo forward continous...if object is detected, then stop, then back up and turn and run again
its simple to do in PBASIC...but I can't do it in C...it shouldn't be that hard
Title: Re: I can't get sevo to work right
Post by: retrolefty on Jun 22, 2011, 02:00 am

@KE7GKP...it works with a basic stamp....i want it to run continous until i tell it to stop or backup if a object is detected with infrared detector.....the servo is fine, it works....its just the arduino can't do a pulsout command like the stamp can


If a basic stamp can make the servo do what you wish, then a arduino most certainly can. Your problem is that you have not yet figured out how to translate a basic program (used in your stamp) to a valid c/c++ language (used by the arduino). So bottom line it can, but you can't (as of yet). Possibly if you can post the basic stamp program here we/someone can help you with the conversion required.

Lefty
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 02:08 am
thanks lefty..hers what i want to do....maybe i am saying it wrong...ok here is a PBASIC program

Code: [Select]
'BS2 Robot Demo
'By oldELECTROguy
'Shows How To Use BS2 For Robotic Use
'Cyles Servos and Bumper Switches

'----------------------------- Constants --------------------------------------------

dirs=%11111111     ' Make all I/O Pins Output
Mstop     Con 150   ' Brake Servo
MfFor     Con 200   ' Run Servo Foward Fast
MsFor     Con 160   ' Run Servo Forward Slow
MfRev     Con 80    ' Run Servo Reverse Fast
MsRev     Con 140   ' Run Servo Reverse Slow
VOn       Con 2     ' Enable Talk (For Talk Chip - ISD1110P
VOff      Con 3     ' Disable Talk
SDelay    Con 15    ' 10 millisecond delay for Servo

'------------------------------Variables----------------------------------------------

RBump   Var Bit    'Sensor 1=OK 0=Touched Something
LBump   Var Bit    'Bit = 0 to 1
X       Var Byte   'X = 0 to 255
Y       Var Nib    'Y = 0 to 15
Z       Var Word   'Z = 0 to 65535

'-------------------------------MAIN LOOP---------------------------------------------

again:
gosub forward
gosub Fslow
gosub Back
gosub BackS
gosub Brake
gosub Soundme
goto again

'------------------------------SUB ROUTINES-------------------------------------------
forward:
debug "Forward",cr
for x=1 to 200
pulsout 0,mffor
pulsout 1,mffor
pause sdelay
next
return

Fslow:
debug "Forward Slow",cr
for x=1 to 200
pulsout 0,msfor
pulsout 1,msfor
pause sdelay
next
return

Back:
debug "Reverse",cr
for x=1 to 200
pulsout 0,mfrev
pulsout 1,mfrev
pause sdelay
next
return

BackS:
debug "Reverse Slow",cr
for x=1 to 200
pulsout 0,msrev
pulsout 1,msrev
pause sdelay
next
return

Brake:
debug "Brake",cr
for z=1 to 1000
pulsout 0,mStop
pulsout 1,mStop
pause sdelay
next
return

SOUNDME:
debug "Danger Will Robinson",cr
freqout 7,1500,1000,1200
pause 5
freqout 7,1000,1200,1800
pause 5
return
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 02:10 am
@KE7GKP the servo works, i just tested it with a basic stamp...it runs forward, stops when i tell it, and reverses when i tell it
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 02:39 am
@KE7GKP....it works both the analog and digital ports....i used all the code i could find...it just doesn't work right...just give me one piece of code that can rotate the servo for atleast 10 seconds and stop then start again
Title: Re: I can't get sevo to work right
Post by: zoomkat on Jun 22, 2011, 02:41 am
Quote
don't think the arduino can handle continous rotation on a servo.


Sure it does. You just need to understand how they work. I have two continous rotation servos and my code controls them without issue. Often people have issues with their servos due to inadequate power supplies. How is your servo powered? Below is a recomended setup using an external power supply.

(http://web.comporium.net/~shb/pix/servo-wire.jpg)
Title: Re: I can't get sevo to work right
Post by: retrolefty on Jun 22, 2011, 02:47 am
Ok here are the key things to utilise from your basic program to arudino code:

Quote

Stamp uses:
SDelay    Con 15    ' 10 millisecond delay for Servo
Mstop     Con 150   ' Brake Servo
MfFor     Con 200   ' Run Servo Foward Fast
MsFor     Con 160   ' Run Servo Forward Slow
MfRev     Con 80    ' Run Servo Reverse Fast
MsRev     Con 140   ' Run Servo Reverse Slow

Arduino would use:

#define Mstop  1500  // Brake Servo
#define MfFor  2000  // Run Servo Foward Fast
#define MsFor  1600  // Run Servo Forward Slow
#define MfRev  800   // Run Servo Reverse Fast
#define MsRev  1400 // Run Servo Reverse Slow
#define SDelay  2000    // 2 second delay for Servo

And where your stamp program uses:


forward:
debug "Forward",cr
for x=1 to 200
pulsout 0,mffor
pulsout 1,mffor
pause sdelay
next
return

// and

Brake:
debug "Brake",cr
for z=1 to 1000
pulsout 0,mStop
pulsout 1,mStop
pause sdelay
next
return



your arduino code would use:

void forward(void)
{
  Serial.println("Forward");
  myservo.writeMicroseconds(MfFor);
  delay(SDelay);  // delay for 2 secs
}

// and


void Brake(void)
{
   Serial.println("Break");
   myservo.writeMicroseconds(Mstop);
   delay(SDelay * 5); //delay for 10 sec
}



Certainly not a complete sketch, and probably not the best use of the C syntex, but it's more to show a simple conversion from basic to C equivelent.  It shows the critical servo statment and constant values to duplicate what your basic stamp program uses. Hope that's enough to get you headed in the right direction. Now after having to read a basic program I feel kind of dirty and need to go take a shower.  :D

Lefty
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 03:02 am
thanks RETROLEFTY

here is the code i came up with as a test it works.......

Code: [Select]

#include <Servo.h>

Servo myservo;
int pos;
int x;

void forward(){
pos = 2000;  // use 1000 or 2000 for forward or backward
  myservo.writeMicroseconds(pos);  // servo forward
delay(15);
}

void reverse(){
pos = 1000;  // use 1000 or 2000 for forward or backward
  myservo.writeMicroseconds(pos);  // servo forward
delay(15);
}


void servoStop(){
pos = 1500;  // use 1000 or 2000 for forward or backward
  myservo.writeMicroseconds(pos);  // servo forward
delay(15);
}

void setup()
{
   myservo.attach(9);

}

void loop() {
for(x=0; x<1000; x++){
  forward(); }

for(x=0; x<1000; x++){
  servoStop(); }

for(x=0; x<1000; x++){
  reverse(); }
  }

Title: Re: I can't get sevo to work right
Post by: retrolefty on Jun 22, 2011, 03:11 am
Quote
here is the code i came up with as a test it works.......


Great, you got the idea. The arduino servo library code is automatically handling (and hiding from you) a lot of the stuff that the stamp code had to deal with.

See we can help you 'unlearn' all that nasty BASIC stuff and help you on the journey to learning a real programming language.  ;)

Lefty
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 22, 2011, 03:15 am
Hey..i want to learn C....RetroLefty.....thanks to all...for the help
Title: Re: I can't get sevo to work right
Post by: PaulS on Jun 22, 2011, 02:54 pm
The servo library has two functions to send data to a servo - write() and writeMicroseconds(). Continuous rotation "servos" are generally commanded using writeMicroseconds().

The values for writeMicroseconds() generally range from about 500 to about 2500. The lowest value causes fastest speed in one direction. As the values get larger, the speed gets smaller. At some point, around 1500, the "servo" stops altogether. As the values continue to increase, the direction changes, and the servo moves slowly in the other direction, increasing speed up to the upper limit.

Try a loop that calls Servo::writeMicroseconds() with values from 500 to 2500, in steps of 100, with a 1 second delay between calls.
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 25, 2011, 10:42 pm
ok here's what i came up with......it works great

Code: [Select]
// Continous Rotation Servo Test
// By Herman O Darr (OldElectroGuy)
// June 12 2011
// Revised June 25 2011
// Use Numbers between 0 to 180...90 is usually stop
// Adjust for your servo's using the pot adjust,if installed
// this is for continous rotating servo's...not standard
// servo's from 0 to 180 degree's rotation only



//----------------------- Include's -------------------------

#include <Servo.h>

//------------------------ Variables -------------------------

Servo servo;             // Define servo

//-------------------------- Setup ----------------------------
void setup() {
    servo.attach(9);     // Set servo to digital pin 9
}
//----------------------- Main Loop ------------------------

void loop() {            // Loop through motion tests
  forward();           
  stopRobot();
  reverseslow();
  stopRobot();
  forwardslow();
  stopRobot();
  reverse();
  stopRobot();
}
//------------------------ Sub Voids ------------------------


     // Motion routines for forward, reverse, slow, and stop
void forward() {
   servo.write(0);delay(6000);   }

void reverse() {
  servo.write(180);delay(6000);    }

void stopRobot() {
   servo.write(90);delay(5000);    }
   
void forwardslow() {
   servo.write(88);delay(6000);   }
   
void reverseslow() {
   servo.write(92);delay(6000);   }   
Title: Re: I can't get sevo to work right
Post by: oldPGMguy on Jun 25, 2011, 10:48 pm
@PaulS....yeah i did that with 1000 being forward and 2000 being reverse and 1500 being stop......if you have servo's with an adjustable pot you can set the stop with what ever number yo use....thanks for the info code
Title: Re: I can't get sevo to work right
Post by: gelfling6 on Jul 11, 2011, 04:21 am
It also depends on ho the Constant Rotation servos are wired.. Even though I saw a plan for converting standard servos to C-R type, using 2x 2.2K resistors wired in series, across the old POT pins, with the center bridge to the wiper solder point, I never liked the idea of sloppy tolerance on the resistors. (and, isn't 2.2K a bit low? I thought the POTs were closer to 10K end to end?)

I've done this with modified HiTec & Futaba servos, grinding down the stop tab, and replacing the POT with a mini PC mount 10K, accessible through a side hole... Even replaced the two C-R servos on an old BOE-Bot that the motors had begun screaming.

The servo code works with the VEX Motor modules (essentially Futababa servos with the post ground down, and a square key in the center, but with a custom chip for the driver).

Unlike the VEX controller code to motors, there is no constant pulse going to the signal pins, just for the brief moment you digitalwrite to the pin, or servowrite to the pin.. Once you finish that instruction, the servo would stop anyways, because you are no-longer sending the pulses to it. just a dead GND signal.  Same goes with the Basic Stamp, once you write the pulsout, there is no constant signal.. the delay, just gives the code time to stop, while the servo performs the movement, then waits for the next pulse. (with the VEX, if you wrote out to the motors, the 2nd PIC inside would maintain the pulses, until you told it to stop, which lead to a lot of run-away problems if the code crashed.)

Title: Re: I can't get sevo to work right
Post by: mem on Jul 11, 2011, 08:10 am

Unlike the VEX controller code to motors, there is no constant pulse going to the signal pins, just for the brief moment you digitalwrite to the pin, or servowrite to the pin.. Once you finish that instruction, the servo would stop anyways, because you are no-longer sending the pulses to it. just a dead GND signal.  Same goes with the Basic Stamp, once you write the pulsout, there is no constant signal.. the delay, just gives the code time to stop, while the servo performs the movement, then waits for the next pulse. (with the VEX, if you wrote out to the motors, the 2nd PIC inside would maintain the pulses, until you told it to stop, which lead to a lot of run-away problems if the code crashed.)


The Arduino Servo library is constantly sending pulses to the servos (every 20ms) without requiring any attention from the sketch.
You only need to write a new values when you want to change the angle, or as in this application, speed.