still locks up

This commit is contained in:
sessionm21 2020-05-16 07:12:03 +01:00
parent a33b6c39eb
commit 47040057df
4 changed files with 181 additions and 103 deletions

View File

@ -19,15 +19,13 @@ CFLAGS?=-Datmega328p\
-DARDUINO_ARCH_AVR\ -DARDUINO_ARCH_AVR\
-DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\ -DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\
-Os\ -Os\
-ffunction-sections\
-fdata-sections\ -fdata-sections\
-MMD\ -MMD\
-flto\ -flto\
-fno-fat-lto-objects\ -fno-fat-lto-objects\
-fuse-linker-plugin\ -fuse-linker-plugin\
-Wall\ -Wall\
# -finline-functions\ -ffunction-sections\
# -fipa-sra\
@ -42,15 +40,13 @@ CXXFLAGS?=-Datmega328p\
-DARDUINO_ARCH_AVR\ -DARDUINO_ARCH_AVR\
-DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\ -DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\
-Os\ -Os\
-ffunction-sections\
-fdata-sections\ -fdata-sections\
-MMD\ -MMD\
-flto\ -flto\
-fno-fat-lto-objects\ -fno-fat-lto-objects\
-fuse-linker-plugin\ -fuse-linker-plugin\
-Wall\ -Wall\
# -finline-functions\ -ffunction-sections\
# -fipa-sra\
INC_DIRS?=-I./\ INC_DIRS?=-I./\
@ -138,6 +134,7 @@ $(OUT)eep.hex: $(OUT)$(NAME).elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(OUT)$(NAME).elf $(OUT)eep.hex $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(OUT)$(NAME).elf $(OUT)eep.hex
$(OUT)$(NAME).elf: protobufg bin $(OBJDIRS) $(OBJS) $(OUT)$(NAME).elf: protobufg bin $(OBJDIRS) $(OBJS)
$(CC) $(CXXFLAGS) -o $(OUT)$(NAME).elf collar.cpp $(INC_DIRS) $(OBJS) $(CC) $(CXXFLAGS) -o $(OUT)$(NAME).elf collar.cpp $(INC_DIRS) $(OBJS)
avr-size --mcu=atmega328p --format=avr $(OUT)$(NAME).elf
test: test:
g++ -o $(OUT)test test.c $(INC_DIRS) $(SRC_FILES_COMMON) g++ -o $(OUT)test test.c $(INC_DIRS) $(SRC_FILES_COMMON)
@ -156,6 +153,10 @@ flash: $(OUT)$(NAME)
run: flash run: flash
screen /dev/ttyUSB0 screen /dev/ttyUSB0
frun:
avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:$(OUT)$(NAME).hex:i
screen /dev/ttyUSB0
start: flash start: flash
systemctl start lora-gateway-bridge loraserver systemctl start lora-gateway-bridge loraserver

View File

@ -10,25 +10,22 @@ static const PROGMEM u1_t NWKSKEY[16] = { 0x52, 0x92, 0xC0, 0x72, 0x2D, 0x3C, 0x
// LoRaWAN AppSKey, application session key // LoRaWAN AppSKey, application session key
static const u1_t PROGMEM APPSKEY[16] = { 0xC4, 0x30, 0xEF, 0x56, 0x4F, 0x6D, 0xA2, 0x56, 0x1F, 0x15, 0x2F, 0xB8, 0x62, 0xC7, 0xCA, 0xC2 }; static const u1_t PROGMEM APPSKEY[16] = { 0xC4, 0x30, 0xEF, 0x56, 0x4F, 0x6D, 0xA2, 0x56, 0x1F, 0x15, 0x2F, 0xB8, 0x62, 0xC7, 0xCA, 0xC2 };
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
static osjob_t sendjob;
// Chirpstack keys // Chirpstack keys
// static const u1_t PROGMEM NWKSKEY[16] = {0x5b,0xe6,0x8b,0xb7,0xaa,0x4f,0x01,0x85,0x54,0x72,0xd9,0x6f,0xd8,0xba,0xbc,0x99}; // static const u1_t PROGMEM NWKSKEY[16] = {0x5b,0xe6,0x8b,0xb7,0xaa,0x4f,0x01,0x85,0x54,0x72,0xd9,0x6f,0xd8,0xba,0xbc,0x99};
// static const u1_t PROGMEM APPSKEY[16] = {0xee,0x9a,0x94,0x96,0x9d,0x59,0xfb,0xc2,0x7a,0xe6,0x07,0xe1,0x6e,0x04,0x37,0x5b}; // static const u1_t PROGMEM APPSKEY[16] = {0xee,0x9a,0x94,0x96,0x9d,0x59,0xfb,0xc2,0x7a,0xe6,0x07,0xe1,0x6e,0x04,0x37,0x5b};
// static const u4_t DEVADDR = 0x005d96f5; // static const u4_t DEVADDR = 0x005d96f5;
// LoRaWAN end-device address (DevAddr)
// See http://thethingsnetwork.org/wiki/AddressSpace
// The library converts the address to network byte order as needed.
#ifndef COMPILE_REGRESSION_TEST
static const u4_t DEVADDR = 0x260212B6; static const u4_t DEVADDR = 0x260212B6;
#else
static const u4_t DEVADDR = 0;
#endif
// void printf(char *str) { // void printf(char *str) {
// Serial.println(str); // Serial.println(str);
// } // }
static osjob_t sendjob;
void debug_function(char *str) { void debug_function(char *str) {
Serial.println(str); Serial.println(str);
} }
@ -36,7 +33,7 @@ void debug_function(char *str) {
// Schedule TX every this many seconds (might become longer due to duty // Schedule TX every this many seconds (might become longer due to duty
// cycle limitations). // cycle limitations).
const unsigned TX_INTERVAL = 10; const unsigned TX_INTERVAL = 5;
// Pin mapping for Adafruit Feather M0 LoRa // Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = { const lmic_pinmap lmic_pins = {
@ -98,14 +95,14 @@ const int check_bounds(const float x, const float y) {
* - loading arrays in nanopb does not appear * - loading arrays in nanopb does not appear
* to work. * to work.
***************************************************/ ***************************************************/
void import_protobuf(const uint8_t *protobuffer, const uint32_t size) { static volatile uint8_t data_available = 0;
static volatile uint8_t done_sending = 0;
inline void import_protobuf(const uint8_t *protobuffer, const uint8_t size) {
if(size != 122) { if(size != 122) {
Serial.println("nmd"); Serial.println("nmd");
return; return;
} }
Serial.println("Recieved valid protobuf data?");
isr = protobuffer[1]; isr = protobuffer[1];
if(isr>N_POLY_MAX) isr = 0; if(isr>N_POLY_MAX) isr = 0;
@ -113,114 +110,63 @@ void import_protobuf(const uint8_t *protobuffer, const uint32_t size) {
for(uint8_t i=0;i<isr && i<N_POLY_MAX;i++) { for(uint8_t i=0;i<isr && i<N_POLY_MAX;i++) {
memcpy(&polyx[i], ptr + i*12, 4); memcpy(&polyx[i], ptr + i*12, 4);
memcpy(&polyy[i], ptr + i*12+5, 4); memcpy(&polyy[i], ptr + i*12+5, 4);
Serial.print((int)(polyx[i]*100));
Serial.print(' ');
Serial.print((int)(polyy[i]*100));
Serial.print('\n');
} }
n_poly = isr; n_poly = isr;
data_available = 1;
} }
void do_send(osjob_t* j);
static volatile uint8_t is_sending = 0;
void onEvent (ev_t ev) { void onEvent (ev_t ev) {
// Serial.print(os_getTime());
// Serial.print(": ");
if(ev == EV_TXCOMPLETE) { if(ev == EV_TXCOMPLETE) {
Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)"); // Serial.print(F("- EV_TXCOMPLETE"));
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) { if (LMIC.dataLen) {
Serial.println(F("Received ")); Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
if (LMIC.dataLen) {
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen); import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
} }
is_sending = 0; done_sending = 1;
} }
else if(ev == EV_TXSTART) { else if(ev == EV_TXSTART) {
Serial.println(F("EV_TXSTART")); // Serial.print(F("- EV_TXSTART"));
} }
else { else {
Serial.print(F("Unknown event: ")); Serial.print(F("- Unknown event: "));
} }
} }
#define TYPE_STRING 0x0A enum STATE_ {
#define TYPE_VARIANT 0x10 START,
#define PROTO_LEN 0x0A START_GPS,
#define FIELD_ONE_FLOAT 0x0D WAIT_GPS,
#define FIELD_TWO_FLOAT 0x15 START_LORA,
#define FIELD_TWO_VARIANT 0x10 WAIT_LORA,
END
const char oob[] = "OUT OF BOUNDS"; };
const char inb[] = "IN BOUNDS";
uint8_t buffer[] = {
TYPE_STRING,
PROTO_LEN,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
FIELD_TWO_VARIANT, 0, 0};
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
//Serial.println(F("OP_TXRXPEND, not sending"));
} else {
// prepare upstream data transmission at the next possible time.
// 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.
const float latitude = gps.location.lat();
const float longitude = gps.location.lng();
if(n_poly>0) {
uint8_t out_of_bounds = !check_bounds(latitude, longitude);
if(out_of_bounds) {
Serial.println(oob);
} else {
Serial.println(inb);
}
digitalWrite(LED_BUILTIN, out_of_bounds);
buffer[13] = out_of_bounds;
} else {
uint8_t out_of_bounds = 0;
buffer[13] = out_of_bounds;
}
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
LMIC_setTxData2(1, buffer, sizeof(buffer)-1, 0);
}
}
#define GPS_MAX_ENCODES 60
void read_gps(){
general_int = softserial_available();
while(--general_int > 0) {
gps.encode(softserial_read());
}
if(!is_sending && gps.location.isValid()) {
Serial.println("gps");
is_sending = 1;
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
}
}
volatile uint8_t state;
volatile uint32_t since;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println(F("Starting"));
softserial_init(); softserial_init();
delay(100); // softserial_init();
Serial.println(F("Starting")); // pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
// LMIC init
os_init(); os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset(); LMIC_reset();
// Set static session parameters. Instead of dynamically establishing a session
// by joining the network, precomputed session parameters are be provided.
// On AVR, these values are stored in flash and only copied to RAM
// once. Copy them to a temporary buffer here, LMIC_setSession will
// copy them into a buffer of its own again.
uint8_t appskey[sizeof(APPSKEY)]; uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)]; uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
@ -239,9 +185,140 @@ void setup() {
LMIC_setDrTxpow(DR_SF7,14); LMIC_setDrTxpow(DR_SF7,14);
delay(1000); delay(1000);
data_available = 0;
state = START_GPS;
do_send(&sendjob);
} }
void loop() { inline void read_gps() {
os_runloop_once(); uint8_t available = softserial_available();
read_gps(); while(available> 0) {
gps.encode(softserial_read());
available--;
}
}
inline void on_start_gps() {
Serial.println("Starting gps");
state = WAIT_GPS;
}
inline void on_wait_gps() {
Serial.println("Waiting for gps");
if(gps.location.isValid()) {
state = START_LORA;
Serial.println("end of gps");
}
}
#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
const char oob[] = "OUT OF BOUNDS";
const char inb[] = "IN BOUNDS";
uint8_t buffer[] = {
TYPE_STRING,
PROTO_LEN,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
FIELD_TWO_VARIANT, 0, 0};
void on_start_lora() {
// Serial.println("Starting lora");
}
void send_lora() {
if (LMIC.opmode & OP_TXRXPEND) {
Serial.print(" -cns");
done_sending = 1;
} else {
const float latitude = gps.location.lat();
const float longitude = gps.location.lng();
if(n_poly>0) {
const uint8_t out_of_bounds = 0;//!check_bounds(latitude, longitude);
if(out_of_bounds) {
Serial.println(oob);
} else {
Serial.println(inb);
}
// digitalWrite(LED_BUILTIN, out_of_bounds);
buffer[13] = out_of_bounds;
} else {
const uint8_t out_of_bounds = 0;
buffer[13] = out_of_bounds;
}
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
LMIC_setTxData2(1, buffer, sizeof(buffer)-1, 0);
}
since = millis();
state = WAIT_LORA;
}
void do_send(osjob_t* j){
send_lora();
}
void on_wait_lora() {
if(millis()- since > TX_INTERVAL*1000UL || done_sending) {
Serial.println(" -Lora Done Sending");
done_sending = 0;
state = START_GPS;
}
}
#define LOOP_LATENCY_MS 200L
uint32_t time_last = 0;
void loop() {
uint32_t time = millis();
if((time - time_last) > LOOP_LATENCY_MS) {
time_last = time;
Serial.print(state);
Serial.print('.');
if(state == START_GPS) {
// softserial_init();
on_start_gps();
}
else if(state == WAIT_GPS) {
on_wait_gps();
}
else if(state == START_LORA) {
// softserial_end();
on_start_lora();
}
else if(state == WAIT_LORA) {
on_wait_lora();
}
} else {
// Serial.print('.');
}
if(data_available) {
Serial.println("Data available");
for(uint8_t i=0;i<n_poly;i++) {
Serial.print('(');
Serial.print((int)(polyy[i]*100));
Serial.print(',');
Serial.print((int)(polyx[i]*100));
Serial.print(')');
Serial.print(',');
}
data_available = 0;
}
if(state == WAIT_GPS)
read_gps();
os_runloop_once();
// Serial.print(time);
// Serial.print(':');
// Serial.println(state);
// delay(100);
} }

View File

@ -12,8 +12,7 @@
#define DISABLE_PING #define DISABLE_PING
#define DISABLE_BEACONS #define DISABLE_BEACONS
// #define DISABLE_LMIC_FAILURE_TO // #define DISABLE_LMIC_FAILURE_TO
#define USE_IDEETRON_AES // #define USE_IDEETRON_AES
#ifndef LORA_DEBUG_F #ifndef LORA_DEBUG_F
#define LORA_DEBUG_F #define LORA_DEBUG_F

View File

@ -36,6 +36,7 @@
const int softserial_available(); const int softserial_available();
const int softserial_read(); const int softserial_read();
void softserial_end(void);
void softserial_flush(); void softserial_flush();
void softserial_init(); void softserial_init();