From b7782db478879d402368b05042c03bfade4aa9f1 Mon Sep 17 00:00:00 2001 From: "Pawel Spychalski (DzikuVx)" Date: Sun, 29 Oct 2017 13:08:39 +0100 Subject: [PATCH] Audible alarms on TX side --- crossbow.ino | 43 ++++++++++++++++++++++++++++++++++++++++++- qsp.cpp | 1 + txbuzzer.cpp | 5 ++--- txbuzzer.h | 4 ++-- variables.h | 8 ++++++-- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/crossbow.ino b/crossbow.ino index 0e0bc26..e727a38 100644 --- a/crossbow.ino +++ b/crossbow.ino @@ -178,7 +178,7 @@ void setup(void) pinMode(TX_BUZZER_PIN, OUTPUT); //Play single tune to indicate power up - buzzerSingleMode(BUZZER_MODE_CHIRP, TX_BUZZER_PIN, millis(), &buzzer); + buzzerSingleMode(BUZZER_MODE_CHIRP, &buzzer); #endif pinMode(LED_BUILTIN, OUTPUT); @@ -275,6 +275,7 @@ int8_t getFrameToTransmit(QspConfiguration_t *qsp) { void loop(void) { + #ifdef DEVICE_MODE_TX if (txDeviceState.readPacket) { int incomingByte = LoRa.read(); @@ -290,6 +291,7 @@ void loop(void) uint32_t currentMillis = millis(); bool transmitPayload = false; + static uint32_t previousAnyFrameReceivedAt = 0; /* * Watchdog for frame decoding stuck somewhere in the middle of a process @@ -401,6 +403,45 @@ void loop(void) buzzerProcess(TX_BUZZER_PIN, currentMillis, &buzzer); + // This routing enables when TX starts to receive signal from RX for a first time or after + // failsafe + if (txDeviceState.isReceiving == false && qsp.anyFrameRecivedAt != 0) { + //TX module started to receive data + buzzerSingleMode(BUZZER_MODE_DOUBLE_CHIRP, &buzzer); + txDeviceState.isReceiving = true; + qsp.deviceState = DEVICE_STATE_OK; + } + + //Here we detect failsafe state on TX module + if (txDeviceState.isReceiving && abs(currentMillis - qsp.anyFrameRecivedAt) > TX_FAILSAFE_DELAY) { + txDeviceState.isReceiving = false; + rxDeviceState.a1Voltage = 0; + rxDeviceState.a2Voltage = 0; + rxDeviceState.rxVoltage = 0; + rxDeviceState.rssi = 0; + rxDeviceState.snr = 0; + rxDeviceState.flags = 0; + qsp.deviceState = DEVICE_STATE_FAILSAFE; + qsp.anyFrameRecivedAt = 0; + } + + //FIXME rxDeviceState should be resetted also in RC_HEALT frame is not received in a long period + + //Handle audible alarms + if (qsp.deviceState == DEVICE_STATE_FAILSAFE) { + //Failsafe detected by TX + buzzerContinousMode(BUZZER_MODE_SLOW_BEEP, &buzzer); + } else if (txDeviceState.isReceiving && (rxDeviceState.flags & 0x1) == 1) { + //Failsafe reported by RX module + buzzerContinousMode(BUZZER_MODE_SLOW_BEEP, &buzzer); + } else if (txDeviceState.isReceiving && txDeviceState.rssi < 100) { + buzzerContinousMode(BUZZER_MODE_DOUBLE_CHIRP, &buzzer); + } else if (txDeviceState.isReceiving && txDeviceState.rssi < 128) { + buzzerContinousMode(BUZZER_MODE_CHIRP, &buzzer); + } else { + buzzerContinousMode(BUZZER_MODE_OFF, &buzzer); + } + #ifdef FEATURE_TX_OLED if ( currentMillis - lastOledTaskTime > OLED_UPDATE_RATE diff --git a/qsp.cpp b/qsp.cpp index c584b60..44d733d 100644 --- a/qsp.cpp +++ b/qsp.cpp @@ -218,6 +218,7 @@ void qspDecodeIncomingFrame( if (frameId < QSP_FRAME_COUNT) { qsp->lastFrameReceivedAt[frameId] = millis(); } + qsp->anyFrameRecivedAt = millis(); switch (frameId) { case QSP_FRAME_RC_DATA: diff --git a/txbuzzer.cpp b/txbuzzer.cpp index b01473d..5994977 100644 --- a/txbuzzer.cpp +++ b/txbuzzer.cpp @@ -5,18 +5,17 @@ * This method plays selected pattern only once * It disables continious mode */ -void buzzerSingleMode(uint8_t mode, uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer) { +void buzzerSingleMode(uint8_t mode, BuzzerState_t *buzzer) { buzzer->singleModeEnabled = true; buzzer->enabled = false; buzzer->mode = mode; buzzer->tick = 0; } -void buzzerContinousMode(uint8_t mode, uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer) { +void buzzerContinousMode(uint8_t mode, BuzzerState_t *buzzer) { buzzer->singleModeEnabled = false; buzzer->enabled = true; buzzer->mode = mode; - buzzer->tick = 0; } void buzzerProcess(uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer) diff --git a/txbuzzer.h b/txbuzzer.h index 90d00ba..aa2bb45 100644 --- a/txbuzzer.h +++ b/txbuzzer.h @@ -41,6 +41,6 @@ struct BuzzerState_t { }; -void buzzerSingleMode(uint8_t mode, uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer); -void buzzerContinousMode(uint8_t mode, uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer); +void buzzerSingleMode(uint8_t mode, BuzzerState_t *buzzer); +void buzzerContinousMode(uint8_t mode, BuzzerState_t *buzzer); void buzzerProcess(uint8_t pin, uint32_t timestamp, BuzzerState_t *buzzer); \ No newline at end of file diff --git a/variables.h b/variables.h index 6df3a2d..8840dca 100644 --- a/variables.h +++ b/variables.h @@ -13,6 +13,7 @@ #define TX_TRANSMIT_SLOT_RATE 50 //ms #define RX_FAILSAFE_DELAY (TX_TRANSMIT_SLOT_RATE * 8) +#define TX_FAILSAFE_DELAY (RX_FAILSAFE_DELAY * 4) #define CHANNEL_ID 0x01 #define QSP_PREAMBLE 0x51 @@ -44,7 +45,8 @@ enum dataStates { enum deviceStates { DEVICE_STATE_OK, - DEVICE_STATE_FAILSAFE + DEVICE_STATE_FAILSAFE, + DEVICE_STATE_UNDETERMINED }; enum debugConfigFlags { @@ -74,7 +76,8 @@ struct QspConfiguration_t { uint8_t payloadLength = 0; uint8_t frameToSend = 0; uint32_t lastFrameReceivedAt[QSP_FRAME_COUNT] = {0}; - uint8_t deviceState = DEVICE_STATE_OK; + uint32_t anyFrameRecivedAt = 0; + uint8_t deviceState = DEVICE_STATE_UNDETERMINED; void (* hardwareWriteFunction)(uint8_t, QspConfiguration_t*); bool canTransmit = false; bool forcePongFrame = false; @@ -90,6 +93,7 @@ struct TxDeviceState_t { uint8_t flags = 0; uint32_t roundtrip = 0; bool readPacket = false; + bool isReceiving = false; //Indicates that TX module is receiving frames from RX module }; struct RxDeviceState_t {