Line following bot using MAXREFDES89, MAX32600MBED, and Pololu QTR-RC sensor

Dependencies:   MAX14871_Shield mbed

Revision:
5:c673430c8a32
Parent:
4:eb6d6d25355c
Child:
6:dfbcdc63d4f8
--- a/main.cpp	Wed Jan 06 22:57:35 2016 +0000
+++ b/main.cpp	Sun Jan 10 19:12:08 2016 +0000
@@ -17,7 +17,7 @@
 //comment out following line for normal operation
 //#define TUNE_PID 1
 
-
+    
 //state variables for ISR
 volatile bool run = false;
 BusOut start_stop_led(D6, D7);
@@ -54,8 +54,8 @@
     
     //MAXREFDES89# object
     Max14871_Shield motor_shield(D14, D15, true);// Default config
-    
-    //local vars that are more meaningful and easier to use than the class name with scope operator
+        
+    // local vars with names that are more meaningful and easier to use than the class name with scope operator
     Max14871_Shield::max14871_motor_driver_t r_motor_driver = Max14871_Shield::MD4;
     Max14871_Shield::max14871_motor_driver_t l_motor_driver = Max14871_Shield::MD3;
     
@@ -65,10 +65,10 @@
     int32_t r_duty_cycle = 0;
     int32_t l_duty_cycle = 0;
     
-    const int32_t MAX_DUTY_CYCLE = 100;
-    const int32_t MIN_DUTY_CYCLE = -100;
+    const int32_t MAX_DUTY_CYCLE = 80;
+    const int32_t MIN_DUTY_CYCLE = -80;
     
-    const int32_t TARGET_DUTY_CYCLE = 35;
+    const int32_t TARGET_DUTY_CYCLE = 37;
     
     
     /***************************************************
@@ -85,14 +85,13 @@
     
     //Kc = critical gain, gain with just P control at which system becomes marginally stable
     //Pc = period of oscillation for previous scenario.
-    //for values below Kc = 10 and Pc was measured at 0.33
     
-    //set PID terms to 0 if not used/needed
-    //calculated starting points given in comments below
+    //Set PID terms to 0 if not used/needed
+    //For values below Kc = 10 and Pc was measured at 0.33 calculated starting points given in comments below
     //Ended up decreasing integral term, increasing derivative term and small decrease in proportional term.
-    int32_t kp = 5; //6
-    int32_t ki = 10; //.0576, divide by 1000 later
-    int32_t kd = 500; //156.25 
+    int32_t kp = 4; //6
+    int32_t ki = 3; //.0576, divide by 1000 later, 7
+    int32_t kd = 250; //156.25, 500 
     
     //initialize vars
     int32_t current_error = 0;
@@ -118,18 +117,6 @@
     const uint8_t ERROR_SIGNAL_N6 = 0x3F; //b00111111
     const uint8_t ERROR_SIGNAL_N7 = 0x7F; //b01111111
     
-    //special case error signals, 90 degree turns
-    const uint8_t ERROR_LT0 = 0xE0; //b11100000
-    const uint8_t ERROR_LT1 = 0xF0; //b11110000
-    const uint8_t ERROR_LT2 = 0xF8; //b11111000
-    const uint8_t ERROR_RT0 = 0x07; //b00000111
-    const uint8_t ERROR_RT1 = 0x0F; //b00001111
-    const uint8_t ERROR_RT2 = 0x1F; //b00011111
-    
-    //Lost bot error signal
-    const uint8_t ERROR_OFF_TRACK = 0xFF; //b11111111
-    
-    
     #ifdef TUNE_PID
         char response = 'N';
         
@@ -235,36 +222,7 @@
                 case(ERROR_SIGNAL_N7):
                     current_error = -7;
                 break;
-        
-                case(ERROR_LT0):
-                    current_error = 7;
-                break;
-                
-                case(ERROR_LT1):
-                    current_error = 7;
-                break;
-                
-                case(ERROR_LT2):
-                    current_error = 7;
-                break;
-                
-                case(ERROR_RT0):
-                    current_error = -7;
-                break;
-                
-                case(ERROR_RT1):
-                    current_error = -7;
-                break;
-                
-                case(ERROR_RT2):
-                    current_error = -7;
-                break;
-                
-                case(ERROR_OFF_TRACK):
-                    //requires reset
-                    motor_shield.set_pwm_duty_cycle(r_motor_driver, 0.0f);
-                    motor_shield.set_pwm_duty_cycle(l_motor_driver, 0.0f);
-                    while(1);
+
                 default:
                     current_error = previous_error;  
                 break;    
@@ -341,29 +299,25 @@
             if(r_duty_cycle < 0)
             {
                 r_duty_cycle = (r_duty_cycle * -1);
-                
                 motor_shield.set_operating_mode(r_motor_driver, Max14871_Shield::REVERSE);
-                motor_shield.set_pwm_duty_cycle(r_motor_driver, ((float) r_duty_cycle)/100.0f);
             }
             else
             {
                 motor_shield.set_operating_mode(r_motor_driver, Max14871_Shield::FORWARD);
-                motor_shield.set_pwm_duty_cycle(r_motor_driver, ((float) r_duty_cycle)/100.0f);
             }
+            motor_shield.set_pwm_duty_cycle(r_motor_driver, ((float) r_duty_cycle)/100.0f);
             
             //update direction and duty cycle for left motor
             if(l_duty_cycle < 0)
             {
                 l_duty_cycle = (l_duty_cycle * -1);
-                
                 motor_shield.set_operating_mode(l_motor_driver, Max14871_Shield::REVERSE);
-                motor_shield.set_pwm_duty_cycle(l_motor_driver, ((float) l_duty_cycle)/100.0f);
             }
             else
             {
                 motor_shield.set_operating_mode(l_motor_driver, Max14871_Shield::FORWARD);
-                motor_shield.set_pwm_duty_cycle(l_motor_driver, ((float) l_duty_cycle)/100.0f);
             }
+            motor_shield.set_pwm_duty_cycle(l_motor_driver, ((float) l_duty_cycle)/100.0f);
         }
       
         //shutdown