
first commit
steering_methods.h@23:4c743746533c, 2021-11-03 (annotated)
- Committer:
- aalawfi
- Date:
- Wed Nov 03 21:52:00 2021 +0000
- Revision:
- 23:4c743746533c
- Parent:
- 22:08d30ea47111
- Child:
- 24:7bf492bf50f4
- Most recent 11/3/2021
Who changed what in which revision?
User | Revision | Line number | New 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 | 23:4c743746533c | 37 | Timer land; |
aalawfi | 2:c857935f928e | 38 | int counter = 0; |
quarren42 | 19:65fecaa2a387 | 39 | volatile bool landmark_triggered=false; // Debugging purposes only |
quarren42 | 19:65fecaa2a387 | 40 | volatile bool restarted = false; // Debugging purposes only |
aalawfi | 23:4c743746533c | 41 | |
aalawfi | 2:c857935f928e | 42 | void landmark_counter(void){ |
aalawfi | 23:4c743746533c | 43 | land.start(); |
quarren42 | 19:65fecaa2a387 | 44 | landmark_triggered=false; // Debugging purposes only |
quarren42 | 19:65fecaa2a387 | 45 | restarted = false; // Debugging purposes only |
aalawfi | 2:c857935f928e | 46 | if(counter >= 15) |
aalawfi | 2:c857935f928e | 47 | counter =0; |
aalawfi | 2:c857935f928e | 48 | turn_seg_off(seg1); |
aalawfi | 23:4c743746533c | 49 | // if(right_landmark_sensor.read() == 1 || left_landmark_sensor.read() == 1 ) |
aalawfi | 23:4c743746533c | 50 | // { |
aalawfi | 23:4c743746533c | 51 | if(land.read_us() > 1) |
aalawfi | 23:4c743746533c | 52 | { |
aalawfi | 23:4c743746533c | 53 | landmark_triggered =true; // Debugging purpose only |
aalawfi | 23:4c743746533c | 54 | counter++ ; |
aalawfi | 23:4c743746533c | 55 | land.stop(); |
aalawfi | 23:4c743746533c | 56 | } |
aalawfi | 23:4c743746533c | 57 | |
aalawfi | 23:4c743746533c | 58 | // } |
aalawfi | 2:c857935f928e | 59 | if(right_landmark_sensor.read() == 1 && left_landmark_sensor.read() == 1 ) |
quarren42 | 19:65fecaa2a387 | 60 | { |
aalawfi | 23:4c743746533c | 61 | if(land.read_us() > 1) |
aalawfi | 23:4c743746533c | 62 | { |
aalawfi | 23:4c743746533c | 63 | counter = 0; |
aalawfi | 23:4c743746533c | 64 | restarted=true; |
aalawfi | 23:4c743746533c | 65 | } // Debugging purposes only |
quarren42 | 19:65fecaa2a387 | 66 | } |
aalawfi | 23:4c743746533c | 67 | land.stop(); |
aalawfi | 2:c857935f928e | 68 | show_decimal(counter, seg1); |
aalawfi | 2:c857935f928e | 69 | } |
aalawfi | 2:c857935f928e | 70 | |
aalawfi | 2:c857935f928e | 71 | |
aalawfi | 2:c857935f928e | 72 | float rad2dc (float angle){ |
aalawfi | 2:c857935f928e | 73 | angle = std::max(MIN_TURN_ANGLE, std::min(angle, MAX_TURN_ANGLE)); |
aalawfi | 2:c857935f928e | 74 | float x = (angle - MIN_TURN_ANGLE)/(MAX_TURN_ANGLE - MIN_TURN_ANGLE); |
aalawfi | 2:c857935f928e | 75 | float dutyCycle = 0.05*x+0.05; |
aalawfi | 2:c857935f928e | 76 | return dutyCycle; |
aalawfi | 2:c857935f928e | 77 | } |
aalawfi | 2:c857935f928e | 78 | |
aalawfi | 2:c857935f928e | 79 | float current_duty_cycle=0.075; |
aalawfi | 2:c857935f928e | 80 | // -------------- PD CONTROLLER -------------- |
aalawfi | 2:c857935f928e | 81 | // Global declearation |
aalawfi | 2:c857935f928e | 82 | float err_prev = 0; |
quarren42 | 6:d2bd68ba99c9 | 83 | float prev_duty = 0; |
aalawfi | 22:08d30ea47111 | 84 | float errorArea; |
aalawfi | 22:08d30ea47111 | 85 | float errorAreaPrev =0.0; |
aalawfi | 2:c857935f928e | 86 | void steering_control(void) |
aalawfi | 2:c857935f928e | 87 | { |
aalawfi | 2:c857935f928e | 88 | |
aalawfi | 2:c857935f928e | 89 | float err; |
aalawfi | 2:c857935f928e | 90 | if(steering_enabled == true ){ |
aalawfi | 2:c857935f928e | 91 | //Read each sensor and calculate the sensor difference |
quarren42 | 19:65fecaa2a387 | 92 | float sensor_difference = left_distance_sensor.read() - |
aalawfi | 22:08d30ea47111 | 93 | right_distance_sensor.read(); |
aalawfi | 2:c857935f928e | 94 | |
aalawfi | 2:c857935f928e | 95 | //calculate the feedback term for the controller. distance (meters) from |
aalawfi | 2:c857935f928e | 96 | //track to sensor |
aalawfi | 2:c857935f928e | 97 | float feedback = (sensor_difference/SEN_DIFF_TO_DIST); // |
aalawfi | 2:c857935f928e | 98 | |
aalawfi | 2:c857935f928e | 99 | //calculate the error term for the controller. distance (meters) from |
aalawfi | 2:c857935f928e | 100 | //desired position to sensor |
aalawfi | 2:c857935f928e | 101 | err = REF - feedback; // |
aalawfi | 22:08d30ea47111 | 102 | |
aalawfi | 22:08d30ea47111 | 103 | //Area of the error: TimeStep*err (width*length of a rectangle) |
aalawfi | 22:08d30ea47111 | 104 | errorArea= TI_STEERING*err + errorAreaPrev; |
aalawfi | 23:4c743746533c | 105 | |
aalawfi | 22:08d30ea47111 | 106 | |
aalawfi | 2:c857935f928e | 107 | //Calculate the derivative of the error term for the controller. |
aalawfi | 2:c857935f928e | 108 | // e'(t) = (error(t) - error(t-1))/dt |
aalawfi | 22:08d30ea47111 | 109 | float errChange = (err - err_prev)/(TI_STEERING); |
aalawfi | 2:c857935f928e | 110 | |
aalawfi | 2:c857935f928e | 111 | //Calculate the controller output |
aalawfi | 2:c857935f928e | 112 | //Angle in radians |
aalawfi | 23:4c743746533c | 113 | float servo_angle = (KP*err + KD*errChange*TIME_DERIVATIVE + KI_STEERING*errorArea*TIME_INTEGRAL); |
aalawfi | 2:c857935f928e | 114 | //Calculate the duty cycle for the servo motor |
aalawfi | 2:c857935f928e | 115 | current_duty_cycle = rad2dc(servo_angle); |
aalawfi | 22:08d30ea47111 | 116 | servo_motor_pwm.write(current_duty_cycle); |
aalawfi | 22:08d30ea47111 | 117 | |
aalawfi | 2:c857935f928e | 118 | } |
aalawfi | 9:5320c2dfb913 | 119 | else { |
aalawfi | 2:c857935f928e | 120 | current_duty_cycle = rad2dc(M_PI/4); |
aalawfi | 2:c857935f928e | 121 | servo_motor_pwm.write(current_duty_cycle); |
aalawfi | 9:5320c2dfb913 | 122 | }; |
aalawfi | 2:c857935f928e | 123 | //Save the error for the next calculation. |
aalawfi | 2:c857935f928e | 124 | prev_duty = current_duty_cycle; |
aalawfi | 2:c857935f928e | 125 | err_prev = err; |
aalawfi | 22:08d30ea47111 | 126 | errorAreaPrev = errorArea; |
aalawfi | 2:c857935f928e | 127 | } |
aalawfi | 2:c857935f928e | 128 | |
aalawfi | 2:c857935f928e | 129 | |
aalawfi | 2:c857935f928e | 130 |