first commit

Dependencies:   mbed MMA8451Q

Committer:
aalawfi
Date:
Tue Oct 26 01:22:05 2021 +0000
Revision:
9:5320c2dfb913
Parent:
6:d2bd68ba99c9
Child:
13:0091da3021df
-

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;
aalawfi 2:c857935f928e 38 void landmark_counter(void){
aalawfi 2:c857935f928e 39 if(counter >= 15)
aalawfi 2:c857935f928e 40 counter =0;
aalawfi 2:c857935f928e 41 turn_seg_off(seg1);
aalawfi 2:c857935f928e 42 if(right_landmark_sensor.read() == 1 || left_landmark_sensor.read() == 1 )
aalawfi 2:c857935f928e 43 counter++ ;
aalawfi 2:c857935f928e 44 if(right_landmark_sensor.read() == 1 && left_landmark_sensor.read() == 1 )
aalawfi 2:c857935f928e 45 counter = 0;
aalawfi 2:c857935f928e 46
aalawfi 2:c857935f928e 47 show_decimal(counter, seg1);
aalawfi 2:c857935f928e 48 }
aalawfi 2:c857935f928e 49
aalawfi 2:c857935f928e 50
aalawfi 2:c857935f928e 51 float rad2dc (float angle){
aalawfi 2:c857935f928e 52 angle = std::max(MIN_TURN_ANGLE, std::min(angle, MAX_TURN_ANGLE));
aalawfi 2:c857935f928e 53 float x = (angle - MIN_TURN_ANGLE)/(MAX_TURN_ANGLE - MIN_TURN_ANGLE);
aalawfi 2:c857935f928e 54 float dutyCycle = 0.05*x+0.05;
aalawfi 2:c857935f928e 55 return dutyCycle;
aalawfi 2:c857935f928e 56 }
aalawfi 2:c857935f928e 57
aalawfi 2:c857935f928e 58 float current_duty_cycle=0.075;
aalawfi 2:c857935f928e 59 // -------------- PD CONTROLLER --------------
aalawfi 2:c857935f928e 60 // Global declearation
aalawfi 2:c857935f928e 61 float err_prev = 0;
quarren42 6:d2bd68ba99c9 62 float prev_duty = 0;
aalawfi 2:c857935f928e 63 void steering_control(void)
aalawfi 2:c857935f928e 64 {
aalawfi 2:c857935f928e 65
aalawfi 2:c857935f928e 66 float err;
aalawfi 2:c857935f928e 67 if(steering_enabled == true ){
aalawfi 2:c857935f928e 68 //Read each sensor and calculate the sensor difference
aalawfi 2:c857935f928e 69 float sensor_difference = left_distance_sensor.read() -
aalawfi 2:c857935f928e 70 right_distance_sensor.read();
aalawfi 2:c857935f928e 71
aalawfi 2:c857935f928e 72 //calculate the feedback term for the controller. distance (meters) from
aalawfi 2:c857935f928e 73 //track to sensor
aalawfi 2:c857935f928e 74 float feedback = (sensor_difference/SEN_DIFF_TO_DIST); //
aalawfi 2:c857935f928e 75
aalawfi 2:c857935f928e 76 //calculate the error term for the controller. distance (meters) from
aalawfi 2:c857935f928e 77 //desired position to sensor
aalawfi 2:c857935f928e 78 err = REF - feedback; //
aalawfi 2:c857935f928e 79
aalawfi 2:c857935f928e 80 //Calculate the derivative of the error term for the controller.
aalawfi 2:c857935f928e 81 // e'(t) = (error(t) - error(t-1))/dt
aalawfi 2:c857935f928e 82 float errChange = (err - err_prev)/(TI);
aalawfi 2:c857935f928e 83
aalawfi 2:c857935f928e 84 //Calculate the controller output
aalawfi 2:c857935f928e 85 //Angle in radians
aalawfi 2:c857935f928e 86 float servo_angle = (KP*err + KD*errChange*TIME_DERIVATIVE)/10;
aalawfi 2:c857935f928e 87 //Calculate the duty cycle for the servo motor
aalawfi 2:c857935f928e 88 current_duty_cycle = rad2dc(servo_angle);
aalawfi 2:c857935f928e 89 if( abs(current_duty_cycle - prev_duty) >= 0.001){
aalawfi 2:c857935f928e 90 servo_motor_pwm.write(current_duty_cycle);
aalawfi 9:5320c2dfb913 91 };
aalawfi 2:c857935f928e 92 }
aalawfi 9:5320c2dfb913 93 else {
aalawfi 2:c857935f928e 94 current_duty_cycle = rad2dc(M_PI/4);
aalawfi 2:c857935f928e 95 servo_motor_pwm.write(current_duty_cycle);
aalawfi 9:5320c2dfb913 96 };
aalawfi 2:c857935f928e 97 //Save the error for the next calculation.
aalawfi 2:c857935f928e 98 prev_duty = current_duty_cycle;
aalawfi 2:c857935f928e 99 err_prev = err;
aalawfi 2:c857935f928e 100
aalawfi 2:c857935f928e 101 }
aalawfi 2:c857935f928e 102
aalawfi 2:c857935f928e 103
aalawfi 2:c857935f928e 104