first commit

Dependencies:   mbed MMA8451Q

Committer:
aalawfi
Date:
Mon Nov 22 22:27:14 2021 +0000
Revision:
34:cb9a0cec2feb
Parent:
31:d570f957e083
- 3 Laps, fastest is ~~12.3s

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 */
quarren42 19:65fecaa2a387 5 #define MMA8451_I2C_ADDRESS (0x1d<<1)
quarren42 19:65fecaa2a387 6
aalawfi 4:8b1d52dab862 7 InterruptIn stop_button(PTD0);
aalawfi 4:8b1d52dab862 8 InterruptIn run_button(PTD5);
aalawfi 4:8b1d52dab862 9 InterruptIn wait_button(PTA13);
aalawfi 4:8b1d52dab862 10 DigitalOut red_led(PTC16);
aalawfi 4:8b1d52dab862 11 DigitalOut green_led(PTC13);
aalawfi 4:8b1d52dab862 12 DigitalOut yellow_led(PTC12);
quarren42 19:65fecaa2a387 13
quarren42 19:65fecaa2a387 14 PinName const SDA = PTE25;
quarren42 19:65fecaa2a387 15 PinName const SCL = PTE24;
quarren42 19:65fecaa2a387 16
quarren42 19:65fecaa2a387 17 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
aalawfi 2:c857935f928e 18
aalawfi 2:c857935f928e 19 // Available states
aalawfi 2:c857935f928e 20 enum states {STOP, WAIT, RUN};
aalawfi 2:c857935f928e 21
aalawfi 2:c857935f928e 22 // Available LED colors
aalawfi 2:c857935f928e 23 enum led_colors {RED, YELLOW, GREEN};
aalawfi 2:c857935f928e 24
aalawfi 2:c857935f928e 25 void turn_leds_off(void){ yellow_led =0; green_led =0; red_led=0;};
quarren42 19:65fecaa2a387 26 enum fault_list {CLEAR,OFF_TRACK, COLLISION, LOW_VOLTAGE};
quarren42 19:65fecaa2a387 27 fault_list fault_type=CLEAR;
quarren42 19:65fecaa2a387 28 volatile bool _fault = false;
aalawfi 2:c857935f928e 29 // turns an led
aalawfi 2:c857935f928e 30 void turn_led(led_colors Color){
aalawfi 2:c857935f928e 31 turn_leds_off();
aalawfi 2:c857935f928e 32 switch(Color) {
aalawfi 2:c857935f928e 33 case RED : {red_led = 1; green_led=0; yellow_led=0;}; break;
aalawfi 2:c857935f928e 34 case YELLOW : {yellow_led = 1; green_led =0; red_led=0;}; break;
aalawfi 2:c857935f928e 35 case GREEN : {green_led = 1; yellow_led =0; red_led=0;}; break;
aalawfi 2:c857935f928e 36 }
aalawfi 2:c857935f928e 37 };
aalawfi 2:c857935f928e 38
aalawfi 2:c857935f928e 39 void _stop(void){
aalawfi 2:c857935f928e 40 steering_enabled =false;
aalawfi 16:8cd4dd323941 41 enable_brakes();
aalawfi 5:636c3fe18aa8 42 motor_enabled = false;
aalawfi 2:c857935f928e 43 turn_led(RED);
aalawfi 2:c857935f928e 44
aalawfi 2:c857935f928e 45 };
aalawfi 2:c857935f928e 46 void _run(void){
aalawfi 12:f6139597354e 47 // TODO: realease brakes, start the motors
aalawfi 16:8cd4dd323941 48 //
quarren42 17:d2c98ebda90b 49 disable_brakes();
aalawfi 5:636c3fe18aa8 50 motor_enabled = true;
aalawfi 2:c857935f928e 51 turn_led(GREEN);
aalawfi 2:c857935f928e 52 };
aalawfi 2:c857935f928e 53 void _wait(void){
aalawfi 2:c857935f928e 54 // release brakes, turn on steering system, do not start the motor
aalawfi 12:f6139597354e 55 // disable_brakes();
aalawfi 16:8cd4dd323941 56 disable_brakes();
aalawfi 5:636c3fe18aa8 57 motor_enabled = false;
aalawfi 9:5320c2dfb913 58 setpointLeft = 0.0;
aalawfi 9:5320c2dfb913 59 setpointRight = 0.0;
aalawfi 2:c857935f928e 60 steering_enabled = true;
aalawfi 2:c857935f928e 61 turn_led(YELLOW);
aalawfi 2:c857935f928e 62 };
aalawfi 2:c857935f928e 63
aalawfi 2:c857935f928e 64 states current_state = WAIT; // By default the car waits
aalawfi 2:c857935f928e 65 states prev_state = WAIT;
quarren42 19:65fecaa2a387 66
aalawfi 2:c857935f928e 67 // This function gets called when a button is pushed
aalawfi 2:c857935f928e 68 void state_update (void){
aalawfi 2:c857935f928e 69 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 70 switch(current_state) {
aalawfi 2:c857935f928e 71 case STOP : _stop(); break;
aalawfi 2:c857935f928e 72 case WAIT : _wait(); break;
aalawfi 2:c857935f928e 73 case RUN : _run(); break;
aalawfi 2:c857935f928e 74 }
aalawfi 2:c857935f928e 75 prev_state = current_state;
aalawfi 2:c857935f928e 76 }
quarren42 19:65fecaa2a387 77
aalawfi 2:c857935f928e 78 void fault_check(void){
quarren42 20:7dcdadbd7bc0 79 _fault = false;
quarren42 19:65fecaa2a387 80 fault_type = CLEAR;
aalawfi 7:05ea1c004b49 81 batteryVoltage = battInput * 3.3 * battDividerScalar;
aalawfi 7:05ea1c004b49 82 avgCellVoltage = batteryVoltage / 3.0;
quarren42 20:7dcdadbd7bc0 83
aalawfi 31:d570f957e083 84 if (left_distance_sensor.read() < (0.400/3.3) && right_distance_sensor.read() < (0.400/3.3))
aalawfi 2:c857935f928e 85 {
aalawfi 2:c857935f928e 86 _fault = true;
aalawfi 2:c857935f928e 87 fault_type = OFF_TRACK;
aalawfi 2:c857935f928e 88 }
quarren42 20:7dcdadbd7bc0 89
quarren42 20:7dcdadbd7bc0 90
aalawfi 26:54ce9f642477 91 if
aalawfi 26:54ce9f642477 92
aalawfi 26:54ce9f642477 93 (avgCellVoltage <= 3.4){
quarren42 6:d2bd68ba99c9 94 _fault = true;
quarren42 6:d2bd68ba99c9 95 fault_type = LOW_VOLTAGE;
aalawfi 23:4c743746533c 96 }
aalawfi 23:4c743746533c 97 if (abs(acc.getAccX() > 1.48)){ // was 1.5
aalawfi 23:4c743746533c 98 _fault = true;
aalawfi 23:4c743746533c 99 fault_type = COLLISION;
aalawfi 23:4c743746533c 100 }
quarren42 20:7dcdadbd7bc0 101
aalawfi 2:c857935f928e 102
aalawfi 2:c857935f928e 103 state_update();
aalawfi 2:c857935f928e 104 };
aalawfi 2:c857935f928e 105