Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:5eb97170c199, 2015-03-25 (annotated)
- Committer:
- bbbobbbieo
- Date:
- Wed Mar 25 16:20:52 2015 +0000
- Revision:
- 2:5eb97170c199
- Parent:
- 1:e561f697985b
- Child:
- 3:e14a4ea167ca
drives around the track;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bbbobbbieo | 0:bf39986ebff7 | 1 | //#include "mbed.h" |
bbbobbbieo | 0:bf39986ebff7 | 2 | #include "TFC.h" |
bbbobbbieo | 0:bf39986ebff7 | 3 | #include <iostream> |
bbbobbbieo | 0:bf39986ebff7 | 4 | #include <stdio.h> |
bbbobbbieo | 0:bf39986ebff7 | 5 | //#include "serialib.h" |
bbbobbbieo | 0:bf39986ebff7 | 6 | |
bbbobbbieo | 0:bf39986ebff7 | 7 | const float AGGRESSIVE = .55; |
bbbobbbieo | 0:bf39986ebff7 | 8 | const float MODERATE =.48; |
bbbobbbieo | 2:5eb97170c199 | 9 | const float CONSERVATIVE =.39; |
bbbobbbieo | 0:bf39986ebff7 | 10 | const float STOP =0; |
bbbobbbieo | 0:bf39986ebff7 | 11 | const float PROTECTION_THRESHOLD_UPPER =.7; |
bbbobbbieo | 0:bf39986ebff7 | 12 | const float PROTECTION_THRESHOLD_LOWER =-.7; |
bbbobbbieo | 0:bf39986ebff7 | 13 | const float TURN_FORWARD_ACCEL =0.045; |
bbbobbbieo | 0:bf39986ebff7 | 14 | const float TURN_BACKWARD_ACCEL =0.025; |
bbbobbbieo | 0:bf39986ebff7 | 15 | const float SERVO_CAN_MOVE_IN_ONE_FRAME =0.1; |
bbbobbbieo | 0:bf39986ebff7 | 16 | const float SERVO_MAX =.5; |
bbbobbbieo | 0:bf39986ebff7 | 17 | |
bbbobbbieo | 0:bf39986ebff7 | 18 | const int BLACK_THRESHOLD =63; |
bbbobbbieo | 0:bf39986ebff7 | 19 | const int LINE_SCAN_LENGTH =128; |
bbbobbbieo | 0:bf39986ebff7 | 20 | |
bbbobbbieo | 0:bf39986ebff7 | 21 | |
bbbobbbieo | 0:bf39986ebff7 | 22 | DigitalOut myled(LED1); |
bbbobbbieo | 0:bf39986ebff7 | 23 | |
bbbobbbieo | 0:bf39986ebff7 | 24 | int main() |
bbbobbbieo | 0:bf39986ebff7 | 25 | { |
bbbobbbieo | 0:bf39986ebff7 | 26 | //run this before anything |
bbbobbbieo | 0:bf39986ebff7 | 27 | TFC_Init(); |
bbbobbbieo | 0:bf39986ebff7 | 28 | |
bbbobbbieo | 0:bf39986ebff7 | 29 | //variables |
bbbobbbieo | 0:bf39986ebff7 | 30 | float current_servo_position = 0; |
bbbobbbieo | 0:bf39986ebff7 | 31 | float previous_servo_position = 0; |
bbbobbbieo | 0:bf39986ebff7 | 32 | float current_left_motor_speed = 0; |
bbbobbbieo | 0:bf39986ebff7 | 33 | float current_right_motor_speed = 0; |
bbbobbbieo | 0:bf39986ebff7 | 34 | |
bbbobbbieo | 0:bf39986ebff7 | 35 | float proportional = 0; |
bbbobbbieo | 0:bf39986ebff7 | 36 | float last_proportional = 0; |
bbbobbbieo | 0:bf39986ebff7 | 37 | float integral = 0; |
bbbobbbieo | 0:bf39986ebff7 | 38 | float derivative = 0; |
bbbobbbieo | 0:bf39986ebff7 | 39 | float output = 0; |
bbbobbbieo | 0:bf39986ebff7 | 40 | |
bbbobbbieo | 0:bf39986ebff7 | 41 | // gains on prop, int, der |
bbbobbbieo | 0:bf39986ebff7 | 42 | // subject to change, need to fine tune |
bbbobbbieo | 0:bf39986ebff7 | 43 | float kp = 1.8960; |
bbbobbbieo | 0:bf39986ebff7 | 44 | float ki = 0.6170; |
bbbobbbieo | 0:bf39986ebff7 | 45 | float kd = 1.5590; |
bbbobbbieo | 0:bf39986ebff7 | 46 | |
bbbobbbieo | 0:bf39986ebff7 | 47 | bool rear_motor_enable_flag = true; |
bbbobbbieo | 0:bf39986ebff7 | 48 | bool linescan_ping_pong = false; |
bbbobbbieo | 0:bf39986ebff7 | 49 | bool linescan_enable = true; |
bbbobbbieo | 0:bf39986ebff7 | 50 | |
bbbobbbieo | 0:bf39986ebff7 | 51 | int black_values_list[LINE_SCAN_LENGTH]; |
bbbobbbieo | 0:bf39986ebff7 | 52 | int black_value_count = 0; |
bbbobbbieo | 0:bf39986ebff7 | 53 | int black_center_value = 0; |
bbbobbbieo | 0:bf39986ebff7 | 54 | int sum_black = 0; |
bbbobbbieo | 0:bf39986ebff7 | 55 | int violence_level = 0; |
bbbobbbieo | 0:bf39986ebff7 | 56 | |
bbbobbbieo | 0:bf39986ebff7 | 57 | int accelList[3]; |
bbbobbbieo | 0:bf39986ebff7 | 58 | int lastAccessed = 0; |
bbbobbbieo | 0:bf39986ebff7 | 59 | |
bbbobbbieo | 0:bf39986ebff7 | 60 | int centers_List[50]; |
bbbobbbieo | 0:bf39986ebff7 | 61 | |
bbbobbbieo | 0:bf39986ebff7 | 62 | int center_now = 63; |
bbbobbbieo | 0:bf39986ebff7 | 63 | int center_past_1 = 63; |
bbbobbbieo | 0:bf39986ebff7 | 64 | int center_past_2 = 63; |
bbbobbbieo | 0:bf39986ebff7 | 65 | int center_past_3 = 63; |
bbbobbbieo | 0:bf39986ebff7 | 66 | int center_past_4 = 63; |
bbbobbbieo | 0:bf39986ebff7 | 67 | //int best_guess_center = 64; |
bbbobbbieo | 0:bf39986ebff7 | 68 | |
bbbobbbieo | 0:bf39986ebff7 | 69 | int position = 0; |
bbbobbbieo | 0:bf39986ebff7 | 70 | int set_point = 63; |
bbbobbbieo | 0:bf39986ebff7 | 71 | int previous_error = 0; |
bbbobbbieo | 0:bf39986ebff7 | 72 | int error = 0; |
bbbobbbieo | 0:bf39986ebff7 | 73 | |
bbbobbbieo | 0:bf39986ebff7 | 74 | for(int i = 0; i < 50; i++) |
bbbobbbieo | 0:bf39986ebff7 | 75 | centers_List[i] = 63; |
bbbobbbieo | 0:bf39986ebff7 | 76 | |
bbbobbbieo | 0:bf39986ebff7 | 77 | float left_counter =0; |
bbbobbbieo | 0:bf39986ebff7 | 78 | float right_counter =0; |
bbbobbbieo | 0:bf39986ebff7 | 79 | bool turn_left=false; |
bbbobbbieo | 0:bf39986ebff7 | 80 | bool turn_right=false; |
bbbobbbieo | 0:bf39986ebff7 | 81 | |
bbbobbbieo | 2:5eb97170c199 | 82 | float bullshit_offset = .08; |
bbbobbbieo | 0:bf39986ebff7 | 83 | |
bbbobbbieo | 0:bf39986ebff7 | 84 | // major loop |
bbbobbbieo | 0:bf39986ebff7 | 85 | while(1) { |
bbbobbbieo | 0:bf39986ebff7 | 86 | |
bbbobbbieo | 0:bf39986ebff7 | 87 | // manual servo control, unused |
bbbobbbieo | 0:bf39986ebff7 | 88 | if (TFC_ReadPushButton(0) != 0 ) { |
bbbobbbieo | 0:bf39986ebff7 | 89 | current_servo_position = current_servo_position-.005; |
bbbobbbieo | 0:bf39986ebff7 | 90 | if(current_servo_position <= -0.4) |
bbbobbbieo | 0:bf39986ebff7 | 91 | current_servo_position = -0.4; |
bbbobbbieo | 0:bf39986ebff7 | 92 | TFC_SetServo(0, current_servo_position); |
bbbobbbieo | 0:bf39986ebff7 | 93 | }// end check button0 |
bbbobbbieo | 0:bf39986ebff7 | 94 | |
bbbobbbieo | 0:bf39986ebff7 | 95 | else {} |
bbbobbbieo | 0:bf39986ebff7 | 96 | |
bbbobbbieo | 0:bf39986ebff7 | 97 | |
bbbobbbieo | 0:bf39986ebff7 | 98 | // manual servo control, unused |
bbbobbbieo | 0:bf39986ebff7 | 99 | if (TFC_ReadPushButton(1) != 0 ) { |
bbbobbbieo | 0:bf39986ebff7 | 100 | current_servo_position = current_servo_position+.005; |
bbbobbbieo | 0:bf39986ebff7 | 101 | if(current_servo_position >= 0.4) |
bbbobbbieo | 0:bf39986ebff7 | 102 | current_servo_position = 0.4; |
bbbobbbieo | 0:bf39986ebff7 | 103 | TFC_SetServo(0, current_servo_position); |
bbbobbbieo | 0:bf39986ebff7 | 104 | }// end check button1 |
bbbobbbieo | 0:bf39986ebff7 | 105 | |
bbbobbbieo | 0:bf39986ebff7 | 106 | else {} |
bbbobbbieo | 0:bf39986ebff7 | 107 | |
bbbobbbieo | 0:bf39986ebff7 | 108 | // initial motor stuff |
bbbobbbieo | 0:bf39986ebff7 | 109 | if(rear_motor_enable_flag) { |
bbbobbbieo | 0:bf39986ebff7 | 110 | TFC_HBRIDGE_ENABLE; |
bbbobbbieo | 0:bf39986ebff7 | 111 | |
bbbobbbieo | 0:bf39986ebff7 | 112 | |
bbbobbbieo | 0:bf39986ebff7 | 113 | |
bbbobbbieo | 0:bf39986ebff7 | 114 | // checking behavior level |
bbbobbbieo | 0:bf39986ebff7 | 115 | violence_level = int(TFC_GetDIP_Switch()); |
bbbobbbieo | 0:bf39986ebff7 | 116 | |
bbbobbbieo | 0:bf39986ebff7 | 117 | if (violence_level==3) { |
bbbobbbieo | 0:bf39986ebff7 | 118 | current_left_motor_speed = -(AGGRESSIVE); |
bbbobbbieo | 0:bf39986ebff7 | 119 | current_right_motor_speed = AGGRESSIVE; |
bbbobbbieo | 0:bf39986ebff7 | 120 | } |
bbbobbbieo | 0:bf39986ebff7 | 121 | else if (violence_level==2) { |
bbbobbbieo | 0:bf39986ebff7 | 122 | current_left_motor_speed = -(MODERATE); |
bbbobbbieo | 0:bf39986ebff7 | 123 | current_right_motor_speed = (MODERATE); |
bbbobbbieo | 0:bf39986ebff7 | 124 | } |
bbbobbbieo | 0:bf39986ebff7 | 125 | else if (violence_level==1) { |
bbbobbbieo | 0:bf39986ebff7 | 126 | current_left_motor_speed = -(CONSERVATIVE); |
bbbobbbieo | 0:bf39986ebff7 | 127 | current_right_motor_speed = CONSERVATIVE; |
bbbobbbieo | 0:bf39986ebff7 | 128 | } |
bbbobbbieo | 0:bf39986ebff7 | 129 | else if (violence_level==0) { |
bbbobbbieo | 0:bf39986ebff7 | 130 | current_left_motor_speed = STOP; |
bbbobbbieo | 0:bf39986ebff7 | 131 | current_right_motor_speed = STOP; |
bbbobbbieo | 0:bf39986ebff7 | 132 | } |
bbbobbbieo | 0:bf39986ebff7 | 133 | else { |
bbbobbbieo | 0:bf39986ebff7 | 134 | current_left_motor_speed = STOP; |
bbbobbbieo | 0:bf39986ebff7 | 135 | current_right_motor_speed = STOP; |
bbbobbbieo | 0:bf39986ebff7 | 136 | } |
bbbobbbieo | 0:bf39986ebff7 | 137 | |
bbbobbbieo | 0:bf39986ebff7 | 138 | |
bbbobbbieo | 0:bf39986ebff7 | 139 | // protection block |
bbbobbbieo | 0:bf39986ebff7 | 140 | if(current_left_motor_speed >= PROTECTION_THRESHOLD_UPPER) |
bbbobbbieo | 0:bf39986ebff7 | 141 | current_left_motor_speed= PROTECTION_THRESHOLD_UPPER; |
bbbobbbieo | 0:bf39986ebff7 | 142 | if(current_right_motor_speed >= PROTECTION_THRESHOLD_UPPER) |
bbbobbbieo | 0:bf39986ebff7 | 143 | current_right_motor_speed = PROTECTION_THRESHOLD_UPPER; |
bbbobbbieo | 0:bf39986ebff7 | 144 | if(current_left_motor_speed <= PROTECTION_THRESHOLD_LOWER) |
bbbobbbieo | 0:bf39986ebff7 | 145 | current_left_motor_speed = PROTECTION_THRESHOLD_LOWER; |
bbbobbbieo | 0:bf39986ebff7 | 146 | if(current_right_motor_speed <= PROTECTION_THRESHOLD_LOWER) |
bbbobbbieo | 0:bf39986ebff7 | 147 | current_right_motor_speed = PROTECTION_THRESHOLD_LOWER; |
bbbobbbieo | 0:bf39986ebff7 | 148 | |
bbbobbbieo | 0:bf39986ebff7 | 149 | TFC_SetMotorPWM(current_left_motor_speed, current_right_motor_speed); |
bbbobbbieo | 0:bf39986ebff7 | 150 | }// end motor enabled |
bbbobbbieo | 0:bf39986ebff7 | 151 | else { |
bbbobbbieo | 0:bf39986ebff7 | 152 | TFC_HBRIDGE_DISABLE; |
bbbobbbieo | 0:bf39986ebff7 | 153 | }// end motor disabled |
bbbobbbieo | 0:bf39986ebff7 | 154 | |
bbbobbbieo | 0:bf39986ebff7 | 155 | // camera stuff |
bbbobbbieo | 0:bf39986ebff7 | 156 | if (linescan_enable) { |
bbbobbbieo | 0:bf39986ebff7 | 157 | if (TFC_LineScanImageReady !=0) { |
bbbobbbieo | 0:bf39986ebff7 | 158 | |
bbbobbbieo | 0:bf39986ebff7 | 159 | if (linescan_ping_pong) { |
bbbobbbieo | 0:bf39986ebff7 | 160 | //checking channel 0 |
bbbobbbieo | 0:bf39986ebff7 | 161 | |
bbbobbbieo | 0:bf39986ebff7 | 162 | //checking center pixel, displays aprox value on leds |
bbbobbbieo | 0:bf39986ebff7 | 163 | uint8_t shitnum = 1; |
bbbobbbieo | 0:bf39986ebff7 | 164 | |
bbbobbbieo | 0:bf39986ebff7 | 165 | |
bbbobbbieo | 0:bf39986ebff7 | 166 | // checking for center line (single line) |
bbbobbbieo | 0:bf39986ebff7 | 167 | for (uint16_t i=15; i<113; i++) { |
bbbobbbieo | 0:bf39986ebff7 | 168 | if ((*(TFC_LineScanImage0+i) < 450)) { |
bbbobbbieo | 0:bf39986ebff7 | 169 | black_values_list[black_value_count] = i; |
bbbobbbieo | 0:bf39986ebff7 | 170 | black_value_count++; |
bbbobbbieo | 0:bf39986ebff7 | 171 | } |
bbbobbbieo | 0:bf39986ebff7 | 172 | } |
bbbobbbieo | 0:bf39986ebff7 | 173 | |
bbbobbbieo | 0:bf39986ebff7 | 174 | for(int i=0; i<black_value_count; i++) { |
bbbobbbieo | 0:bf39986ebff7 | 175 | sum_black += black_values_list[i]; |
bbbobbbieo | 0:bf39986ebff7 | 176 | } |
bbbobbbieo | 0:bf39986ebff7 | 177 | |
bbbobbbieo | 0:bf39986ebff7 | 178 | //update history |
bbbobbbieo | 0:bf39986ebff7 | 179 | center_past_4= center_past_3; |
bbbobbbieo | 0:bf39986ebff7 | 180 | center_past_3= center_past_2; |
bbbobbbieo | 0:bf39986ebff7 | 181 | center_past_2= center_past_1; |
bbbobbbieo | 0:bf39986ebff7 | 182 | center_past_1= center_now; |
bbbobbbieo | 0:bf39986ebff7 | 183 | |
bbbobbbieo | 0:bf39986ebff7 | 184 | |
bbbobbbieo | 0:bf39986ebff7 | 185 | //if (black_value_count>2) |
bbbobbbieo | 0:bf39986ebff7 | 186 | center_now = sum_black / black_value_count; |
bbbobbbieo | 0:bf39986ebff7 | 187 | |
bbbobbbieo | 0:bf39986ebff7 | 188 | uint8_t num = 0; |
bbbobbbieo | 0:bf39986ebff7 | 189 | |
bbbobbbieo | 0:bf39986ebff7 | 190 | if(center_now > 15 && center_now < 27) |
bbbobbbieo | 0:bf39986ebff7 | 191 | num = 1; |
bbbobbbieo | 0:bf39986ebff7 | 192 | else if(center_now >= 27 && center_now < 54) |
bbbobbbieo | 0:bf39986ebff7 | 193 | num = 2; |
bbbobbbieo | 0:bf39986ebff7 | 194 | else if(center_now > 60 && center_now < 70) |
bbbobbbieo | 0:bf39986ebff7 | 195 | num = 15; |
bbbobbbieo | 0:bf39986ebff7 | 196 | else if(center_now >= 54 && center_now < 81) |
bbbobbbieo | 0:bf39986ebff7 | 197 | num = 4; |
bbbobbbieo | 0:bf39986ebff7 | 198 | else if(center_now >= 81 && center_now < 113) |
bbbobbbieo | 0:bf39986ebff7 | 199 | num = 8; |
bbbobbbieo | 0:bf39986ebff7 | 200 | |
bbbobbbieo | 0:bf39986ebff7 | 201 | else |
bbbobbbieo | 0:bf39986ebff7 | 202 | num = 0; |
bbbobbbieo | 0:bf39986ebff7 | 203 | |
bbbobbbieo | 2:5eb97170c199 | 204 | if (black_value_count<2) |
bbbobbbieo | 2:5eb97170c199 | 205 | num = 0; |
bbbobbbieo | 2:5eb97170c199 | 206 | |
bbbobbbieo | 0:bf39986ebff7 | 207 | TFC_SetBatteryLED(num); |
bbbobbbieo | 0:bf39986ebff7 | 208 | |
bbbobbbieo | 0:bf39986ebff7 | 209 | // best guess of center based on weighted average of history |
bbbobbbieo | 0:bf39986ebff7 | 210 | //black_center_value = (5*center_now + 10*center_past_1 + 15*center_past_2 +30*center_past_3 +40*center_past_4)/100; |
bbbobbbieo | 0:bf39986ebff7 | 211 | //black_center_value = (15*center_now + 15*center_past_1 + 15*center_past_2 +25*center_past_3 +30*center_past_4)/100; |
bbbobbbieo | 0:bf39986ebff7 | 212 | black_center_value = center_now; |
bbbobbbieo | 0:bf39986ebff7 | 213 | |
bbbobbbieo | 0:bf39986ebff7 | 214 | |
bbbobbbieo | 0:bf39986ebff7 | 215 | // turn left |
bbbobbbieo | 0:bf39986ebff7 | 216 | //if (black_center_value > BLACK_THRESHOLD+30) { |
bbbobbbieo | 2:5eb97170c199 | 217 | if (num==8 and right_counter <.2 ) |
bbbobbbieo | 0:bf39986ebff7 | 218 | { |
bbbobbbieo | 0:bf39986ebff7 | 219 | //left_counter += (128-black_center_value); |
bbbobbbieo | 0:bf39986ebff7 | 220 | //left_counter+=20; |
bbbobbbieo | 0:bf39986ebff7 | 221 | left_counter=-0.4; |
bbbobbbieo | 0:bf39986ebff7 | 222 | turn_left=true; |
bbbobbbieo | 0:bf39986ebff7 | 223 | turn_right=false; |
bbbobbbieo | 0:bf39986ebff7 | 224 | |
bbbobbbieo | 0:bf39986ebff7 | 225 | } |
bbbobbbieo | 2:5eb97170c199 | 226 | if (num==4 and right_counter <.2) |
bbbobbbieo | 0:bf39986ebff7 | 227 | { |
bbbobbbieo | 0:bf39986ebff7 | 228 | //left_counter += (128-black_center_value); |
bbbobbbieo | 0:bf39986ebff7 | 229 | //left_counter+=20; |
bbbobbbieo | 0:bf39986ebff7 | 230 | left_counter=-0.6; |
bbbobbbieo | 0:bf39986ebff7 | 231 | turn_left=true; |
bbbobbbieo | 0:bf39986ebff7 | 232 | turn_right=false; |
bbbobbbieo | 0:bf39986ebff7 | 233 | |
bbbobbbieo | 0:bf39986ebff7 | 234 | } |
bbbobbbieo | 0:bf39986ebff7 | 235 | |
bbbobbbieo | 0:bf39986ebff7 | 236 | // need to turn right |
bbbobbbieo | 0:bf39986ebff7 | 237 | //else if (black_center_value < BLACK_THRESHOLD-30) { |
bbbobbbieo | 2:5eb97170c199 | 238 | else if (num==1 and left_counter >-.2) |
bbbobbbieo | 0:bf39986ebff7 | 239 | { |
bbbobbbieo | 0:bf39986ebff7 | 240 | //right_counter += black_center_value; |
bbbobbbieo | 0:bf39986ebff7 | 241 | |
bbbobbbieo | 0:bf39986ebff7 | 242 | //right_counter +=20; |
bbbobbbieo | 0:bf39986ebff7 | 243 | right_counter =.4; |
bbbobbbieo | 0:bf39986ebff7 | 244 | turn_left=false; |
bbbobbbieo | 0:bf39986ebff7 | 245 | turn_right=true; |
bbbobbbieo | 0:bf39986ebff7 | 246 | |
bbbobbbieo | 0:bf39986ebff7 | 247 | } |
bbbobbbieo | 2:5eb97170c199 | 248 | else if (num==2 and left_counter >-.2) |
bbbobbbieo | 0:bf39986ebff7 | 249 | { |
bbbobbbieo | 0:bf39986ebff7 | 250 | //right_counter += black_center_value; |
bbbobbbieo | 0:bf39986ebff7 | 251 | //right_counter +=20; |
bbbobbbieo | 0:bf39986ebff7 | 252 | right_counter =.6; |
bbbobbbieo | 0:bf39986ebff7 | 253 | turn_left=false; |
bbbobbbieo | 0:bf39986ebff7 | 254 | turn_right=true; |
bbbobbbieo | 0:bf39986ebff7 | 255 | |
bbbobbbieo | 0:bf39986ebff7 | 256 | } |
bbbobbbieo | 0:bf39986ebff7 | 257 | //else if (black_value_count < 2) |
bbbobbbieo | 0:bf39986ebff7 | 258 | // { |
bbbobbbieo | 0:bf39986ebff7 | 259 | // turn_right=false; |
bbbobbbieo | 0:bf39986ebff7 | 260 | //// turn_left=false; |
bbbobbbieo | 0:bf39986ebff7 | 261 | // TFC_SetServo(0,0.0); |
bbbobbbieo | 0:bf39986ebff7 | 262 | // } |
bbbobbbieo | 2:5eb97170c199 | 263 | |
bbbobbbieo | 2:5eb97170c199 | 264 | //else if (num==0) |
bbbobbbieo | 2:5eb97170c199 | 265 | //{ |
bbbobbbieo | 2:5eb97170c199 | 266 | // TFC_SetServo(0,0.0); |
bbbobbbieo | 2:5eb97170c199 | 267 | //} |
bbbobbbieo | 2:5eb97170c199 | 268 | |
bbbobbbieo | 2:5eb97170c199 | 269 | |
bbbobbbieo | 2:5eb97170c199 | 270 | else if (turn_right == false and turn_left == false) |
bbbobbbieo | 2:5eb97170c199 | 271 | { |
bbbobbbieo | 2:5eb97170c199 | 272 | TFC_SetServo(0,(0.0+ bullshit_offset)); |
bbbobbbieo | 2:5eb97170c199 | 273 | } |
bbbobbbieo | 2:5eb97170c199 | 274 | |
bbbobbbieo | 0:bf39986ebff7 | 275 | else |
bbbobbbieo | 0:bf39986ebff7 | 276 | { |
bbbobbbieo | 1:e561f697985b | 277 | //turn_right=false; |
bbbobbbieo | 1:e561f697985b | 278 | //turn_left=false; |
bbbobbbieo | 1:e561f697985b | 279 | //TFC_SetServo(0,0.0); |
bbbobbbieo | 0:bf39986ebff7 | 280 | } |
bbbobbbieo | 0:bf39986ebff7 | 281 | |
bbbobbbieo | 0:bf39986ebff7 | 282 | //dealwiththeshit |
bbbobbbieo | 0:bf39986ebff7 | 283 | if(turn_left) |
bbbobbbieo | 0:bf39986ebff7 | 284 | { |
bbbobbbieo | 0:bf39986ebff7 | 285 | turn_right = false; |
bbbobbbieo | 0:bf39986ebff7 | 286 | //TFC_SetServo(0, ((left_counter)*(-.05))); |
bbbobbbieo | 0:bf39986ebff7 | 287 | TFC_SetServo(0,left_counter); |
bbbobbbieo | 2:5eb97170c199 | 288 | left_counter += .01; |
bbbobbbieo | 2:5eb97170c199 | 289 | if (left_counter > (0+ bullshit_offset)) |
bbbobbbieo | 1:e561f697985b | 290 | turn_left = false; |
bbbobbbieo | 2:5eb97170c199 | 291 | |
bbbobbbieo | 2:5eb97170c199 | 292 | TFC_SetMotorPWM(current_left_motor_speed+(.2*left_counter), current_right_motor_speed+(.2*left_counter)); |
bbbobbbieo | 1:e561f697985b | 293 | //left_counter -= 1; |
bbbobbbieo | 0:bf39986ebff7 | 294 | //if (left_counter < 10) |
bbbobbbieo | 0:bf39986ebff7 | 295 | // turn_left =false; |
bbbobbbieo | 0:bf39986ebff7 | 296 | } |
bbbobbbieo | 0:bf39986ebff7 | 297 | |
bbbobbbieo | 0:bf39986ebff7 | 298 | if(turn_right) |
bbbobbbieo | 0:bf39986ebff7 | 299 | { |
bbbobbbieo | 0:bf39986ebff7 | 300 | turn_left =false; |
bbbobbbieo | 0:bf39986ebff7 | 301 | //TFC_SetServo(0, ((right_counter)*(.05))); |
bbbobbbieo | 0:bf39986ebff7 | 302 | TFC_SetServo(0,right_counter); |
bbbobbbieo | 2:5eb97170c199 | 303 | right_counter -= .01; |
bbbobbbieo | 2:5eb97170c199 | 304 | if (right_counter < (0+ bullshit_offset)) |
bbbobbbieo | 1:e561f697985b | 305 | turn_right = false; |
bbbobbbieo | 2:5eb97170c199 | 306 | |
bbbobbbieo | 2:5eb97170c199 | 307 | TFC_SetMotorPWM(current_left_motor_speed-(.2*right_counter), current_right_motor_speed-(.2*right_counter)); |
bbbobbbieo | 1:e561f697985b | 308 | //right_counter -= 1; |
bbbobbbieo | 0:bf39986ebff7 | 309 | // if (right_counter < 10) |
bbbobbbieo | 0:bf39986ebff7 | 310 | // turn_right =false; |
bbbobbbieo | 0:bf39986ebff7 | 311 | } |
bbbobbbieo | 0:bf39986ebff7 | 312 | |
bbbobbbieo | 0:bf39986ebff7 | 313 | // clearing values for next image processing round |
bbbobbbieo | 0:bf39986ebff7 | 314 | black_value_count = 0; |
bbbobbbieo | 0:bf39986ebff7 | 315 | //black_center_value = 0; |
bbbobbbieo | 0:bf39986ebff7 | 316 | sum_black = 0; |
bbbobbbieo | 0:bf39986ebff7 | 317 | |
bbbobbbieo | 0:bf39986ebff7 | 318 | // end image processing |
bbbobbbieo | 0:bf39986ebff7 | 319 | |
bbbobbbieo | 0:bf39986ebff7 | 320 | linescan_ping_pong = false; |
bbbobbbieo | 0:bf39986ebff7 | 321 | } // end checking channel 0 |
bbbobbbieo | 0:bf39986ebff7 | 322 | |
bbbobbbieo | 0:bf39986ebff7 | 323 | else { //checking channel 1 |
bbbobbbieo | 0:bf39986ebff7 | 324 | linescan_ping_pong = true; |
bbbobbbieo | 0:bf39986ebff7 | 325 | } |
bbbobbbieo | 0:bf39986ebff7 | 326 | |
bbbobbbieo | 0:bf39986ebff7 | 327 | TFC_LineScanImageReady = 0; // since we used it, we reset the flag |
bbbobbbieo | 0:bf39986ebff7 | 328 | }// end imageready |
bbbobbbieo | 0:bf39986ebff7 | 329 | }// end linescan stuff |
bbbobbbieo | 0:bf39986ebff7 | 330 | } |
bbbobbbieo | 0:bf39986ebff7 | 331 | } |
bbbobbbieo | 0:bf39986ebff7 | 332 | |
bbbobbbieo | 0:bf39986ebff7 | 333 | // shit code down here |
bbbobbbieo | 0:bf39986ebff7 | 334 |