From 8294950b63a945d49e6cf2ccad92b1117170536e Mon Sep 17 00:00:00 2001 From: willem oldemans Date: Thu, 1 Oct 2020 08:35:17 +0200 Subject: [PATCH] initial commit --- radio.cpp | 123 +++++++++++++++++++++++++++++++++++++++ radio.h | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 radio.cpp create mode 100644 radio.h diff --git a/radio.cpp b/radio.cpp new file mode 100644 index 0000000..ca2fd4d --- /dev/null +++ b/radio.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include "radio.h" + +static unsigned long lastSerialTime; + +RcData data; + +RcData* getDataptr( void ) +{ + return &data; +} + +void radio_c::begin( ) +{ +if (!RadioManager.init()) + { + Serial.println("init failed"); + } +} + +bool radio_c::transmitData( void ) +{ + RcData* data = getDataptr(); + uint8_t* payload = data->getByteArray(); + if (RadioManager.sendtoWait(payload, sizeof(payload), m_myaddress)) + { + // Now wait for a reply from the server + uint8_t len = sizeof(buf); + uint8_t from; + if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from)) + { + Serial.print("got reply from : 0x"); + Serial.print(from, HEX); + Serial.print(": "); + Serial.println((char*)buf); + return true; + } + else + { + Serial.println("No reply, is nrf24_reliable_datagram_server running?"); + return false; + } + } + else + { + Serial.println("sending failed"); + return false; + } +} + +bool radio_c::receiveData( void ) +{ + RcData* data = getDataptr(); + + if (RadioManager.available()) + { + // Wait for a message addressed to us from the client + uint8_t len = sizeof(buf); + uint8_t from; + if (RadioManager.recvfromAck(buf, &len, &from)) + { + lastRecvTime = millis(); + if(!data->setRcData(buf)) + { + #ifdef DEBUG + Serial.println("package inclompete") + #endif + return false; + } + // Send a reply back to the originator client, check for error + if (!RadioManager.sendtoWait(data->getReturnData(), sizeof(data->getReturnData()), from)) + { + #ifdef DEBUG + Serial.println("sendtoWait failed"); + #endif + return false; + } + } + + // set all analog values to their middle position, if no RC signal is received during 1s! + if (millis() - lastRecvTime > 1000) + { + data->setfailsafe(); + #ifdef DEBUG + Serial.println("No Radio Available - Check Transmitter!"); + #endif + return false; + } + + if (millis() - lastRecvTime > 2000) { + begin(); // re-initialize radio + lastRecvTime = millis(); + return false; + } + } + return true; +} + + +void sendSerialCommands( void ) +{ + RcData* data = getDataptr(); + + if (millis() - lastSerialTime > 20) { // Send the data every 20ms + lastSerialTime = millis(); + Serial.print('<'); // Start marker + Serial.println(data->axis1); + Serial.println(data->axis2); + Serial.println(data->axis3); + Serial.println(data->axis4); + Serial.println(data->pot); + Serial.println(data->momentary1); + Serial.println(data->momentary2); + Serial.println(data->getfailsafe()); + Serial.print('>'); // End marker + } +} + + + diff --git a/radio.h b/radio.h new file mode 100644 index 0000000..ec92ba7 --- /dev/null +++ b/radio.h @@ -0,0 +1,171 @@ +#ifndef RADIOH +#define RADIOH + +#include +#include +#include +#include + + + +enum dataFields +{ + AXIS1=0, + AXIS2, + AXIS3, + AXIS4, + POT, + MODE1, + MODE2, + MOMENTARY1, + MOMENTARY2, + FAILSAFE, + DUMMY, + LASTDATAFIELD +}; + +class RcData +{ + const byte dummy; + uint8_t dataArray[LASTDATAFIELD]; + +public: + RcData():dummy(0xAA) + { + // All axes to neutral position + clearData(); + } + byte axis1; + byte axis2; + byte axis3; + byte axis4; + byte pot; + bool mode1; + bool mode2; + bool momentary1; // Momentary push button + bool momentary2; + bool failsafe; + + //return data + int voltage; + bool batteryOk; + int dx; + int dy; + int dz; + + void setfailsafe( void) + { + failsafe = true; + clearData(); + } + void clearFailsafe( void ) + { + failsafe = false; + } + + bool getfailsafe( void ) + { + return failsafe; + } + + void clearData( void ) + { + axis1 = 50; + axis2 = 50; + axis3 = 50; + axis4 = 50; + pot = 50; + batteryOk = true; + mode1 = false; + mode2 = false; + momentary2 = false; + momentary1 = false; + failsafe = false; + voltage = 0; + dx=0; + dy=0; + dz=0; + } + uint8_t* getByteArray( void ) + { + dataArray[AXIS1] = axis1; + dataArray[AXIS2] = axis2; + dataArray[AXIS3] = axis3; + dataArray[AXIS4] = axis4; + dataArray[MODE1] = mode1; + dataArray[MODE2] = mode2; + dataArray[POT] = pot; + dataArray[MOMENTARY1] = uint8_t (momentary1); + dataArray[MOMENTARY2] = uint8_t (momentary2); + dataArray[FAILSAFE] = byte (failsafe); + dataArray[DUMMY] = dummy; + + return dataArray; + } + + uint8_t* getReturnData( void ) + { + dataArray[0] = voltage; + dataArray[1] = dx; + dataArray[2] = dy; + dataArray[3] = dz; + dataArray[4] = 0; + dataArray[5] = 0; + dataArray[6] = 0; + dataArray[7] = 0; + dataArray[DUMMY] = dummy; + return dataArray; + } + + bool setRcData( uint8_t* buf ) + { + if (dataArray[DUMMY] == dummy) + { + axis1 = dataArray[AXIS1]; + axis2 = dataArray[AXIS2]; + axis3 = dataArray[AXIS3]; + axis4 = dataArray[AXIS4]; + mode1 = dataArray[MODE1]; + mode2 = dataArray[MODE2]; + pot = dataArray[POT]; + momentary1 = bool(dataArray[MOMENTARY1]); + momentary2 = bool(dataArray[MOMENTARY2]); + failsafe = bool(dataArray[FAILSAFE]); + return true; + } + return false; + } + + void setVoltage( int volt) + { + voltage = volt; + } +}; + +class radio_c +{ + uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; + + // Create an instance of the radio driver + RH_NRF24 RadioDriver; + + // Sets the radio driver to NRF24 and the client address to 1 + RHReliableDatagram RadioManager; + + int m_myaddress; + + unsigned long lastRecvTime; + +public: + radio_c(int address, int CE, int CSN) :RadioDriver(CE, CSN), RadioManager(RadioDriver, address) {}; + void begin( void ); + bool transmitData( void ); + bool receiveData( void ); + +}; + +void sendSerialCommands( void ); +RcData* getDataptr( void ); + + +#endif //RADIOH \ No newline at end of file