first commit

Dependencies:   mbed MMA8451Q

Committer:
aalawfi
Date:
Tue Oct 26 14:02:20 2021 +0000
Revision:
12:f6139597354e
Parent:
10:b0999f69c775
Child:
14:eb9c58c0f8cd
- Added enable and disable brakes to state_control (commented, did not implement yet)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aalawfi 2:c857935f928e 1 /*
aalawfi 2:c857935f928e 2 This file controls the three states of the car
aalawfi 2:c857935f928e 3
aalawfi 2:c857935f928e 4 */
aalawfi 4:8b1d52dab862 5 InterruptIn stop_button(PTD0);
aalawfi 4:8b1d52dab862 6 InterruptIn run_button(PTD5);
aalawfi 4:8b1d52dab862 7 InterruptIn wait_button(PTA13);
aalawfi 4:8b1d52dab862 8 DigitalOut red_led(PTC16);
aalawfi 4:8b1d52dab862 9 DigitalOut green_led(PTC13);
aalawfi 4:8b1d52dab862 10 DigitalOut yellow_led(PTC12);
aalawfi 2:c857935f928e 11
aalawfi 2:c857935f928e 12 // Available states
aalawfi 2:c857935f928e 13 enum states {STOP, WAIT, RUN};
aalawfi 2:c857935f928e 14
aalawfi 2:c857935f928e 15 // Available LED colors
aalawfi 2:c857935f928e 16 enum led_colors {RED, YELLOW, GREEN};
aalawfi 2:c857935f928e 17
aalawfi 2:c857935f928e 18 void turn_leds_off(void){ yellow_led =0; green_led =0; red_led=0;};
aalawfi 2:c857935f928e 19
aalawfi 2:c857935f928e 20 // turns an led
aalawfi 2:c857935f928e 21 void turn_led(led_colors Color){
aalawfi 2:c857935f928e 22 turn_leds_off();
aalawfi 2:c857935f928e 23 switch(Color) {
aalawfi 2:c857935f928e 24 case RED : {red_led = 1; green_led=0; yellow_led=0;}; break;
aalawfi 2:c857935f928e 25 case YELLOW : {yellow_led = 1; green_led =0; red_led=0;}; break;
aalawfi 2:c857935f928e 26 case GREEN : {green_led = 1; yellow_led =0; red_led=0;}; break;
aalawfi 2:c857935f928e 27 }
aalawfi 2:c857935f928e 28 };
aalawfi 2:c857935f928e 29
aalawfi 2:c857935f928e 30 void _stop(void){
aalawfi 2:c857935f928e 31 steering_enabled =false;
aalawfi 12:f6139597354e 32 // enable_brakes();
quarren42 6:d2bd68ba99c9 33 brakeLeftBool = true;
quarren42 6:d2bd68ba99c9 34 brakeRightBool = true;
aalawfi 5:636c3fe18aa8 35 motor_enabled = false;
aalawfi 2:c857935f928e 36 turn_led(RED);
aalawfi 2:c857935f928e 37
aalawfi 2:c857935f928e 38 };
aalawfi 2:c857935f928e 39 void _run(void){
aalawfi 12:f6139597354e 40 // TODO: realease brakes, start the motors
aalawfi 12:f6139597354e 41 // disable_brakes();
quarren42 6:d2bd68ba99c9 42 brakeLeftBool = false;
quarren42 6:d2bd68ba99c9 43 brakeLeftBool = false;
aalawfi 5:636c3fe18aa8 44 motor_enabled = true;
aalawfi 9:5320c2dfb913 45 setpointLeft = 0.4;
aalawfi 9:5320c2dfb913 46 setpointRight = 0.4;
aalawfi 2:c857935f928e 47 turn_led(GREEN);
aalawfi 2:c857935f928e 48 };
aalawfi 2:c857935f928e 49 void _wait(void){
aalawfi 2:c857935f928e 50 // release brakes, turn on steering system, do not start the motor
aalawfi 12:f6139597354e 51
aalawfi 12:f6139597354e 52 // disable_brakes();
quarren42 6:d2bd68ba99c9 53 brakeLeftBool = false;
quarren42 6:d2bd68ba99c9 54 brakeLeftBool = false;
aalawfi 5:636c3fe18aa8 55 motor_enabled = false;
aalawfi 9:5320c2dfb913 56 setpointLeft = 0.0;
aalawfi 9:5320c2dfb913 57 setpointRight = 0.0;
aalawfi 2:c857935f928e 58 steering_enabled = true;
aalawfi 2:c857935f928e 59 turn_led(YELLOW);
aalawfi 2:c857935f928e 60 };
aalawfi 2:c857935f928e 61
aalawfi 2:c857935f928e 62 states current_state = WAIT; // By default the car waits
aalawfi 2:c857935f928e 63 states prev_state = WAIT;
aalawfi 2:c857935f928e 64 volatile bool _fault = false;
aalawfi 2:c857935f928e 65 // This function gets called when a button is pushed
aalawfi 2:c857935f928e 66 void state_update (void){
aalawfi 2:c857935f928e 67 current_state = (stop_button.read()==1 || _fault == true) ? STOP: ((run_button.read() ==1 && prev_state == WAIT) ? RUN : (wait_button.read() ==1 )? WAIT: prev_state);
aalawfi 2:c857935f928e 68 switch(current_state) {
aalawfi 2:c857935f928e 69 case STOP : _stop(); break;
aalawfi 2:c857935f928e 70 case WAIT : _wait(); break;
aalawfi 2:c857935f928e 71 case RUN : _run(); break;
aalawfi 2:c857935f928e 72 }
aalawfi 2:c857935f928e 73 prev_state = current_state;
aalawfi 2:c857935f928e 74 }
aalawfi 2:c857935f928e 75 enum fault_list {CLEAR,OFF_TRACK, BUMPER, LOW_VOLTAGE};
aalawfi 2:c857935f928e 76 fault_list fault_type=CLEAR;
aalawfi 2:c857935f928e 77 void fault_check(void){
aalawfi 7:05ea1c004b49 78 batteryVoltage = battInput * 3.3 * battDividerScalar;
aalawfi 7:05ea1c004b49 79 avgCellVoltage = batteryVoltage / 3.0;
aalawfi 9:5320c2dfb913 80 if (left_distance_sensor.read() < (0.700/3.3) && right_distance_sensor.read()*3.3 < (0.700/3.3))
aalawfi 2:c857935f928e 81 {
aalawfi 2:c857935f928e 82 _fault = true;
aalawfi 2:c857935f928e 83 fault_type = OFF_TRACK;
aalawfi 2:c857935f928e 84 }
aalawfi 9:5320c2dfb913 85 /* else if (avgCellVoltage <= 3.4){
quarren42 6:d2bd68ba99c9 86 _fault = true;
quarren42 6:d2bd68ba99c9 87 fault_type = LOW_VOLTAGE;
aalawfi 9:5320c2dfb913 88 }*/
aalawfi 7:05ea1c004b49 89 else { //we should only set _fault to false when the user manually clears a fault w a button
aalawfi 7:05ea1c004b49 90 _fault = false;
aalawfi 7:05ea1c004b49 91 fault_type = CLEAR;
aalawfi 7:05ea1c004b49 92 };
aalawfi 7:05ea1c004b49 93
quarren42 6:d2bd68ba99c9 94
aalawfi 2:c857935f928e 95 /*
aalawfi 2:c857935f928e 96 else if (hit something){
aalawfi 2:c857935f928e 97
aalawfi 2:c857935f928e 98 _fault = true;
aalawfi 2:c857935f928e 99 fault_type = BUMPER;
aalawfi 2:c857935f928e 100 }
aalawfi 2:c857935f928e 101
aalawfi 2:c857935f928e 102 else if(low battery)
aalawfi 2:c857935f928e 103 _fault = true;
aalawfi 2:c857935f928e 104 fault_type = LOW_VOLTAGE;
aalawfi 2:c857935f928e 105 else {
aalawfi 2:c857935f928e 106 _faule =false;
aalawfi 2:c857935f928e 107 fault_type = CLEAR;
aalawfi 2:c857935f928e 108 };
aalawfi 2:c857935f928e 109 */
aalawfi 2:c857935f928e 110 //_fault=true;
aalawfi 2:c857935f928e 111
aalawfi 2:c857935f928e 112 state_update();
aalawfi 2:c857935f928e 113 };
aalawfi 2:c857935f928e 114