/*MOTORS*/
#define MOTOR_1 0 /*left*/
#define MOTOR_2 1 /*right */
#define MOTOR_3 4 /*chomper*/
#define MOTOR_4 2
#define MOTOR_5 3
#define MOTOR_6 5
/*SENSORS*/
#define LEFT_DIST_PORT 4 /*left*/
#define RIGHT_DIST_PORT 2 /*right*/
#define LEFT_ENCODER 7
#define RIGHT_ENCODER 8
#define TOP_LEFT_SWITCH 22
#define TOP_RIGHT_SWITCH 23
#define BOTTOM_RIGHT_SWITCH 24
#define BOTTOM_LEFT_SWITCH 25
#define START_PORT 5
#define TOP_LEFT_LIGHT 16
#define TOP_RIGHT_LIGHT 17
#define BOTTOM_LEFT_LIGHT 18
#define BOTTOM_RIGHT_LIGHT 20
#define LEFT_LINE 21
#define RIGHT_LINE 22
#define IR_1 12
#define IR_2 13
#define IR_3 14
#define IR_4 15
#define BALL_SWITCH 30
/*sensors*/
#define ENCODER_THRESHOLD 5
#define LIGHT_THRESHOLD 1
#define DIST_SAMPLES 5
int LEFT_WALL_DIST = 95;
int FAR_LEFT_WALL_DIST= 55;
int RIGHT_WALL_DIST= 95;
int FAR_RIGHT_WALL_DIST = 55;
#define DIST_THRESHOLD 5
#define FAR_DIST_THRESHOLD 2
#define FORWARD_THRESHOLD 10
#define VEER_TIME .1
#define DEFAULT_VEER_POWER 66
#define MOTOR_CORRECTION 50
#define LINE_THRESHOLD 3
#define TOP_LEFT_LIGHT_ADJUST 5
#define LIGHT_SAMPLES 25
#define SHAFT_THRESHOLD 2
/*turning*/
#define TICKS_90 65
#define WALL_FOLLOW 1
#define NO_WALL_FOLLOW 0
#define LFORWARD 1
#define RFORWARD 2
#define LEFT 3
#define RIGHT 4
#define BACK 5
#define BACKUP_TICKS 75
/***GLOBALS***********************/
int color=-1; /*1 for blue 0 for white*/
int topleft=-1;
int topright=-1;
int leftline=-1;
int rightline=-1;
int rflag=0;
int lflag=0;
int ttick=0;
int balls=0;
int moving=0;
int rtickstogo=0;
int ltickstogo=0;
int wall_follow_dist=0;
int cflag=0;
/***FUNCTIONS*********************/
void setup() {
ao();
balls=0;
enable_encoder(LEFT_ENCODER);
enable_encoder(RIGHT_ENCODER);
}
void activate_ball_switch()
{
while(balls<4)
{
if (!digital(BALL_SWITCH))
{
++balls;
while(digital(BALL_SWITCH));
}
}
stop_chomp();
}
void stop_chomp()
{
drive(MOTOR_6, 0);
drive(MOTOR_3, 0);
drive(MOTOR_4, 0);
drive(MOTOR_5, 0);
}
void orient()
{
int i;
while(1)
{
if (digital(IR_2))
{
left(90);
beep();
break;
}
else if (digital(IR_3))
{
left(180);
beep();
break;
}
else if (digital(IR_4))
{
right(90);
beep();
break;
}
else if (digital(IR_1))
{
break;
}
}
}
/**********************************************************************
* Drives both motors at the indicated speed. Note that the handyboard
* only has 7 speeds, so 0, 16, 33, 50, 66, 83, and 100 are really
* the only speeds that mean much.
**********************************************************************/
void drive(int port, int speed) /*drive is an alias for motor*/
{
motor(port, speed);
}
void drive_ticks(int motor_port, int encoder_port, int ticks, int power)
{
int currticks=0;
reset_encoder(encoder_port);
drive(motor_port, power);
while (1)
{
currticks=read_encoder(encoder_port);
if (currticks>ticks)
{
drive(motor_port, 0);
++ttick;
break;
}
sleep(.01);
}
}
/**********************************************************************
* Drive forward at full speed for the number of ticks indicated by
* dist.
*********************************************************************/
void chomp()
{
drive(MOTOR_6, 100);
drive(MOTOR_3, 100);
drive(MOTOR_4, 100);
drive(MOTOR_5, 100);
}
void forward(int totaldist, int wall_follow_flag, int port, int ideal) {
int i, avg, j;
int CPID=-1;
int thresh=0;
if ((ideal==FAR_LEFT_WALL_DIST)||(ideal==FAR_RIGHT_WALL_DIST))
{
thresh=FAR_DIST_THRESHOLD;
}
else
{
thresh=DIST_THRESHOLD;
}
wall_follow_dist=ideal;
if (port==LEFT_DIST_PORT)
{
moving=LFORWARD;
}
else
{
moving=RFORWARD;
}
reset_encoder(LEFT_ENCODER);
reset_encoder(RIGHT_ENCODER);
fd(MOTOR_1);
fd(MOTOR_2);
if (!wall_follow_flag)
{
while(1)
{
if ((read_encoder(RIGHT_ENCODER)>=totaldist) && (read_encoder(LEFT_ENCODER)>=totaldist))
{
stop();
break;
}
else if ((read_encoder(RIGHT_ENCODER) - read_encoder(LEFT_ENCODER)) >=FORWARD_THRESHOLD)
{
/*drop power to right side*/
drive(MOTOR_2, DEFAULT_VEER_POWER);
drive(MOTOR_1, 100);
}
else if ((read_encoder(LEFT_ENCODER) - read_encoder(RIGHT_ENCODER)) >=FORWARD_THRESHOLD)
{
/*drop power to left*/
drive(MOTOR_1, DEFAULT_VEER_POWER);
drive(MOTOR_2, 100);
}
}
}
else if (wall_follow_flag)
{
while(1)
{
avg=analog(port);
fd(MOTOR_1);
fd(MOTOR_2);
if (abs(ideal-avg) > thresh)
{ /*we gotta do something*/
if ((ideal-avg) > 0)
{
/*turn into wall*/
if (port==LEFT_DIST_PORT)
{
veer_left(1);
}
else
{
veer_right(1);
}
}
else
{
/*get outta there*/
if (port==LEFT_DIST_PORT)
{
veer_right(1);
}
else
{
veer_left(1);
}
}
if (read_encoder(RIGHT_ENCODER)>totaldist)
{
break;
}
if (cflag)
{
break;
}
ltickstogo=totaldist-read_encoder(LEFT_ENCODER);
rtickstogo=totaldist-read_encoder(RIGHT_ENCODER);
}
}
}
stop();
if (cflag)
{
cflag=0;
forward_cm();
}
moving=0;
}
void forward_cm()
{
tone(800.0, .5);
backup(BACKUP_TICKS);
if (moving==LFORWARD)
{
if (wall_follow_dist