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@25:806e67a1218f, 2015-03-28 (annotated)
- Committer:
- bbbobbbieo
- Date:
- Sat Mar 28 20:53:49 2015 +0000
- Revision:
- 25:806e67a1218f
- Parent:
- 23:a141ca857b8f
- Child:
- 27:aa81c15e5c1d
reverted back to code that ran the track well. added a stop function for end line detection;
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 | 25:806e67a1218f | 8 | const float MODERATE =.48; | 
| bbbobbbieo | 25:806e67a1218f | 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 | 25:806e67a1218f | 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 | 25:806e67a1218f | 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 | 25:806e67a1218f | 87 | // manual servo control, unused | 
| bbbobbbieo | 25:806e67a1218f | 88 | if (TFC_ReadPushButton(0) != 0 ) { | 
| bbbobbbieo | 25:806e67a1218f | 89 | current_servo_position = current_servo_position-.005; | 
| bbbobbbieo | 25:806e67a1218f | 90 | if(current_servo_position <= -0.4) | 
| bbbobbbieo | 25:806e67a1218f | 91 | current_servo_position = -0.4; | 
| bbbobbbieo | 25:806e67a1218f | 92 | TFC_SetServo(0, current_servo_position); | 
| bbbobbbieo | 25:806e67a1218f | 93 | }// end check button0 | 
| bbbobbbieo | 25:806e67a1218f | 94 | |
| bbbobbbieo | 25:806e67a1218f | 95 | else {} | 
| bbbobbbieo | 25:806e67a1218f | 96 | |
| bbbobbbieo | 25:806e67a1218f | 97 | |
| bbbobbbieo | 25:806e67a1218f | 98 | // manual servo control, unused | 
| bbbobbbieo | 25:806e67a1218f | 99 | if (TFC_ReadPushButton(1) != 0 ) { | 
| bbbobbbieo | 25:806e67a1218f | 100 | current_servo_position = current_servo_position+.005; | 
| bbbobbbieo | 25:806e67a1218f | 101 | if(current_servo_position >= 0.4) | 
| bbbobbbieo | 25:806e67a1218f | 102 | current_servo_position = 0.4; | 
| bbbobbbieo | 25:806e67a1218f | 103 | TFC_SetServo(0, current_servo_position); | 
| bbbobbbieo | 25:806e67a1218f | 104 | }// end check button1 | 
| bbbobbbieo | 25:806e67a1218f | 105 | |
| bbbobbbieo | 25:806e67a1218f | 106 | else {} | 
| bbbobbbieo | 25:806e67a1218f | 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 | 25:806e67a1218f | 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 | 25:806e67a1218f | 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 | 25:806e67a1218f | 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 | 25:806e67a1218f | 165 | |
| bbbobbbieo | 0:bf39986ebff7 | 166 | // checking for center line (single line) | 
| bbbobbbieo | 25:806e67a1218f | 167 | for (uint16_t i=15; i<113; i++) { | 
| bbbobbbieo | 25:806e67a1218f | 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 | 25:806e67a1218f | 174 | for(int i=0; i<black_value_count; i++) { | 
| bbbobbbieo | 0:bf39986ebff7 | 175 | sum_black += black_values_list[i]; | 
| bbbobbbieo | 25:806e67a1218f | 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 | 25:806e67a1218f | 184 | |
| bbbobbbieo | 25:806e67a1218f | 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 | 25:806e67a1218f | 190 | if(center_now > 15 && center_now < 27) | 
| bbbobbbieo | 0:bf39986ebff7 | 191 | num = 1; | 
| bbbobbbieo | 25:806e67a1218f | 192 | else if(center_now >= 27 && center_now < 54) | 
| bbbobbbieo | 0:bf39986ebff7 | 193 | num = 2; | 
| bbbobbbieo | 25:806e67a1218f | 194 | else if(center_now > 60 && center_now < 70) | 
| bbbobbbieo | 0:bf39986ebff7 | 195 | num = 15; | 
| bbbobbbieo | 25:806e67a1218f | 196 | else if(center_now >= 54 && center_now < 81) | 
| bbbobbbieo | 0:bf39986ebff7 | 197 | num = 4; | 
| bbbobbbieo | 25:806e67a1218f | 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 | 25:806e67a1218f | 206 | |
| bbbobbbieo | 19:545e9ddba0e8 | 207 | if (black_value_count>30) | 
| bbbobbbieo | 19:545e9ddba0e8 | 208 | { | 
| bbbobbbieo | 19:545e9ddba0e8 | 209 | while(1) | 
| bbbobbbieo | 19:545e9ddba0e8 | 210 | TFC_SetMotorPWM(0, 0); | 
| bbbobbbieo | 25:806e67a1218f | 211 | } | 
| bbbobbbieo | 25:806e67a1218f | 212 | |
| bbbobbbieo | 19:545e9ddba0e8 | 213 | |
| bbbobbbieo | 0:bf39986ebff7 | 214 | TFC_SetBatteryLED(num); | 
| bbbobbbieo | 0:bf39986ebff7 | 215 | |
| bbbobbbieo | 0:bf39986ebff7 | 216 | // best guess of center based on weighted average of history | 
| bbbobbbieo | 25:806e67a1218f | 217 | //black_center_value = (5*center_now + 10*center_past_1 + 15*center_past_2 +30*center_past_3 +40*center_past_4)/100; | 
| bbbobbbieo | 25:806e67a1218f | 218 | //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 | 219 | black_center_value = center_now; | 
| bbbobbbieo | 0:bf39986ebff7 | 220 | |
| bbbobbbieo | 0:bf39986ebff7 | 221 | |
| bbbobbbieo | 0:bf39986ebff7 | 222 | // turn left | 
| bbbobbbieo | 25:806e67a1218f | 223 | //if (black_center_value > BLACK_THRESHOLD+30) { | 
| bbbobbbieo | 25:806e67a1218f | 224 | if (num==8 and right_counter <.2 ) | 
| bbbobbbieo | 0:bf39986ebff7 | 225 | { | 
| bbbobbbieo | 25:806e67a1218f | 226 | //left_counter += (128-black_center_value); | 
| bbbobbbieo | 25:806e67a1218f | 227 | //left_counter+=20; | 
| bbbobbbieo | 25:806e67a1218f | 228 | left_counter=-0.4; | 
| bbbobbbieo | 14:1a408e13679d | 229 | turn_left=true; | 
| bbbobbbieo | 14:1a408e13679d | 230 | turn_right=false; | 
| bbbobbbieo | 25:806e67a1218f | 231 | |
| bbbobbbieo | 14:1a408e13679d | 232 | } | 
| bbbobbbieo | 25:806e67a1218f | 233 | if (num==4 and right_counter <.2) | 
| bbbobbbieo | 0:bf39986ebff7 | 234 | { | 
| bbbobbbieo | 25:806e67a1218f | 235 | //left_counter += (128-black_center_value); | 
| bbbobbbieo | 25:806e67a1218f | 236 | //left_counter+=20; | 
| bbbobbbieo | 25:806e67a1218f | 237 | left_counter=-0.6; | 
| bbbobbbieo | 0:bf39986ebff7 | 238 | turn_left=true; | 
| bbbobbbieo | 0:bf39986ebff7 | 239 | turn_right=false; | 
| bbbobbbieo | 25:806e67a1218f | 240 | |
| bbbobbbieo | 0:bf39986ebff7 | 241 | } | 
| bbbobbbieo | 0:bf39986ebff7 | 242 | |
| bbbobbbieo | 25:806e67a1218f | 243 | // need to turn right | 
| bbbobbbieo | 25:806e67a1218f | 244 | //else if (black_center_value < BLACK_THRESHOLD-30) { | 
| bbbobbbieo | 25:806e67a1218f | 245 | else if (num==1 and left_counter >-.2) | 
| bbbobbbieo | 14:1a408e13679d | 246 | { | 
| bbbobbbieo | 25:806e67a1218f | 247 | //right_counter += black_center_value; | 
| bbbobbbieo | 25:806e67a1218f | 248 | |
| bbbobbbieo | 25:806e67a1218f | 249 | //right_counter +=20; | 
| bbbobbbieo | 25:806e67a1218f | 250 | right_counter =.4; | 
| bbbobbbieo | 14:1a408e13679d | 251 | turn_left=false; | 
| bbbobbbieo | 14:1a408e13679d | 252 | turn_right=true; | 
| bbbobbbieo | 25:806e67a1218f | 253 | |
| bbbobbbieo | 14:1a408e13679d | 254 | } | 
| bbbobbbieo | 25:806e67a1218f | 255 | else if (num==2 and left_counter >-.2) | 
| bbbobbbieo | 25:806e67a1218f | 256 | { | 
| bbbobbbieo | 25:806e67a1218f | 257 | //right_counter += black_center_value; | 
| bbbobbbieo | 25:806e67a1218f | 258 | //right_counter +=20; | 
| bbbobbbieo | 25:806e67a1218f | 259 | right_counter =.6; | 
| bbbobbbieo | 0:bf39986ebff7 | 260 | turn_left=false; | 
| bbbobbbieo | 0:bf39986ebff7 | 261 | turn_right=true; | 
| bbbobbbieo | 25:806e67a1218f | 262 | |
| bbbobbbieo | 0:bf39986ebff7 | 263 | } | 
| bbbobbbieo | 25:806e67a1218f | 264 | //else if (black_value_count < 2) | 
| bbbobbbieo | 25:806e67a1218f | 265 | // { | 
| bbbobbbieo | 25:806e67a1218f | 266 | // turn_right=false; | 
| bbbobbbieo | 25:806e67a1218f | 267 | //// turn_left=false; | 
| bbbobbbieo | 25:806e67a1218f | 268 | // TFC_SetServo(0,0.0); | 
| bbbobbbieo | 25:806e67a1218f | 269 | // } | 
| bbbobbbieo | 25:806e67a1218f | 270 | |
| bbbobbbieo | 25:806e67a1218f | 271 | //else if (num==0) | 
| bbbobbbieo | 25:806e67a1218f | 272 | //{ | 
| bbbobbbieo | 25:806e67a1218f | 273 | // TFC_SetServo(0,0.0); | 
| bbbobbbieo | 25:806e67a1218f | 274 | //} | 
| bbbobbbieo | 25:806e67a1218f | 275 | |
| bbbobbbieo | 25:806e67a1218f | 276 | |
| bbbobbbieo | 25:806e67a1218f | 277 | else if (turn_right == false and turn_left == false) | 
| bbbobbbieo | 2:5eb97170c199 | 278 | { | 
| bbbobbbieo | 2:5eb97170c199 | 279 | TFC_SetServo(0,(0.0+ bullshit_offset)); | 
| bbbobbbieo | 2:5eb97170c199 | 280 | } | 
| bbbobbbieo | 2:5eb97170c199 | 281 | |
| bbbobbbieo | 25:806e67a1218f | 282 | else | 
| bbbobbbieo | 25:806e67a1218f | 283 | { | 
| bbbobbbieo | 25:806e67a1218f | 284 | //turn_right=false; | 
| bbbobbbieo | 25:806e67a1218f | 285 | //turn_left=false; | 
| bbbobbbieo | 25:806e67a1218f | 286 | //TFC_SetServo(0,0.0); | 
| bbbobbbieo | 25:806e67a1218f | 287 | } | 
| bbbobbbieo | 0:bf39986ebff7 | 288 | |
| bbbobbbieo | 0:bf39986ebff7 | 289 | //dealwiththeshit | 
| bbbobbbieo | 25:806e67a1218f | 290 | if(turn_left) | 
| bbbobbbieo | 0:bf39986ebff7 | 291 | { | 
| bbbobbbieo | 25:806e67a1218f | 292 | turn_right = false; | 
| bbbobbbieo | 25:806e67a1218f | 293 | //TFC_SetServo(0, ((left_counter)*(-.05))); | 
| bbbobbbieo | 25:806e67a1218f | 294 | TFC_SetServo(0,left_counter); | 
| bbbobbbieo | 25:806e67a1218f | 295 | left_counter += .01; | 
| bbbobbbieo | 2:5eb97170c199 | 296 | if (left_counter > (0+ bullshit_offset)) | 
| bbbobbbieo | 1:e561f697985b | 297 | turn_left = false; | 
| bbbobbbieo | 25:806e67a1218f | 298 | |
| bbbobbbieo | 25:806e67a1218f | 299 | TFC_SetMotorPWM(current_left_motor_speed+(.2*left_counter), current_right_motor_speed+(.2*left_counter)); | 
| bbbobbbieo | 25:806e67a1218f | 300 | //left_counter -= 1; | 
| bbbobbbieo | 25:806e67a1218f | 301 | //if (left_counter < 10) | 
| bbbobbbieo | 25:806e67a1218f | 302 | // turn_left =false; | 
| bbbobbbieo | 25:806e67a1218f | 303 | } | 
| bbbobbbieo | 25:806e67a1218f | 304 | |
| bbbobbbieo | 25:806e67a1218f | 305 | if(turn_right) | 
| bbbobbbieo | 0:bf39986ebff7 | 306 | { | 
| bbbobbbieo | 0:bf39986ebff7 | 307 | turn_left =false; | 
| bbbobbbieo | 25:806e67a1218f | 308 | //TFC_SetServo(0, ((right_counter)*(.05))); | 
| bbbobbbieo | 25:806e67a1218f | 309 | TFC_SetServo(0,right_counter); | 
| bbbobbbieo | 25:806e67a1218f | 310 | right_counter -= .01; | 
| bbbobbbieo | 2:5eb97170c199 | 311 | if (right_counter < (0+ bullshit_offset)) | 
| bbbobbbieo | 1:e561f697985b | 312 | turn_right = false; | 
| bbbobbbieo | 25:806e67a1218f | 313 | |
| bbbobbbieo | 25:806e67a1218f | 314 | TFC_SetMotorPWM(current_left_motor_speed-(.2*right_counter), current_right_motor_speed-(.2*right_counter)); | 
| bbbobbbieo | 25:806e67a1218f | 315 | //right_counter -= 1; | 
| bbbobbbieo | 25:806e67a1218f | 316 | // if (right_counter < 10) | 
| bbbobbbieo | 25:806e67a1218f | 317 | // turn_right =false; | 
| bbbobbbieo | 25:806e67a1218f | 318 | } | 
| bbbobbbieo | 0:bf39986ebff7 | 319 | |
| bbbobbbieo | 0:bf39986ebff7 | 320 | // clearing values for next image processing round | 
| bbbobbbieo | 0:bf39986ebff7 | 321 | black_value_count = 0; | 
| bbbobbbieo | 25:806e67a1218f | 322 | //black_center_value = 0; | 
| bbbobbbieo | 0:bf39986ebff7 | 323 | sum_black = 0; | 
| bbbobbbieo | 25:806e67a1218f | 324 | |
| bbbobbbieo | 0:bf39986ebff7 | 325 | // end image processing | 
| bbbobbbieo | 0:bf39986ebff7 | 326 | |
| bbbobbbieo | 0:bf39986ebff7 | 327 | linescan_ping_pong = false; | 
| bbbobbbieo | 0:bf39986ebff7 | 328 | } // end checking channel 0 | 
| bbbobbbieo | 0:bf39986ebff7 | 329 | |
| bbbobbbieo | 0:bf39986ebff7 | 330 | else { //checking channel 1 | 
| bbbobbbieo | 0:bf39986ebff7 | 331 | linescan_ping_pong = true; | 
| bbbobbbieo | 0:bf39986ebff7 | 332 | } | 
| bbbobbbieo | 0:bf39986ebff7 | 333 | |
| bbbobbbieo | 0:bf39986ebff7 | 334 | TFC_LineScanImageReady = 0; // since we used it, we reset the flag | 
| bbbobbbieo | 0:bf39986ebff7 | 335 | }// end imageready | 
| bbbobbbieo | 0:bf39986ebff7 | 336 | }// end linescan stuff | 
| bbbobbbieo | 25:806e67a1218f | 337 | } | 
| bbbobbbieo | 25:806e67a1218f | 338 | } | 
| bbbobbbieo | 25:806e67a1218f | 339 | |
| bbbobbbieo | 25:806e67a1218f | 340 | // shit code down here | 
| bbbobbbieo | 25:806e67a1218f | 341 |