
- This code combines steering and driving in one ticker - Fault check is in a ticker instead of while loop
Diff: state_control.h
- Revision:
- 2:c857935f928e
- Child:
- 4:8b1d52dab862
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/state_control.h Mon Oct 25 01:28:53 2021 +0000 @@ -0,0 +1,100 @@ +/* + This file controls the three states of the car + +*/ +InterruptIn stop_button(PTD0); +InterruptIn wait_button(PTD5); +InterruptIn run_button(PTA13); +DigitalOut red_led(PTA16); +DigitalOut green_led(PTA17); +DigitalOut yellow_led(PTE31); + +// 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: + // left_brakes_enabled = true; + // right_brakes_enabled = true; + // motor_enabled = false; + turn_led(RED); + + }; +void _run(void){ + // TODO: realease brakes, start the motors + // 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; + // 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 { + + _fault = false; + fault_type = CLEAR; + }; + + /* + 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(); + }; +