Skip to content

Commit

Permalink
#34 First working code for multiple DotBots with TDMA
Browse files Browse the repository at this point in the history
  • Loading branch information
trifuns committed Jun 23, 2022
1 parent bbe4b29 commit 63654e2
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions projects/03app_us_ranging/03app_us_ranging.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "board.h"
#include "hc_sr04.h"
#include "radio.h"
#include "timer.h"

//=========================== defines ===========================================

Expand All @@ -29,21 +30,29 @@

// Defines for the Radio
#define NUMBER_OF_BYTES_IN_PACKET 32
#define TIME_SLOT_MS 10
#define RTC_CHANNEL 0

// First byte in the Gateway message which correspond to the US ranging app type
#define ID_RANGE_ALL 0x11
#define ID_RANGE_PAIR 0x22
#define ID_RANGE_NONE 0xff

// Robot ID will be used as a uniqe identifier or Address of a robot
#define ROBOT_ID 0x01
//#define ROBOT_ID 0x01
//#define ROBOT_ID 0x02
//#define ROBOT_ID 0x03
#define ROBOT_ID 0x04
//#define ROBOT_ID 0x05

// Gateway ID will be used as a uniqe identifier or Address of a robot
#define GATEWAY_ID 0x77

//=========================== prototypes =========================================

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
void us_read_callback(uint32_t us_reading); // callback for the US echo measurement
void us_trigger_callback(void); // callback for timer0 after pulse_offset_ms + pulse_duration_ms
void timer_callback(void);

/**
* callback for the radio, called when Rx event
Expand Down Expand Up @@ -78,13 +87,13 @@ typedef struct {
uint32_t trigger_offset_ms;

app_state_t state; // Variable for storing the state of the ranging app

volatile uint8_t flag; // Stores the app flags

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_radio_mode_t radio_mode; // Stores the current Radio mode

uint32_t robot_time_slot_ms; // Stores the time in ms when to send the US measurement to the Gateway

} app_vars_t;

static app_vars_t app_vars;
Expand All @@ -109,11 +118,15 @@ int main(void) {
app_vars.trigger_duration_ms = PULSE_DURATION_MS;
app_vars.trigger_offset_ms = PULSE_OFFSET_MS;

app_vars.robot_time_slot_ms = TIME_SLOT_MS * ROBOT_ID;

app_vars.state = RANGE_NONE;

// uncomment if using this code on DotBot Turn ON the DotBot board regulator
db_board_init();

db_timer_init();

//=========================== Configure Radio =========================================

db_radio_init_lr(&radio_callback); // Set radio callback and initialize Long Range BLE
Expand All @@ -126,7 +139,7 @@ int main(void) {
//=========================== Configure HC_SR04 =========================================

// initilize the US sensor, set callback and choose the timers
hc_sr04_init(&us_callback, &timer_callback, app_vars.timer0, app_vars.timer1);
hc_sr04_init(&us_read_callback, &us_trigger_callback, app_vars.timer0, app_vars.timer1);

while (1) {

Expand Down Expand Up @@ -162,33 +175,27 @@ int main(void) {
case RANGE_ALL:
// check if a Robot has received a packet from the Gateway
if((app_vars.flag & APP_FLAG_RADIO_RX) && (app_vars.radio_mode == RADIO_Rx)) {

app_vars.flag &= ~APP_FLAG_RADIO_RX; // Clear the flag
db_radio_rx_disable(); // Disable the radio
app_vars.radio_mode = RADIO_Idle; // Set the radio mode to Idle
app_vars.flag &= ~APP_FLAG_RADIO_RX; // Clear the flag
app_vars.radio_mode = RADIO_Idle; // Set the radio mode to Idle
}
// check if we have a new US reading
if(app_vars.flag & APP_FLAG_US_DONE) {
//TODO START RTC
app_vars.radio_mode = RADIO_Tx; // set the Radio mode to Tx

hc_sr04_stop(); // stop the ranging we already have a measurement

db_radio_tx(app_vars.packet_tx, NUMBER_OF_BYTES_IN_PACKET); // send the US reading with ROBOT ID to Gateway
memset(app_vars.packet_tx, 0, sizeof(app_vars.packet_tx)); // clear the buffer

db_radio_rx_enable(); // enable the Radio packet reception
app_vars.radio_mode = RADIO_Rx; // set the Radio mode to Rx
app_vars.flag &= ~APP_FLAG_US_DONE; // Clear the flag
app_vars.flag &= ~APP_FLAG_US_DONE; // Clear the flag
hc_sr04_stop(); // stop the ranging, we already have a measurement
app_vars.radio_mode = RADIO_Tx; // set the Radio mode to Tx

}
// ready to send a packet with US measurement
if((app_vars.flag & APP_FLAG_RTC) && (app_vars.radio_mode == RADIO_Tx)) {
//db_radio_tx(app_vars.packet_tx, NUMBER_OF_BYTES_IN_PACKET);
app_vars.radio_mode = RADIO_Rx;
app_vars.flag &= ~APP_FLAG_RTC; // Clear the flag
//TODO

if((app_vars.flag & APP_FLAG_RTC) && (app_vars.radio_mode == RADIO_Tx)) {

app_vars.flag &= ~APP_FLAG_RTC; // Clear the flag
db_radio_tx(app_vars.packet_tx, NUMBER_OF_BYTES_IN_PACKET); // send the US reading with ROBOT ID to Gateway
memset(app_vars.packet_tx, 0, sizeof(app_vars.packet_tx)); // clear the buffer
db_radio_rx_enable(); // enable the Radio packet reception
app_vars.radio_mode = RADIO_Rx; // set the Radio mode to Rx
}

break; // RANGE ALL case break
Expand Down Expand Up @@ -257,7 +264,7 @@ void radio_callback(uint8_t *packet, uint8_t length) {
hc_sr04_on_set_trigger(app_vars.trigger_duration_ms, (double)app_vars.trigger_offset_ms);

hc_sr04_start();
NRF_P0->OUTSET = 1 << GPIO_OUTSET_PIN31_Pos; // ranging started set the pin HIGH.

}
else if (app_vars.state == RANGE_PAIR) {
//TODO
Expand All @@ -269,7 +276,7 @@ void radio_callback(uint8_t *packet, uint8_t length) {
* @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) {
void us_read_callback(uint32_t us_reading) {

// set the flag indicating that we got a new US measurement
app_vars.flag |= APP_FLAG_US_DONE;
Expand All @@ -293,6 +300,20 @@ void us_callback(uint32_t us_reading) {
* The code ends up here every pulse_offset_ms + pulse_duration_ms times.
* We could change the pulse_offset_ms here by writting a new value to timer0->CC[0] and timer0->CC[1]
*/
void us_trigger_callback(void) {
NRF_P0->OUTSET = 1 << GPIO_OUTSET_PIN31_Pos; // ranging started set the pin HIGH.

//TODO START RTC
db_timer_set_callback_ms(RTC_CHANNEL, app_vars.robot_time_slot_ms, timer_callback);
}

/**
* @brief This is a callback for the RTC timer.
* The code ends up here every time db_timer_set_callback_ms(uint8_t channel, uint32_t ms, timer_cb_t callback) is called
*/
void timer_callback(void) {
NRF_P0->OUTCLR = 1 << GPIO_OUTCLR_PIN31_Pos; // US trigger pulse finish set the pin LOW.
app_vars.flag |= APP_FLAG_RTC;

}

0 comments on commit 63654e2

Please sign in to comment.