final project  1
drive.c
Go to the documentation of this file.
1 
11 #include "main.h"
12 
13 short speed = 150; //linear speed
14 short turnSpeed = 150; //speed to turn
15 
16 float turnScalar = 5; //use this to calibrate turn-ness per robot
17 float distScalar = 2.5; //use this to calibrate linear distance - bigger number = shorter dist
18 
19 
20 char sumData[3]; //temp var, used for debug
21 
22 float sum = 0;
23 short ret = 0; //return value, includes distance traveled, bump detect, and groundData
24 char bumpL = 0; //left bump data
25 char bumpR = 0; //right bump data
26 
28 
32 void turn_right(oi_t *sensor, int degrees) { //turns clockwise degrees degrees
33  sum = 0;
34  oi_update(sensor);
35  oi_setWheels(-turnSpeed, turnSpeed);
36  while (sum < degrees) {
37  oi_update(sensor);
38  sum += -turnScalar * (float) sensor->angle;
39  timer_waitMillis(20); //wait for sensors to update
40  }
41  stopmoving(); // stop
42 }
43 
45 
49 void turn_left(oi_t *sensor, int degrees) { //turns anticlockwise
50  sum = 0;
51  oi_update(sensor);
52  oi_setWheels(turnSpeed, -turnSpeed);
53  while (sum < degrees) {
54  oi_update(sensor);
55  sum += turnScalar * (float) sensor->angle;
56  timer_waitMillis(20); //wait for sensors to update
57  }
58  stopmoving(); // stop
59 
60 }
61 
63 
67 short move_back(oi_t *sensor, short cm) {
68  short sumBack = 0;
69  oi_update(sensor);
70  oi_setWheels(-speed, -speed);
71  while (abs(sumBack) < cm*10) {
72  oi_update(sensor);
73  sumBack += distScalar * (float)sensor->distance; //measure traveled distance, scale to fit
74  }
75  stopmoving();
76  return sumBack;
77 }
78 
80 
84 short move_forward(oi_t *sensor, short cm) { //moves forward cm
85  sum = 0;
86  ret = 0;
87  oi_update(sensor);
88  oi_setWheels(speed, speed);
89 
90  while ((sum) < cm*10) {
91 
92  sum += distScalar * (float)sensor->distance; //measure traveled distance, scale to fitt
93 
94  oi_update(sensor);
95  bumpL = sensor->bumpLeft; //get bumper data
96  bumpR = sensor->bumpRight;
97 
98  //detect hole/tape
99  char groundDat = getGroundData(sensor);
100  //if sensor is tripped and bot is moving forward
101  if(groundDat) {
102  stopmoving(); //stop, don't cross line or hole
103  ret += 100*groundDat; //return which sensor was tripped
104  break; //end loop
105  }
106 
107 
108  //detect bump
109  //if the bumpers are hit, then stop, backup, turn
110  if(bumpL || bumpR) {
111  stopmoving();
112  //ret += 6; //account for the backup amount (that way we see 1000 or 2000 instead of 995 or 1995
113  move_back(sensor, 5); // moves back 5
114  timer_waitMillis(200); //wait to ensure bump sensors are clear
115  if (bumpL) { //if left (or both) gets hit
116  ret += 1000;
117  break;
118  } else if (bumpR) { // if right gets hit
119  ret += 2000;
120  break;
121  }
122  }
123  }
124  stopmoving();
125  return ret + (int)sum/10;
126 }
127 
129 
132 void stopmoving() {
133  oi_setWheels(0, 0);
134 }
void turn_left(oi_t *sensor, int degrees)
Turns the robot left.
Definition: drive.c:49
short move_back(oi_t *sensor, short cm)
Moves the robot back.
Definition: drive.c:67
the main code for the robot to interact with the user
short move_forward(oi_t *sensor, short cm)
Moves the robot forward.
Definition: drive.c:84
iRobot Create Sensor Data
void stopmoving()
Stops robot from moving.
Definition: drive.c:132
void turn_right(oi_t *sensor, int degrees)
Turns the robot right.
Definition: drive.c:32
char getGroundData(oi_t *sensors)
checks the ground for tape or a hole
Definition: groundSensor.c:33