From 47040057dff1c84b86a7fb8af79c83c3fa72ab36 Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Sat, 16 May 2020 07:12:03 +0100 Subject: [PATCH] still locks up --- Makefile | 13 +- collar.cpp | 267 +++++++++++++++--------- fenceless_lmic_config.h | 3 +- libraries/AltSoftSerial/AltSoftSerial.h | 1 + 4 files changed, 181 insertions(+), 103 deletions(-) diff --git a/Makefile b/Makefile index 5583865..ea5cf28 100644 --- a/Makefile +++ b/Makefile @@ -19,15 +19,13 @@ CFLAGS?=-Datmega328p\ -DARDUINO_ARCH_AVR\ -DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\ -Os\ - -ffunction-sections\ -fdata-sections\ -MMD\ -flto\ -fno-fat-lto-objects\ -fuse-linker-plugin\ -Wall\ - # -finline-functions\ - # -fipa-sra\ + -ffunction-sections\ @@ -42,15 +40,13 @@ CXXFLAGS?=-Datmega328p\ -DARDUINO_ARCH_AVR\ -DARDUINO_LMIC_PROJECT_CONFIG_H=fenceless_lmic_config.h\ -Os\ - -ffunction-sections\ -fdata-sections\ -MMD\ -flto\ -fno-fat-lto-objects\ -fuse-linker-plugin\ -Wall\ - # -finline-functions\ - # -fipa-sra\ + -ffunction-sections\ 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 $(OUT)$(NAME).elf: protobufg bin $(OBJDIRS) $(OBJS) $(CC) $(CXXFLAGS) -o $(OUT)$(NAME).elf collar.cpp $(INC_DIRS) $(OBJS) + avr-size --mcu=atmega328p --format=avr $(OUT)$(NAME).elf test: g++ -o $(OUT)test test.c $(INC_DIRS) $(SRC_FILES_COMMON) @@ -156,6 +153,10 @@ flash: $(OUT)$(NAME) run: flash screen /dev/ttyUSB0 +frun: + avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:$(OUT)$(NAME).hex:i + screen /dev/ttyUSB0 + start: flash systemctl start lora-gateway-bridge loraserver diff --git a/collar.cpp b/collar.cpp index 362a7d1..8b18b54 100644 --- a/collar.cpp +++ b/collar.cpp @@ -10,25 +10,22 @@ static const PROGMEM u1_t NWKSKEY[16] = { 0x52, 0x92, 0xC0, 0x72, 0x2D, 0x3C, 0x // 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 }; +void os_getArtEui (u1_t* buf) { } +void os_getDevEui (u1_t* buf) { } +void os_getDevKey (u1_t* buf) { } + +static osjob_t sendjob; // 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 APPSKEY[16] = {0xee,0x9a,0x94,0x96,0x9d,0x59,0xfb,0xc2,0x7a,0xe6,0x07,0xe1,0x6e,0x04,0x37,0x5b}; // 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; -#else -static const u4_t DEVADDR = 0; -#endif // void printf(char *str) { // Serial.println(str); // } -static osjob_t sendjob; void debug_function(char *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 // cycle limitations). -const unsigned TX_INTERVAL = 10; +const unsigned TX_INTERVAL = 5; // Pin mapping for Adafruit Feather M0 LoRa 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 * 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) { Serial.println("nmd"); return; } - Serial.println("Recieved valid protobuf data?"); - isr = protobuffer[1]; 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;i0) { - 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); - } -} +enum STATE_ { + START, + START_GPS, + WAIT_GPS, + START_LORA, + WAIT_LORA, + END +}; +volatile uint8_t state; +volatile uint32_t since; void setup() { Serial.begin(9600); - softserial_init(); - delay(100); Serial.println(F("Starting")); + softserial_init(); - pinMode(LED_BUILTIN, OUTPUT); + // softserial_init(); + // pinMode(LED_BUILTIN, OUTPUT); - // LMIC init os_init(); - // Reset the MAC state. Session and pending data transfers will be discarded. 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 nwkskey[sizeof(NWKSKEY)]; memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); @@ -239,9 +185,140 @@ void setup() { LMIC_setDrTxpow(DR_SF7,14); delay(1000); + + data_available = 0; + state = START_GPS; + + + do_send(&sendjob); } -void loop() { - os_runloop_once(); - read_gps(); +inline void read_gps() { + uint8_t available = softserial_available(); + 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