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@37:56dc8c2e8cf9, 2015-03-31 (annotated)
- Committer:
- bbbobbbieo
- Date:
- Tue Mar 31 15:50:34 2015 +0000
- Revision:
- 37:56dc8c2e8cf9
- Parent:
- 36:3793f21a895e
- Child:
- 39:e9a8e3c59b52
31st at 1200h made it around 5 or 6 laps hoora
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 | 36:3793f21a895e | 7 | const float AGGRESSIVE2 = .43; |
| bbbobbbieo | 36:3793f21a895e | 8 | const float AGGRESSIVE = .40; |
| bbbobbbieo | 36:3793f21a895e | 9 | const float MODERATE =.35; |
| bbbobbbieo | 36:3793f21a895e | 10 | const float CONSERVATIVE =.30; |
| bbbobbbieo | 0:bf39986ebff7 | 11 | const float STOP =0; |
| bbbobbbieo | 0:bf39986ebff7 | 12 | const float PROTECTION_THRESHOLD_UPPER =.7; |
| bbbobbbieo | 0:bf39986ebff7 | 13 | const float PROTECTION_THRESHOLD_LOWER =-.7; |
| bbbobbbieo | 0:bf39986ebff7 | 14 | const float TURN_FORWARD_ACCEL =0.045; |
| bbbobbbieo | 0:bf39986ebff7 | 15 | const float TURN_BACKWARD_ACCEL =0.025; |
| bbbobbbieo | 0:bf39986ebff7 | 16 | const float SERVO_CAN_MOVE_IN_ONE_FRAME =0.1; |
| bbbobbbieo | 0:bf39986ebff7 | 17 | const float SERVO_MAX =.5; |
| bbbobbbieo | 0:bf39986ebff7 | 18 | |
| bbbobbbieo | 0:bf39986ebff7 | 19 | const int BLACK_THRESHOLD =63; |
| bbbobbbieo | 0:bf39986ebff7 | 20 | const int LINE_SCAN_LENGTH =128; |
| bbbobbbieo | 0:bf39986ebff7 | 21 | |
| bbbobbbieo | 0:bf39986ebff7 | 22 | |
| bbbobbbieo | 0:bf39986ebff7 | 23 | DigitalOut myled(LED1); |
| bbbobbbieo | 0:bf39986ebff7 | 24 | |
| bbbobbbieo | 0:bf39986ebff7 | 25 | int main() |
| bbbobbbieo | 0:bf39986ebff7 | 26 | { |
| bbbobbbieo | 0:bf39986ebff7 | 27 | //run this before anything |
| bbbobbbieo | 0:bf39986ebff7 | 28 | TFC_Init(); |
| bbbobbbieo | 0:bf39986ebff7 | 29 | |
| bbbobbbieo | 0:bf39986ebff7 | 30 | //variables |
| bbbobbbieo | 0:bf39986ebff7 | 31 | float current_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 | // gains on prop, int, der |
| bbbobbbieo | 0:bf39986ebff7 | 36 | // subject to change, need to fine tune |
| bbbobbbieo | 0:bf39986ebff7 | 37 | |
| bbbobbbieo | 0:bf39986ebff7 | 38 | bool rear_motor_enable_flag = true; |
| bbbobbbieo | 0:bf39986ebff7 | 39 | bool linescan_ping_pong = false; |
| bbbobbbieo | 0:bf39986ebff7 | 40 | bool linescan_enable = true; |
| mperella | 33:faa8e83f1d45 | 41 | |
| mperella | 33:faa8e83f1d45 | 42 | bool whiteflag = false; |
| mperella | 33:faa8e83f1d45 | 43 | bool blackflag = false; |
| mperella | 33:faa8e83f1d45 | 44 | bool stopflag = false; |
| mperella | 33:faa8e83f1d45 | 45 | |
| mperella | 33:faa8e83f1d45 | 46 | int black_list_one = 0; |
| mperella | 33:faa8e83f1d45 | 47 | int white_list = 0; |
| mperella | 33:faa8e83f1d45 | 48 | int black_list_two = 0; |
| bbbobbbieo | 0:bf39986ebff7 | 49 | |
| bbbobbbieo | 0:bf39986ebff7 | 50 | int black_values_list[LINE_SCAN_LENGTH]; |
| bbbobbbieo | 0:bf39986ebff7 | 51 | int black_value_count = 0; |
| mperella | 34:b97857dd355d | 52 | int black_value_count_previous = 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 center_now = 63; |
| bbbobbbieo | 0:bf39986ebff7 | 58 | int center_past_1 = 63; |
| bbbobbbieo | 0:bf39986ebff7 | 59 | int center_past_2 = 63; |
| bbbobbbieo | 0:bf39986ebff7 | 60 | int center_past_3 = 63; |
| bbbobbbieo | 0:bf39986ebff7 | 61 | int center_past_4 = 63; |
| bbbobbbieo | 0:bf39986ebff7 | 62 | |
| bbbobbbieo | 0:bf39986ebff7 | 63 | float left_counter =0; |
| bbbobbbieo | 0:bf39986ebff7 | 64 | float right_counter =0; |
| bbbobbbieo | 0:bf39986ebff7 | 65 | bool turn_left=false; |
| bbbobbbieo | 0:bf39986ebff7 | 66 | bool turn_right=false; |
| bbbobbbieo | 0:bf39986ebff7 | 67 | |
| mperella | 33:faa8e83f1d45 | 68 | float bullshit_offset = .08; |
| bbbobbbieo | 30:8e0ad64703d0 | 69 | |
| bbbobbbieo | 30:8e0ad64703d0 | 70 | int num_of_straight =0; |
| bbbobbbieo | 30:8e0ad64703d0 | 71 | int num_of_left =0; |
| bbbobbbieo | 30:8e0ad64703d0 | 72 | int num_of_right=0; |
| bbbobbbieo | 30:8e0ad64703d0 | 73 | |
| bbbobbbieo | 30:8e0ad64703d0 | 74 | int values_list_add =0; |
| bbbobbbieo | 30:8e0ad64703d0 | 75 | int value_count =0; |
| bbbobbbieo | 30:8e0ad64703d0 | 76 | int avg_value=0; |
| bbbobbbieo | 30:8e0ad64703d0 | 77 | //int black_threshhold=0; |
| bbbobbbieo | 30:8e0ad64703d0 | 78 | int black_threshhold =450; |
| bbbobbbieo | 0:bf39986ebff7 | 79 | |
| bbbobbbieo | 0:bf39986ebff7 | 80 | // major loop |
| bbbobbbieo | 0:bf39986ebff7 | 81 | while(1) { |
| bbbobbbieo | 0:bf39986ebff7 | 82 | |
| bbbobbbieo | 0:bf39986ebff7 | 83 | // initial motor stuff |
| bbbobbbieo | 0:bf39986ebff7 | 84 | if(rear_motor_enable_flag) { |
| bbbobbbieo | 0:bf39986ebff7 | 85 | TFC_HBRIDGE_ENABLE; |
| bbbobbbieo | 0:bf39986ebff7 | 86 | |
| bbbobbbieo | 0:bf39986ebff7 | 87 | // checking behavior level |
| bbbobbbieo | 0:bf39986ebff7 | 88 | violence_level = int(TFC_GetDIP_Switch()); |
| bbbobbbieo | 0:bf39986ebff7 | 89 | |
| bbbobbbieo | 36:3793f21a895e | 90 | |
| bbbobbbieo | 36:3793f21a895e | 91 | if (violence_level==4) { |
| bbbobbbieo | 36:3793f21a895e | 92 | current_left_motor_speed = -(AGGRESSIVE2); |
| bbbobbbieo | 36:3793f21a895e | 93 | current_right_motor_speed = AGGRESSIVE2; |
| bbbobbbieo | 36:3793f21a895e | 94 | } |
| bbbobbbieo | 0:bf39986ebff7 | 95 | if (violence_level==3) { |
| bbbobbbieo | 0:bf39986ebff7 | 96 | current_left_motor_speed = -(AGGRESSIVE); |
| bbbobbbieo | 0:bf39986ebff7 | 97 | current_right_motor_speed = AGGRESSIVE; |
| bbbobbbieo | 0:bf39986ebff7 | 98 | } |
| bbbobbbieo | 0:bf39986ebff7 | 99 | else if (violence_level==2) { |
| bbbobbbieo | 0:bf39986ebff7 | 100 | current_left_motor_speed = -(MODERATE); |
| bbbobbbieo | 0:bf39986ebff7 | 101 | current_right_motor_speed = (MODERATE); |
| bbbobbbieo | 0:bf39986ebff7 | 102 | } |
| bbbobbbieo | 0:bf39986ebff7 | 103 | else if (violence_level==1) { |
| bbbobbbieo | 0:bf39986ebff7 | 104 | current_left_motor_speed = -(CONSERVATIVE); |
| bbbobbbieo | 0:bf39986ebff7 | 105 | current_right_motor_speed = CONSERVATIVE; |
| bbbobbbieo | 0:bf39986ebff7 | 106 | } |
| bbbobbbieo | 0:bf39986ebff7 | 107 | else if (violence_level==0) { |
| bbbobbbieo | 0:bf39986ebff7 | 108 | current_left_motor_speed = STOP; |
| bbbobbbieo | 0:bf39986ebff7 | 109 | current_right_motor_speed = STOP; |
| bbbobbbieo | 0:bf39986ebff7 | 110 | } |
| bbbobbbieo | 0:bf39986ebff7 | 111 | else { |
| bbbobbbieo | 0:bf39986ebff7 | 112 | current_left_motor_speed = STOP; |
| bbbobbbieo | 0:bf39986ebff7 | 113 | current_right_motor_speed = STOP; |
| bbbobbbieo | 0:bf39986ebff7 | 114 | } |
| bbbobbbieo | 0:bf39986ebff7 | 115 | |
| bbbobbbieo | 0:bf39986ebff7 | 116 | // protection block |
| bbbobbbieo | 0:bf39986ebff7 | 117 | if(current_left_motor_speed >= PROTECTION_THRESHOLD_UPPER) |
| bbbobbbieo | 0:bf39986ebff7 | 118 | current_left_motor_speed= PROTECTION_THRESHOLD_UPPER; |
| bbbobbbieo | 0:bf39986ebff7 | 119 | if(current_right_motor_speed >= PROTECTION_THRESHOLD_UPPER) |
| bbbobbbieo | 0:bf39986ebff7 | 120 | current_right_motor_speed = PROTECTION_THRESHOLD_UPPER; |
| bbbobbbieo | 0:bf39986ebff7 | 121 | if(current_left_motor_speed <= PROTECTION_THRESHOLD_LOWER) |
| bbbobbbieo | 0:bf39986ebff7 | 122 | current_left_motor_speed = PROTECTION_THRESHOLD_LOWER; |
| bbbobbbieo | 0:bf39986ebff7 | 123 | if(current_right_motor_speed <= PROTECTION_THRESHOLD_LOWER) |
| bbbobbbieo | 0:bf39986ebff7 | 124 | current_right_motor_speed = PROTECTION_THRESHOLD_LOWER; |
| bbbobbbieo | 0:bf39986ebff7 | 125 | |
| bbbobbbieo | 25:806e67a1218f | 126 | TFC_SetMotorPWM(current_left_motor_speed, current_right_motor_speed); |
| bbbobbbieo | 0:bf39986ebff7 | 127 | }// end motor enabled |
| bbbobbbieo | 0:bf39986ebff7 | 128 | else { |
| bbbobbbieo | 0:bf39986ebff7 | 129 | TFC_HBRIDGE_DISABLE; |
| bbbobbbieo | 0:bf39986ebff7 | 130 | }// end motor disabled |
| bbbobbbieo | 0:bf39986ebff7 | 131 | |
| bbbobbbieo | 0:bf39986ebff7 | 132 | // camera stuff |
| bbbobbbieo | 0:bf39986ebff7 | 133 | if (linescan_enable) { |
| bbbobbbieo | 0:bf39986ebff7 | 134 | if (TFC_LineScanImageReady !=0) { |
| bbbobbbieo | 0:bf39986ebff7 | 135 | |
| bbbobbbieo | 0:bf39986ebff7 | 136 | if (linescan_ping_pong) { |
| bbbobbbieo | 0:bf39986ebff7 | 137 | //checking channel 0 |
| bbbobbbieo | 0:bf39986ebff7 | 138 | |
| bbbobbbieo | 30:8e0ad64703d0 | 139 | |
| bbbobbbieo | 0:bf39986ebff7 | 140 | // checking for center line (single line) |
| bbbobbbieo | 27:aa81c15e5c1d | 141 | for (uint16_t i=10; i<118; i++) { |
| bbbobbbieo | 30:8e0ad64703d0 | 142 | |
| bbbobbbieo | 36:3793f21a895e | 143 | int black_threshhold =600*(TFC_ReadPot(0)); |
| bbbobbbieo | 30:8e0ad64703d0 | 144 | if ((*(TFC_LineScanImage0+i) < black_threshhold)) { |
| bbbobbbieo | 0:bf39986ebff7 | 145 | black_values_list[black_value_count] = i; |
| bbbobbbieo | 0:bf39986ebff7 | 146 | black_value_count++; |
| mperella | 33:faa8e83f1d45 | 147 | //black_list_one++; |
| bbbobbbieo | 0:bf39986ebff7 | 148 | } |
| mperella | 33:faa8e83f1d45 | 149 | /* |
| mperella | 33:faa8e83f1d45 | 150 | else |
| mperella | 33:faa8e83f1d45 | 151 | { |
| mperella | 33:faa8e83f1d45 | 152 | black_list_one = 0; |
| mperella | 33:faa8e83f1d45 | 153 | } |
| mperella | 33:faa8e83f1d45 | 154 | |
| mperella | 33:faa8e83f1d45 | 155 | if(black_list_one > 5) |
| mperella | 33:faa8e83f1d45 | 156 | { |
| mperella | 33:faa8e83f1d45 | 157 | for(uint16_t j = i+black_list_one; j<118; j++) |
| mperella | 33:faa8e83f1d45 | 158 | { |
| mperella | 33:faa8e83f1d45 | 159 | if((*(TFC_LineScanImage0+j) > black_threshhold)) |
| mperella | 33:faa8e83f1d45 | 160 | { |
| mperella | 33:faa8e83f1d45 | 161 | white_list++; |
| mperella | 33:faa8e83f1d45 | 162 | whiteflag = true; |
| mperella | 33:faa8e83f1d45 | 163 | } |
| mperella | 33:faa8e83f1d45 | 164 | else |
| mperella | 33:faa8e83f1d45 | 165 | { |
| mperella | 33:faa8e83f1d45 | 166 | whiteflag = false; |
| mperella | 33:faa8e83f1d45 | 167 | } |
| mperella | 33:faa8e83f1d45 | 168 | if(!whiteflag) |
| mperella | 33:faa8e83f1d45 | 169 | { |
| mperella | 33:faa8e83f1d45 | 170 | j = 200; |
| mperella | 33:faa8e83f1d45 | 171 | } |
| mperella | 33:faa8e83f1d45 | 172 | else if(whiteflag and white_list > 4) |
| mperella | 33:faa8e83f1d45 | 173 | { |
| mperella | 33:faa8e83f1d45 | 174 | for(uint16_t k = j+white_list; k < 118; k++) |
| mperella | 33:faa8e83f1d45 | 175 | { |
| mperella | 33:faa8e83f1d45 | 176 | if((*(TFC_LineScanImage0+k) < black_threshhold)) |
| mperella | 33:faa8e83f1d45 | 177 | { |
| mperella | 33:faa8e83f1d45 | 178 | black_list_two++; |
| mperella | 33:faa8e83f1d45 | 179 | blackflag = true; |
| mperella | 33:faa8e83f1d45 | 180 | } |
| mperella | 33:faa8e83f1d45 | 181 | else |
| mperella | 33:faa8e83f1d45 | 182 | { |
| mperella | 33:faa8e83f1d45 | 183 | blackflag = false; |
| mperella | 33:faa8e83f1d45 | 184 | } |
| mperella | 33:faa8e83f1d45 | 185 | if(!blackflag) |
| mperella | 33:faa8e83f1d45 | 186 | { |
| mperella | 33:faa8e83f1d45 | 187 | k = 200; |
| mperella | 33:faa8e83f1d45 | 188 | } |
| mperella | 33:faa8e83f1d45 | 189 | else if(blackflag and black_list_two > 3) |
| mperella | 33:faa8e83f1d45 | 190 | { |
| mperella | 33:faa8e83f1d45 | 191 | stopflag = true; |
| mperella | 33:faa8e83f1d45 | 192 | } |
| mperella | 33:faa8e83f1d45 | 193 | } |
| mperella | 33:faa8e83f1d45 | 194 | } |
| mperella | 33:faa8e83f1d45 | 195 | } |
| mperella | 33:faa8e83f1d45 | 196 | } |
| mperella | 33:faa8e83f1d45 | 197 | */ |
| mperella | 33:faa8e83f1d45 | 198 | |
| bbbobbbieo | 0:bf39986ebff7 | 199 | } |
| bbbobbbieo | 0:bf39986ebff7 | 200 | |
| bbbobbbieo | 25:806e67a1218f | 201 | for(int i=0; i<black_value_count; i++) { |
| bbbobbbieo | 0:bf39986ebff7 | 202 | sum_black += black_values_list[i]; |
| bbbobbbieo | 25:806e67a1218f | 203 | } |
| bbbobbbieo | 0:bf39986ebff7 | 204 | |
| bbbobbbieo | 0:bf39986ebff7 | 205 | //update history |
| bbbobbbieo | 30:8e0ad64703d0 | 206 | /* |
| bbbobbbieo | 0:bf39986ebff7 | 207 | center_past_4= center_past_3; |
| bbbobbbieo | 0:bf39986ebff7 | 208 | center_past_3= center_past_2; |
| bbbobbbieo | 0:bf39986ebff7 | 209 | center_past_2= center_past_1; |
| bbbobbbieo | 0:bf39986ebff7 | 210 | center_past_1= center_now; |
| bbbobbbieo | 30:8e0ad64703d0 | 211 | */ |
| bbbobbbieo | 25:806e67a1218f | 212 | |
| mperella | 34:b97857dd355d | 213 | black_value_count_previous = black_value_count; |
| mperella | 34:b97857dd355d | 214 | |
| bbbobbbieo | 25:806e67a1218f | 215 | //if (black_value_count>2) |
| bbbobbbieo | 0:bf39986ebff7 | 216 | center_now = sum_black / black_value_count; |
| bbbobbbieo | 0:bf39986ebff7 | 217 | |
| bbbobbbieo | 0:bf39986ebff7 | 218 | uint8_t num = 0; |
| bbbobbbieo | 0:bf39986ebff7 | 219 | |
| bbbobbbieo | 37:56dc8c2e8cf9 | 220 | if(center_now > 10 && center_now < 19) // turn right little |
| bbbobbbieo | 0:bf39986ebff7 | 221 | num = 1; |
| bbbobbbieo | 37:56dc8c2e8cf9 | 222 | else if(center_now >= 19 && center_now < 39)// moderate right |
| mperella | 33:faa8e83f1d45 | 223 | num = 3; |
| bbbobbbieo | 37:56dc8c2e8cf9 | 224 | else if(center_now >= 39 && center_now < 62)// snap right |
| bbbobbbieo | 0:bf39986ebff7 | 225 | num = 2; |
| bbbobbbieo | 36:3793f21a895e | 226 | else if(center_now >= 62 && center_now < 65) // center do nothing |
| bbbobbbieo | 0:bf39986ebff7 | 227 | num = 15; |
| bbbobbbieo | 37:56dc8c2e8cf9 | 228 | else if(center_now >= 65 && center_now < 89)// snap left |
| bbbobbbieo | 0:bf39986ebff7 | 229 | num = 4; |
| bbbobbbieo | 37:56dc8c2e8cf9 | 230 | else if(center_now >=89 && center_now < 109) // moderate left |
| mperella | 33:faa8e83f1d45 | 231 | num = 12; |
| bbbobbbieo | 37:56dc8c2e8cf9 | 232 | else if(center_now >= 109 && center_now < 118) // turn left little |
| bbbobbbieo | 0:bf39986ebff7 | 233 | num = 8; |
| bbbobbbieo | 0:bf39986ebff7 | 234 | |
| bbbobbbieo | 0:bf39986ebff7 | 235 | else |
| bbbobbbieo | 0:bf39986ebff7 | 236 | num = 0; |
| bbbobbbieo | 0:bf39986ebff7 | 237 | |
| bbbobbbieo | 2:5eb97170c199 | 238 | if (black_value_count<2) |
| bbbobbbieo | 2:5eb97170c199 | 239 | num = 0; |
| bbbobbbieo | 25:806e67a1218f | 240 | |
| bbbobbbieo | 30:8e0ad64703d0 | 241 | //if (black_value_count>26 and ((*(TFC_LineScanImage0+64) > 450) or (*(TFC_LineScanImage0+63) > 450)or(*(TFC_LineScanImage0+62) > 450))) |
| mperella | 35:22c4eef7e423 | 242 | //if (black_value_count + black_value_count_previous > 30 && (black_value_count > 15 or black_value_count_previous > 15) ) |
| mperella | 34:b97857dd355d | 243 | // if(black_value_count > 22) |
| mperella | 33:faa8e83f1d45 | 244 | //if (stopflag) |
| mperella | 35:22c4eef7e423 | 245 | if(0) |
| bbbobbbieo | 19:545e9ddba0e8 | 246 | { |
| bbbobbbieo | 19:545e9ddba0e8 | 247 | while(1) |
| bbbobbbieo | 19:545e9ddba0e8 | 248 | TFC_SetMotorPWM(0, 0); |
| bbbobbbieo | 25:806e67a1218f | 249 | } |
| bbbobbbieo | 19:545e9ddba0e8 | 250 | |
| bbbobbbieo | 0:bf39986ebff7 | 251 | TFC_SetBatteryLED(num); |
| bbbobbbieo | 0:bf39986ebff7 | 252 | |
| bbbobbbieo | 0:bf39986ebff7 | 253 | // best guess of center based on weighted average of history |
| bbbobbbieo | 0:bf39986ebff7 | 254 | black_center_value = center_now; |
| bbbobbbieo | 0:bf39986ebff7 | 255 | |
| bbbobbbieo | 0:bf39986ebff7 | 256 | |
| bbbobbbieo | 0:bf39986ebff7 | 257 | // turn left |
| bbbobbbieo | 30:8e0ad64703d0 | 258 | if (num==8 and right_counter <.35) |
| bbbobbbieo | 0:bf39986ebff7 | 259 | { |
| mperella | 34:b97857dd355d | 260 | /* |
| bbbobbbieo | 27:aa81c15e5c1d | 261 | if (center_now <113) |
| bbbobbbieo | 27:aa81c15e5c1d | 262 | left_counter=-0.4; |
| bbbobbbieo | 27:aa81c15e5c1d | 263 | else |
| bbbobbbieo | 27:aa81c15e5c1d | 264 | { |
| bbbobbbieo | 27:aa81c15e5c1d | 265 | if (left_counter > -.15) |
| bbbobbbieo | 27:aa81c15e5c1d | 266 | left_counter=-0.15; // less drastic for outside parts |
| bbbobbbieo | 27:aa81c15e5c1d | 267 | } |
| bbbobbbieo | 14:1a408e13679d | 268 | turn_left=true; |
| bbbobbbieo | 14:1a408e13679d | 269 | turn_right=false; |
| mperella | 33:faa8e83f1d45 | 270 | */ |
| mperella | 33:faa8e83f1d45 | 271 | |
| mperella | 33:faa8e83f1d45 | 272 | |
| mperella | 33:faa8e83f1d45 | 273 | |
| mperella | 33:faa8e83f1d45 | 274 | if (left_counter > -.15) |
| mperella | 33:faa8e83f1d45 | 275 | left_counter=-0.15; // less drastic for outside parts |
| mperella | 33:faa8e83f1d45 | 276 | |
| mperella | 33:faa8e83f1d45 | 277 | turn_left=true; |
| mperella | 33:faa8e83f1d45 | 278 | turn_right=false; |
| mperella | 34:b97857dd355d | 279 | |
| bbbobbbieo | 25:806e67a1218f | 280 | |
| bbbobbbieo | 14:1a408e13679d | 281 | } |
| mperella | 33:faa8e83f1d45 | 282 | else if(num == 12 and right_counter <.35) |
| mperella | 33:faa8e83f1d45 | 283 | { |
| mperella | 35:22c4eef7e423 | 284 | if(left_counter > -.4) |
| mperella | 35:22c4eef7e423 | 285 | left_counter=-0.4; |
| mperella | 33:faa8e83f1d45 | 286 | turn_left = true; |
| mperella | 33:faa8e83f1d45 | 287 | turn_right = false; |
| mperella | 33:faa8e83f1d45 | 288 | } |
| mperella | 33:faa8e83f1d45 | 289 | |
| bbbobbbieo | 36:3793f21a895e | 290 | else if (num==4 and right_counter <.45) |
| bbbobbbieo | 0:bf39986ebff7 | 291 | { |
| bbbobbbieo | 25:806e67a1218f | 292 | left_counter=-0.6; |
| bbbobbbieo | 30:8e0ad64703d0 | 293 | //left_counter=-0.55; |
| bbbobbbieo | 0:bf39986ebff7 | 294 | turn_left=true; |
| bbbobbbieo | 0:bf39986ebff7 | 295 | turn_right=false; |
| bbbobbbieo | 0:bf39986ebff7 | 296 | } |
| bbbobbbieo | 0:bf39986ebff7 | 297 | |
| bbbobbbieo | 25:806e67a1218f | 298 | // need to turn right |
| bbbobbbieo | 30:8e0ad64703d0 | 299 | else if (num==1 and left_counter >-.35) |
| bbbobbbieo | 14:1a408e13679d | 300 | { |
| mperella | 34:b97857dd355d | 301 | /* |
| bbbobbbieo | 27:aa81c15e5c1d | 302 | if (center_now >15) |
| bbbobbbieo | 27:aa81c15e5c1d | 303 | right_counter =.4; |
| bbbobbbieo | 27:aa81c15e5c1d | 304 | else |
| bbbobbbieo | 27:aa81c15e5c1d | 305 | { |
| mperella | 34:b97857dd355d | 306 | */ |
| bbbobbbieo | 27:aa81c15e5c1d | 307 | if (right_counter <.15) |
| bbbobbbieo | 27:aa81c15e5c1d | 308 | right_counter =.15; // less drastic for outside parts |
| mperella | 34:b97857dd355d | 309 | |
| bbbobbbieo | 25:806e67a1218f | 310 | |
| bbbobbbieo | 14:1a408e13679d | 311 | turn_left=false; |
| bbbobbbieo | 14:1a408e13679d | 312 | turn_right=true; |
| bbbobbbieo | 25:806e67a1218f | 313 | |
| bbbobbbieo | 14:1a408e13679d | 314 | } |
| mperella | 33:faa8e83f1d45 | 315 | |
| mperella | 33:faa8e83f1d45 | 316 | else if(num == 3 and left_counter >-.35) |
| mperella | 33:faa8e83f1d45 | 317 | { |
| mperella | 35:22c4eef7e423 | 318 | if(right_counter < 0.4) |
| mperella | 35:22c4eef7e423 | 319 | right_counter = 0.4; |
| mperella | 33:faa8e83f1d45 | 320 | |
| mperella | 33:faa8e83f1d45 | 321 | turn_left = false; |
| mperella | 33:faa8e83f1d45 | 322 | turn_right = true; |
| mperella | 33:faa8e83f1d45 | 323 | } |
| bbbobbbieo | 30:8e0ad64703d0 | 324 | |
| bbbobbbieo | 36:3793f21a895e | 325 | else if (num==2 and left_counter >-.45) |
| bbbobbbieo | 25:806e67a1218f | 326 | { |
| bbbobbbieo | 25:806e67a1218f | 327 | right_counter =.6; |
| bbbobbbieo | 30:8e0ad64703d0 | 328 | //right_counter =.55; |
| bbbobbbieo | 0:bf39986ebff7 | 329 | turn_left=false; |
| bbbobbbieo | 0:bf39986ebff7 | 330 | turn_right=true; |
| bbbobbbieo | 0:bf39986ebff7 | 331 | } |
| bbbobbbieo | 29:4bf08c74f792 | 332 | |
| bbbobbbieo | 30:8e0ad64703d0 | 333 | //straight |
| bbbobbbieo | 25:806e67a1218f | 334 | else if (turn_right == false and turn_left == false) |
| bbbobbbieo | 2:5eb97170c199 | 335 | { |
| mperella | 33:faa8e83f1d45 | 336 | //TFC_SetServo(0,(0.0+ bullshit_offset)); |
| mperella | 33:faa8e83f1d45 | 337 | TFC_SetServo(0,bullshit_offset); |
| mperella | 33:faa8e83f1d45 | 338 | TFC_SetMotorPWM(current_left_motor_speed-(.0008*num_of_straight), current_right_motor_speed+(.0008*num_of_straight)); // --left is faster, ++right is faster |
| bbbobbbieo | 30:8e0ad64703d0 | 339 | if (violence_level !=0) |
| bbbobbbieo | 30:8e0ad64703d0 | 340 | num_of_straight++; |
| bbbobbbieo | 2:5eb97170c199 | 341 | } |
| bbbobbbieo | 2:5eb97170c199 | 342 | |
| bbbobbbieo | 25:806e67a1218f | 343 | else |
| bbbobbbieo | 25:806e67a1218f | 344 | { |
| bbbobbbieo | 25:806e67a1218f | 345 | } |
| bbbobbbieo | 0:bf39986ebff7 | 346 | |
| bbbobbbieo | 0:bf39986ebff7 | 347 | //dealwiththeshit |
| bbbobbbieo | 25:806e67a1218f | 348 | if(turn_left) |
| bbbobbbieo | 0:bf39986ebff7 | 349 | { |
| bbbobbbieo | 25:806e67a1218f | 350 | turn_right = false; |
| bbbobbbieo | 30:8e0ad64703d0 | 351 | num_of_straight = 0; // no longer on a straight |
| bbbobbbieo | 29:4bf08c74f792 | 352 | |
| bbbobbbieo | 37:56dc8c2e8cf9 | 353 | if (violence_level == 2 or violence_level == 1) |
| bbbobbbieo | 37:56dc8c2e8cf9 | 354 | TFC_SetServo(0,left_counter+ bullshit_offset ); |
| bbbobbbieo | 37:56dc8c2e8cf9 | 355 | if (violence_level == 3 or violence_level == 4) |
| bbbobbbieo | 37:56dc8c2e8cf9 | 356 | TFC_SetServo(0,(left_counter+ bullshit_offset)*.8 ); |
| bbbobbbieo | 37:56dc8c2e8cf9 | 357 | |
| bbbobbbieo | 31:55d26f4ef5f0 | 358 | //TFC_SetServo(0,left_counter); |
| bbbobbbieo | 25:806e67a1218f | 359 | left_counter += .01; |
| bbbobbbieo | 2:5eb97170c199 | 360 | if (left_counter > (0+ bullshit_offset)) |
| bbbobbbieo | 1:e561f697985b | 361 | turn_left = false; |
| bbbobbbieo | 25:806e67a1218f | 362 | |
| bbbobbbieo | 37:56dc8c2e8cf9 | 363 | TFC_SetMotorPWM(current_left_motor_speed+(.2*left_counter), current_right_motor_speed+(.2*left_counter)); // ++left is slower, ++right is faster |
| bbbobbbieo | 37:56dc8c2e8cf9 | 364 | |
| bbbobbbieo | 37:56dc8c2e8cf9 | 365 | |
| bbbobbbieo | 37:56dc8c2e8cf9 | 366 | |
| bbbobbbieo | 25:806e67a1218f | 367 | } |
| bbbobbbieo | 25:806e67a1218f | 368 | |
| bbbobbbieo | 25:806e67a1218f | 369 | if(turn_right) |
| bbbobbbieo | 0:bf39986ebff7 | 370 | { |
| bbbobbbieo | 0:bf39986ebff7 | 371 | turn_left =false; |
| bbbobbbieo | 30:8e0ad64703d0 | 372 | num_of_straight = 0; // no longer on a straight |
| bbbobbbieo | 30:8e0ad64703d0 | 373 | |
| bbbobbbieo | 30:8e0ad64703d0 | 374 | //TFC_SetServo(0,right_counter- bullshit_offset ); |
| mperella | 33:faa8e83f1d45 | 375 | //TFC_SetServo(0,right_counter); |
| bbbobbbieo | 37:56dc8c2e8cf9 | 376 | |
| bbbobbbieo | 37:56dc8c2e8cf9 | 377 | if (violence_level == 2 or violence_level == 1) |
| bbbobbbieo | 37:56dc8c2e8cf9 | 378 | TFC_SetServo(0,(right_counter+ bullshit_offset)); |
| bbbobbbieo | 37:56dc8c2e8cf9 | 379 | if (violence_level == 3 or violence_level == 4) |
| bbbobbbieo | 37:56dc8c2e8cf9 | 380 | TFC_SetServo(0,(right_counter+ bullshit_offset)*.8 ); |
| bbbobbbieo | 37:56dc8c2e8cf9 | 381 | |
| bbbobbbieo | 25:806e67a1218f | 382 | right_counter -= .01; |
| bbbobbbieo | 2:5eb97170c199 | 383 | if (right_counter < (0+ bullshit_offset)) |
| bbbobbbieo | 1:e561f697985b | 384 | turn_right = false; |
| bbbobbbieo | 25:806e67a1218f | 385 | |
| bbbobbbieo | 36:3793f21a895e | 386 | TFC_SetMotorPWM(current_left_motor_speed-(.2*right_counter), current_right_motor_speed-(.2*right_counter)); // --left is faster, --right is slower |
| bbbobbbieo | 25:806e67a1218f | 387 | } |
| bbbobbbieo | 0:bf39986ebff7 | 388 | |
| bbbobbbieo | 0:bf39986ebff7 | 389 | // clearing values for next image processing round |
| bbbobbbieo | 0:bf39986ebff7 | 390 | black_value_count = 0; |
| bbbobbbieo | 0:bf39986ebff7 | 391 | sum_black = 0; |
| bbbobbbieo | 30:8e0ad64703d0 | 392 | values_list_add =0; |
| bbbobbbieo | 30:8e0ad64703d0 | 393 | value_count =0; |
| bbbobbbieo | 30:8e0ad64703d0 | 394 | avg_value=0; |
| bbbobbbieo | 30:8e0ad64703d0 | 395 | black_threshhold=0; |
| mperella | 33:faa8e83f1d45 | 396 | |
| mperella | 33:faa8e83f1d45 | 397 | black_list_one = 0; |
| mperella | 33:faa8e83f1d45 | 398 | white_list = 0; |
| mperella | 33:faa8e83f1d45 | 399 | black_list_two = 0; |
| mperella | 33:faa8e83f1d45 | 400 | whiteflag = false; |
| mperella | 33:faa8e83f1d45 | 401 | blackflag = false; |
| mperella | 33:faa8e83f1d45 | 402 | stopflag = false; |
| bbbobbbieo | 25:806e67a1218f | 403 | |
| bbbobbbieo | 0:bf39986ebff7 | 404 | // end image processing |
| bbbobbbieo | 0:bf39986ebff7 | 405 | |
| bbbobbbieo | 0:bf39986ebff7 | 406 | linescan_ping_pong = false; |
| bbbobbbieo | 0:bf39986ebff7 | 407 | } // end checking channel 0 |
| bbbobbbieo | 0:bf39986ebff7 | 408 | |
| bbbobbbieo | 0:bf39986ebff7 | 409 | else { //checking channel 1 |
| bbbobbbieo | 0:bf39986ebff7 | 410 | linescan_ping_pong = true; |
| bbbobbbieo | 0:bf39986ebff7 | 411 | } |
| bbbobbbieo | 0:bf39986ebff7 | 412 | |
| bbbobbbieo | 0:bf39986ebff7 | 413 | TFC_LineScanImageReady = 0; // since we used it, we reset the flag |
| bbbobbbieo | 0:bf39986ebff7 | 414 | }// end imageready |
| bbbobbbieo | 0:bf39986ebff7 | 415 | }// end linescan stuff |
| bbbobbbieo | 25:806e67a1218f | 416 | } |
| bbbobbbieo | 25:806e67a1218f | 417 | } |
| bbbobbbieo | 25:806e67a1218f | 418 | |
| bbbobbbieo | 25:806e67a1218f | 419 | // shit code down here |