Skip to content

Commit

Permalink
Move ranging storage to ranging phase
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Nov 15, 2023
1 parent 808a7af commit 11223ff
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 78 deletions.
2 changes: 1 addition & 1 deletion software/firmware/launchConfigs/TestBleAndRanging.launch
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
</listAttribute>
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;memoryBlockExpressionList context=&quot;Context string&quot;/&gt;&#10;"/>
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;memoryBlockExpressionList context=&quot;Context string&quot;/&gt;&#13;&#10;"/>
<stringAttribute key="process_factory_id" value="org.eclipse.cdt.dsf.gdb.GdbProcessFactory"/>
</launchConfiguration>
47 changes: 2 additions & 45 deletions software/firmware/src/tasks/ranging/computation_phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

#include "logging.h"
#include "computation_phase.h"
#include "ranging_phase.h"


// Static Global Variables ---------------------------------------------------------------------------------------------

static ranging_device_state_t state[MAX_NUM_RANGING_DEVICES];
static int distances_millimeters[RANGING_NUM_RANGE_ATTEMPTS];
static uint8_t num_scheduled_devices;

Expand Down Expand Up @@ -36,49 +36,14 @@ void insert_sorted(int arr[], int new, unsigned end)
void reset_computation_phase(uint8_t schedule_length)
{
num_scheduled_devices = schedule_length;
memset(&state, 0, sizeof(state));
}

void associate_eui_with_index(uint32_t index, uint8_t eui)
{
state[index].device_eui = eui;
}

void add_ranging_times_poll_tx(uint32_t index, uint8_t sequence_number, uint32_t tx_time)
{
state[index].poll_tx_times[sequence_number] = tx_time;
}

void add_ranging_times_poll_rx(uint32_t index, uint8_t sequence_number, uint32_t rx_time)
{
state[index].poll_rx_times[sequence_number] = rx_time;
}

void add_ranging_times_response_tx(uint32_t index, uint8_t sequence_number, uint32_t tx_time)
{
state[index].resp_tx_times[sequence_number] = tx_time;
}

void add_ranging_times_response_rx(uint32_t index, uint8_t sequence_number, uint32_t rx_time)
{
state[index].resp_rx_times[sequence_number] = rx_time;
}

void add_ranging_times_final_tx(uint32_t index, uint8_t sequence_number, uint32_t tx_time)
{
state[index].final_tx_times[sequence_number] = tx_time;
}

void add_ranging_times_final_rx(uint32_t index, uint8_t sequence_number, uint32_t rx_time)
{
state[index].final_rx_times[sequence_number] = rx_time;
}

void compute_ranges(uint8_t *ranging_results)
{
// Iterate through all responses to calculate the range from this to that device
ranging_results[0] = 0;
uint8_t output_buffer_index = 1;
const ranging_device_state_t *state = ranging_phase_get_measurements();
for (uint8_t dev_index = 0; dev_index < num_scheduled_devices; ++dev_index)
{
// Calculate the device distances using symmetric two-way TOFs
Expand Down Expand Up @@ -122,11 +87,3 @@ void compute_ranges(uint8_t *ranging_results)
}
}
}

bool responses_received(void)
{
for (uint8_t dev_index = 0; dev_index < num_scheduled_devices; ++dev_index)
if (state[dev_index].device_eui)
return true;
return false;
}
19 changes: 0 additions & 19 deletions software/firmware/src/tasks/ranging/computation_phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,9 @@
#include "scheduler.h"


// Data Structures -----------------------------------------------------------------------------------------------------

typedef struct
{
uint8_t device_eui;
uint32_t poll_tx_times[RANGING_NUM_RANGE_ATTEMPTS], poll_rx_times[RANGING_NUM_RANGE_ATTEMPTS];
uint32_t resp_tx_times[RANGING_NUM_RANGE_ATTEMPTS], resp_rx_times[RANGING_NUM_RANGE_ATTEMPTS];
uint32_t final_tx_times[RANGING_NUM_RANGE_ATTEMPTS], final_rx_times[RANGING_NUM_RANGE_ATTEMPTS];
} ranging_device_state_t;


// Public API ----------------------------------------------------------------------------------------------------------

void reset_computation_phase(uint8_t schedule_length);
void associate_eui_with_index(uint32_t index, uint8_t eui);
void add_ranging_times_poll_tx(uint32_t index, uint8_t sequence_number, uint32_t tx_time);
void add_ranging_times_poll_rx(uint32_t index, uint8_t sequence_number, uint32_t rx_time);
void add_ranging_times_response_tx(uint32_t index, uint8_t sequence_number, uint32_t tx_time);
void add_ranging_times_response_rx(uint32_t index, uint8_t sequence_number, uint32_t rx_time);
void add_ranging_times_final_tx(uint32_t index, uint8_t sequence_number, uint32_t tx_time);
void add_ranging_times_final_rx(uint32_t index, uint8_t sequence_number, uint32_t rx_time);
void compute_ranges(uint8_t *ranging_results);
bool responses_received(void);

#endif // #ifndef __COMPUTATION_PHASE_HEADER_H__
41 changes: 28 additions & 13 deletions software/firmware/src/tasks/ranging/ranging_phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

static scheduler_phase_t current_phase;
static ranging_packet_t ranging_packet;
static ranging_device_state_t measurements[MAX_NUM_RANGING_DEVICES];
static uint32_t time_slot, my_slot, extended_slot, num_slots, slots_per_range;
static uint32_t schedule_length, next_action_timestamp, ranging_phase_duration, temp_resp_rx;
static uint16_t extended_packet_length;
Expand Down Expand Up @@ -90,6 +91,7 @@ scheduler_phase_t ranging_phase_begin(uint8_t scheduled_slot, uint8_t schedule_s
// Ensure there are at least two devices to begin ranging
my_slot = scheduled_slot;
reset_computation_phase(schedule_size);
memset(&measurements, 0, sizeof(measurements));
slots_per_range = (uint32_t)schedule_size * RANGING_NUM_PACKETS_PER_DEVICE;
extended_slot = (uint32_t)schedule_size * (RANGING_NUM_PACKETS_PER_DEVICE - 1);
num_slots = slots_per_range * RANGING_NUM_RANGE_ATTEMPTS;
Expand Down Expand Up @@ -133,13 +135,13 @@ scheduler_phase_t ranging_phase_tx_complete(void)
if (slot < schedule_length)
{
for (uint32_t i = 0; i < my_slot; ++i)
add_ranging_times_response_tx(i, (uint8_t)sequence_number, ranging_packet.tx_rx_times[0]);
measurements[i].resp_tx_times[sequence_number] = ranging_packet.tx_rx_times[0];
for (uint32_t i = my_slot + 1; i < schedule_length; ++i)
add_ranging_times_poll_tx(i, (uint8_t)sequence_number, ranging_packet.tx_rx_times[0]);
measurements[i].poll_tx_times[sequence_number] = ranging_packet.tx_rx_times[0];
}
else if (slot < extended_slot)
for (uint32_t i = my_slot + 1; i < schedule_length; ++i)
add_ranging_times_final_tx(i, (uint8_t)sequence_number, ranging_packet.tx_rx_times[0]);
measurements[i].final_tx_times[sequence_number] = ranging_packet.tx_rx_times[0];

// Move to the next time slot operation
++time_slot;
Expand Down Expand Up @@ -170,39 +172,39 @@ scheduler_phase_t ranging_phase_rx_complete(ranging_packet_t* packet)
{
register const uint32_t storage_index = schedule_length - my_slot - 1 + slot + slot;
ranging_packet.tx_rx_times[storage_index] = (uint32_t)ranging_radio_readrxtimestamp();
add_ranging_times_poll_tx(slot, (uint8_t)sequence_number, packet->tx_rx_times[0]);
add_ranging_times_poll_rx(slot, (uint8_t)sequence_number, ranging_packet.tx_rx_times[storage_index]);
measurements[slot].poll_tx_times[sequence_number] = packet->tx_rx_times[0];
measurements[slot].poll_rx_times[sequence_number] = ranging_packet.tx_rx_times[storage_index];
if (!storage_index)
temp_resp_rx = ranging_packet.tx_rx_times[storage_index];
}
else if (slot < schedule_length)
{
register const uint32_t storage_index = slot - my_slot - 1;
ranging_packet.tx_rx_times[storage_index] = (uint32_t)ranging_radio_readrxtimestamp();
add_ranging_times_response_tx(slot, (uint8_t)sequence_number, packet->tx_rx_times[0]);
add_ranging_times_response_rx(slot, (uint8_t)sequence_number, ranging_packet.tx_rx_times[storage_index]);
measurements[slot].resp_tx_times[sequence_number] = packet->tx_rx_times[0];
measurements[slot].resp_rx_times[sequence_number] = ranging_packet.tx_rx_times[storage_index];
if (!storage_index)
temp_resp_rx = ranging_packet.tx_rx_times[storage_index];
}
else if (slot >= extended_slot)
{
register const uint32_t tx_device_slot = slot - extended_slot;
associate_eui_with_index(tx_device_slot, packet->header.sourceAddr[0]);
measurements[tx_device_slot].device_eui = packet->header.sourceAddr[0];
if (my_slot > tx_device_slot)
add_ranging_times_response_rx(tx_device_slot, (uint8_t)sequence_number, packet->tx_rx_times[my_slot - tx_device_slot - 1]);
measurements[tx_device_slot].resp_rx_times[sequence_number] = packet->tx_rx_times[my_slot - tx_device_slot - 1];
else
{
register const uint32_t poll_times_offset = schedule_length - tx_device_slot - 1;
add_ranging_times_poll_rx(tx_device_slot, (uint8_t)sequence_number, packet->tx_rx_times[poll_times_offset + my_slot + my_slot]);
add_ranging_times_final_rx(tx_device_slot, (uint8_t)sequence_number, packet->tx_rx_times[poll_times_offset + my_slot + my_slot + 1]);
measurements[tx_device_slot].poll_rx_times[sequence_number] = packet->tx_rx_times[poll_times_offset + my_slot + my_slot];
measurements[tx_device_slot].final_rx_times[sequence_number] = packet->tx_rx_times[poll_times_offset + my_slot + my_slot + 1];
}
}
else if ((slot - schedule_length) < my_slot)
{
register const uint32_t storage_index = slot + slot - schedule_length - my_slot;
ranging_packet.tx_rx_times[storage_index] = (uint32_t)ranging_radio_readrxtimestamp();
add_ranging_times_final_tx(slot - schedule_length, (uint8_t)sequence_number, packet->tx_rx_times[0]);
add_ranging_times_final_rx(slot - schedule_length, (uint8_t)sequence_number, ranging_packet.tx_rx_times[storage_index]);
measurements[slot - schedule_length].final_tx_times[sequence_number] = packet->tx_rx_times[0];
measurements[slot - schedule_length].final_rx_times[sequence_number] = ranging_packet.tx_rx_times[storage_index];
}

// Move to the next time slot operation
Expand Down Expand Up @@ -241,6 +243,11 @@ scheduler_phase_t ranging_phase_rx_error(void)
start_rx("ERROR: Unable to start listening for RANGING packets after error\n");
}

ranging_device_state_t* ranging_phase_get_measurements(void)
{
return measurements;
}

uint32_t ranging_phase_get_duration(void)
{
return ranging_phase_duration;
Expand All @@ -250,3 +257,11 @@ bool ranging_phase_was_scheduled(void)
{
return (my_slot != UNSCHEDULED_SLOT);
}

bool responses_received(void)
{
for (uint32_t i = 0; i < schedule_length; ++i)
if (measurements[i].device_eui)
return true;
return false;
}
10 changes: 10 additions & 0 deletions software/firmware/src/tasks/ranging/ranging_phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ typedef struct __attribute__ ((__packed__))
ieee154_footer_t footer;
} ranging_packet_t;

typedef struct
{
uint8_t device_eui;
uint32_t poll_tx_times[RANGING_NUM_RANGE_ATTEMPTS], poll_rx_times[RANGING_NUM_RANGE_ATTEMPTS];
uint32_t resp_tx_times[RANGING_NUM_RANGE_ATTEMPTS], resp_rx_times[RANGING_NUM_RANGE_ATTEMPTS];
uint32_t final_tx_times[RANGING_NUM_RANGE_ATTEMPTS], final_rx_times[RANGING_NUM_RANGE_ATTEMPTS];
} ranging_device_state_t;


// Public API ----------------------------------------------------------------------------------------------------------

Expand All @@ -23,7 +31,9 @@ scheduler_phase_t ranging_phase_begin(uint8_t scheduled_slot, uint8_t schedule_s
scheduler_phase_t ranging_phase_tx_complete(void);
scheduler_phase_t ranging_phase_rx_complete(ranging_packet_t* packet);
scheduler_phase_t ranging_phase_rx_error(void);
ranging_device_state_t* ranging_phase_get_measurements(void);
uint32_t ranging_phase_get_duration(void);
bool ranging_phase_was_scheduled(void);
bool responses_received(void);

#endif // #ifndef __RANGING_PHASE_HEADER_H__

0 comments on commit 11223ff

Please sign in to comment.