Society of Robots - Robot Forum

Software => Software => Topic started by: Xyver on March 10, 2010, 08:40:08 PM

Title: Making servos drive backwards? SOLVED!
Post by: Xyver on March 10, 2010, 08:40:08 PM
i've been writing the code for my robot (photovore)  and this is what it looks like so far.

#include <Servo.h>

Servo leftservo;  // create servo object to control a servo
Servo rightservo;

int potpin0 = 0;  // analog pin used to connect the potentiometer
int left;    // variable to read the value from the analog pin
int potpin1 = 1;
int right;
void setup()
{
  leftservo.attach(7);  // attaches the servo on pin 9 to the servo object
  rightservo.attach(8);
}

void loop()
{
  left = analogRead(potpin0);            // reads the value of the potentiometer (value between 0 and 1023)
  right = analogRead(potpin1);
  left = map(left, 0, 1023, 0, 179);
  right = map(right, 0, 1023, 0, 179);  


I'm pretty sure the beginning is right, but once i get past the void loop () part I have no idea what to do.  So far, it says (or at least i think it does/want it to) that left is the value that is read from potpin0, and right is the value read from potpin1.  It then converts it from 0-1023 to the 0-180 that the servo can read.  Now, I;m trying to make it say that if left < right, then turn left.  If left > right, then turn right.  And if left == right, then go straight.  How do I do that?

(I already tried downloading the pre written code for the roboduino, but for some reason its not letting me open the file, or at least i cannot find the code.  So I'm writing my own.)
Title: Re: Making servos drive...?
Post by: Razor Concepts on March 10, 2010, 09:00:37 PM
if(left < right)
{
..
}
else if(right < left)
{
}
else
{
}

etc etc.
Title: Re: Making servos drive...?
Post by: Xyver on March 10, 2010, 09:11:40 PM
I got that much but what goes between the {   }'s? left wheel has to spin faster to turn right, and right wheel has to turn faster to go left....
Title: Re: Making servos drive...?
Post by: teoxan on March 11, 2010, 08:51:44 AM


Why do they have to move faster?

If you want to turn left, you stop the left servo and move the right one and vice versa.

As far as I understand, your code is Arduino or Wiring based, so it's easy to do that.

So, it's like:

leftservo.write(0);
rightservo.write( speed parameter);

and vice versa if you want to move right.



Theo


Title: Re: Making servos drive...?
Post by: Xyver on March 11, 2010, 12:06:25 PM
Ok thank you :) I figured that this morning... its amazing what you learn when you sleep haha.  I'll probably be posting the finished code too soon if I can't trouble shoot it myself
Title: Re: Making servos drive backwards?
Post by: Xyver on March 12, 2010, 06:00:56 PM
I seem to be having trouble with making my servos go backwards.  When you modify them, its supposed to be that a number between 0-89 is backwards, 90 is neutral, and 91-180 is forewards right?  But when I try, 0 is neutral and anything above is forewards... How can I make it go backwards? Or did I modify it wrong?

( I glued the potentiometer, not soldered resistors. And these http://www.robotshop.ca/hitec-hs422-servo-motor.html (http://www.robotshop.ca/hitec-hs422-servo-motor.html) are the servos I used)

And this is the simple code I wrote:

#include <Servo.h>

Servo leftservo;

void setup()
{
 leftservo.attach(8);
}

void loop()
{
leftservo.write(200);

}

Title: Re: Making servos drive backwards?
Post by: Razor Concepts on March 12, 2010, 06:59:42 PM
Did you glue the potentiometer while the servo was in the neutral position?
Title: Re: Making servos drive backwards?
Post by: Xyver on March 12, 2010, 07:08:09 PM
I believe so.  I was very careful following the instructions, listed here: http://www.societyofrobots.com/actuators_modifyservo.shtml. (http://www.societyofrobots.com/actuators_modifyservo.shtml.)  I sent the signal and made sure the servo gears had stopped rotating the best i could, it was stopped but would sometimes twitch (after like 10-20 sec), so i thought i was close enough. If i tried to make it perfect, it would start spinning like mad.  When I was glueing it, it was blowing on the glue to dry it and the gears started spinning slightly so I adjusted it till they stopped again.  

I tried the servo.write() command, where it should be 0-89 is back, 90 is neutral and 91-180 is forewards.  I also tried the servo.writeMicroseconds(), where 1000 is back, 1500 is neutral, and 2000 is forewards.  Nothing seems to be working....


EDIT::  AHA! When I was told to send the signal to the servo to know where to glue the potentiometer, it said send it to neutral position.  I sent it go to 0 position.  So that means that anything above 0 is forewards and below 0 is backwards, right?  So, If i redo it with 90 being neutral then it should work, correct? AND IT DOES WORK!!!! stupid me for mis interpreting instructions....

THANKS ALOT FOR YOUR PATIENCE/HELP!