From c2d852a5e6aadc68f415a3226c3875c4054228cd Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Wed, 13 May 2020 06:37:12 +0100 Subject: [PATCH] add out of bounds and comments --- collar.cpp | 81 +++++++++++++++++++++++++++++---------------------- gateway | 2 +- prototest.cpp | 2 ++ 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/collar.cpp b/collar.cpp index c70ad61..5db8d1a 100644 --- a/collar.cpp +++ b/collar.cpp @@ -37,13 +37,17 @@ uint8_t DevAddr[4] = {0x26, 0x02, 0x12, 0xB6}; /************************** Example Begins Here ***********************************/ // Data Packet to Send to TTN -unsigned char loraData[50] = {"hello LoRa"}; +unsigned char loraData[Fenceless_CollarResponse_size+1] = {0}; // How many times data transfer should occur, in seconds -// const unsigned int sendInterval = 30; const unsigned int sendInterval = 3000; -// Pinout for Adafruit Feather 32u4 LoRa +/**************************************************** + * Arduino drivers + * - LoRaWAN + * - GPS + * - Software Serial + ***************************************************/ TinyLoRa lora = TinyLoRa(2, 10, 9); TinyGPSPlus gps; SoftwareSerial ss(6, 7); @@ -81,7 +85,7 @@ void clear_verts() { * - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html ***************************************************/ int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) -{ //from stack overflow, check that coordinate is within polygon boundary +{ int i, j, c = 0; for (i = 0, j = nvert-1; i < nvert; j = i++) { if ( ((verty[i]>testy) != (verty[j]>testy)) && @@ -146,20 +150,6 @@ void import_protobuf(uint8_t *protobuffer, uint32_t size) { push_vert(m.coord0.x, m.coord0.y); } } -/**************************************************** - * live test of protobuf - ***************************************************/ -void test() { - Serial.println("Testing protobuf"); - uint8_t buffer[100] = {0}; - Fenceless_Coordinates m = Fenceless_Coordinates_init_zero; - pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - int status = pb_encode(&stream, Fenceless_Coordinates_fields, &m); - if(!status){ - Serial.println("Failed to encode"); - } - import_protobuf(buffer, sizeof(buffer)); -} /**************************************************** * Initialize values ***************************************************/ @@ -184,19 +174,6 @@ void setup() ***************************************************/ pinMode(LED_BUILTIN, OUTPUT); - // Initialize GPS - GPS does not work indoors - This will block until GPS available - // Serial.println("Starting GPS"); - // while(!gps.location.isValid()) { - // while(ss.available()>0) { - // gps.encode(ss.read()); - // } - // if (millis() > 5000 && gps.charsProcessed() < 10) - // { - // Serial.println(F("No GPS detected: check wiring.")); - // while(true); - // } - // } - push_vert(44.55818, -123.28341); push_vert(44.55818, -123.28332); push_vert(44.558308, -123.28332); @@ -222,8 +199,6 @@ void setup() Serial.println("OK"); } -const int16_t PROGRESS_BAR_COUNT = 50; -const int16_t START_OF_LINE = 13; /**************************************************** * Read a byte from GPS over software serial ***************************************************/ @@ -238,6 +213,9 @@ int read_gps() { /**************************************************** * Set cursor to beginning of line and clear it ***************************************************/ +const int16_t PROGRESS_BAR_COUNT = 50; +const int16_t START_OF_LINE = 13; + void clear_line() { Serial.write(START_OF_LINE); for(int i=0;i 5000 && gps.charsProcessed() < 10) + { + Serial.println(F("No GPS detected: check wiring.")); + while(true); + } + /**************************************************** + * only send to LoRaWAN if valid GPS coordinates are + * available + ***************************************************/ if(gps.location.isValid()) state = SENDING_LORA; else state = WAITING_GPS; } else if(state == SENDING_LORA) { + /**************************************************** + * encode device information into a buffer using + * protobuf + ***************************************************/ Fenceless_CollarResponse coord; coord.loc.x = gps.location.lat(); coord.loc.y = gps.location.lng(); @@ -301,18 +298,32 @@ void loop() stream = pb_ostream_from_buffer(loraData, sizeof(loraData)); pb_encode(&stream, Fenceless_CollarResponse_fields, &coord); + /**************************************************** + * send encoded buffer over LoRaWAN + ***************************************************/ Serial.println("Sending LoRa Data..."); lora.sendData(loraData, stream.bytes_written, lora.frameCounter); Serial.print("Frame Counter: "); Serial.println(lora.frameCounter); + + /**************************************************** + * set reference time for LoRaWAN transmission delay + ***************************************************/ startTime = millis(); state = WAITING_LORA; } else if(state == WAITING_LORA) { + /**************************************************** + * don't block the GPS from reading here + ***************************************************/ read_gps(); - + + /**************************************************** + * if enough seconds have been delayed then move to + * next state + ***************************************************/ if(millis()/1000 - startTime >= sendInterval) { state = LORA_DONE; } diff --git a/gateway b/gateway index d39b4e8..cf51b0d 160000 --- a/gateway +++ b/gateway @@ -1 +1 @@ -Subproject commit d39b4e8494780050fe9875ee2f97a40122e6e9ac +Subproject commit cf51b0da696ff3c838a504a547dc9a287a54afb4 diff --git a/prototest.cpp b/prototest.cpp index defdfda..52f50b6 100644 --- a/prototest.cpp +++ b/prototest.cpp @@ -49,6 +49,7 @@ int main() { Fenceless_CollarResponse collar = Fenceless_CollarResponse_init_zero; collar.loc.x = 200; collar.loc.y = 100; + collar.oob = 1; stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); err = pb_encode(&stream, Fenceless_CollarResponse_fields, &collar); @@ -63,6 +64,7 @@ int main() { // check result assert(collar0.loc.x == collar.loc.x); assert(collar0.loc.y == collar.loc.y); + assert(collar0.oob == collar.oob); // encode 10 coordinates Fenceless_Coordinates coords;