Urbanite: Parking aid system
fsm_ultrasound.h File Reference

Header for fsm_ultrasound.c file. More...

#include <stdint.h>
#include <stdbool.h>
#include "fsm.h"

Macros

#define FSM_ULTRASOUND_NUM_MEASUREMENTS   5
 

Typedefs

typedef struct fsm_ultrasound_t fsm_ultrasound_t
 Structure to define the ultrasound FSM.
 

Enumerations

enum  FSM_ULTRASOUND {
  WAIT_START = 0,
  TRIGGER_START,
  WAIT_ECHO_START,
  WAIT_ECHO_END,
  SET_DISTANCE
}
 Enumerator for the ultrasound finite state machine. More...
 

Functions

fsm_ultrasound_tfsm_ultrasound_new (uint32_t ultrasound_id)
 Create a new ultrasound FSM. More...
 
void fsm_ultrasound_destroy (fsm_ultrasound_t *p_fsm)
 Destroy an ultrasound FSM. More...
 
uint32_t fsm_ultrasound_get_distance (fsm_ultrasound_t *p_fsm)
 Return the distance of the last object detected by the ultrasound sensor.a64l. More...
 
void fsm_ultrasound_fire (fsm_ultrasound_t *p_fsm)
 Fire the ultrasound FSM. More...
 
bool fsm_ultrasound_get_status (fsm_ultrasound_t *p_fsm)
 Get the status of the ultrasound transceiver FSM. More...
 
void fsm_ultrasound_set_status (fsm_ultrasound_t *p_fsm, bool status)
 Set the status of the ultrasound sensor. true means that the ultrasound sensor is active and a distance measurement must be performed. false means that the ultrasound sensor is inactive. More...
 
bool fsm_ultrasound_get_ready (fsm_ultrasound_t *p_fsm)
 Get the ready flag of the trigger signal in the ultrasound HW. More...
 
bool fsm_ultrasound_get_new_measurement_ready (fsm_ultrasound_t *p_fsm)
 Return the flag that indicates if a new measurement is ready. More...
 
void fsm_ultrasound_stop (fsm_ultrasound_t *p_fsm)
 Stop the ultrasound sensor. More...
 
void fsm_ultrasound_start (fsm_ultrasound_t *p_fsm)
 Start the ultrasound sensor. More...
 
fsm_t * fsm_ultrasound_get_inner_fsm (fsm_ultrasound_t *p_fsm)
 Get the inner FSM of the ultrasound. More...
 
uint32_t fsm_ultrasound_get_state (fsm_ultrasound_t *p_fsm)
 Get the state of the ultrasound FSM. More...
 
void fsm_ultrasound_set_state (fsm_ultrasound_t *p_fsm, int8_t state)
 Set the state of the ultrasound FSM. More...
 
bool fsm_ultrasound_check_activity (fsm_ultrasound_t *p_fsm)
 Check if the ultrasound sensor is doing a distance measurement. More...
 

Detailed Description

Header for fsm_ultrasound.c file.

Author
Sistemas Digitales II
Date
2025-01-01

Macro Definition Documentation

◆ FSM_ULTRASOUND_NUM_MEASUREMENTS

#define FSM_ULTRASOUND_NUM_MEASUREMENTS   5

Number of measurements to store in the array

Enumeration Type Documentation

◆ FSM_ULTRASOUND

Enumerator for the ultrasound finite state machine.

This enumerator defines the different states that the ultrasound finite state machine can be in. Each state represents a specific condition or step in the ultrasound distance measurement process.

Enumerator
WAIT_START 

Starting state. Also comes here when the distance measurement has been completed or a timeout has occurred

TRIGGER_START 

State to send the trigger pulse to the ultrasound sensor

WAIT_ECHO_START 

State to wait for the echo signal

WAIT_ECHO_END 

State to wait for the echo signal

SET_DISTANCE 

State to compute the distance from the echo signal

Function Documentation

◆ fsm_ultrasound_check_activity()

bool fsm_ultrasound_check_activity ( fsm_ultrasound_t p_fsm)

Check if the ultrasound sensor is doing a distance measurement.

The ultrasound sensor is always inactive because all the transitions are due to HW interrupts.

TODO alumnos Version 4:

✅ 1. Return false always.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
Returns
true
false

◆ fsm_ultrasound_destroy()

void fsm_ultrasound_destroy ( fsm_ultrasound_t p_fsm)

Destroy an ultrasound FSM.

This function destroys an ultrasound transceiver FSM and frees the memory.

TODO alumnos:

✅ 1. Implement this function analogously to the fsm_button_destroy() function.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.

◆ fsm_ultrasound_fire()

void fsm_ultrasound_fire ( fsm_ultrasound_t p_fsm)

Fire the ultrasound FSM.

This function is used to fire the ultrasound FSM. It is used to check the transitions and execute the actions of the ultrasound FSM.

TODO alumnos:

✅ 1. Call function fsm_fire() with the received pointer to fsm_t

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.

◆ fsm_ultrasound_get_distance()

uint32_t fsm_ultrasound_get_distance ( fsm_ultrasound_t p_fsm)

Return the distance of the last object detected by the ultrasound sensor.a64l.

The function also resets the field new_measurement to indicate that the distance has been read.

TODO alumnos:

✅ 1. Retrieve and return the field distance_cm.
✅ 2. Reset the field new_measurement.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
Returns
uint32_t Distance measured by the ultrasound sensor in centimeters.

◆ fsm_ultrasound_get_inner_fsm()

fsm_t* fsm_ultrasound_get_inner_fsm ( fsm_ultrasound_t p_fsm)

Get the inner FSM of the ultrasound.

This function returns the inner FSM of the ultrasound.

    💡 This function is important because the struct is private and external functions such as those of the unit tests cannot access the inner FSM directly.

TODO alumnos:

✅ 1. Return the address of the f field of the struct.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
Returns
fsm_t* Pointer to the inner FSM.

◆ fsm_ultrasound_get_new_measurement_ready()

bool fsm_ultrasound_get_new_measurement_ready ( fsm_ultrasound_t p_fsm)

Return the flag that indicates if a new measurement is ready.

TODO alumnos:

✅ 1. Retrieve and return the field new_measurement.

Parameters
p_fsmPointer to the ultrasound FSM.
Returns
true
false

◆ fsm_ultrasound_get_ready()

bool fsm_ultrasound_get_ready ( fsm_ultrasound_t p_fsm)

Get the ready flag of the trigger signal in the ultrasound HW.

This function returns the ready flag of trigger signal in the ultrasound HW. This function might be used for testing and debugging purposes.

TODO alumnos:

✅ 1. Call function port_ultrasound_get_trigger_ready() with the ultrasound ID and return the result.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
Returns
true If the port indicates that the trigger signal is ready to start a new measurement.
false If the port indicates that the trigger signal is not ready to start a new measurement.

◆ fsm_ultrasound_get_state()

uint32_t fsm_ultrasound_get_state ( fsm_ultrasound_t p_fsm)

Get the state of the ultrasound FSM.

This function returns the current state of the ultrasound FSM.

    💡 This function is important because the struct is private and external functions such as those of the unit tests cannot access the state of the FSM directly.

TODO alumnos:

✅ 1. Retrieve and return the field current_state of the FSM (field f of the struct).

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
Returns
uint32_t Current state of the ultrasound FSM.

◆ fsm_ultrasound_get_status()

bool fsm_ultrasound_get_status ( fsm_ultrasound_t p_fsm)

Get the status of the ultrasound transceiver FSM.

This function returns the status of the ultrasound. This function might be used for testing and debugging purposes.

TODO alumnos:

✅ 1. Retrieve and return the field status.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
Returns
true If the ultrasound system has been indicated to be active.
false If the ultrasound system has been indicated to be paused.

◆ fsm_ultrasound_new()

fsm_ultrasound_t* fsm_ultrasound_new ( uint32_t  ultrasound_id)

Create a new ultrasound FSM.

This function creates a new ultrasound transceiver FSM with the given ultrasound ID.

Parameters
ultrasound_idUltrasound ID. Must be unique.
Returns
fsm_ultrasound_t* Pointer to the ultrasound FSM.

◆ fsm_ultrasound_set_state()

void fsm_ultrasound_set_state ( fsm_ultrasound_t p_fsm,
int8_t  state 
)

Set the state of the ultrasound FSM.

This function sets the current state of the ultrasound FSM.

    💡 This function is important because the struct is private and external functions such as those of the unit tests cannot access the state of the FSM directly.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
stateNew state of the ultrasound FSM.

◆ fsm_ultrasound_set_status()

void fsm_ultrasound_set_status ( fsm_ultrasound_t p_fsm,
bool  status 
)

Set the status of the ultrasound sensor. true means that the ultrasound sensor is active and a distance measurement must be performed. false means that the ultrasound sensor is inactive.

TODO alumnos:

✅ 1. Update the field status with the received value

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.
statusStatus of the ultrasound sensor.

◆ fsm_ultrasound_start()

void fsm_ultrasound_start ( fsm_ultrasound_t p_fsm)

Start the ultrasound sensor.

This function starts the ultrasound sensor by indicating to the port to start the ultrasound sensor (to reset all timer ticks) and to set the status of the ultrasound sensor to active.

TODO alumnos:

✅ 1. Set the field status.
✅ 2. Reset the field distance_idx.
✅ 3. Reset the field distance_cm.
✅ 4. Call function port_ultrasound_reset_echo_ticks() with the right parameters.
✅ 5. Call function port_ultrasound_set_trigger_ready() with the right parameters to indicate that the ultrasound sensor is ready to start a new measurement.
✅ 6. Call function port_ultrasound_start_new_measurement_timer() to force the new measurement timer to start to provoke the first interrupt.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.

◆ fsm_ultrasound_stop()

void fsm_ultrasound_stop ( fsm_ultrasound_t p_fsm)

Stop the ultrasound sensor.

This function stops the ultrasound sensor by indicating to the port to stop the ultrasound sensor (to reset all timer ticks) and to set the status of the ultrasound sensor to inactive.

TODO alumnos:

✅ 1. Reset the field status.
✅ 2. Call function port_ultrasound_stop_ultrasound() with the right parameters.

Parameters
p_fsmPointer to an fsm_ultrasound_t struct.