Log in

View Full Version : Turning problems in Vex autonomous.


jamesgecko
09-03-2006, 00:20
I wrote the following code using easyC for Vex.
It's a function, it's supposed to take the angle I give it and turn in that direction. If I give it a positive angle, it turns to the right. I'm trying to make it turn to the left if I give it a negative angle, but it ain't happening. For some reason it simply turns to the right again. Any ideas?


#include "Main.h"

void turn ( int angle )
{ // Turns the robot a set number of degrees
int done = 0;
int abs;

angle = angle/4 ; //For the optical sensors, one turn is 90 pulses.
if ( angle < 0 )
{
abs = -angle ;
}
else
{
abs = angle ;
}
PresetEncoder ( L_OPTICAL_SENSOR_CHANNEL , Left_Optical_Sensor ) ;
PresetEncoder ( R_OPTICAL_SENSOR_CHANNEL , Right_Optical_Sensor ) ;
StartEncoder ( L_OPTICAL_SENSOR_CHANNEL ) ;
StartEncoder ( R_OPTICAL_SENSOR_CHANNEL ) ;
while ( done != 2 )
{
Left_Optical_Sensor = GetEncoder ( L_OPTICAL_SENSOR_CHANNEL ) ;
Right_Optical_Sensor = GetEncoder ( R_OPTICAL_SENSOR_CHANNEL ) ;
if ( Left_Optical_Sensor < abs )
{
if ( angle > 0 & angle <= 90 )
{
SetMotor ( L_MOTOR , 255 ) ;
}
if ( angle >= -90 & angle < 0 )
{
SetMotor ( L_MOTOR , 0 ) ;
}
}
else
{
SetMotor ( L_MOTOR , 127 ) ;
done = done + 1 ;
StopEncoder ( L_OPTICAL_SENSOR_CHANNEL ) ;
}
if ( Right_Optical_Sensor < abs )
{
if ( angle > 0 & angle <= 90 )
{
SetMotor ( R_MOTOR , 255 ) ;
}
if ( angle >= -90 & angle < 0 )
{
SetMotor ( R_MOTOR , 0 ) ;
}
}
else
{
SetMotor ( R_MOTOR , 127 ) ;
done = done + 1 ;
StopEncoder ( R_OPTICAL_SENSOR_CHANNEL ) ;
}
}
return ;
}

Keith Watson
09-03-2006, 00:58
if ( angle > 0 & angle <= 90 )This should be the logical operator &&, not the bit operator &.

Personally, I always write multiple expressions in the following form. It is more readable and forces the precedence I want. I have to type a few more keystrokes but it is worth the correctness and clarity. if ( (angle > 0) && (angle <= 90) )

Otherwise someone who knows the functions you are calling will have to help you.

koenig3456
09-03-2006, 07:18
Since the wheels are mounted on opposite sides of the robot, they need to be operated opposite from each other. For example: To drive straight you would use:
SetMotor ( L_MOTOR , 255 ) ;
SetMotor ( R_MOTOR , 0 ) ;

Try swapping the SetMotor ( R_MOTOR , 255 ) ; and SetMotor ( R_MOTOR , 0 ) ; calls.

jamesgecko
09-03-2006, 16:41
Thanks, Keith. It works great now.

koenig3456: But I want the robot to turn... ;-)