
first commit
state_control.h
- Committer:
- quarren42
- Date:
- 2021-10-25
- Revision:
- 6:d2bd68ba99c9
- Parent:
- 5:636c3fe18aa8
- Child:
- 7:05ea1c004b49
File content as of revision 6:d2bd68ba99c9:
/* This file controls the three states of the car */ InterruptIn stop_button(PTD0); InterruptIn run_button(PTD5); InterruptIn wait_button(PTA13); DigitalOut red_led(PTC16); DigitalOut green_led(PTC13); DigitalOut yellow_led(PTC12); // Available states enum states {STOP, WAIT, RUN}; // Available LED colors enum led_colors {RED, YELLOW, GREEN}; void turn_leds_off(void){ yellow_led =0; green_led =0; red_led=0;}; // turns an led void turn_led(led_colors Color){ turn_leds_off(); switch(Color) { case RED : {red_led = 1; green_led=0; yellow_led=0;}; break; case YELLOW : {yellow_led = 1; green_led =0; red_led=0;}; break; case GREEN : {green_led = 1; yellow_led =0; red_led=0;}; break; } }; void _stop(void){ steering_enabled =false; // TODO: Activate brakes. Example: DONE // left_brakes_enabled = true; // right_brakes_enabled = true; brakeLeftBool = true; brakeRightBool = true; motor_enabled = false; turn_led(RED); }; void _run(void){ // TODO: realease brakes, start the motors brakeLeftBool = false; brakeLeftBool = false; motor_enabled = true; turn_led(GREEN); }; void _wait(void){ // release brakes, turn on steering system, do not start the motor // left_brakes_enabled = false; // right_brakes_enabled = false; brakeLeftBool = false; brakeLeftBool = false; motor_enabled = false; steering_enabled = true; turn_led(YELLOW); }; states current_state = WAIT; // By default the car waits states prev_state = WAIT; volatile bool _fault = false; // This function gets called when a button is pushed void state_update (void){ current_state = (stop_button.read()==1 || _fault == true) ? STOP: ((run_button.read() ==1 && prev_state == WAIT) ? RUN : (wait_button.read() ==1 )? WAIT: prev_state); switch(current_state) { case STOP : _stop(); break; case WAIT : _wait(); break; case RUN : _run(); break; } prev_state = current_state; } enum fault_list {CLEAR,OFF_TRACK, BUMPER, LOW_VOLTAGE}; fault_list fault_type=CLEAR; void fault_check(void){ if (left_distance_sensor.read()*3.3 < 0.700 && right_distance_sensor.read()*3.3 < 0.700) { _fault = true; fault_type = OFF_TRACK; } else { //we should only set _fault to false when the user manually clears a fault w a button _fault = false; fault_type = CLEAR; } batteryVoltage = battInput * 3.3 * battDividerScalar; avgCellVoltage = batteryVoltage / 3.0; if (avgCellVoltage <= 3.4){ _fault = true; fault_type = LOW_VOLTAGE; } /* else if (hit something){ _fault = true; fault_type = BUMPER; } else if(low battery) _fault = true; fault_type = LOW_VOLTAGE; else { _faule =false; fault_type = CLEAR; }; */ //_fault=true; state_update(); };