Urbanite: Parking aid system
fsm_urbanite.c File Reference

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_tfsm_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...
 

Detailed Description

Urbanite FSM main file.

Author
Sistemas Digitales II
Date
2025-01-01

Function Documentation

◆ check_activity()

static bool check_activity ( fsm_t *  p_this)
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, return false

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.
Returns
true
false

◆ check_activity_in_measure()

static bool check_activity_in_measure ( fsm_t *  p_this)
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.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.
Returns
true
false

◆ check_new_measure()

static bool check_new_measure ( fsm_t *  p_this)
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.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.
Returns
true
false

◆ check_no_activity()

static bool check_no_activity ( fsm_t *  p_this)
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 be true if all the elements of the system are inactive, otherwise it will be false.

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_urbanite_t.
Returns
true
false

◆ check_off()

static bool check_off ( fsm_t *  p_this)
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.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.
Returns
true
false

◆ check_on()

static bool check_on ( fsm_t *  p_this)
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. Return true if the duration is greater than 0 and greater than the required time to turn ON the system. Otherwise, return false

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.
Returns
true
false

◆ check_pause_display()

static bool check_pause_display ( fsm_t *  p_this)
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. Return true 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, return false

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.
Returns
true
false

◆ do_display_distance()

static void do_display_distance ( fsm_t *  p_this)
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 than WARNING_MIN_CM / 2 cm, set the distance to the display and set the display status to true. Otherwise, set the display status to false.
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:

printf("[URBANITE][%ld] Distance: %ld cm\n", port_system_get_millis(), distance_cm);

Where port_system_get_millis() returns the current system tick.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_pause_display()

static void do_pause_display ( fsm_t *  p_this)
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 to false. If the system is not paused, set the pause status to true.
✅ 3. Activate or deactivate the display depending on the new pause status by calling fsm_display_set_status() with the right parameter.
✅ 4. Print status depending on the pause status. You could print messages like:

printf("[URBANITE][%ld] Urbanite system display PAUSE\n", port_system_get_millis());
printf("[URBANITE][%ld] Urbanite system display RESUME\n", port_system_get_millis());

Where port_system_get_millis() returns the current system tick.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_sleep_off()

static void do_sleep_off ( fsm_t *  p_this)
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

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_sleep_while_measure()

static void do_sleep_while_measure ( fsm_t *  p_this)
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

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_sleep_while_off()

static void do_sleep_while_off ( fsm_t *  p_this)
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

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_sleep_while_on()

static void do_sleep_while_on ( fsm_t *  p_this)
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

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_start_up_measure()

static void do_start_up_measure ( fsm_t *  p_this)
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:

printf("[URBANITE][%ld] Urbanite system ON\n", port_system_get_millis());

Where port_system_get_millis() returns the current system tick.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ do_stop_urbanite()

static void do_stop_urbanite ( fsm_t *  p_this)
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 calling fsm_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 calling fsm_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:

printf("[URBANITE][%ld] Urbanite system OFF\n", port_system_get_millis());

Where port_system_get_millis() returns the current system tick.

Parameters
p_thisPointer to an fsm_t struct that contains an fsm_urbanite_t.

◆ fsm_urbanite_destroy()

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() and fsm_display_destroy() functions.

Parameters
p_fsmPointer to an fsm_urbanite_t struct.

◆ fsm_urbanite_fire()

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 to fsm_t

Parameters
p_fsmPointer to the fsm_urbanite_t struct.

◆ fsm_urbanite_init()

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 
)
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:

Watch the 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 to fsm_t and the transition table.
✅ 2. Initialize the fields p_fsm_button, on_off_press_time_ms p_fsm_ultrasound_rear, pause_display_time_ms, p_fsm_display_rear and p_fsm_display_rear of the Urbanite FSM with the received parameters.
✅ 3. Initialize the field is_paused to false.

Parameters
p_fsm_urbanitePointer to the Urbanite FSM.
p_fsm_buttonPointer to the button FSM that activates the system and disables the display if it disturbs the driver.
on_off_press_time_msButton press time in milliseconds to turn the system ON or OFF
pause_display_time_msTime in milliseconds to pause the display after a short press of the button
p_fsm_ultrasound_rearPointer to the ultrasound FSM that measures the distance to the rear obstacle.
p_fsm_display_rearPointer to the display FSM that shows the distance to the rear obstacle.
Returns
fsm_urbanite_t* Pointer to the Urbanite FSM.

◆ fsm_urbanite_new()

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 the fsm_button_new(), fsm_ultrasound_new() and fsm_display_new() functions.
✅ 2. Initialize the fsm_t struct of the Urbanite FSM by calling the fsm_urbanite_init() function.
✅ 3. Return the pointer to the Urbanite FSM once it has been created and initialized.

Parameters
p_fsm_buttonPointer to the button FSM to interact with the Urbanite.
on_off_press_time_msTime in ms to consider ON/OFF of the Urbanite parking aid system.
pause_display_time_msTime in ms to pause the display system.
p_fsm_ultrasound_rearPointer to the rear ultrasound FSM.
p_fsm_display_rearPointer to the rear display FSM.
Returns
fsm_urbanite_t* Pointer to the Urbanite FSM.

Variable Documentation

◆ fsm_trans_urbanite

fsm_trans_t fsm_trans_urbanite[]

Array representing the transitions table of the FSM Urbanite.

port_system_get_millis
uint32_t port_system_get_millis(void)
Returns the number of milliseconds since the system started.
Definition: stm32f4_system.c:178