Pages: [1]   Go Down
Author Topic: Moving a servo between 2 points  (Read 1752 times)
0 Members and 1 Guest are viewing this topic.
Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using an Adafruit motor shield to control a small robotics platform, the code for the motors works perfectly but the code for the servo will not stop moving after it reaches the end of its cycle. More specifically, the servo stays on, hums loudly because it is trying to overdrive its inner workings in either direction depending on which way you are trying to drive it.

I need 2 things to happen, the servo to move all the way to 0 and all the way to 180. (standard servo, not continuous)

and

Every time I power on this project the servo feels the need to move back to position 0. Can this be eliminated so that the servo just stays wherever it was last? If not not as big a deal as the previous problem.

Code:

#include <AFMotor.h>  //Add support for Motorshield Motor Library
#include <Servo.h> //Add support for Motorshield Servo Library
 
Servo myservo;  // create servo object to control servo (Pin10)
 
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm  - Right Track
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm  - Left Track
AF_DCMotor motor3(3, MOTOR12_1KHZ);  // create motor #3, 1KHz pwm  - Tilt Camera Mount
AF_DCMotor motor4(4, MOTOR12_1KHZ);  // create motor #4, 1KHz pwm  - Lights / Buzzer (Depending on Polarity)
 


void setup() {
myservo.attach(10); // Attaches servo1 on pin 10 to the servo object
Serial.begin(9600);           // Setup Serial library at 9600 bps
}
 
void loop() {
  // read the IO:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
 
    int speed; // Local variable
 
//Motor 1 - LOW Speed - LEFT Track
 
      switch (inByte) {
    case 'Q':   
      motor1.setSpeed(175);
      motor1.run(FORWARD);     // Motor Runs Forward
      break;
 
    case 'W':   
      motor1.run(RELEASE);     // Motor Stops
      break;
 
    case 'E':   
      motor1.setSpeed(175);
      motor1.run(BACKWARD);    // Motor Runs Backwards
      break;
 
//Motor 1 - HIGH Speed - LEFT Track
 
    case 'q':   
      motor1.setSpeed(225);
      motor1.run(FORWARD);     // Motor Runs Forward
      break;
 
    case 'w':   
      motor1.run(RELEASE);     // Motor Stops
      break;
 
    case 'e':   
      motor1.setSpeed(225);
      motor1.run(BACKWARD);    // Motor Runs Backwards
      break;
 
//Motor 2 - LOW Speed - RIGHT Track
 
    case 'A':   
      motor2.setSpeed(175);
      motor2.run(FORWARD);     // Motor Runs Forward
      break;
 
    case 'S':   
      motor2.run(RELEASE);     // Motor Stops
      break;
 
    case 'D':   
      motor2.setSpeed(175);
      motor2.run(BACKWARD);    // Motor Runs Backwards
      break;
 
//Motor 2 - HIGH Speed - RIGHT Track
 
    case 'a':   
      motor2.setSpeed(225);
      motor2.run(FORWARD);     // Motor Runs Forward
      break;
 
    case 's':   
      motor2.run(RELEASE);     // Motor Stops
      break;
 
    case 'd':   
      motor2.setSpeed(225);
      motor2.run(BACKWARD);    // Motor Runs Backwards
      break;
 
//Motor 3 - Custom Speed - PT Cam
 
    case 'z':   
      motor3.setSpeed(125);
      motor3.run(FORWARD);     // Motor Runs Forward
      break;
 
    case 'x':   
      motor3.run(RELEASE);     // Motor Stops
      break;
 
    case 'c':   
      motor3.setSpeed(125);
      motor3.run(BACKWARD);    // Motor Runs Backwards
      break;
 
 //Servo1 - Open and Close Claws
 
    case '1':   
      myservo.write(0);     // Closed
      break;
 
    case '2':     
      myservo.write(180);    // Open
      break;
 
    default:
      // turn all the connections off:
      for (int thisPin = 2; thisPin < 7; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    }
  }
}

Press keyboard key 1 for 0 degrees on servo.

Press keyboard key 2 for 180 degrees on servo.

I have tried changing the degree numbers and that didn't help, is there a line of code that perhaps is supposed to be added to stop the servo ?

Thanks,
Ec7
Logged

He who dares, Wins.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33966
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the servo to move all the way to 0 and all the way to 180.
Is your servo capable of such movement? It sounds like you are hitting the physical end stops.
Logged

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is supposed to be able to do only 180 degrees, I have tried changing the numbers to a smaller number than 180. For instance if you change 180 to 90, it will move and hit the stops anyways.

Update:

Okay still playing with it, if I change one of the numbers to 50, it will not hit the stop and the Servo cuts off like it is supposed to. But if I hit the key to make it go back to 0 degrees, it moves back but will vibrate and still hits the stop.
« Last Edit: May 03, 2012, 08:00:48 pm by EnigmaCypher7 » Logged

He who dares, Wins.

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, I can get the servo to stop vibrating if I change the 0 to larger number, 20 for an example. If I change the 180 to 60 this works as well.

Unfortunately when the shield first starts up the position still seems to move to 0 ish and the servo vibrates.

I added
Code:
int pos = 20;


Hoping the servo would start at 20, in range enough to stop vibrating, but it still seems to go to 0ish and isn't vibrating but isn't 20 either.

This is where I put this line:

#include <AFMotor.h>  //Add support for Motorshield Motor Library
#include <Servo.h> //Add support for Motorshield Servo Library
 
Servo myservo;  // create servo object to control servo (Pin10)
int pos = 20;    // variable to store the servo position

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm  - Right Track
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm  - Left Track
AF_DCMotor motor3(3, MOTOR12_1KHZ);  // create motor #3, 1KHz pwm  - Tilt Camera Mount
AF_DCMotor motor4(4, MOTOR12_1KHZ);  // create motor #4, 1KHz pwm  - Lights / Buzzer (Depending on Polarity)
 


void setup() {
myservo.attach(10); // Attaches servo1 on pin 10 to the servo object
Serial.begin(9600);           // Setup Serial library at 9600 bps
}
Logged

He who dares, Wins.

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As far as the project goes, problem solved it was the servo,

When this new servo is first fired up (Board is powered on) it goes to 90 degrees, I then changed to the following code:

Code:
case '1':   
      myservo.write(120);   
      break;
 
    case '2':     
      myservo.write(55);   
      break;
 

This moves as much as I need it to. The 90 Degrees is close enough to half way for this to work. I know I could always turn the mounting wheel thing so that 90 degrees was closed or open and readjust the numbers in the code.

Yay! =D

Thanks for the help Mike.

Sill a bit curious about:

Code:
int pos = 20;    // variable to store the servo position
Logged

He who dares, Wins.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33966
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

All that does is to declaire a variable you have to use it in setup to send the servo to that position, then put in a bit of a delay so it gets there, befor loop tells it to go some place else.
Logged

South Africa
Offline Offline
Sr. Member
****
Karma: 2
Posts: 406
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a novice question : but wouldn't the servo, on receiving power, need to travel to the zero point ( it's limit ) in one direction or another, so that it has a reference position to work from ?

If so, then how would you prevent this movement ? Even setting the servo position ( eg myservo.write(20) ) would make it ( when powered up ) travel to zero so that it then knows where the 20 point is.

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33966
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but wouldn't the servo, on receiving power, need to travel to the zero point ( it's limit ) in one direction or another, so that it has a reference position to work from ?
No, it is not a stepping motor, the position pot inside a servo gives an absoloute position, so there is no need to find a referance point.
Logged

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

Strangely, you can do a "Servo.write" before you do a "Servo.attach" to set the start "angle".
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.

Virginia, US
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am thinking the pot inside the first servo I used was misaligned, telling the servo shaft to go past the stop to reach the desired degrees. The servo was absolutely defective, it was doing stupid stuff with another code that I know works.

Awol;

Quote
Strangely, you can do a "Servo.write" before you do a "Servo.attach" to set the start "angle".

I will play with this idea when I get home, could be really useful for future projects.

--

Grumpy_Mike;

Quote
All that does is to declaire a variable you have to use it in setup to send the servo to that position, then put in a bit of a delay so it gets there, befor loop tells it to go some place else.

Yeah, that's what I thought it was supposed to do, but it didn't seem to be working, was the way I inserted it in my code wrong?


Thanks again for both of your help,
I will play with these some more then start learning about and playing with my new XBees, don't worry, not related to my other Theoretical question post =P
http://arduino.cc/forum/index.php/topic,103923.0.html
Logged

He who dares, Wins.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 619
Posts: 33966
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
was the way I inserted it in my code wrong?
Probably, but as you didn't post that bit I can't tell. The bit you posted had no movement in the setup.
Logged

Pages: [1]   Go Up
Jump to: