
first commit
state_control.h
- Committer:
- aalawfi
- Date:
- 2021-10-26
- Revision:
- 12:f6139597354e
- Parent:
- 10:b0999f69c775
- Child:
- 14:eb9c58c0f8cd
File content as of revision 12:f6139597354e:
/* 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; // enable_brakes(); brakeLeftBool = true; brakeRightBool = true; motor_enabled = false; turn_led(RED); }; void _run(void){ // TODO: realease brakes, start the motors // disable_brakes(); brakeLeftBool = false; brakeLeftBool = false; motor_enabled = true; setpointLeft = 0.4; setpointRight = 0.4; turn_led(GREEN); }; void _wait(void){ // release brakes, turn on steering system, do not start the motor // disable_brakes(); brakeLeftBool = false; brakeLeftBool = false; motor_enabled = false; setpointLeft = 0.0; setpointRight = 0.0; 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){ batteryVoltage = battInput * 3.3 * battDividerScalar; avgCellVoltage = batteryVoltage / 3.0; if (left_distance_sensor.read() < (0.700/3.3) && right_distance_sensor.read()*3.3 < (0.700/3.3)) { _fault = true; fault_type = OFF_TRACK; } /* else if (avgCellVoltage <= 3.4){ _fault = true; fault_type = LOW_VOLTAGE; }*/ else { //we should only set _fault to false when the user manually clears a fault w a button _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(); };