From 40b27852aa5b8a05b8983abcbf25281fa361f343 Mon Sep 17 00:00:00 2001 From: Trifun Savic Date: Tue, 21 Jun 2022 18:21:46 +0200 Subject: [PATCH] #34 Merging the Long Range radio code to the Ultrasound range code --- projects/03app_us_ranging/03app_us_ranging.c | 127 +++++++++---------- 1 file changed, 59 insertions(+), 68 deletions(-) diff --git a/projects/03app_us_ranging/03app_us_ranging.c b/projects/03app_us_ranging/03app_us_ranging.c index 278eff935..a5baebc5b 100644 --- a/projects/03app_us_ranging/03app_us_ranging.c +++ b/projects/03app_us_ranging/03app_us_ranging.c @@ -17,49 +17,40 @@ #include #include -// uncoment the board.h when porting this code to DotBot -//#include "board.h" +#include "board.h" #include "hc_sr04.h" #include "radio.h" +//=========================== defines =========================================== -//=========================== defines ========================================= +// Defines for setting up the trigger pulse width and frequency +#define PULSE_DURATION_MS 0.01 +#define PULSE_OFFSET_MS 200 -/** - * Struct used to store internal variables - */ -typedef struct { - NRF_TIMER_Type *timer0; // variable for passing the Timer for sensor trigger - NRF_TIMER_Type *timer1; // variable for passing the Timer for sensor echo measurement -} us_ranging_vars_t; +// Defines for the Radio +#define NUMBER_OF_BYTES_IN_PACKET 32 //=========================== prototypes ========================================= -/** - * callback for the US echo measurement - */ -void us_callback(uint32_t us_reading); - -/** - * callback for timer0 after pulse_offset_ms + pulse_duration_ms - */ -void timer_callback(void); +void us_callback(uint32_t us_reading); // callback for the US echo measurement +void timer_callback(void); // callback for timer0 after pulse_offset_ms + pulse_duration_ms /** * callback for the radio, called when Rx event */ void radio_callback(uint8_t *packet, uint8_t length); -/** - * A function to setup the radio for the application needs. This function should be called after db_radio_init - */ -void us_radio_setup(void); - //=========================== variables ========================================= +typedef struct { + NRF_TIMER_Type *timer0; // Pointer to the TIMER structure used for triggering US sensor + NRF_TIMER_Type *timer1; // Pointer to the TIMER structure used for reading the range on the US sensor + + uint8_t packet_tx[NUMBER_OF_BYTES_IN_PACKET]; // Radio packet Tx + uint8_t packet_rx[NUMBER_OF_BYTES_IN_PACKET]; // Radio packet Rx +} app_vars_t; -static us_ranging_vars_t us_ranging_vars; - +static app_vars_t app_vars; //=========================== main ========================================= @@ -68,51 +59,72 @@ static us_ranging_vars_t us_ranging_vars; */ int main(void) { - us_ranging_vars.timer0 = NRF_TIMER0; - us_ranging_vars.timer1 = NRF_TIMER1; + memset(&app_vars, 0, sizeof(app_vars_t)); + + app_vars.timer0 = NRF_TIMER0; + app_vars.timer1 = NRF_TIMER1; // uncomment if using this code on DotBot Turn ON the DotBot board regulator - //db_board_init(); + db_board_init(); - // Initialize Radio - //db_radio_init(&radio_callback); // Set the callback function. - - - db_lr_radio_init(&radio_callback); + //=========================== Configure Radio ========================================= + + db_radio_init_lr(&radio_callback); // Set radio callback and initialize Long Range BLE + db_radio_set_frequency(8); // Set the RX frquency to 2408 MHz. - db_radio_set_frequency(8); // Set the RX frequency to 2408 MHz. - db_radio_rx_enable(); // Start receiving packets. + db_radio_tx(app_vars.packet_tx, NUMBER_OF_BYTES_IN_PACKET); + db_radio_rx_enable(); // Start receiving packets. + //=========================== Configure HC_SR04 ========================================= // initilize the US sensor, set callback and chose the timers - us_init(&us_callback, &timer_callback, us_ranging_vars.timer0, us_ranging_vars.timer1); + hc_sr04_init(&us_callback, &timer_callback, app_vars.timer0, app_vars.timer1); + // initial timer settings for the trigger pulse + hc_sr04_on_set_trigger(PULSE_DURATION_MS, PULSE_OFFSET_MS); + + // start high frequency clock needed for PPI + hfclk_init(); + // start ranging - us_start(); - - // THIS IS ALREADY ENABLED IN db_radio_init - start high frequency clock needed for PPI - //hfclk_init(); + hc_sr04_start(); - while (1) { - + while (1) { __WFE(); __SEV(); __WFE(); - } - // one last instruction, doesn't do anything, it's just to have a place to put a breakpoint. __NOP(); } +//=========================== functions ========================================= + +/** + * @brief Callback function to process received packets + * + * This function gets called each time a packet is received. + * + * @param[in] packet pointer to the array of data to send over the radio (max size = 32) + * @param[in] length Number of bytes to send (max size = 32) + * + */ +void radio_callback(uint8_t *packet, uint8_t length) { + + // stop ranging + hc_sr04_stop(); + + // start ranging + hc_sr04_start(); + +} + /** * @brief This is a callback for US reading. * The code ends up here every time a new measurement is taken. The new measurement is stored in us_reading argument */ void us_callback(uint32_t us_reading) { - __NOP(); - } /** @@ -121,26 +133,5 @@ void us_callback(uint32_t us_reading) { * We could change the pulse_offset_ms here by writting a new value to timer0->CC[0] and timer0->CC[1] */ void timer_callback(void) { - __NOP(); - } - -/** - * @brief This is a callback for the radio Rx event. - * The code ends up here every time a radio packet is received. - * Here we setup the new values for the compare ragisters in order to change the offset of the US trigger timer. - * From here the PPI for US ranging starts every time a packet is received. - */ -void radio_callback(uint8_t *packet, uint8_t length) { - - //TODO - // stop ranging - //us_stop(); - - // start ranging - //us_start(); - __NOP(); - -} -