fatboyslim / Mbed 2 deprecated bouncesinglecam

Dependencies:   FRDM-TFC mbed

Revision:
30:8e0ad64703d0
Parent:
29:4bf08c74f792
Child:
31:55d26f4ef5f0
--- a/main.cpp	Sat Mar 28 22:04:46 2015 +0000
+++ b/main.cpp	Sun Mar 29 17:33:31 2015 +0000
@@ -4,8 +4,8 @@
 #include <stdio.h>
 //#include "serialib.h"
 
-const float AGGRESSIVE = .55;
-const float MODERATE =.48;
+const float AGGRESSIVE = .43;
+const float MODERATE =.41;
 const float CONSERVATIVE =.39;
 const float STOP =0;
 const float PROTECTION_THRESHOLD_UPPER =.7;
@@ -55,32 +55,21 @@
     bool turn_left=false;
     bool turn_right=false;
 
-    float bullshit_offset = .075;
+    float bullshit_offset = .078;
+    
+    int num_of_straight =0;
+    int num_of_left =0;
+    int num_of_right=0;
+    
+    int values_list_add =0;
+    int value_count =0;
+    int avg_value=0;
+    //int black_threshhold=0;
+    int black_threshhold =450;
 
     // major loop
     while(1) {
 
-        // manual servo control, unused
-        if (TFC_ReadPushButton(0) != 0 ) {
-            current_servo_position = current_servo_position-.005;
-            if(current_servo_position <= -0.4)
-                current_servo_position = -0.4;
-            TFC_SetServo(0, current_servo_position);
-        }// end check button0
-
-        else {}
-        
-
-        // manual servo control, unused
-        if (TFC_ReadPushButton(1) != 0 ) {
-            current_servo_position = current_servo_position+.005;
-            if(current_servo_position >= 0.4)
-                current_servo_position = 0.4;
-            TFC_SetServo(0, current_servo_position);
-        }// end check button1
-
-        else {}
-
         // initial motor stuff
         if(rear_motor_enable_flag) {
             TFC_HBRIDGE_ENABLE;
@@ -109,7 +98,6 @@
                 current_right_motor_speed = STOP;
             }
             
-
             // protection block
             if(current_left_motor_speed >= PROTECTION_THRESHOLD_UPPER)
                 current_left_motor_speed= PROTECTION_THRESHOLD_UPPER;
@@ -133,9 +121,12 @@
                 if (linescan_ping_pong) {
                     //checking channel 0
                     
+                    
                     // checking for center line (single line)
                     for (uint16_t i=10; i<118; i++) {
-                        if ((*(TFC_LineScanImage0+i) < 450)) {
+
+                        int black_threshhold =700*(TFC_ReadPot(0));
+                        if ((*(TFC_LineScanImage0+i) < black_threshhold)) {
                             black_values_list[black_value_count] = i;
                             black_value_count++;
                         }
@@ -146,11 +137,12 @@
                     }
 
                     //update history
+                    /*
                     center_past_4= center_past_3;
                     center_past_3= center_past_2;
                     center_past_2= center_past_1;
                     center_past_1= center_now;
-                    
+                    */
                     
                     //if (black_value_count>2)
                     center_now = sum_black / black_value_count;
@@ -174,7 +166,8 @@
                     if (black_value_count<2)
                         num = 0;
                         
-                    if (black_value_count>27)
+                    //if (black_value_count>26 and ((*(TFC_LineScanImage0+64) > 450) or (*(TFC_LineScanImage0+63) > 450)or(*(TFC_LineScanImage0+62) > 450)))
+                    if (black_value_count>26)
                     {
                         while(1)
                             TFC_SetMotorPWM(0, 0);
@@ -187,7 +180,7 @@
   
                     
                     // turn left
-                    if (num==8 and right_counter <.2 )
+                    if (num==8 and right_counter <.35)
                     {
                         
                         if (center_now <113)
@@ -201,15 +194,16 @@
                         turn_right=false;
 
                     }
-                    if (num==4 and right_counter <.2)
+                    if (num==4 and right_counter <.35)
                     {
                         left_counter=-0.6;
+                        //left_counter=-0.55;
                         turn_left=true;
                         turn_right=false;
                     }
                     
                     // need to turn right
-                    else if (num==1 and left_counter >-.2)
+                    else if (num==1 and left_counter >-.35)
                     {
                         if (center_now >15)
                             right_counter =.4;
@@ -223,18 +217,22 @@
                         turn_right=true;
                    
                     }
-                      else if (num==2 and left_counter >-.2)
+                
+                      else if (num==2 and left_counter >-.35)
                     {
                         right_counter =.6;
+                        //right_counter =.55;
                         turn_left=false;
                         turn_right=true;
                     }
 
-                   
-                   
+                    //straight
                    else if (turn_right == false and turn_left == false)
                     {
                         TFC_SetServo(0,(0.0+ bullshit_offset));
+                        TFC_SetMotorPWM(current_left_motor_speed-(.0004*num_of_straight), current_right_motor_speed+(.0004*num_of_straight)); // --left is faster, ++right is faster
+                        if (violence_level !=0)
+                            num_of_straight++;
                     }
                    
                     else
@@ -245,6 +243,7 @@
                     if(turn_left)
                     {
                         turn_right = false;
+                        num_of_straight = 0; // no longer on a straight
                         
                         TFC_SetServo(0,left_counter+ bullshit_offset );
                         left_counter += .01;
@@ -257,7 +256,10 @@
                     if(turn_right)
                     {
                         turn_left =false;
-                        TFC_SetServo(0,right_counter- bullshit_offset );
+                        num_of_straight = 0; // no longer on a straight
+                        
+                        //TFC_SetServo(0,right_counter- bullshit_offset );
+                        TFC_SetServo(0,right_counter);
                         right_counter -= .01;
                         if (right_counter < (0+ bullshit_offset))
                             turn_right = false;
@@ -268,6 +270,10 @@
                     // clearing values for next image processing round
                     black_value_count = 0;
                     sum_black = 0;
+                    values_list_add =0;
+                    value_count =0;
+                    avg_value=0;
+                    black_threshhold=0;
 
                     // end image processing