Go Down

### Topic: Continuous Servo, moving slowly (Read 4152 times)previous topic - next topic

#### RoboElite

##### Feb 20, 2011, 11:21 pm
I have two Parallax servo motors as wheels to move a robot. I was researching about the motors and how to slow them down. I understand you cant really control the speed of the servo but I did manage to find a sketch. The sketch uses ONE continuous servo to move slowly, counterclockwise or clockwise. I was very intrigued by this because it just might work for my design. However, I am using two continuous servos. I've tried to manipulate the sketch to work for two servos. Although I managed to make it work, I don't know how manipulate the code so that one servo goes clockwise and the other counterclockwise. Which is why I am seeking help. It will be very helpful if someone can review the code and help me change it so that one servo goes clockwise and the other goes counterclockwise, slowly and at the same time.
Code: [Select]
`int RW     =  7;    // right wheel int LW     =  8;            // left wheelint minPulse     =  1170;  // maximum servo speed clockwiseint maxPulse     =  1770; // maximum servo speed anticlockwiseint turnRate     =  75;  // servo turn rate increment (larger value, faster rate)int refreshTime  =  20;   // time (ms) between pulses (50Hz)/** The Arduino will calculate these values for you **/int centerServo;         // center servo positionint pulseWidth;          // servo pulse widthint moveServo;           // raw user inputlong lastPulse   = 0;    // recorded time (ms) of the last pulsevoid setup() {  pinMode(RW, OUTPUT);  // Set servo pin as an output pin  pinMode(LW, OUTPUT);  centerServo = maxPulse - ((maxPulse - minPulse)/2);  pulseWidth = centerServo;   // Give the servo a stop command  Serial.begin(115200);  Serial.println("Arduino Serial Continuous Rotation Servo Control");  Serial.println("          by Orfeus for GRobot.gr");  Serial.println("   Press < or > to move, spacebar to center");  Serial.println();}void loop() {  // wait for serial input  if (Serial.available() > 0) {    // read the incoming byte:    moveServo = Serial.read();    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)    if (moveServo == 44) {          pulseWidth = pulseWidth + turnRate;            }            if (moveServo == 46) {          pulseWidth = pulseWidth - turnRate;            }            if (moveServo == 32) {          pulseWidth = centerServo;            }    // stop servo pulse at min and max   if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }    if (pulseWidth < minPulse) { pulseWidth = minPulse; }         //print pulseWidth back to the Serial Monitor (comment to undebug)     Serial.print("Pulse Width: ");     Serial.print(pulseWidth);     Serial.println("us");  }  // pulse the servo every 20 ms (refreshTime) with current pulseWidth  // this will hold the servo's rotation and speed till we told it to do something else.  if (millis() - lastPulse >= refreshTime) {    digitalWrite(RW, HIGH);   // start the pulse    digitalWrite(LW, HIGH);   // start the pulse    delayMicroseconds(pulseWidth);  // pulse width    digitalWrite(RW, LOW);    // stop the pulse      digitalWrite(LW, LOW);    // stop the pulse            lastPulse = millis();           // save the time of the last pulse  }}`

#### zoomkat

#1
##### Feb 20, 2011, 11:44 pm
Below is some servo code I use to test two continous rotation servos using the serial monitor. With 1500us as the mid point the servo direction/speed is varied using ~+-100us or less from the 1500us.

Code: [Select]
`// zoomkat 11-22-10 serial servo (2) test// for writeMicroseconds, use a value like 1500// for IDE 0019 and later// Powering a servo from the arduino usually DOES NOT WORK.// two servo setup with two servo commands// send eight character string like 15001500 or 14501550#include <Servo.h> String readString, servo1, servo2;Servo myservo1;  // create servo object to control a servo Servo myservo2;void setup() {  Serial.begin(9600);  myservo1.attach(6);  //the pin for the servo control   myservo2.attach(7);  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); //see what was received            // expect a string like 07002100 containing the two servo positions            servo1 = readString.substring(0, 4); //get the first four characters      servo2 = readString.substring(4, 8); //get the next four characters             Serial.println(servo1);  //print to serial monitor to see results      Serial.println(servo2);            int n1; //declare as number        int n2;            char carray1[6]; //magic needed to convert string to a number       servo1.toCharArray(carray1, sizeof(carray1));      n1 = atoi(carray1);             char carray2[6];      servo2.toCharArray(carray2, sizeof(carray2));      n2 = atoi(carray2);             myservo1.writeMicroseconds(n1); //set servo position       myservo2.writeMicroseconds(n2);    readString="";  } }`
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

#### RoboElite

#2
##### Feb 21, 2011, 12:15 am
I would like to use the code I have because it works better with my design but I need both servos to rotate together, one clockwise and the other counter clockwise.

#### zoomkat

#3
##### Feb 21, 2011, 12:20 am
Quote
I would like to use the code I have because it works better with my design but I need both servos to rotate together, one clockwise and the other counter clockwise.

One way would be to subtract the current value of one servo from its max position and use the result for the other servo's position.
`int RW     =  7;    // right wheel int LW     =  8;            // left wheelint minPulse     =  1170;  // maximum servo speed clockwiseint maxPulse     =  1770; // maximum servo speed anticlockwiseint turnRate     =  75;  // servo turn rate increment (larger value, faster rate)int refreshTime  =  20;   // time (ms) between pulses (50Hz)/** The Arduino will calculate these values for you **/int centerServo;         // center servo positionint pulseWidth;          // servo pulse widthint moveServo;           // raw user inputlong lastPulse   = 0;    // recorded time (ms) of the last pulsevoid setup() {  pinMode(RW, OUTPUT);  // Set servo pin as an output pin  pinMode(LW, OUTPUT);  centerServo = maxPulse - ((maxPulse - minPulse)/2);  pulseWidth = centerServo;   // Give the servo a stop command  Serial.begin(115200);  Serial.println("Arduino Serial Continuous Rotation Servo Control");  Serial.println("          by Orfeus for GRobot.gr");  Serial.println("   Press < or > to move, spacebar to center");  Serial.println();}void loop() {  // wait for serial input  if (Serial.available() > 0) {    // read the incoming byte:    moveServo = Serial.read();    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)    if (moveServo == 44) {           pulseWidth = pulseWidth + turnRate;            }            if (moveServo == 46) {           pulseWidth = pulseWidth - turnRate;             }            if (moveServo == 32) {           pulseWidth = centerServo;             }    // stop servo pulse at min and max   if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }    if (pulseWidth < minPulse) { pulseWidth = minPulse; }         //print pulseWidth back to the Serial Monitor (comment to undebug)     Serial.print("Pulse Width: ");     Serial.print(pulseWidth);     Serial.println("us");  }  // pulse the servo every 20 ms (refreshTime) with current pulseWidth  // this will hold the servo's rotation and speed till we told it to do something else.  if (millis() - lastPulse >= refreshTime) {    digitalWrite(RW, HIGH);   // start the pulse    digitalWrite(LW, HIGH);   // start the pulse    delayMicroseconds(pulseWidth);  // pulse width    digitalWrite(RW, LOW);    // stop the pulse       digitalWrite(LW, LOW);    // stop the pulse            lastPulse = millis();           // save the time of the last pulse  }}`