
- This code combines steering and driving in one ticker - Fault check is in a ticker instead of while loop
steering_header.h@5:636c3fe18aa8, 2021-10-25 (annotated)
- Committer:
- aalawfi
- Date:
- Mon Oct 25 02:14:11 2021 +0000
- Revision:
- 5:636c3fe18aa8
- Parent:
- 4:8b1d52dab862
- Attempted to extend state control to driving: I wrapped the PI controller in an if-else statement, if the motor is enabled, allow PI controll, if the motor is disabled, write duty cycle of zero.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aalawfi | 2:c857935f928e | 1 | /* |
aalawfi | 2:c857935f928e | 2 | Include libraries, IO pins |
aalawfi | 2:c857935f928e | 3 | */ |
aalawfi | 2:c857935f928e | 4 | #include "cmath" |
aalawfi | 2:c857935f928e | 5 | #include <algorithm> // std::min, std::max |
aalawfi | 2:c857935f928e | 6 | #define M_PI 3.14159265359f |
aalawfi | 2:c857935f928e | 7 | #define MAX_TURN_ANGLE M_PI/4 |
aalawfi | 2:c857935f928e | 8 | #define MIN_TURN_ANGLE -M_PI/4 |
aalawfi | 2:c857935f928e | 9 | #define SERVO_MOTOR_FREQ 50.0f |
aalawfi | 2:c857935f928e | 10 | #define CENTER_DUTY_CYCLE 0.075f |
aalawfi | 2:c857935f928e | 11 | #define REF 0.0f //in meters |
aalawfi | 2:c857935f928e | 12 | #define KP 16.45 // 16.65 optimal |
aalawfi | 2:c857935f928e | 13 | #define KD 0.98 // 0.88 optimal |
aalawfi | 2:c857935f928e | 14 | #define TIME_DERIVATIVE 0.9 // good 0.8 |
aalawfi | 2:c857935f928e | 15 | //#define TI 0.02 // in seconds |
aalawfi | 2:c857935f928e | 16 | #define SEN_DIFF_TO_DIST 0.97 //in [V/m] |
aalawfi | 2:c857935f928e | 17 | #define BITS 4 |
aalawfi | 2:c857935f928e | 18 | //Construct the Ticker to call the steering control system at a set rate |
aalawfi | 2:c857935f928e | 19 | // INPUT PINS |
aalawfi | 2:c857935f928e | 20 | AnalogIn left_distance_sensor(PTB1); |
aalawfi | 2:c857935f928e | 21 | AnalogIn right_distance_sensor(PTB0); |
aalawfi | 2:c857935f928e | 22 | |
aalawfi | 2:c857935f928e | 23 | // PWM out |
aalawfi | 2:c857935f928e | 24 | PwmOut servo_motor_pwm(PTD4); |
aalawfi | 2:c857935f928e | 25 | |
aalawfi | 2:c857935f928e | 26 | // Digital outs |
aalawfi | 4:8b1d52dab862 | 27 | DigitalOut seg1[BITS]={PTC17, PTA16,PTA17,PTE31}; // insert pin numbers |
aalawfi | 2:c857935f928e | 28 | |
aalawfi | 2:c857935f928e | 29 | |
aalawfi | 2:c857935f928e | 30 | //Interrupt In |
aalawfi | 4:8b1d52dab862 | 31 | InterruptIn left_landmark_sensor(PTD6); |
aalawfi | 4:8b1d52dab862 | 32 | InterruptIn right_landmark_sensor(PTD7); |
aalawfi | 2:c857935f928e | 33 | |
aalawfi | 2:c857935f928e | 34 | // Tickers |
aalawfi | 5:636c3fe18aa8 | 35 | volatile bool steering_enabled = true; |
aalawfi | 2:c857935f928e | 36 | Ticker steering_control_ticker; |
aalawfi | 2:c857935f928e | 37 | |
aalawfi | 2:c857935f928e | 38 |