Skip to content
Snippets Groups Projects
Commit 06a45f31 authored by jlKronos01's avatar jlKronos01
Browse files

Merge branch 'UWB'

parents ba8ead16 da185ea3
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@
#define MY_ADDR ANCHOR1
#define RX_TIMEOUT 5000
#define INTER_RANGING_DELAY 5
#define INTER_RANGING_DELAY 0
#define NUMBER_OF_BEACONS_TO_RANGE 4 - MY_ADDR
......@@ -49,16 +49,12 @@ static dwt_config_t config = {
DWT_PDOA_M0 /* PDOA mode off */
};
size_t rxLen;
static uint8_t rxBuffer[FRAME_LEN_MAX - MHR_LENGTH] = {};
/* Hold copy of status register state here for reference so that it can be examined at a debug breakpoint. */
uint32_t status_reg;
extern dwt_txconfig_t txconfig_options;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
deviceState state = RESPONDER;
void setup()
......@@ -107,6 +103,8 @@ void setup()
while (1)
;
}
txconfig_options.power = 0xffffffff;
dwt_configuretxrf(&txconfig_options);
dwt_setrxantennadelay(RX_ANT_DLY);
......@@ -121,16 +119,13 @@ void setup()
void loop()
{
// Reset global parameters
clearMacHeader(&txMHR);
clearMacHeader(&rxMHR);
rxLen = 0;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
size_t rxLen = 0;
memset(rxBuffer, 0, sizeof(rxBuffer));
frameReceived = 0;
switch (state) {
// Beacons will only ever be in BEACON_POSITIONING_MODE mode during beacon positioning
// hence this portion of code only does ranging with other anchors
case BEACON_POSITIONING_MODE:
double dist;
if (NUMBER_OF_BEACONS_TO_RANGE > 0) {
......@@ -142,6 +137,7 @@ void loop()
addresses[i] = i + MY_ADDR + 1; // store destination addresss
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Ranging %d retry %d\n", addresses[i], retries);
distances[i] = getRangingDistance(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
// distances[i] = getRangingDistanceDelayedTX(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
if (distances[i] != 0.0) {break;}
......@@ -153,18 +149,20 @@ void loop()
// ack = sendDistancesWithAck(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, &rxMHR, 5);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("send distance retry %d\n", retries);
sendDistances(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
delay(INTER_RANGING_DELAY);
}
if (MY_ADDR < ANCHOR3) {
// Serial.printf("Anchor %d passing bpm to anchor %d\n", MY_ADDR, MY_ADDR + 1);
// ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, &rxMHR, 3);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Send bpm retry %d\n", retries);
sendCommand(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
......
......@@ -28,7 +28,7 @@
#define MY_ADDR ANCHOR2
#define RX_TIMEOUT 5000
#define INTER_RANGING_DELAY 5
#define INTER_RANGING_DELAY 0
#define NUMBER_OF_BEACONS_TO_RANGE 4 - MY_ADDR
......@@ -49,16 +49,12 @@ static dwt_config_t config = {
DWT_PDOA_M0 /* PDOA mode off */
};
size_t rxLen;
static uint8_t rxBuffer[FRAME_LEN_MAX - MHR_LENGTH] = {};
/* Hold copy of status register state here for reference so that it can be examined at a debug breakpoint. */
uint32_t status_reg;
extern dwt_txconfig_t txconfig_options;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
deviceState state = RESPONDER;
void setup()
......@@ -107,6 +103,8 @@ void setup()
while (1)
;
}
txconfig_options.power = 0xffffffff;
dwt_configuretxrf(&txconfig_options);
dwt_setrxantennadelay(RX_ANT_DLY);
......@@ -121,16 +119,13 @@ void setup()
void loop()
{
// Reset global parameters
clearMacHeader(&txMHR);
clearMacHeader(&rxMHR);
rxLen = 0;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
size_t rxLen = 0;
memset(rxBuffer, 0, sizeof(rxBuffer));
frameReceived = 0;
switch (state) {
// Beacons will only ever be in BEACON_POSITIONING_MODE mode during beacon positioning
// hence this portion of code only does ranging with other anchors
case BEACON_POSITIONING_MODE:
double dist;
if (NUMBER_OF_BEACONS_TO_RANGE > 0) {
......@@ -142,6 +137,7 @@ void loop()
addresses[i] = i + MY_ADDR + 1; // store destination addresss
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Ranging %d retry %d\n", addresses[i], retries);
distances[i] = getRangingDistance(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
// distances[i] = getRangingDistanceDelayedTX(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
if (distances[i] != 0.0) {break;}
......@@ -153,18 +149,20 @@ void loop()
// ack = sendDistancesWithAck(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, &rxMHR, 5);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("send distance retry %d\n", retries);
sendDistances(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
delay(INTER_RANGING_DELAY);
}
if (MY_ADDR < ANCHOR3) {
// Serial.printf("Anchor %d passing bpm to anchor %d\n", MY_ADDR, MY_ADDR + 1);
// ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, &rxMHR, 3);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Send bpm retry %d\n", retries);
sendCommand(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
......
......@@ -28,7 +28,7 @@
#define MY_ADDR ANCHOR3
#define RX_TIMEOUT 5000
#define INTER_RANGING_DELAY 5
#define INTER_RANGING_DELAY 0
#define NUMBER_OF_BEACONS_TO_RANGE 4 - MY_ADDR
......@@ -49,16 +49,12 @@ static dwt_config_t config = {
DWT_PDOA_M0 /* PDOA mode off */
};
size_t rxLen;
static uint8_t rxBuffer[FRAME_LEN_MAX - MHR_LENGTH] = {};
/* Hold copy of status register state here for reference so that it can be examined at a debug breakpoint. */
uint32_t status_reg;
extern dwt_txconfig_t txconfig_options;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
deviceState state = RESPONDER;
void setup()
......@@ -107,6 +103,8 @@ void setup()
while (1)
;
}
txconfig_options.power = 0xffffffff;
dwt_configuretxrf(&txconfig_options);
dwt_setrxantennadelay(RX_ANT_DLY);
......@@ -121,16 +119,13 @@ void setup()
void loop()
{
// Reset global parameters
clearMacHeader(&txMHR);
clearMacHeader(&rxMHR);
rxLen = 0;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
size_t rxLen = 0;
memset(rxBuffer, 0, sizeof(rxBuffer));
frameReceived = 0;
switch (state) {
// Beacons will only ever be in BEACON_POSITIONING_MODE mode during beacon positioning
// hence this portion of code only does ranging with other anchors
case BEACON_POSITIONING_MODE:
double dist;
if (NUMBER_OF_BEACONS_TO_RANGE > 0) {
......@@ -142,6 +137,7 @@ void loop()
addresses[i] = i + MY_ADDR + 1; // store destination addresss
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Ranging %d retry %d\n", addresses[i], retries);
distances[i] = getRangingDistance(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
// distances[i] = getRangingDistanceDelayedTX(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
if (distances[i] != 0.0) {break;}
......@@ -153,18 +149,20 @@ void loop()
// ack = sendDistancesWithAck(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, &rxMHR, 5);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("send distance retry %d\n", retries);
sendDistances(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
delay(INTER_RANGING_DELAY);
}
if (MY_ADDR < ANCHOR3) {
// Serial.printf("Anchor %d passing bpm to anchor %d\n", MY_ADDR, MY_ADDR + 1);
// ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, &rxMHR, 3);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Send bpm retry %d\n", retries);
sendCommand(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
......
......@@ -28,7 +28,7 @@
#define MY_ADDR ANCHOR4
#define RX_TIMEOUT 5000
#define INTER_RANGING_DELAY 5
#define INTER_RANGING_DELAY 0
#define NUMBER_OF_BEACONS_TO_RANGE 4 - MY_ADDR
......@@ -49,16 +49,12 @@ static dwt_config_t config = {
DWT_PDOA_M0 /* PDOA mode off */
};
size_t rxLen;
static uint8_t rxBuffer[FRAME_LEN_MAX - MHR_LENGTH] = {};
/* Hold copy of status register state here for reference so that it can be examined at a debug breakpoint. */
uint32_t status_reg;
extern dwt_txconfig_t txconfig_options;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
deviceState state = RESPONDER;
void setup()
......@@ -107,6 +103,8 @@ void setup()
while (1)
;
}
txconfig_options.power = 0xffffffff;
dwt_configuretxrf(&txconfig_options);
dwt_setrxantennadelay(RX_ANT_DLY);
......@@ -121,16 +119,13 @@ void setup()
void loop()
{
// Reset global parameters
clearMacHeader(&txMHR);
clearMacHeader(&rxMHR);
rxLen = 0;
macHeader txMHR, rxMHR;
bool frameReceived = 0;
bool ack;
size_t rxLen = 0;
memset(rxBuffer, 0, sizeof(rxBuffer));
frameReceived = 0;
switch (state) {
// Beacons will only ever be in BEACON_POSITIONING_MODE mode during beacon positioning
// hence this portion of code only does ranging with other anchors
case BEACON_POSITIONING_MODE:
double dist;
if (NUMBER_OF_BEACONS_TO_RANGE > 0) {
......@@ -142,6 +137,7 @@ void loop()
addresses[i] = i + MY_ADDR + 1; // store destination addresss
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Ranging %d retry %d\n", addresses[i], retries);
distances[i] = getRangingDistance(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
// distances[i] = getRangingDistanceDelayedTX(&txMHR, PAN_ID, MY_ADDR, addresses[i], &rxMHR); // range beacon at addresses[i]
if (distances[i] != 0.0) {break;}
......@@ -153,18 +149,20 @@ void loop()
// ack = sendDistancesWithAck(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, &rxMHR, 5);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("send distance retry %d\n", retries);
sendDistances(&txMHR, PAN_ID, MY_ADDR, TAG1, NUMBER_OF_BEACONS_TO_RANGE, addresses, distances, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
delay(INTER_RANGING_DELAY);
}
if (MY_ADDR < ANCHOR3) {
// Serial.printf("Anchor %d passing bpm to anchor %d\n", MY_ADDR, MY_ADDR + 1);
// ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, &rxMHR, 3);
// delay(INTER_RANGING_DELAY);
for (uint8_t retries = 0; retries < 10; retries++) {
// Serial.printf("Send bpm retry %d\n", retries);
sendCommand(&txMHR, PAN_ID, MY_ADDR, MY_ADDR + 1, BPM_COMMAND, true);
ack = receiveAcknowledgement(&rxMHR);
if (ack) {break;}
......
......@@ -2,10 +2,10 @@ from scipy.optimize import least_squares
import numpy as np
# Measured distances arrive in order: A, E, D, B, F, C
measured_distances = [550.00,514.78,700.00,269.26,890.22,1051.19]
measured_distances = [1496.223841, 1503.259376, 677.756567, 945.575945, 1419.301988, 923.531267 ]
# Introduce ±10 cm of noise
noise_level = 10
noise_level =0
measured_distances_noisy = measured_distances + np.random.uniform(-noise_level, noise_level, size=len(measured_distances))
# Automatically generate a reasonable initial guess
......@@ -22,33 +22,16 @@ def generate_initial_guess(measured_distances):
return [x_B, y_B, x_C, y_C, y_A]
# Automatically generate reasonable bounds
def generate_bounds(measured_distances):
min_dist = min(measured_distances)
max_dist = max(measured_distances)
# Define lower and upper bounds based on measured distances
lower_bound = [
-max_dist / 2, # x_B lower bound
min_dist / 2, # y_B lower bound (above y_C)
-max_dist / 2, # x_C lower bound
min_dist / 4, # y_C lower bound
min_dist / 2 # y_A lower bound
]
upper_bound = [
max_dist * 1.5, # x_B upper bound
max_dist * 1.5, # y_B upper bound
max_dist * 1.5, # x_C upper bound
max_dist * 1.25, # y_C upper bound
max_dist * 1.5 # y_A upper bound
]
return lower_bound, upper_bound
# Generate the initial guess and bounds
# Generate the initial guess
initial_guess = generate_initial_guess(measured_distances_noisy)
lower_bounds, upper_bounds = generate_bounds(measured_distances_noisy)
# Define the error function with constraint y_B > y_C
# Simplified uniform bounds for all variables
bounds = ([-2000, -2000, -2000, -2000, -2000], [2000, 2000, 2000, 0, 2000])
# Ensure the initial guess is within bounds
initial_guess = np.clip(initial_guess, bounds[0], bounds[1])
# Define the error function
def error_function(variables, measured):
x_B, y_B, x_C, y_C, y_A = variables
......@@ -88,10 +71,11 @@ result_noisy = least_squares(
error_function,
initial_guess,
args=(measured_distances_noisy,),
bounds=(lower_bounds, upper_bounds),
bounds=bounds,
loss='soft_l1'
)
# Extract optimized coordinates
optimized_coords_noisy = result_noisy.x
print("Optimized coordinates with noise:", optimized_coords_noisy)
......
......@@ -196,7 +196,7 @@ class AnchorTagGUI:
# Guess for B based on distance A-B and B-D
x_B = measured_distances[0] / 2
y_B = measured_distances[4] / 2 + 100 # Start y_B above y_C
y_B = measured_distances[4] / 2 + 200 # Start y_B above y_C
# Guess for C based on distance C-D and A-C
x_C = measured_distances[5] / 2
......@@ -211,17 +211,17 @@ class AnchorTagGUI:
# Define lower and upper bounds based on measured distances
lower_bound = [
-max_dist / 2, # x_B lower bound
min_dist / 2, # y_B lower bound (above y_C)
-max_dist / 2, # x_C lower bound
min_dist / 4, # y_C lower bound
-max_dist, # x_B lower bound
-max_dist, # y_B lower bound (above y_C)
-max_dist, # x_C lower bound
-max_dist, # y_C lower bound
min_dist / 2 # y_A lower bound
]
upper_bound = [
max_dist * 1.5, # x_B upper bound
max_dist * 1.5, # y_B upper bound
max_dist * 1.5, # x_C upper bound
max_dist * 1.25, # y_C upper bound
max_dist * 1.5, # y_C upper bound
max_dist * 1.5 # y_A upper bound
]
return lower_bound, upper_bound
......@@ -274,14 +274,14 @@ class AnchorTagGUI:
)
optimized_coords_noisy = result_noisy.x
h=250
print(f"Optimized coordinates: {optimized_coords_noisy}")
# print(f"Optimized coordinates: {optimized_coords_noisy}")
self.anchors = {
"A1": (0, optimized_coords_noisy[4], h),
"A2": (optimized_coords_noisy[0], optimized_coords_noisy[1], h),
"A3": (optimized_coords_noisy[2], optimized_coords_noisy[3], h),
"A4": (0, 0, h),
}
print(self.anchors)
# print(self.anchors)
self.draw_canvas()
print(f"Optimized anchor coordinates: {self.anchors}")
return {k: (round(v[0], 2), round(v[1], 2)) for k, v in self.anchors.items()}
......@@ -416,7 +416,7 @@ class AnchorTagGUI:
canvas_width, canvas_height = 600, 600
center_x, center_y = canvas_width // 2, canvas_height // 2
scale_factor = 0.1
scale_factor = 0.2
# Determine avg anchor position for centering
if hasattr(self, 'anchors') and self.anchors:
......@@ -429,8 +429,8 @@ class AnchorTagGUI:
y_offset = center_y - avg_y
def transform_coordinates(x, y):
x_scaled = int((x + x_offset) * scale_factor) +100
y_scaled = int(canvas_height - (y + y_offset) * scale_factor) - 200
x_scaled = int((x + x_offset) * scale_factor) + 200
y_scaled = int(canvas_height - (y + y_offset) * scale_factor) - 250
return x_scaled, y_scaled
# Draw anchorss
......
......@@ -162,8 +162,8 @@ void loop()
// Send acknowledgement if AR bit is set
if (checkAckRequest(&rxMHR)) {
Serial.println("Sending acknowledgement...");
initializeEnhAckMHR(&txMHR, PAN_ID, MY_ADDR, 0x1234);
// initializeEnhAckMHR(&txMHR, PAN_ID, MY_ADDR, getMacSrcAddr(&rxMHR));
// initializeEnhAckMHR(&txMHR, PAN_ID, MY_ADDR, 0x1234);
initializeEnhAckMHR(&txMHR, PAN_ID, MY_ADDR, getMacSrcAddr(&rxMHR));
Serial.printf("ACK MHR: ");
for (int i = 0; i < MHR_LENGTH; i++) {
......
......@@ -29,7 +29,7 @@
#define RX_TIMEOUT 5000
#define INTER_RANGING_DELAY 5
#define TIMEOUT_MILLIS 300
#define TIMEOUT_MILLIS 2000
unsigned long startTime;
......@@ -124,6 +124,7 @@ void setup()
}
// Serial.println("CONFIG OK");
/* Configure the TX spectrum parameters (power PG delay and PG Count) */
// txconfig_options.power = 0xffffffff;
dwt_configuretxrf(&txconfig_options);
dwt_setrxantennadelay(RX_ANT_DLY);
......@@ -172,15 +173,15 @@ void loop()
if (strcmp(serialBuffer, "bpm") == 0) {
//Perform live checks on all 4 anchors
for (uint8_t beacon = ANCHOR1; beacon <= ANCHOR4; beacon++) {
ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, beacon, LIVE_CHECK, &rxMHR, 3);
// ack = 1; // DEBUGGING
anchorAcks[beacon - 1] = ack;
if (!ack) {allTrue = false;}
delay(INTER_RANGING_DELAY);
}
// for (uint8_t beacon = ANCHOR1; beacon <= ANCHOR4; beacon++) {
// ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, beacon, LIVE_CHECK, &rxMHR, 3);
// // ack = 1; // DEBUGGING
// anchorAcks[beacon - 1] = ack;
// if (!ack) {allTrue = false;}
// delay(INTER_RANGING_DELAY);
// }
if (allTrue) { // do BPM if all anchors are alive
// if (allTrue) { // do BPM if all anchors are alive
BPMResponseCount = 0;
memset(BPMDistances, 0, sizeof(BPMDistances));
ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, ANCHOR1, BPM_COMMAND, &rxMHR); // initiate BPM chain reaction starting with ANCHOR1
......@@ -189,15 +190,24 @@ void loop()
memset(serialBuffer, 0, sizeof(serialBuffer)); //clear the serial buffer once done
serialDataReady = false; //reset serial data ready flag
return;
}
else { // tell the pi which anchors are offline
Serial.printf("%d %d %d %d", anchorAcks[0], anchorAcks[1], anchorAcks[2], anchorAcks[3]);
Serial.println();
}
// }
// else { // tell the pi which anchors are offline
// Serial.printf("%d %d %d %d", anchorAcks[0], anchorAcks[1], anchorAcks[2], anchorAcks[3]);
// Serial.println();
// }
}
else if (strcmp(serialBuffer, "rng") == 0) {
state = RANGING_INITIATOR;
}
// else if (strcmp(serialBuffer, "lc") == 0) {
// Serial.print("Live check: ");
// for (uint8_t beacon = ANCHOR1; beacon <= ANCHOR4; beacon++) {
// ack = sendCommandWithAck(&txMHR, PAN_ID, MY_ADDR, beacon, LIVE_CHECK, &rxMHR, 3);
// printf("%d: %d ", beacon, ack);
// // delay(INTER_RANGING_DELAY);
// }
// Serial.println();
// }
// else {
// Serial.println("Serial command not recognized. ");
// }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment