Skip to content

Commit

Permalink
#34 Merging the Long Range radio code to the Ultrasound range code
Browse files Browse the repository at this point in the history
  • Loading branch information
trifuns authored and aabadie committed Jun 23, 2022
1 parent 0e5d058 commit 5e12433
Showing 1 changed file with 59 additions and 68 deletions.
127 changes: 59 additions & 68 deletions projects/03app_us_ranging/03app_us_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,40 @@
#include <stdlib.h>
#include <string.h>

// 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 =========================================

Expand All @@ -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();

}

/**
Expand All @@ -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();

}

0 comments on commit 5e12433

Please sign in to comment.