add out of bounds and comments
This commit is contained in:
parent
b84a0fea12
commit
c2d852a5e6
79
collar.cpp
79
collar.cpp
|
@ -37,13 +37,17 @@ uint8_t DevAddr[4] = {0x26, 0x02, 0x12, 0xB6};
|
||||||
|
|
||||||
/************************** Example Begins Here ***********************************/
|
/************************** Example Begins Here ***********************************/
|
||||||
// Data Packet to Send to TTN
|
// 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
|
// How many times data transfer should occur, in seconds
|
||||||
// const unsigned int sendInterval = 30;
|
|
||||||
const unsigned int sendInterval = 3000;
|
const unsigned int sendInterval = 3000;
|
||||||
|
|
||||||
// Pinout for Adafruit Feather 32u4 LoRa
|
/****************************************************
|
||||||
|
* Arduino drivers
|
||||||
|
* - LoRaWAN
|
||||||
|
* - GPS
|
||||||
|
* - Software Serial
|
||||||
|
***************************************************/
|
||||||
TinyLoRa lora = TinyLoRa(2, 10, 9);
|
TinyLoRa lora = TinyLoRa(2, 10, 9);
|
||||||
TinyGPSPlus gps;
|
TinyGPSPlus gps;
|
||||||
SoftwareSerial ss(6, 7);
|
SoftwareSerial ss(6, 7);
|
||||||
|
@ -81,7 +85,7 @@ void clear_verts() {
|
||||||
* - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
|
* - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
|
||||||
***************************************************/
|
***************************************************/
|
||||||
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
|
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;
|
int i, j, c = 0;
|
||||||
for (i = 0, j = nvert-1; i < nvert; j = i++) {
|
for (i = 0, j = nvert-1; i < nvert; j = i++) {
|
||||||
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
|
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);
|
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
|
* Initialize values
|
||||||
***************************************************/
|
***************************************************/
|
||||||
|
@ -184,19 +174,6 @@ void setup()
|
||||||
***************************************************/
|
***************************************************/
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
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.28341);
|
||||||
push_vert(44.55818, -123.28332);
|
push_vert(44.55818, -123.28332);
|
||||||
push_vert(44.558308, -123.28332);
|
push_vert(44.558308, -123.28332);
|
||||||
|
@ -222,8 +199,6 @@ void setup()
|
||||||
Serial.println("OK");
|
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
|
* Read a byte from GPS over software serial
|
||||||
***************************************************/
|
***************************************************/
|
||||||
|
@ -238,6 +213,9 @@ int read_gps() {
|
||||||
/****************************************************
|
/****************************************************
|
||||||
* Set cursor to beginning of line and clear it
|
* 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() {
|
void clear_line() {
|
||||||
Serial.write(START_OF_LINE);
|
Serial.write(START_OF_LINE);
|
||||||
for(int i=0;i<PROGRESS_BAR_COUNT;i++)
|
for(int i=0;i<PROGRESS_BAR_COUNT;i++)
|
||||||
|
@ -272,7 +250,9 @@ void loop()
|
||||||
}
|
}
|
||||||
else if(state == WAITING_GPS) {
|
else if(state == WAITING_GPS) {
|
||||||
int got_data = read_gps();
|
int got_data = read_gps();
|
||||||
|
/****************************************************
|
||||||
|
* loading bar animation
|
||||||
|
***************************************************/
|
||||||
if(got_data) {
|
if(got_data) {
|
||||||
if(loopCounter%100==0)
|
if(loopCounter%100==0)
|
||||||
Serial.write('.');
|
Serial.write('.');
|
||||||
|
@ -286,12 +266,29 @@ void loop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(state == VERIFYING_GPS) {
|
else if(state == VERIFYING_GPS) {
|
||||||
|
/****************************************************
|
||||||
|
* if no data has been received from the gps in 5 seconds
|
||||||
|
* then the GPS is probably not connected properly
|
||||||
|
***************************************************/
|
||||||
|
if (millis() > 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())
|
if(gps.location.isValid())
|
||||||
state = SENDING_LORA;
|
state = SENDING_LORA;
|
||||||
else
|
else
|
||||||
state = WAITING_GPS;
|
state = WAITING_GPS;
|
||||||
}
|
}
|
||||||
else if(state == SENDING_LORA) {
|
else if(state == SENDING_LORA) {
|
||||||
|
/****************************************************
|
||||||
|
* encode device information into a buffer using
|
||||||
|
* protobuf
|
||||||
|
***************************************************/
|
||||||
Fenceless_CollarResponse coord;
|
Fenceless_CollarResponse coord;
|
||||||
coord.loc.x = gps.location.lat();
|
coord.loc.x = gps.location.lat();
|
||||||
coord.loc.y = gps.location.lng();
|
coord.loc.y = gps.location.lng();
|
||||||
|
@ -301,18 +298,32 @@ void loop()
|
||||||
stream = pb_ostream_from_buffer(loraData, sizeof(loraData));
|
stream = pb_ostream_from_buffer(loraData, sizeof(loraData));
|
||||||
pb_encode(&stream, Fenceless_CollarResponse_fields, &coord);
|
pb_encode(&stream, Fenceless_CollarResponse_fields, &coord);
|
||||||
|
|
||||||
|
/****************************************************
|
||||||
|
* send encoded buffer over LoRaWAN
|
||||||
|
***************************************************/
|
||||||
Serial.println("Sending LoRa Data...");
|
Serial.println("Sending LoRa Data...");
|
||||||
lora.sendData(loraData, stream.bytes_written, lora.frameCounter);
|
lora.sendData(loraData, stream.bytes_written, lora.frameCounter);
|
||||||
|
|
||||||
Serial.print("Frame Counter: ");
|
Serial.print("Frame Counter: ");
|
||||||
Serial.println(lora.frameCounter);
|
Serial.println(lora.frameCounter);
|
||||||
|
|
||||||
|
/****************************************************
|
||||||
|
* set reference time for LoRaWAN transmission delay
|
||||||
|
***************************************************/
|
||||||
startTime = millis();
|
startTime = millis();
|
||||||
|
|
||||||
state = WAITING_LORA;
|
state = WAITING_LORA;
|
||||||
}
|
}
|
||||||
else if(state == WAITING_LORA) {
|
else if(state == WAITING_LORA) {
|
||||||
|
/****************************************************
|
||||||
|
* don't block the GPS from reading here
|
||||||
|
***************************************************/
|
||||||
read_gps();
|
read_gps();
|
||||||
|
|
||||||
|
/****************************************************
|
||||||
|
* if enough seconds have been delayed then move to
|
||||||
|
* next state
|
||||||
|
***************************************************/
|
||||||
if(millis()/1000 - startTime >= sendInterval) {
|
if(millis()/1000 - startTime >= sendInterval) {
|
||||||
state = LORA_DONE;
|
state = LORA_DONE;
|
||||||
}
|
}
|
||||||
|
|
2
gateway
2
gateway
|
@ -1 +1 @@
|
||||||
Subproject commit d39b4e8494780050fe9875ee2f97a40122e6e9ac
|
Subproject commit cf51b0da696ff3c838a504a547dc9a287a54afb4
|
|
@ -49,6 +49,7 @@ int main() {
|
||||||
Fenceless_CollarResponse collar = Fenceless_CollarResponse_init_zero;
|
Fenceless_CollarResponse collar = Fenceless_CollarResponse_init_zero;
|
||||||
collar.loc.x = 200;
|
collar.loc.x = 200;
|
||||||
collar.loc.y = 100;
|
collar.loc.y = 100;
|
||||||
|
collar.oob = 1;
|
||||||
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
err = pb_encode(&stream, Fenceless_CollarResponse_fields, &collar);
|
err = pb_encode(&stream, Fenceless_CollarResponse_fields, &collar);
|
||||||
|
@ -63,6 +64,7 @@ int main() {
|
||||||
// check result
|
// check result
|
||||||
assert(collar0.loc.x == collar.loc.x);
|
assert(collar0.loc.x == collar.loc.x);
|
||||||
assert(collar0.loc.y == collar.loc.y);
|
assert(collar0.loc.y == collar.loc.y);
|
||||||
|
assert(collar0.oob == collar.oob);
|
||||||
|
|
||||||
// encode 10 coordinates
|
// encode 10 coordinates
|
||||||
Fenceless_Coordinates coords;
|
Fenceless_Coordinates coords;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user