workss
Dependencies: mbed BLE_API nRF51822 VL53L0X
Diff: main.cpp
- Revision:
- 48:78310b56de00
- Parent:
- 47:eeed074999ab
--- a/main.cpp Tue Mar 19 18:25:39 2019 +0000
+++ b/main.cpp Sun Mar 24 15:37:28 2019 +0000
@@ -7,13 +7,13 @@
#define range1_addr (0x56)
#define range2_addr (0x60)
-#define range1_XSHUT p15
-#define range2_XSHUT p16
+#define range1_XSHUT p16
+#define range2_XSHUT p15
#define VL53L0_I2C_SDA p30
#define VL53L0_I2C_SCL p7
#define TIME_SCALE 3 // sensors activated every 100ms * TIME_SCALE = 0.3 seconds
#define DIST_MIN 20 // PROD
-#define DIST_MAX 100 // PROD
+#define DIST_MAX 120 // PROD
//#define DIST_MIN 0 // DEV
//#define DIST_MAX 22 // DEV
//#define TIMESTAMP_FREQ 3000 // PROD: time granularity is 100ms * TIMESTAMP_FREQ = 5 minutes
@@ -41,7 +41,7 @@
int packet_queue[4000] = {0};
int packet_queue_index = 2;
int current_index = 0, send_count = 0, triple_send_count = 0;
-
+
const static int cw = 20 / TIME_SCALE;
const static int time_cycle = TIMESTAMP_FREQ / TIME_SCALE;
const static int maximum_time = MAX_TIME / TIMESTAMP_FREQ;
@@ -59,7 +59,8 @@
HeartRateService *hrService;
uint8_t hrmCounter = 0;
-
+
+// converts distance to cm
int format_dist(int distance) {
int result;
if (distance > 2550)
@@ -68,7 +69,9 @@
result = distance/10;
return result;
}
-
+
+// starts the clock after one sensor was triggered: the next must be triggered
+// within 2 seconds for a step to be counted
void check_for_countdown(bool countdown_triggered) {
if (countdown_triggered) {
::countdown--;
@@ -78,13 +81,14 @@
}
}
-
+// needed for the stalling of the queue after the first 4 packets are transmitted:
+// pads queue with "fake" packets that will not be transmitted
void optimize_transmissions(int packet_queue[]) {
for (int count = 4; count < 12; count++) {
packet_queue[count] = 1;
}
}
-
+
void dec_to_bin(int decimal, int bin[]) {
int temp;
// encoding little-endian
@@ -93,7 +97,7 @@
bin[i] = temp&1;
}
}
-
+
int bin_to_dec(int binary) {
int rem, temp, dec = 0, b = 1;
temp = binary;
@@ -107,6 +111,7 @@
return dec;
}
+// creates the encoding that fits all required info into 2 bytes
void encode_bin(int direction, int time, int encoding[]) {
time = time % maximum_time;
int bin_timestamp[11] = {0};
@@ -116,7 +121,7 @@
// sending as 2 messages; designating them with 0 (1st part) and 1 (2nd part)
encoding [8] = 1;
encoding [0] = 0;
-
+
// used to send the current time when Bluetooth connection established
if (direction == -1) {
encoding[15] = 1;
@@ -143,7 +148,8 @@
}
}
}
-
+
+// converts the binary encoding back into 2 byte-sized packets
void create_packets(int encoding[], double& packet1, double& packet2) {
double binary1 = 0, binary2 = 0;
for (int i = 0; i < 8; i++) {
@@ -153,14 +159,15 @@
packet1 = bin_to_dec(binary1);
packet2 = bin_to_dec(binary2);
}
-
+
void send_packet() {
hrmCounter = packet_queue[current_index];
hrService->updateHeartRate(hrmCounter);
- printf("sent packet %i\n", hrmCounter);
+ printf("sent packet %i\r\n", hrmCounter);
::current_index++;
}
-
+
+// steps are added to a queue to be sent as soon as the gateway connects
void enqueue_packet(int direction, int time, int packet_queue[], int priority) {
int encoding [16] = {0};
encode_bin(direction, time, encoding);
@@ -173,8 +180,8 @@
packet_queue[0] = p1;
packet_queue[1] = p2;
- printf("sudo enqueued packet %i\n", packet_queue[0]);
- printf("sudo enqueued packet %i\n", packet_queue[1]);
+ printf("sudo enqueued packet %i\r\n", packet_queue[0]);
+ printf("sudo enqueued packet %i\r\n", packet_queue[1]);
} else {
if (packet_queue_index == 4) {
::packet_queue_index = 12;
@@ -182,24 +189,28 @@
packet_queue[packet_queue_index] = p1;
packet_queue[packet_queue_index + 1] = p2;
- printf("enqueued packet %i\n", packet_queue[packet_queue_index]);
- printf("enqueued packet %i\n", packet_queue[packet_queue_index+1]);
+ printf("enqueued packet %i\r\n", packet_queue[packet_queue_index]);
+ printf("enqueued packet %i\r\n", packet_queue[packet_queue_index+1]);
::packet_queue_index += 2;
}
}
-
+
+// the end of the queue transmission is signalled so gateway users know
+// when it is safe to disconnect
void enqueue_transmission_break() {
int p1 = 0;
int p2 = 0;
packet_queue[packet_queue_index] = p1;
packet_queue[packet_queue_index + 1] = p2;
- printf("enqueued transmission breaks %i and %i\n", packet_queue[packet_queue_index], packet_queue[packet_queue_index+1]);
+ printf("enqueued transmission breaks %i and %i\r\n", packet_queue[packet_queue_index], packet_queue[packet_queue_index+1]);
::packet_queue_index += 2;
}
-
+
+// adapted from code found at https://os.mbed.com/users/jkjk010695/code/Multi_VL53L0X/
+// checks sensor readings to determine whether a step has been taken
void check_for_step(int direction, int status, int dist, DigitalOut led, bool& this_countdown_triggered,
bool& other_countdown_triggered, bool& this_range_triggered, bool& other_range_triggered,
int packet_queue[]) {
@@ -212,7 +223,7 @@
this_countdown_triggered = true;
::countdown = cw;
} else if (other_countdown_triggered && !this_range_triggered) {
- printf("STEP %i DETECTED\n", direction);
+ printf("STEP %i DETECTED\r\n", direction);
enqueue_packet(direction, current_time, packet_queue, 0);
other_countdown_triggered = false;
}
@@ -223,12 +234,13 @@
}
} else {
led = 1;
- other_range_triggered = false;
+ this_range_triggered = false;
}
}
-
+
+// when connected to gateway, the HEAD and TAIL are added to the queue
void connectionCallback(const Gap::ConnectionCallbackParams_t *) {
- printf("Bluetooth connected at %i\n", current_time);
+ printf("Bluetooth connected at %i\r\n", current_time);
::master_connected = true;
::connection_triggered = false;
enqueue_packet(-1, current_time, packet_queue, 1);
@@ -237,9 +249,11 @@
wait(5);
}
+// when disconnected from gateway, all variables reset to the initial state
+// as though system had restarted
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
{
- printf("Bluetooth disconnected at %i\n", current_time);
+ printf("Bluetooth disconnected at %i\r\n", current_time);
::master_connected = false;
::timestamp = time_cycle;
::current_index = 0;
@@ -248,7 +262,8 @@
::triple_send_count = 0;
BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
}
-
+
+// standard BLE init code found at https://os.mbed.com/teams/Bluetooth-Low-Energy/code/BLE_HeartRate/
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE &ble = params->ble;
@@ -272,6 +287,8 @@
ble.gap().startAdvertising();
}
+// called every 300ms to check whether a step has been taken and whether there
+// is anything to be transmitted
void wakeup_event_cb() {
::timestamp++;
::current_time = timestamp / time_cycle;
@@ -305,6 +322,8 @@
range2_triggered, range1_triggered, packet_queue);
}
+// system put to sleep by waitForEvent() call and periodically interrupted
+// by Ticker to check for steps and packets to send
int main(void)
{
range1.init_sensor(range1_addr);