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@16:25eb20a73e59, 2015-03-26 (annotated)
- Committer:
- bbbobbbieo
- Date:
- Thu Mar 26 21:26:54 2015 +0000
- Revision:
- 16:25eb20a73e59
- Parent:
- 15:60d699b587c5
- Child:
- 17:b5886b554381
fixed some turning shit
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; |
mperella | 8:2a238dbd0386 | 8 | const float MODERATE =.45; |
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; |
mperella | 8:2a238dbd0386 | 73 | |
mperella | 8:2a238dbd0386 | 74 | float increment = 0; |
mperella | 8:2a238dbd0386 | 75 | int right_turn_count = 0; |
mperella | 8:2a238dbd0386 | 76 | int left_turn_count = 0; |
bbbobbbieo | 0:bf39986ebff7 | 77 | |
bbbobbbieo | 0:bf39986ebff7 | 78 | for(int i = 0; i < 50; i++) |
bbbobbbieo | 0:bf39986ebff7 | 79 | centers_List[i] = 63; |
bbbobbbieo | 0:bf39986ebff7 | 80 | |
bbbobbbieo | 0:bf39986ebff7 | 81 | float left_counter =0; |
bbbobbbieo | 0:bf39986ebff7 | 82 | float right_counter =0; |
bbbobbbieo | 0:bf39986ebff7 | 83 | bool turn_left=false; |
bbbobbbieo | 0:bf39986ebff7 | 84 | bool turn_right=false; |
bbbobbbieo | 7:f21986164bf1 | 85 | bool need_decel=false; |
bbbobbbieo | 9:c4a2a99b61e0 | 86 | int num_of_straight =0; |
bbbobbbieo | 12:915f22e7d7d9 | 87 | int num_of_left =0; |
bbbobbbieo | 12:915f22e7d7d9 | 88 | int num_of_right=0; |
bbbobbbieo | 0:bf39986ebff7 | 89 | |
bbbobbbieo | 6:ed97e4324202 | 90 | //servo is offset zero by some bullshit number |
bbbobbbieo | 16:25eb20a73e59 | 91 | float bullshit_offset = .068; |
bbbobbbieo | 0:bf39986ebff7 | 92 | |
bbbobbbieo | 0:bf39986ebff7 | 93 | // major loop |
bbbobbbieo | 0:bf39986ebff7 | 94 | while(1) { |
bbbobbbieo | 0:bf39986ebff7 | 95 | |
bbbobbbieo | 0:bf39986ebff7 | 96 | // initial motor stuff |
bbbobbbieo | 0:bf39986ebff7 | 97 | if(rear_motor_enable_flag) { |
bbbobbbieo | 0:bf39986ebff7 | 98 | TFC_HBRIDGE_ENABLE; |
bbbobbbieo | 0:bf39986ebff7 | 99 | |
bbbobbbieo | 0:bf39986ebff7 | 100 | |
bbbobbbieo | 0:bf39986ebff7 | 101 | // checking behavior level |
bbbobbbieo | 0:bf39986ebff7 | 102 | violence_level = int(TFC_GetDIP_Switch()); |
bbbobbbieo | 0:bf39986ebff7 | 103 | |
bbbobbbieo | 0:bf39986ebff7 | 104 | if (violence_level==3) { |
bbbobbbieo | 0:bf39986ebff7 | 105 | current_left_motor_speed = -(AGGRESSIVE); |
bbbobbbieo | 0:bf39986ebff7 | 106 | current_right_motor_speed = AGGRESSIVE; |
mperella | 8:2a238dbd0386 | 107 | increment = 0.02; |
bbbobbbieo | 0:bf39986ebff7 | 108 | } |
bbbobbbieo | 0:bf39986ebff7 | 109 | else if (violence_level==2) { |
bbbobbbieo | 0:bf39986ebff7 | 110 | current_left_motor_speed = -(MODERATE); |
bbbobbbieo | 0:bf39986ebff7 | 111 | current_right_motor_speed = (MODERATE); |
mperella | 8:2a238dbd0386 | 112 | increment = 0.03; |
bbbobbbieo | 0:bf39986ebff7 | 113 | } |
bbbobbbieo | 0:bf39986ebff7 | 114 | else if (violence_level==1) { |
bbbobbbieo | 0:bf39986ebff7 | 115 | current_left_motor_speed = -(CONSERVATIVE); |
bbbobbbieo | 0:bf39986ebff7 | 116 | current_right_motor_speed = CONSERVATIVE; |
mperella | 8:2a238dbd0386 | 117 | increment = 0.04; |
bbbobbbieo | 0:bf39986ebff7 | 118 | } |
bbbobbbieo | 0:bf39986ebff7 | 119 | else if (violence_level==0) { |
bbbobbbieo | 0:bf39986ebff7 | 120 | current_left_motor_speed = STOP; |
bbbobbbieo | 0:bf39986ebff7 | 121 | current_right_motor_speed = STOP; |
bbbobbbieo | 13:8a667757beb3 | 122 | TFC_SetMotorPWM(current_left_motor_speed, current_right_motor_speed); |
bbbobbbieo | 0:bf39986ebff7 | 123 | } |
bbbobbbieo | 0:bf39986ebff7 | 124 | else { |
bbbobbbieo | 0:bf39986ebff7 | 125 | current_left_motor_speed = STOP; |
bbbobbbieo | 0:bf39986ebff7 | 126 | current_right_motor_speed = STOP; |
bbbobbbieo | 13:8a667757beb3 | 127 | TFC_SetMotorPWM(current_left_motor_speed, current_right_motor_speed); |
bbbobbbieo | 0:bf39986ebff7 | 128 | } |
bbbobbbieo | 0:bf39986ebff7 | 129 | |
bbbobbbieo | 0:bf39986ebff7 | 130 | |
bbbobbbieo | 0:bf39986ebff7 | 131 | // protection block |
bbbobbbieo | 0:bf39986ebff7 | 132 | if(current_left_motor_speed >= PROTECTION_THRESHOLD_UPPER) |
bbbobbbieo | 0:bf39986ebff7 | 133 | current_left_motor_speed= PROTECTION_THRESHOLD_UPPER; |
bbbobbbieo | 0:bf39986ebff7 | 134 | if(current_right_motor_speed >= PROTECTION_THRESHOLD_UPPER) |
bbbobbbieo | 0:bf39986ebff7 | 135 | current_right_motor_speed = PROTECTION_THRESHOLD_UPPER; |
bbbobbbieo | 0:bf39986ebff7 | 136 | if(current_left_motor_speed <= PROTECTION_THRESHOLD_LOWER) |
bbbobbbieo | 0:bf39986ebff7 | 137 | current_left_motor_speed = PROTECTION_THRESHOLD_LOWER; |
bbbobbbieo | 0:bf39986ebff7 | 138 | if(current_right_motor_speed <= PROTECTION_THRESHOLD_LOWER) |
bbbobbbieo | 0:bf39986ebff7 | 139 | current_right_motor_speed = PROTECTION_THRESHOLD_LOWER; |
bbbobbbieo | 0:bf39986ebff7 | 140 | |
bbbobbbieo | 9:c4a2a99b61e0 | 141 | //TFC_SetMotorPWM(current_left_motor_speed, current_right_motor_speed); |
bbbobbbieo | 0:bf39986ebff7 | 142 | }// end motor enabled |
bbbobbbieo | 0:bf39986ebff7 | 143 | else { |
bbbobbbieo | 0:bf39986ebff7 | 144 | TFC_HBRIDGE_DISABLE; |
bbbobbbieo | 0:bf39986ebff7 | 145 | }// end motor disabled |
bbbobbbieo | 0:bf39986ebff7 | 146 | |
bbbobbbieo | 0:bf39986ebff7 | 147 | // camera stuff |
bbbobbbieo | 0:bf39986ebff7 | 148 | if (linescan_enable) { |
bbbobbbieo | 0:bf39986ebff7 | 149 | if (TFC_LineScanImageReady !=0) { |
bbbobbbieo | 0:bf39986ebff7 | 150 | |
bbbobbbieo | 0:bf39986ebff7 | 151 | if (linescan_ping_pong) { |
bbbobbbieo | 0:bf39986ebff7 | 152 | //checking channel 0 |
bbbobbbieo | 0:bf39986ebff7 | 153 | |
bbbobbbieo | 0:bf39986ebff7 | 154 | //checking center pixel, displays aprox value on leds |
bbbobbbieo | 0:bf39986ebff7 | 155 | uint8_t shitnum = 1; |
bbbobbbieo | 0:bf39986ebff7 | 156 | |
bbbobbbieo | 0:bf39986ebff7 | 157 | |
bbbobbbieo | 0:bf39986ebff7 | 158 | // checking for center line (single line) |
bbbobbbieo | 14:1a408e13679d | 159 | for (uint16_t i=10; i<118; i++) { |
bbbobbbieo | 0:bf39986ebff7 | 160 | if ((*(TFC_LineScanImage0+i) < 450)) { |
bbbobbbieo | 0:bf39986ebff7 | 161 | black_values_list[black_value_count] = i; |
bbbobbbieo | 0:bf39986ebff7 | 162 | black_value_count++; |
bbbobbbieo | 0:bf39986ebff7 | 163 | } |
bbbobbbieo | 0:bf39986ebff7 | 164 | } |
bbbobbbieo | 0:bf39986ebff7 | 165 | |
bbbobbbieo | 0:bf39986ebff7 | 166 | for(int i=0; i<black_value_count; i++) { |
bbbobbbieo | 0:bf39986ebff7 | 167 | sum_black += black_values_list[i]; |
bbbobbbieo | 0:bf39986ebff7 | 168 | } |
bbbobbbieo | 0:bf39986ebff7 | 169 | |
bbbobbbieo | 0:bf39986ebff7 | 170 | //update history |
bbbobbbieo | 0:bf39986ebff7 | 171 | center_past_4= center_past_3; |
bbbobbbieo | 0:bf39986ebff7 | 172 | center_past_3= center_past_2; |
bbbobbbieo | 0:bf39986ebff7 | 173 | center_past_2= center_past_1; |
bbbobbbieo | 0:bf39986ebff7 | 174 | center_past_1= center_now; |
bbbobbbieo | 0:bf39986ebff7 | 175 | |
bbbobbbieo | 0:bf39986ebff7 | 176 | |
bbbobbbieo | 0:bf39986ebff7 | 177 | //if (black_value_count>2) |
bbbobbbieo | 0:bf39986ebff7 | 178 | center_now = sum_black / black_value_count; |
bbbobbbieo | 0:bf39986ebff7 | 179 | |
bbbobbbieo | 0:bf39986ebff7 | 180 | uint8_t num = 0; |
bbbobbbieo | 0:bf39986ebff7 | 181 | |
bbbobbbieo | 15:60d699b587c5 | 182 | if(center_now > 10 && center_now < 45) // linear func |
bbbobbbieo | 0:bf39986ebff7 | 183 | num = 1; |
bbbobbbieo | 16:25eb20a73e59 | 184 | else if(center_now >= 45 && center_now < 65) // snap |
bbbobbbieo | 0:bf39986ebff7 | 185 | num = 2; |
bbbobbbieo | 16:25eb20a73e59 | 186 | else if(center_now > 64 && center_now < 64) // nothing zone |
bbbobbbieo | 0:bf39986ebff7 | 187 | num = 15; |
bbbobbbieo | 15:60d699b587c5 | 188 | else if(center_now >= 65 && center_now < 83) // snap |
bbbobbbieo | 0:bf39986ebff7 | 189 | num = 4; |
bbbobbbieo | 15:60d699b587c5 | 190 | else if(center_now >= 83 && center_now < 118) // linear func |
bbbobbbieo | 0:bf39986ebff7 | 191 | num = 8; |
bbbobbbieo | 0:bf39986ebff7 | 192 | |
bbbobbbieo | 0:bf39986ebff7 | 193 | else |
bbbobbbieo | 0:bf39986ebff7 | 194 | num = 0; |
bbbobbbieo | 0:bf39986ebff7 | 195 | |
bbbobbbieo | 6:ed97e4324202 | 196 | // get rid of garbage data sets |
bbbobbbieo | 2:5eb97170c199 | 197 | if (black_value_count<2) |
bbbobbbieo | 2:5eb97170c199 | 198 | num = 0; |
bbbobbbieo | 2:5eb97170c199 | 199 | |
bbbobbbieo | 0:bf39986ebff7 | 200 | TFC_SetBatteryLED(num); |
bbbobbbieo | 0:bf39986ebff7 | 201 | |
bbbobbbieo | 0:bf39986ebff7 | 202 | // best guess of center based on weighted average of history |
bbbobbbieo | 0:bf39986ebff7 | 203 | black_center_value = center_now; |
bbbobbbieo | 0:bf39986ebff7 | 204 | |
bbbobbbieo | 0:bf39986ebff7 | 205 | |
bbbobbbieo | 0:bf39986ebff7 | 206 | // turn left |
bbbobbbieo | 6:ed97e4324202 | 207 | // hit wall a little bit on the right |
bbbobbbieo | 15:60d699b587c5 | 208 | if (num==8 and right_counter <.2 ) // only if we arent turning right |
bbbobbbieo | 15:60d699b587c5 | 209 | //if(center_now >= 110 && center_now < 118 and right_counter <.2) |
bbbobbbieo | 0:bf39986ebff7 | 210 | { |
bbbobbbieo | 6:ed97e4324202 | 211 | //turn away a little bit for each frame that is wall |
bbbobbbieo | 14:1a408e13679d | 212 | if (left_counter >-.45) |
bbbobbbieo | 14:1a408e13679d | 213 | //left_counter -=.05; |
bbbobbbieo | 15:60d699b587c5 | 214 | //left_counter -=.03; |
bbbobbbieo | 16:25eb20a73e59 | 215 | //left_counter -= (.001428571*(128-center_now)); |
bbbobbbieo | 16:25eb20a73e59 | 216 | left_counter -= (.0015*(128-center_now)); |
bbbobbbieo | 14:1a408e13679d | 217 | |
bbbobbbieo | 14:1a408e13679d | 218 | turn_left=true; |
bbbobbbieo | 14:1a408e13679d | 219 | turn_right=false; |
bbbobbbieo | 14:1a408e13679d | 220 | } |
bbbobbbieo | 14:1a408e13679d | 221 | |
bbbobbbieo | 6:ed97e4324202 | 222 | // turn left real hard |
bbbobbbieo | 6:ed97e4324202 | 223 | // wall is close to center on right |
bbbobbbieo | 12:915f22e7d7d9 | 224 | if (num==4 and right_counter <.2) // only if we arent turning right |
bbbobbbieo | 0:bf39986ebff7 | 225 | { |
bbbobbbieo | 14:1a408e13679d | 226 | left_counter=-0.54; |
bbbobbbieo | 0:bf39986ebff7 | 227 | turn_left=true; |
bbbobbbieo | 0:bf39986ebff7 | 228 | turn_right=false; |
bbbobbbieo | 7:f21986164bf1 | 229 | need_decel=true; |
bbbobbbieo | 0:bf39986ebff7 | 230 | } |
bbbobbbieo | 0:bf39986ebff7 | 231 | |
bbbobbbieo | 14:1a408e13679d | 232 | // turn right hard |
bbbobbbieo | 14:1a408e13679d | 233 | // wall is close to center on left |
bbbobbbieo | 14:1a408e13679d | 234 | else if (num==2 and left_counter >-.2)// only if we arent turning left |
bbbobbbieo | 14:1a408e13679d | 235 | { |
bbbobbbieo | 14:1a408e13679d | 236 | right_counter =.54; |
bbbobbbieo | 14:1a408e13679d | 237 | turn_left=false; |
bbbobbbieo | 14:1a408e13679d | 238 | turn_right=true; |
bbbobbbieo | 14:1a408e13679d | 239 | need_decel=true; |
bbbobbbieo | 14:1a408e13679d | 240 | } |
bbbobbbieo | 14:1a408e13679d | 241 | |
bbbobbbieo | 14:1a408e13679d | 242 | |
bbbobbbieo | 6:ed97e4324202 | 243 | // turn right |
bbbobbbieo | 6:ed97e4324202 | 244 | // hit wall a little bit on the left |
bbbobbbieo | 15:60d699b587c5 | 245 | else if (num==1 and left_counter >-.2) // only if we arent turning left |
bbbobbbieo | 15:60d699b587c5 | 246 | //else if ( center_now > 15 && center_now < 27 and left_counter >-.2) // only if we arent turning left |
bbbobbbieo | 6:ed97e4324202 | 247 | { |
bbbobbbieo | 6:ed97e4324202 | 248 | //turn away a little bit for each frame that is wall |
bbbobbbieo | 14:1a408e13679d | 249 | if (right_counter <.45) |
bbbobbbieo | 15:60d699b587c5 | 250 | //right_counter +=.05; |
bbbobbbieo | 12:915f22e7d7d9 | 251 | //right_counter +=.03; |
bbbobbbieo | 16:25eb20a73e59 | 252 | //right_counter +=(.001428571*center_now); |
bbbobbbieo | 16:25eb20a73e59 | 253 | right_counter +=(.0015*center_now); |
bbbobbbieo | 6:ed97e4324202 | 254 | |
bbbobbbieo | 0:bf39986ebff7 | 255 | turn_left=false; |
bbbobbbieo | 0:bf39986ebff7 | 256 | turn_right=true; |
bbbobbbieo | 0:bf39986ebff7 | 257 | } |
bbbobbbieo | 6:ed97e4324202 | 258 | |
bbbobbbieo | 15:60d699b587c5 | 259 | |
bbbobbbieo | 6:ed97e4324202 | 260 | |
bbbobbbieo | 14:1a408e13679d | 261 | |
bbbobbbieo | 6:ed97e4324202 | 262 | // going straight yesssss |
bbbobbbieo | 10:761333231e50 | 263 | else if (turn_right == false and turn_left == false and (violence_level !=0)) |
bbbobbbieo | 2:5eb97170c199 | 264 | { |
bbbobbbieo | 2:5eb97170c199 | 265 | TFC_SetServo(0,(0.0+ bullshit_offset)); |
bbbobbbieo | 11:e04dc2090433 | 266 | TFC_SetMotorPWM(current_left_motor_speed-(.00005*num_of_straight), current_right_motor_speed+(.00005*num_of_straight)); // --left is faster, ++right is faster |
bbbobbbieo | 12:915f22e7d7d9 | 267 | num_of_left = 0; |
bbbobbbieo | 12:915f22e7d7d9 | 268 | num_of_right= 0; |
bbbobbbieo | 10:761333231e50 | 269 | if (violence_level !=0) |
bbbobbbieo | 10:761333231e50 | 270 | num_of_straight++; |
bbbobbbieo | 2:5eb97170c199 | 271 | } |
bbbobbbieo | 2:5eb97170c199 | 272 | |
bbbobbbieo | 6:ed97e4324202 | 273 | else{} |
bbbobbbieo | 0:bf39986ebff7 | 274 | |
bbbobbbieo | 0:bf39986ebff7 | 275 | //dealwiththeshit |
bbbobbbieo | 6:ed97e4324202 | 276 | // set servo and motors according to how much left we need to turn |
bbbobbbieo | 10:761333231e50 | 277 | if(turn_left and (violence_level !=0)) |
bbbobbbieo | 0:bf39986ebff7 | 278 | { |
bbbobbbieo | 9:c4a2a99b61e0 | 279 | num_of_straight = 0; // no longer on a straight |
bbbobbbieo | 12:915f22e7d7d9 | 280 | num_of_right = 0; |
bbbobbbieo | 12:915f22e7d7d9 | 281 | num_of_left++; |
bbbobbbieo | 9:c4a2a99b61e0 | 282 | turn_right = false; |
bbbobbbieo | 16:25eb20a73e59 | 283 | if (left_counter + bullshit_offset > -.35) |
bbbobbbieo | 16:25eb20a73e59 | 284 | TFC_SetServo(0,left_counter + bullshit_offset ); // set turning servo |
bbbobbbieo | 9:c4a2a99b61e0 | 285 | |
bbbobbbieo | 6:ed97e4324202 | 286 | // normalize to center each frame |
bbbobbbieo | 9:c4a2a99b61e0 | 287 | // left turning is - servo |
bbbobbbieo | 9:c4a2a99b61e0 | 288 | if(left_counter > -.2) // small turn, normalize quickly |
bbbobbbieo | 12:915f22e7d7d9 | 289 | left_counter += .017; |
bbbobbbieo | 9:c4a2a99b61e0 | 290 | else // hard turn, normalize slowly |
bbbobbbieo | 11:e04dc2090433 | 291 | left_counter += .01; |
bbbobbbieo | 9:c4a2a99b61e0 | 292 | |
bbbobbbieo | 9:c4a2a99b61e0 | 293 | // no longer turning boolean |
bbbobbbieo | 2:5eb97170c199 | 294 | if (left_counter > (0+ bullshit_offset)) |
bbbobbbieo | 1:e561f697985b | 295 | turn_left = false; |
bbbobbbieo | 6:ed97e4324202 | 296 | |
bbbobbbieo | 9:c4a2a99b61e0 | 297 | if (need_decel) // need to deal with the decel |
bbbobbbieo | 7:f21986164bf1 | 298 | { |
bbbobbbieo | 14:1a408e13679d | 299 | TFC_SetMotorPWM(current_left_motor_speed+(.7*left_counter), current_right_motor_speed-(.5*left_counter)); // ++left is slowed,--right is slowed |
bbbobbbieo | 7:f21986164bf1 | 300 | need_decel = false; |
bbbobbbieo | 7:f21986164bf1 | 301 | } |
bbbobbbieo | 9:c4a2a99b61e0 | 302 | else // turning speeds |
bbbobbbieo | 16:25eb20a73e59 | 303 | TFC_SetMotorPWM(current_left_motor_speed+(.25*left_counter), current_right_motor_speed+(.3*left_counter)+(.0005*num_of_left)); // ++left is slowed, ++right is faster |
bbbobbbieo | 9:c4a2a99b61e0 | 304 | }// end of turn left |
bbbobbbieo | 6:ed97e4324202 | 305 | |
bbbobbbieo | 6:ed97e4324202 | 306 | // set servo and motors according to how much right we need to turn |
bbbobbbieo | 10:761333231e50 | 307 | if(turn_right and (violence_level !=0)) |
bbbobbbieo | 0:bf39986ebff7 | 308 | { |
bbbobbbieo | 9:c4a2a99b61e0 | 309 | num_of_straight = 0; // no longer going straight |
bbbobbbieo | 12:915f22e7d7d9 | 310 | num_of_right++; |
bbbobbbieo | 12:915f22e7d7d9 | 311 | num_of_left=0; |
bbbobbbieo | 12:915f22e7d7d9 | 312 | |
bbbobbbieo | 0:bf39986ebff7 | 313 | turn_left =false; |
bbbobbbieo | 16:25eb20a73e59 | 314 | |
bbbobbbieo | 16:25eb20a73e59 | 315 | if (right_counter - bullshit_offset < .5) |
bbbobbbieo | 16:25eb20a73e59 | 316 | TFC_SetServo(0,right_counter - bullshit_offset); // set servo |
bbbobbbieo | 9:c4a2a99b61e0 | 317 | |
bbbobbbieo | 6:ed97e4324202 | 318 | // normalize to center each frame |
bbbobbbieo | 9:c4a2a99b61e0 | 319 | // right turning is + servo |
bbbobbbieo | 9:c4a2a99b61e0 | 320 | if(right_counter < .2) // small turn, normalize quickly |
bbbobbbieo | 12:915f22e7d7d9 | 321 | right_counter -= .017; |
bbbobbbieo | 9:c4a2a99b61e0 | 322 | else // hard turn, normalize slowly |
bbbobbbieo | 11:e04dc2090433 | 323 | right_counter -= .01; |
bbbobbbieo | 9:c4a2a99b61e0 | 324 | |
bbbobbbieo | 9:c4a2a99b61e0 | 325 | |
bbbobbbieo | 9:c4a2a99b61e0 | 326 | // no longer turning boolean |
bbbobbbieo | 2:5eb97170c199 | 327 | if (right_counter < (0+ bullshit_offset)) |
bbbobbbieo | 1:e561f697985b | 328 | turn_right = false; |
bbbobbbieo | 6:ed97e4324202 | 329 | |
bbbobbbieo | 9:c4a2a99b61e0 | 330 | if(need_decel)// need to deal with the decel |
bbbobbbieo | 7:f21986164bf1 | 331 | { |
bbbobbbieo | 14:1a408e13679d | 332 | TFC_SetMotorPWM(current_left_motor_speed+(.5*right_counter), current_right_motor_speed-(.7*right_counter)); // ++left is slowed,--right is slowed |
bbbobbbieo | 7:f21986164bf1 | 333 | need_decel = false; |
bbbobbbieo | 7:f21986164bf1 | 334 | } |
bbbobbbieo | 9:c4a2a99b61e0 | 335 | else // turning speeds |
bbbobbbieo | 16:25eb20a73e59 | 336 | TFC_SetMotorPWM(current_left_motor_speed-(.3*right_counter)-(.0005*num_of_right), current_right_motor_speed-(.25*right_counter)); // --left is faster, --right is slowed |
bbbobbbieo | 9:c4a2a99b61e0 | 337 | } // end with turn right |
bbbobbbieo | 9:c4a2a99b61e0 | 338 | |
bbbobbbieo | 0:bf39986ebff7 | 339 | |
bbbobbbieo | 0:bf39986ebff7 | 340 | // clearing values for next image processing round |
bbbobbbieo | 0:bf39986ebff7 | 341 | black_value_count = 0; |
bbbobbbieo | 0:bf39986ebff7 | 342 | sum_black = 0; |
bbbobbbieo | 0:bf39986ebff7 | 343 | // end image processing |
bbbobbbieo | 0:bf39986ebff7 | 344 | |
bbbobbbieo | 0:bf39986ebff7 | 345 | linescan_ping_pong = false; |
bbbobbbieo | 0:bf39986ebff7 | 346 | } // end checking channel 0 |
bbbobbbieo | 0:bf39986ebff7 | 347 | |
bbbobbbieo | 0:bf39986ebff7 | 348 | else { //checking channel 1 |
bbbobbbieo | 0:bf39986ebff7 | 349 | linescan_ping_pong = true; |
bbbobbbieo | 0:bf39986ebff7 | 350 | } |
bbbobbbieo | 0:bf39986ebff7 | 351 | |
bbbobbbieo | 0:bf39986ebff7 | 352 | TFC_LineScanImageReady = 0; // since we used it, we reset the flag |
bbbobbbieo | 0:bf39986ebff7 | 353 | }// end imageready |
bbbobbbieo | 0:bf39986ebff7 | 354 | }// end linescan stuff |
bbbobbbieo | 6:ed97e4324202 | 355 | } // end major loop |
bbbobbbieo | 6:ed97e4324202 | 356 | }// end main |