Urbanite: Parking aid system
|
Urbanite FSM main file. More...
#include <stdlib.h>
#include <stdio.h>
#include "port_system.h"
#include "fsm.h"
#include "fsm_urbanite.h"
Data Structures | |
struct | fsm_urbanite_t |
Structure to define the Urbanite FSM. More... | |
Functions | |
static bool | check_on (fsm_t *p_this) |
Check if the button has been pressed for the required time to turn ON the Urbanite system. More... | |
static bool | check_off (fsm_t *p_this) |
Check if the button has been pressed for the required time to turn OFF the system. More... | |
static bool | check_new_measure (fsm_t *p_this) |
Check if a new measurement is ready. More... | |
static bool | check_pause_display (fsm_t *p_this) |
Check if it has been required to pause the display. More... | |
static bool | check_activity (fsm_t *p_this) |
Check if any of the elements of the system is active. More... | |
static bool | check_no_activity (fsm_t *p_this) |
Check if all the elements of the system are inactive. More... | |
static bool | check_activity_in_measure (fsm_t *p_this) |
Check if any a new measurement is ready while the system is in low power mode. More... | |
static void | do_start_up_measure (fsm_t *p_this) |
Turn the Urbanite system ON. More... | |
static void | do_display_distance (fsm_t *p_this) |
Display the distance measured by the ultrasound sensor. More... | |
static void | do_pause_display (fsm_t *p_this) |
Pause or resume the display system. More... | |
static void | do_stop_urbanite (fsm_t *p_this) |
Turn the Urbanite system OFF. More... | |
static void | do_sleep_while_off (fsm_t *p_this) |
Start the low power mode while the Urbanite is awakened by a debug breakpoint or similar in the SLEEP_WHILE_OFF state. More... | |
static void | do_sleep_while_on (fsm_t *p_this) |
Start the low power mode while the Urbanite is awakened by a debug breakpoint or similar in the SLEEP_WHILE_ON state. More... | |
static void | do_sleep_while_measure (fsm_t *p_this) |
Start the low power mode while the Urbanite is measuring the distance and it is waiting for a new measurement. More... | |
static void | do_sleep_off (fsm_t *p_this) |
Start the low power mode while the Urbanite is OFF. More... | |
static void | fsm_urbanite_init (fsm_urbanite_t *p_fsm_urbanite, fsm_button_t *p_fsm_button, uint32_t on_off_press_time_ms, uint32_t pause_display_time_ms, fsm_ultrasound_t *p_fsm_ultrasound_rear, fsm_display_t *p_fsm_display_rear) |
Create a new Urbanite FSM. More... | |
fsm_urbanite_t * | fsm_urbanite_new (fsm_button_t *p_fsm_button, uint32_t on_off_press_time_ms, uint32_t pause_display_time_ms, fsm_ultrasound_t *p_fsm_ultrasound_rear, fsm_display_t *p_fsm_display_rear) |
Create a new Urbanite FSM. More... | |
void | fsm_urbanite_fire (fsm_urbanite_t *p_fsm_urbanite) |
Fire the Urbanite FSM. More... | |
void | fsm_urbanite_destroy (fsm_urbanite_t *p_fsm) |
Destroy an Urbanite FSM. More... | |
Variables | |
fsm_trans_t | fsm_trans_urbanite [] |
Array representing the transitions table of the FSM Urbanite. More... | |
Urbanite FSM main file.
|
static |
Check if any of the elements of the system is active.
TODO alumnos:
✅ 1. Return
true
if any of the elements (button, ultrasound, or display) is active. Otherwise, returnfalse
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Check if any a new measurement is ready while the system is in low power mode.
TODO alumnos:
✅ 1. Call function
check_new_measure()
to check if a new measurement is ready and return the result.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Check if a new measurement is ready.
TODO alumnos:
✅ 1. Call function
fsm_ultrasound_get_new_measurement_ready()
to check if a new measurement is ready and return the result.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Check if all the elements of the system are inactive.
TODO alumnos:
✅ 1. Call function
check_activity()
and return the inverse of the result. The result will betrue
if all the elements of the system are inactive, otherwise it will befalse
.
p_this | Pointer to an fsm_t struct than contains an fsm_urbanite_t . |
|
static |
Check if the button has been pressed for the required time to turn OFF the system.
TODO alumnos:
✅ 1. It is enough to call the function
check_on()
because the required time to turn ON and OFF the system is the same.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Check if the button has been pressed for the required time to turn ON the Urbanite system.
TODO alumnos:
✅ 1. Call function
fsm_button_get_duration()
to get the duration of the button press
✅ 2. Returntrue
if the duration is greater than 0 and greater than the required time to turn ON the system. Otherwise, returnfalse
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Check if it has been required to pause the display.
TODO alumnos:
✅ 1. Call function
fsm_button_get_duration()
to get the duration of the button press
✅ 2. Returntrue
if the duration is greater than 0, less than the required time to turn ON the system, and greater than the required time to pause the display. Otherwise, returnfalse
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Display the distance measured by the ultrasound sensor.
TODO alumnos:
✅ 1. Get the distance measured by the ultrasound sensor by calling
fsm_ultrasound_get_distance()
with the right ultrasound sensor.
✅ 2. If the system is paused: If the distance is less thanWARNING_MIN_CM / 2
cm, set the distance to the display and set the display status totrue
. Otherwise, set the display status tofalse
.
If the system is not paused, set the distance to the display.
✅ 3. Print a message to help yourself to debug and log the distance measured by the ultrasound sensor. You could do something like:
Where
port_system_get_millis()
returns the current system tick.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Pause or resume the display system.
TODO alumnos:
✅ 1. Reset the duration of the button by calling
fsm_button_reset_duration()
to avoid the system to pause again.
✅ 2. Invert the pause status. If the system is paused, set the pause status tofalse
. If the system is not paused, set the pause status totrue
.
✅ 3. Activate or deactivate the display depending on the new pause status by callingfsm_display_set_status()
with the right parameter.
✅ 4. Print status depending on the pause status. You could print messages like:
Where
port_system_get_millis()
returns the current system tick.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Start the low power mode while the Urbanite is OFF.
TODO alumnos:
✅ 1. Call function
port_system_sleep()
to start the low power mode
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Start the low power mode while the Urbanite is measuring the distance and it is waiting for a new measurement.
TODO alumnos:
✅ 1. Call function
port_system_sleep()
to start the low power mode
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Start the low power mode while the Urbanite is awakened by a debug breakpoint or similar in the SLEEP_WHILE_OFF
state.
TODO alumnos:
✅ 1. Call function
port_system_sleep()
to start the low power mode
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Start the low power mode while the Urbanite is awakened by a debug breakpoint or similar in the SLEEP_WHILE_ON
state.
TODO alumnos:
✅ 1. Call function
port_system_sleep()
to start the low power mode
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Turn the Urbanite system ON.
TODO alumnos:
✅ 1. Reset the duration of the button by calling
fsm_button_reset_duration()
to avoid the system to turn OFF again.
✅ 2. Start the ultrasound sensor by calling its appropriate function with the right parameter. With this the ultrasound sensor will start measuring the distance.
✅ 3. Set the appropriate status of the display system by calling its function with the right parameter. With this the display system will start showing the distance.
💡 Print a message to help yourself to debug and log the status of the system. You could do something like:
Where
port_system_get_millis()
returns the current system tick.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
|
static |
Turn the Urbanite system OFF.
TODO alumnos:
✅ 1. Reset the duration of the button by calling
fsm_button_reset_duration()
to avoid the system to turn ON again.
✅ 2. Stop the ultrasound sensor by callingfsm_ultrasound_stop()
with the right parameters. With this the ultrasound sensor will stop measuring the distance if it was measuring.
✅ 3. Turn the display system off by callingfsm_display_set_status()
with the right parameter. With this the display system will stop showing the distance if it was showing it
✅ 4. If the system is paused, remove the pause status to avoid the system to turn ON again with the display paused.
✅ 5. Print a message to help yourself to debug and log the status of the system. You could do something like:
Where
port_system_get_millis()
returns the current system tick.
p_this | Pointer to an fsm_t struct that contains an fsm_urbanite_t . |
void fsm_urbanite_destroy | ( | fsm_urbanite_t * | p_fsm | ) |
Destroy an Urbanite FSM.
This function destroys an Urbanite FSM and frees the memory.
TODO alumnos:
✅ 1. Implement this function analogously to the
fsm_button_destroy()
,fsm_ultrasound_destroy()
andfsm_display_destroy()
functions.
p_fsm | Pointer to an fsm_urbanite_t struct. |
void fsm_urbanite_fire | ( | fsm_urbanite_t * | p_fsm | ) |
Fire the Urbanite FSM.
This function is used to check the transitions and execute the actions of the Urbanite FSM.
TODO alumnos:
✅ 1. Call function
fsm_fire()
with the received pointer tofsm_t
p_fsm | Pointer to the fsm_urbanite_t struct. |
|
static |
Create a new Urbanite FSM.
This FSM implements a system that measures the distance to the nearest obstacle and shows it on a display. The display is a RGB LED that changes its color depending on the distance to the obstacle. The system is activated by pressing a button. The system is deactivated by pressing the same button for the same time.
The basic implementation of this FSM assumes that the system is mounted on a car and the distance is measured by an ultrasound sensor located at the rear of the car. The display is located on the dashboard of the car. The button is assumed that activates when the driver is parking the car. The system can add more sensors and displays to cover more areas of the car.
A short press of the button pauses the display if it disturbs the driver, but the system continues measuring the distance. In case the driver wants to activate the display again, he must press the button again and the display will show the last distance measured. A long press of the button deactivates the ultrasounds and the displays.
When the system is OFF it does not measure the distance and the display is OFF. The system is in a low power mode.
To better understand the system, please watch the following video:
This is the HW diagram of the system:
This function initializes the default values of the FSM struct and calls to the port
to initialize the HWs associated to the devices.
TODO alumnos:
✅ 1. Call function
fsm_init()
with the received pointer tofsm_t
and the transition table.
✅ 2. Initialize the fieldsp_fsm_button
,on_off_press_time_ms
p_fsm_ultrasound_rear
,pause_display_time_ms
,p_fsm_display_rear
andp_fsm_display_rear
of the Urbanite FSM with the received parameters.
✅ 3. Initialize the fieldis_paused
tofalse
.
p_fsm_urbanite | Pointer to the Urbanite FSM. |
p_fsm_button | Pointer to the button FSM that activates the system and disables the display if it disturbs the driver. |
on_off_press_time_ms | Button press time in milliseconds to turn the system ON or OFF |
pause_display_time_ms | Time in milliseconds to pause the display after a short press of the button |
p_fsm_ultrasound_rear | Pointer to the ultrasound FSM that measures the distance to the rear obstacle. |
p_fsm_display_rear | Pointer to the display FSM that shows the distance to the rear obstacle. |
fsm_urbanite_t* fsm_urbanite_new | ( | fsm_button_t * | p_fsm_button, |
uint32_t | on_off_press_time_ms, | ||
uint32_t | pause_display_time_ms, | ||
fsm_ultrasound_t * | p_fsm_ultrasound_rear, | ||
fsm_display_t * | p_fsm_display_rear | ||
) |
Create a new Urbanite FSM.
This function creates a new Urbanite FSM with the given button, ultrasound, display FSMs and the required times for configuration.
TODO alumnos:
✅ 1. Allocate memory for the
fsm_urbanite_t
struct in the same way as thefsm_button_new()
,fsm_ultrasound_new()
andfsm_display_new()
functions.
✅ 2. Initialize thefsm_t
struct of the Urbanite FSM by calling thefsm_urbanite_init()
function.
✅ 3. Return the pointer to the Urbanite FSM once it has been created and initialized.
p_fsm_button | Pointer to the button FSM to interact with the Urbanite. |
on_off_press_time_ms | Time in ms to consider ON/OFF of the Urbanite parking aid system. |
pause_display_time_ms | Time in ms to pause the display system. |
p_fsm_ultrasound_rear | Pointer to the rear ultrasound FSM. |
p_fsm_display_rear | Pointer to the rear display FSM. |
fsm_trans_t fsm_trans_urbanite[] |
Array representing the transitions table of the FSM Urbanite.