fatboyslim / Mbed 2 deprecated bouncesinglecam

Dependencies:   FRDM-TFC mbed

Committer:
bbbobbbieo
Date:
Thu Mar 26 20:18:04 2015 +0000
Revision:
14:1a408e13679d
Parent:
13:8a667757beb3
Child:
15:60d699b587c5
garbage and shit, added two cases. mikes working on a linear function with 4 cases, we're going to move to that

Who changed what in which revision?

UserRevisionLine numberNew 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 3:e14a4ea167ca 91 float bullshit_offset = .074;
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 14:1a408e13679d 182 if(center_now > 10 && center_now < 27)
bbbobbbieo 0:bf39986ebff7 183 num = 1;
bbbobbbieo 14:1a408e13679d 184 else if(center_now >= 27 && center_now < 63)
bbbobbbieo 0:bf39986ebff7 185 num = 2;
bbbobbbieo 14:1a408e13679d 186 else if(center_now > 63 && center_now < 65)
bbbobbbieo 0:bf39986ebff7 187 num = 15;
bbbobbbieo 14:1a408e13679d 188 else if(center_now >= 65 && center_now < 81)
bbbobbbieo 0:bf39986ebff7 189 num = 4;
bbbobbbieo 14:1a408e13679d 190 else if(center_now >= 81 && center_now < 118)
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 14:1a408e13679d 208 //if (num==8 and right_counter <.2 ) // only if we arent turning right
bbbobbbieo 14:1a408e13679d 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 14:1a408e13679d 214 left_counter -=.03;
bbbobbbieo 14:1a408e13679d 215
bbbobbbieo 14:1a408e13679d 216 turn_left=true;
bbbobbbieo 14:1a408e13679d 217 turn_right=false;
bbbobbbieo 14:1a408e13679d 218 }
bbbobbbieo 14:1a408e13679d 219
bbbobbbieo 14:1a408e13679d 220 // turn left
bbbobbbieo 14:1a408e13679d 221 // hit wall a little bit on the right
bbbobbbieo 14:1a408e13679d 222 if(center_now >= 81 && center_now < 110 and right_counter <.2)
bbbobbbieo 14:1a408e13679d 223 {
bbbobbbieo 14:1a408e13679d 224 //turn away a little bit for each frame that is wall
bbbobbbieo 14:1a408e13679d 225 if (left_counter >-.45)
bbbobbbieo 12:915f22e7d7d9 226 left_counter -=.05;
bbbobbbieo 12:915f22e7d7d9 227 //left_counter -=.03;
bbbobbbieo 4:18f5328ff5a3 228
bbbobbbieo 0:bf39986ebff7 229 turn_left=true;
bbbobbbieo 0:bf39986ebff7 230 turn_right=false;
bbbobbbieo 0:bf39986ebff7 231 }
bbbobbbieo 6:ed97e4324202 232
bbbobbbieo 14:1a408e13679d 233
bbbobbbieo 6:ed97e4324202 234 // turn left real hard
bbbobbbieo 6:ed97e4324202 235 // wall is close to center on right
bbbobbbieo 12:915f22e7d7d9 236 if (num==4 and right_counter <.2) // only if we arent turning right
bbbobbbieo 0:bf39986ebff7 237 {
bbbobbbieo 14:1a408e13679d 238 left_counter=-0.54;
bbbobbbieo 0:bf39986ebff7 239 turn_left=true;
bbbobbbieo 0:bf39986ebff7 240 turn_right=false;
bbbobbbieo 7:f21986164bf1 241 need_decel=true;
bbbobbbieo 0:bf39986ebff7 242 }
bbbobbbieo 0:bf39986ebff7 243
bbbobbbieo 14:1a408e13679d 244 // turn right hard
bbbobbbieo 14:1a408e13679d 245 // wall is close to center on left
bbbobbbieo 14:1a408e13679d 246 else if (num==2 and left_counter >-.2)// only if we arent turning left
bbbobbbieo 14:1a408e13679d 247 {
bbbobbbieo 14:1a408e13679d 248 right_counter =.54;
bbbobbbieo 14:1a408e13679d 249 turn_left=false;
bbbobbbieo 14:1a408e13679d 250 turn_right=true;
bbbobbbieo 14:1a408e13679d 251 need_decel=true;
bbbobbbieo 14:1a408e13679d 252 }
bbbobbbieo 14:1a408e13679d 253
bbbobbbieo 14:1a408e13679d 254
bbbobbbieo 6:ed97e4324202 255 // turn right
bbbobbbieo 6:ed97e4324202 256 // hit wall a little bit on the left
bbbobbbieo 14:1a408e13679d 257 //else if (num==1 and left_counter >-.2) // only if we arent turning left
bbbobbbieo 14:1a408e13679d 258 else if ( center_now > 15 && center_now < 27 and left_counter >-.2) // only if we arent turning left
bbbobbbieo 6:ed97e4324202 259 {
bbbobbbieo 6:ed97e4324202 260 //turn away a little bit for each frame that is wall
bbbobbbieo 14:1a408e13679d 261 if (right_counter <.45)
bbbobbbieo 12:915f22e7d7d9 262 right_counter +=.05;
bbbobbbieo 12:915f22e7d7d9 263 //right_counter +=.03;
bbbobbbieo 6:ed97e4324202 264
bbbobbbieo 0:bf39986ebff7 265 turn_left=false;
bbbobbbieo 0:bf39986ebff7 266 turn_right=true;
bbbobbbieo 0:bf39986ebff7 267 }
bbbobbbieo 6:ed97e4324202 268
bbbobbbieo 14:1a408e13679d 269 // turn right
bbbobbbieo 14:1a408e13679d 270 // hit wall a little bit on the left
bbbobbbieo 14:1a408e13679d 271 else if ( center_now > 10 && center_now < 15 and left_counter >-.2) // only if we arent turning left
bbbobbbieo 14:1a408e13679d 272 {
bbbobbbieo 14:1a408e13679d 273 //turn away a little bit for each frame that is wall
bbbobbbieo 14:1a408e13679d 274 if (right_counter <.45)
bbbobbbieo 14:1a408e13679d 275 //right_counter +=.05;
bbbobbbieo 14:1a408e13679d 276 right_counter +=.03;
bbbobbbieo 14:1a408e13679d 277
bbbobbbieo 0:bf39986ebff7 278 turn_left=false;
bbbobbbieo 0:bf39986ebff7 279 turn_right=true;
bbbobbbieo 0:bf39986ebff7 280 }
bbbobbbieo 6:ed97e4324202 281
bbbobbbieo 14:1a408e13679d 282
bbbobbbieo 6:ed97e4324202 283 // going straight yesssss
bbbobbbieo 10:761333231e50 284 else if (turn_right == false and turn_left == false and (violence_level !=0))
bbbobbbieo 2:5eb97170c199 285 {
bbbobbbieo 2:5eb97170c199 286 TFC_SetServo(0,(0.0+ bullshit_offset));
bbbobbbieo 11:e04dc2090433 287 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 288 num_of_left = 0;
bbbobbbieo 12:915f22e7d7d9 289 num_of_right= 0;
bbbobbbieo 10:761333231e50 290 if (violence_level !=0)
bbbobbbieo 10:761333231e50 291 num_of_straight++;
bbbobbbieo 2:5eb97170c199 292 }
bbbobbbieo 2:5eb97170c199 293
bbbobbbieo 6:ed97e4324202 294 else{}
bbbobbbieo 0:bf39986ebff7 295
bbbobbbieo 0:bf39986ebff7 296 //dealwiththeshit
bbbobbbieo 6:ed97e4324202 297 // set servo and motors according to how much left we need to turn
bbbobbbieo 10:761333231e50 298 if(turn_left and (violence_level !=0))
bbbobbbieo 0:bf39986ebff7 299 {
bbbobbbieo 9:c4a2a99b61e0 300 num_of_straight = 0; // no longer on a straight
bbbobbbieo 12:915f22e7d7d9 301 num_of_right = 0;
bbbobbbieo 12:915f22e7d7d9 302 num_of_left++;
bbbobbbieo 9:c4a2a99b61e0 303 turn_right = false;
bbbobbbieo 9:c4a2a99b61e0 304 TFC_SetServo(0,left_counter + bullshit_offset ); // set turning servo
bbbobbbieo 9:c4a2a99b61e0 305
bbbobbbieo 6:ed97e4324202 306 // normalize to center each frame
bbbobbbieo 9:c4a2a99b61e0 307 // left turning is - servo
bbbobbbieo 9:c4a2a99b61e0 308 if(left_counter > -.2) // small turn, normalize quickly
bbbobbbieo 12:915f22e7d7d9 309 left_counter += .017;
bbbobbbieo 9:c4a2a99b61e0 310 else // hard turn, normalize slowly
bbbobbbieo 11:e04dc2090433 311 left_counter += .01;
bbbobbbieo 9:c4a2a99b61e0 312
bbbobbbieo 9:c4a2a99b61e0 313 // no longer turning boolean
bbbobbbieo 2:5eb97170c199 314 if (left_counter > (0+ bullshit_offset))
bbbobbbieo 1:e561f697985b 315 turn_left = false;
bbbobbbieo 6:ed97e4324202 316
bbbobbbieo 9:c4a2a99b61e0 317 if (need_decel) // need to deal with the decel
bbbobbbieo 7:f21986164bf1 318 {
bbbobbbieo 14:1a408e13679d 319 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 320 need_decel = false;
bbbobbbieo 7:f21986164bf1 321 }
bbbobbbieo 9:c4a2a99b61e0 322 else // turning speeds
bbbobbbieo 12:915f22e7d7d9 323 TFC_SetMotorPWM(current_left_motor_speed+(.3*left_counter), current_right_motor_speed+(.3*left_counter)+(.00005*num_of_left)); // ++left is slowed, ++right is faster
bbbobbbieo 9:c4a2a99b61e0 324 }// end of turn left
bbbobbbieo 6:ed97e4324202 325
bbbobbbieo 6:ed97e4324202 326 // set servo and motors according to how much right we need to turn
bbbobbbieo 10:761333231e50 327 if(turn_right and (violence_level !=0))
bbbobbbieo 0:bf39986ebff7 328 {
bbbobbbieo 9:c4a2a99b61e0 329 num_of_straight = 0; // no longer going straight
bbbobbbieo 12:915f22e7d7d9 330 num_of_right++;
bbbobbbieo 12:915f22e7d7d9 331 num_of_left=0;
bbbobbbieo 12:915f22e7d7d9 332
bbbobbbieo 0:bf39986ebff7 333 turn_left =false;
bbbobbbieo 9:c4a2a99b61e0 334 TFC_SetServo(0,right_counter - bullshit_offset); // set servo
bbbobbbieo 9:c4a2a99b61e0 335
bbbobbbieo 6:ed97e4324202 336 // normalize to center each frame
bbbobbbieo 9:c4a2a99b61e0 337 // right turning is + servo
bbbobbbieo 9:c4a2a99b61e0 338 if(right_counter < .2) // small turn, normalize quickly
bbbobbbieo 12:915f22e7d7d9 339 right_counter -= .017;
bbbobbbieo 9:c4a2a99b61e0 340 else // hard turn, normalize slowly
bbbobbbieo 11:e04dc2090433 341 right_counter -= .01;
bbbobbbieo 9:c4a2a99b61e0 342
bbbobbbieo 9:c4a2a99b61e0 343
bbbobbbieo 9:c4a2a99b61e0 344 // no longer turning boolean
bbbobbbieo 2:5eb97170c199 345 if (right_counter < (0+ bullshit_offset))
bbbobbbieo 1:e561f697985b 346 turn_right = false;
bbbobbbieo 6:ed97e4324202 347
bbbobbbieo 9:c4a2a99b61e0 348 if(need_decel)// need to deal with the decel
bbbobbbieo 7:f21986164bf1 349 {
bbbobbbieo 14:1a408e13679d 350 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 351 need_decel = false;
bbbobbbieo 7:f21986164bf1 352 }
bbbobbbieo 9:c4a2a99b61e0 353 else // turning speeds
bbbobbbieo 12:915f22e7d7d9 354 TFC_SetMotorPWM(current_left_motor_speed-(.3*right_counter)-(.00005*num_of_right), current_right_motor_speed-(.3*right_counter)); // --left is faster, --right is slowed
bbbobbbieo 9:c4a2a99b61e0 355 } // end with turn right
bbbobbbieo 9:c4a2a99b61e0 356
bbbobbbieo 0:bf39986ebff7 357
bbbobbbieo 0:bf39986ebff7 358 // clearing values for next image processing round
bbbobbbieo 0:bf39986ebff7 359 black_value_count = 0;
bbbobbbieo 0:bf39986ebff7 360 sum_black = 0;
bbbobbbieo 0:bf39986ebff7 361 // end image processing
bbbobbbieo 0:bf39986ebff7 362
bbbobbbieo 0:bf39986ebff7 363 linescan_ping_pong = false;
bbbobbbieo 0:bf39986ebff7 364 } // end checking channel 0
bbbobbbieo 0:bf39986ebff7 365
bbbobbbieo 0:bf39986ebff7 366 else { //checking channel 1
bbbobbbieo 0:bf39986ebff7 367 linescan_ping_pong = true;
bbbobbbieo 0:bf39986ebff7 368 }
bbbobbbieo 0:bf39986ebff7 369
bbbobbbieo 0:bf39986ebff7 370 TFC_LineScanImageReady = 0; // since we used it, we reset the flag
bbbobbbieo 0:bf39986ebff7 371 }// end imageready
bbbobbbieo 0:bf39986ebff7 372 }// end linescan stuff
bbbobbbieo 6:ed97e4324202 373 } // end major loop
bbbobbbieo 6:ed97e4324202 374 }// end main