first commit

Dependencies:   mbed MMA8451Q

Committer:
aalawfi
Date:
Wed Nov 03 16:13:02 2021 +0000
Revision:
22:08d30ea47111
Parent:
20:7dcdadbd7bc0
Child:
23:4c743746533c
- PID control + new filters (time derivative, time integral), K gains not exact from matlab

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aalawfi 2:c857935f928e 1 //#include "steering_header.h"
aalawfi 2:c857935f928e 2 /*
aalawfi 2:c857935f928e 3 Include methods and functions
aalawfi 2:c857935f928e 4 */
aalawfi 2:c857935f928e 5
aalawfi 2:c857935f928e 6 // --------- LANDMARK COUNTER --------
aalawfi 2:c857935f928e 7 static int decimal_to_bin[16][BITS] = {
aalawfi 2:c857935f928e 8 {0,0,0,0}, // zero
aalawfi 2:c857935f928e 9 {0,0,0,1}, // one
aalawfi 2:c857935f928e 10 {0,0,1,0}, //two
aalawfi 2:c857935f928e 11 {0,0,1,1}, //three
aalawfi 2:c857935f928e 12 {0,1,0,0}, // four
aalawfi 2:c857935f928e 13 {0,1,0,1}, //five
aalawfi 2:c857935f928e 14 {0,1,1,0}, // six
aalawfi 2:c857935f928e 15 {0,1,1,1}, //seven
aalawfi 2:c857935f928e 16 {1,0,0,0}, //eight
aalawfi 2:c857935f928e 17 {1,0,0,1}, //nine
aalawfi 2:c857935f928e 18 {1,0,1,0}, //ten
aalawfi 2:c857935f928e 19 {1,0,1,1}, //11
aalawfi 2:c857935f928e 20 {1,1,0,0}, //12
aalawfi 2:c857935f928e 21 {1,1,0,1}, //13
aalawfi 2:c857935f928e 22 {1,1,1,0}, //14
aalawfi 2:c857935f928e 23 {1,1,1,1}, //15
aalawfi 2:c857935f928e 24 } ;
aalawfi 2:c857935f928e 25 void turn_seg_off(DigitalOut segment[]){
aalawfi 2:c857935f928e 26 for(int i =0; i<BITS; i++){
aalawfi 2:c857935f928e 27 segment[i] = 0;
aalawfi 2:c857935f928e 28 }
aalawfi 2:c857935f928e 29 }
aalawfi 2:c857935f928e 30 void show_decimal(int number, DigitalOut segment[]) {
aalawfi 2:c857935f928e 31 turn_seg_off(segment);
aalawfi 2:c857935f928e 32 for(int i =0; i < BITS; i++){
aalawfi 2:c857935f928e 33 segment[i] = decimal_to_bin[number][i];
aalawfi 2:c857935f928e 34 };
aalawfi 2:c857935f928e 35 }
aalawfi 2:c857935f928e 36 // ------------ LANDMARK DETECTION ------------
aalawfi 2:c857935f928e 37 int counter = 0;
quarren42 19:65fecaa2a387 38 volatile bool landmark_triggered=false; // Debugging purposes only
quarren42 19:65fecaa2a387 39 volatile bool restarted = false; // Debugging purposes only
aalawfi 2:c857935f928e 40 void landmark_counter(void){
quarren42 19:65fecaa2a387 41 landmark_triggered=false; // Debugging purposes only
quarren42 19:65fecaa2a387 42 restarted = false; // Debugging purposes only
aalawfi 2:c857935f928e 43 if(counter >= 15)
aalawfi 2:c857935f928e 44 counter =0;
aalawfi 2:c857935f928e 45 turn_seg_off(seg1);
quarren42 20:7dcdadbd7bc0 46 //if(right_landmark_sensor.read() == 1 || left_landmark_sensor.read() == 1 )
quarren42 20:7dcdadbd7bc0 47 //{
quarren42 19:65fecaa2a387 48 landmark_triggered =true; // Debugging purpose only
aalawfi 2:c857935f928e 49 counter++ ;
quarren42 19:65fecaa2a387 50
quarren42 20:7dcdadbd7bc0 51 // }
aalawfi 2:c857935f928e 52 if(right_landmark_sensor.read() == 1 && left_landmark_sensor.read() == 1 )
quarren42 19:65fecaa2a387 53 {
quarren42 19:65fecaa2a387 54 counter = 0;
quarren42 19:65fecaa2a387 55 restarted=true; // Debugging purposes only
quarren42 19:65fecaa2a387 56 }
aalawfi 13:0091da3021df 57
aalawfi 2:c857935f928e 58 show_decimal(counter, seg1);
aalawfi 2:c857935f928e 59 }
aalawfi 2:c857935f928e 60
aalawfi 2:c857935f928e 61
aalawfi 2:c857935f928e 62 float rad2dc (float angle){
aalawfi 2:c857935f928e 63 angle = std::max(MIN_TURN_ANGLE, std::min(angle, MAX_TURN_ANGLE));
aalawfi 2:c857935f928e 64 float x = (angle - MIN_TURN_ANGLE)/(MAX_TURN_ANGLE - MIN_TURN_ANGLE);
aalawfi 2:c857935f928e 65 float dutyCycle = 0.05*x+0.05;
aalawfi 2:c857935f928e 66 return dutyCycle;
aalawfi 2:c857935f928e 67 }
aalawfi 2:c857935f928e 68
aalawfi 2:c857935f928e 69 float current_duty_cycle=0.075;
aalawfi 2:c857935f928e 70 // -------------- PD CONTROLLER --------------
aalawfi 2:c857935f928e 71 // Global declearation
aalawfi 2:c857935f928e 72 float err_prev = 0;
quarren42 6:d2bd68ba99c9 73 float prev_duty = 0;
aalawfi 22:08d30ea47111 74 float errorArea;
aalawfi 22:08d30ea47111 75 float errorAreaPrev =0.0;
aalawfi 2:c857935f928e 76 void steering_control(void)
aalawfi 2:c857935f928e 77 {
aalawfi 2:c857935f928e 78
aalawfi 2:c857935f928e 79 float err;
aalawfi 2:c857935f928e 80 if(steering_enabled == true ){
aalawfi 2:c857935f928e 81 //Read each sensor and calculate the sensor difference
quarren42 19:65fecaa2a387 82 float sensor_difference = left_distance_sensor.read() -
aalawfi 22:08d30ea47111 83 right_distance_sensor.read();
aalawfi 2:c857935f928e 84
aalawfi 2:c857935f928e 85 //calculate the feedback term for the controller. distance (meters) from
aalawfi 2:c857935f928e 86 //track to sensor
aalawfi 2:c857935f928e 87 float feedback = (sensor_difference/SEN_DIFF_TO_DIST); //
aalawfi 2:c857935f928e 88
aalawfi 2:c857935f928e 89 //calculate the error term for the controller. distance (meters) from
aalawfi 2:c857935f928e 90 //desired position to sensor
aalawfi 2:c857935f928e 91 err = REF - feedback; //
aalawfi 22:08d30ea47111 92
aalawfi 22:08d30ea47111 93 //Area of the error: TimeStep*err (width*length of a rectangle)
aalawfi 22:08d30ea47111 94 errorArea= TI_STEERING*err + errorAreaPrev;
aalawfi 22:08d30ea47111 95
aalawfi 22:08d30ea47111 96
aalawfi 2:c857935f928e 97 //Calculate the derivative of the error term for the controller.
aalawfi 2:c857935f928e 98 // e'(t) = (error(t) - error(t-1))/dt
aalawfi 22:08d30ea47111 99 float errChange = (err - err_prev)/(TI_STEERING);
aalawfi 2:c857935f928e 100
aalawfi 2:c857935f928e 101 //Calculate the controller output
aalawfi 2:c857935f928e 102 //Angle in radians
aalawfi 22:08d30ea47111 103 float servo_angle = (0.04+KP*err + KD*errChange*TIME_DERIVATIVE + KI_STEERING*errorArea*TIME_INTEGRAL);
aalawfi 2:c857935f928e 104 //Calculate the duty cycle for the servo motor
aalawfi 2:c857935f928e 105 current_duty_cycle = rad2dc(servo_angle);
aalawfi 22:08d30ea47111 106 servo_motor_pwm.write(current_duty_cycle);
aalawfi 22:08d30ea47111 107
aalawfi 2:c857935f928e 108 }
aalawfi 9:5320c2dfb913 109 else {
aalawfi 2:c857935f928e 110 current_duty_cycle = rad2dc(M_PI/4);
aalawfi 2:c857935f928e 111 servo_motor_pwm.write(current_duty_cycle);
aalawfi 9:5320c2dfb913 112 };
aalawfi 2:c857935f928e 113 //Save the error for the next calculation.
aalawfi 2:c857935f928e 114 prev_duty = current_duty_cycle;
aalawfi 2:c857935f928e 115 err_prev = err;
aalawfi 22:08d30ea47111 116 errorAreaPrev = errorArea;
aalawfi 2:c857935f928e 117 }
aalawfi 2:c857935f928e 118
aalawfi 2:c857935f928e 119
aalawfi 2:c857935f928e 120