optimized

This commit is contained in:
matt
2020-05-15 05:30:37 -07:00
parent 1b11b44336
commit 0adeb9a7b1
15 changed files with 983 additions and 342 deletions

View File

@@ -21,39 +21,15 @@
*******************************************************************************/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "Base64.h"
#include "gateway/message.pb.h"
#include <AltSoftSerial.h>
#include "pb_common.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include "gateway/message.pb.h"
// DHT digital pin and sensor type
#define DHTPIN 60
#define DHTTYPE DHT22
//
// For normal use, we require that you edit the sketch to replace FILLMEIN
// with values assigned by the TTN console. However, for regression tests,
// we want to be able to compile these scripts. The regression tests define
// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non-
// working but innocuous value.
//
/*
#ifdef COMPILE_REGRESSION_TEST
# define FILLMEIN 0
#else
# warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!"
# define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN)
#endif
*/
// LoRaWAN NwkSKey, network session key
static const PROGMEM u1_t NWKSKEY[16] = { 0x52, 0x92, 0xC0, 0x72, 0x2D, 0x3C, 0x55, 0x5E, 0xE4, 0xB9, 0x9E, 0x9B, 0x88, 0x66, 0x47, 0xF1 };
@@ -77,16 +53,13 @@ void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
// payload to send to TTN gateway
//static uint8_t payload[] = "Hello, world!";
// Data Packet to Send to TTN
u1_t loraData[Fenceless_CollarResponse_size+1] = {0};
static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 10;
const unsigned GPS_INTERVAL = 1;
// Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = {
@@ -103,10 +76,8 @@ const lmic_pinmap lmic_pins = {
* Arduino drivers
* - LoRaWAN
* - GPS
* - Software Serial
***************************************************/
TinyGPSPlus gps;
// SoftwareSerial ss(6, 7);
/****************************************************
* Track each pair of X and Y coordinates
@@ -121,7 +92,7 @@ int n_poly=0;
* Add a coordinate to the arrays
* - stores a total of N_POLY_MAX pairs
***************************************************/
int push_vert(float x, float y) {
const int push_vert(const float x, const float y) {
if(n_poly>N_POLY_MAX)
return 0;
polyx[n_poly]=x;
@@ -129,6 +100,7 @@ int push_vert(float x, float y) {
n_poly++;
return 1;
}
/****************************************************
* 'Clear' pairs of coordinates
***************************************************/
@@ -141,8 +113,8 @@ void clear_verts() {
* of vertices
* - https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
***************************************************/
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
const int pnpoly(int nvert, const float *vertx, const float *verty, const float testx, const float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
@@ -151,221 +123,67 @@ int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
}
return c;
}
/****************************************************
* Test a coordinate against all vertices
* - takes current GPS coordinates
* - return 1 if in bounds
***************************************************/
int check_bounds(float x, float y) {
const int check_bounds(const float x, const float y) {
return pnpoly(n_poly, polyx, polyy, x, y);
}
/****************************************************
* Load coordinates from protobuff stream
* - currently a maximum of 10 coordinates
* - loading arrays in nanopb does not appear
* to work.
***************************************************/
void import_protobuf(uint8_t *protobuffer, uint32_t size) {
#define TYPE_STRING 0x0A
#define TYPE_VARIANT 0x10
#define PROTO_LEN 0x0A
#define FIELD_ONE_FLOAT 0x0D
#define FIELD_TWO_FLOAT 0x15
#define FIELD_TWO_VARIANT 0x10
#define FIELD_ONE_VARIANT 0x08
#define FIELD_TWO_STRING 0x12
#define FIELD_THREE_STRING 0x1A
#define FIELD_FOUR_STRING 0x22
#define FIELD_FIVE_STRING 0x2A
#define FIELD_SIX_STRING 0x32
#define FIELD_SEVEN_STRING 0x3A
#define FIELD_EIGHT_STRING 0x42
#define FIELD_NINE_STRING 0x4A
#define FIELD_TEN_STRING 0x52
#define FIELD_ELEVEN_STRING 0x5A
/*uint8_t buffer0[] {
FIELD_ONE_VARIANT, 0x01,
FIELD_TWO_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x1B, 0x91, 0xF6, 0xC2,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_THREE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0xB5, 0x3B, 0x32, 0x42,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_FOUR_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_FIVE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_SIX_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_SEVEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_EIGHT_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_NINE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_TEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_ELEVEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40
};*/
Serial.println("DECODE FUNCTION");
void import_protobuf(const uint8_t *protobuffer, const uint32_t size) {
if(size != 122) {
Serial.println("Failed to decode");
Serial.print("Size:");
Serial.println(size);
return;
}
/*
* this stuff does not work yet
*/
Serial.println("Recieved valid protobuf data?");
uint32_t isr;
isr = 0;
isr = protobuffer[1];
Serial.print("Isr: ");
Serial.println(isr);
if(isr>N_POLY_MAX) isr = N_POLY_MAX;
Serial.println("Recieved valid protobuf data?");
clear_verts();
uint8_t *ptr = protobuffer+5;
const uint8_t *ptr = protobuffer + 5;
for(uint32_t i=0;i<isr;i++) {
if(i%5==0)
Serial.println();
float x,y;
memcpy(&x, ptr + i*12, 4);
memcpy(&y, ptr + i*12+5, 4);
Serial.print('(');
Serial.print(x);
Serial.print(',');
Serial.print(y);
Serial.print(')');
Serial.print(' ');
push_vert(x, y);
}
}
void do_send(osjob_t* j);
uint8_t is_sending = 0;
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_RFU1:
|| Serial.println(F("EV_RFU1"));
|| break;
*/
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
// Serial.print(os_getTime());
// Serial.print(": ");
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
if(ev == EV_TXCOMPLETE) {
Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)");
// Serial.println(F(" bytes of payload"));
// for(int i=0;i<LMIC.dataLen;i++) {
// Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX);
// Serial.print(' ');
// Serial.print('-');
// Serial.print(' ');
// if(i%10==0)
// Serial.println();
// }
// Serial.println();
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
}
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_SCAN_FOUND:
|| Serial.println(F("EV_SCAN_FOUND"));
|| break;
*/
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
if (LMIC.dataLen) {
Serial.println(F("Received "));
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
}
is_sending = 0;
}
else if(ev == EV_TXSTART) {
Serial.println(F("EV_TXSTART"));
}
else {
Serial.print(F("Unknown event: "));
}
}
@@ -376,16 +194,16 @@ void onEvent (ev_t ev) {
#define FIELD_TWO_FLOAT 0x15
#define FIELD_TWO_VARIANT 0x10
uint8_t buffer[] = {
TYPE_STRING,
PROTO_LEN,
TYPE_STRING,
PROTO_LEN,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
FIELD_TWO_VARIANT, 0};
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
is_sending = 1;
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
@@ -393,9 +211,9 @@ void do_send(osjob_t* j){
// transmit on port 1 (the first parameter); you can use any value from 1 to 223 (others are reserved).
// don't request an ack (the last parameter, if not zero, requests an ack from the network).
// Remember, acks consume a lot of network resources; don't ask for an ack unless you really need it.
float latitude = gps.location.lat();
float longitude = gps.location.lng();
int oob = check_bounds(latitude, longitude);
const float latitude = gps.location.lat();
const float longitude = gps.location.lng();
const int oob = check_bounds(latitude, longitude);
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
@@ -403,19 +221,25 @@ void do_send(osjob_t* j){
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);
}
// Next TX is scheduled after TX_COMPLETE event.
}
void do_gps(osjob_t* j){
while(softserial_available()>0) {
gps.encode(softserial_read());
}
if(!is_sending && gps.location.isValid()) {
do_send(&sendjob);
}
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps);
}
void setup() {
delay(1000);
Serial.begin(4800);
// ss.begin(4800);
softserial_init();
delay(100);
Serial.println(F("Starting"));
pinMode(LED_BUILTIN, OUTPUT);
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
@@ -430,18 +254,8 @@ void setup() {
uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession (0x13, DEVADDR, nwkskey, appskey);
/*
// We'll disable all 72 channels used by TTN
for (int c = 0; c < 72; c++){
LMIC_disableChannel(c);
}
// We'll only enable Channel 16 (905.5Mhz) since we're transmitting on a single-channel
LMIC_enableChannel(16);
*/
LMIC_selectSubBand(1);
// Disable link check validation
@@ -453,109 +267,9 @@ void setup() {
// Set data rate and transmit power for uplink
LMIC_setDrTxpow(DR_SF7,14);
delay(2000);
// Start job
// do_send(&sendjob);
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(GPS_INTERVAL), do_gps);
}
/****************************************************
* Read a byte from GPS over software serial
***************************************************/
int read_gps() {
int ret = 0;
// int timeout = 0;
while(Serial.available()>0) {// || timeout < 20) {
gps.encode(Serial.read());
ret = 1;
//timeout++;
}
return ret;
}
/****************************************************
* Set cursor to beginning of line and clear it
***************************************************/
const int16_t PROGRESS_BAR_COUNT = 50;
const int16_t START_OF_LINE = 13;
int led_on = 0;
void clear_line() {
Serial.write(START_OF_LINE);
for(int i=0;i<PROGRESS_BAR_COUNT;i++)
Serial.write(' ');
Serial.write(START_OF_LINE);
digitalWrite(LED_BUILTIN, led_on);
led_on = !led_on;
}
/****************************************************
* State variables
* - track events of main loop
***************************************************/
enum STATE_ {
START_GPS,
WAITING_GPS,
VERIFYING_GPS,
SENDING_LORA,
WAITING_LORA,
LORA_DONE
};
int state = START_GPS;
int loopCounter = 0;
int startTime = 0;
uint32_t got_data = 0;
void loop() {
switch(state) {
case START_GPS:
Serial.println("Waiting for GPS");
state = WAITING_GPS;
break;
case WAITING_GPS:
got_data =
read_gps();
/****************************************************
* loading bar animation
***************************************************/
if(got_data) {
if(loopCounter%100==0)
Serial.write('.');
if(loopCounter>PROGRESS_BAR_COUNT*100) {
clear_line();
loopCounter=0;
state = VERIFYING_GPS;
}
loopCounter++;
}
break;
case 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())
state = SENDING_LORA;
else
state = WAITING_GPS;
break;
case SENDING_LORA:
do_send(&sendjob);
digitalWrite(LED_BUILTIN, 0);
state = LORA_DONE;
break;
default:
break;
}
os_runloop_once();
os_runloop_once();
}