Browse Source

Receiver able to get the data

master
Englebert 3 years ago
parent
commit
62b20171f1
  1. 4
      src/BLE.h
  2. 2
      src/Inputs.h
  3. 100
      src/RCController.cpp
  4. 13
      src/RCController.h
  5. 20
      src/main.cpp
  6. 3
      src/main.h
  7. 2
      src/nrf24l01.cpp
  8. 15
      src/nrf24l01.h

4
src/BLE.h

@ -8,9 +8,9 @@
class BLE {
public:
bool nrf_begin = false;
bool ble_begin = false;
bool updated = false;
bool nrf_started = false;
bool ble_started = false;
int16_t sent_counter_raw = 0;
int16_t sent_counter = 0;

2
src/Inputs.h

@ -16,12 +16,14 @@
// For RC Deadband
#define RC_DEADBAND 5
/***
enum input_names {
THROTTLE = 0,
YAW,
PITCH,
ROLL
};
***/
class Inputs {
public:

100
src/RCController.cpp

@ -1,5 +1,26 @@
#include "RCController.h"
// Data Structure:
// Old format:
// TTTT TTTT TTTT ---- YYYY YYYY YYYY ---- PPPP PPPP PPPP ---- RRRR RRRR RRRR ---- SSSS SSSS = 72-bits ( 9 bytes )
//
// 2021 New Data format for saving 2 bytes:
// 1234 5678 9TTT TTTT TTTT -YYY YYYY YYYY -PPP PPPP PPPP -RRR RRRR RRRR (7 Bytes)
// |-------| |-------| |-------| |-------| |-------| |-------| |-------|
// Byte 00 Byte 01 Byte 02 Byte 03 Byte 04 Byte 05 Byte 06
struct TxMessage {
uint8_t Byte00;
uint8_t Byte01;
uint8_t Byte02;
uint8_t Byte03;
uint8_t Byte04;
uint8_t Byte05;
uint8_t Byte06;
};
TxMessage txmessage;
RCController::RCController(void) {
updated = true;
}
@ -27,17 +48,84 @@ void RCController::draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y) {
}
}
void RCController::update(void) {
if(!nrf_begin) {
return;
}
// Mapping before sending...
// txmessage.throttle = map(throttle_value, throttle_min, throttle_max, 1000, 2000);
// Trying to add in another bits of the switch here so we can save more bandwidth using the same rate
// txmessage.throttle = map(throttle_value, throttle_min, throttle_max, 1000, 2000);
// 1234 5678 9TTT TTTT TTTT -YYY YYYY YYYY -PPP PPPP PPPP -RRR RRRR RRRR (7 Bytes)
// |-------| |-------| |-------| |-------| |-------| |-------| |-------|
// Byte 00 Byte 01 Byte 02 Byte 03 Byte 04 Byte 05 Byte 06
// Generating the data
uint16_t raw_data1 = 0;
uint16_t raw_data2 = 0;
// Byte00 ~ Byte06 - START
raw_data1 = map(inputs.throttle, 0, 4096, 1000, 2000);
//// txmessage.Byte00 = sw1 << 7 | sw2 << 6 | sw3 << 5 | sw4 << 4 | sw5 << 3 | sw6 << 2 | sw7 << 1 | sw8;
txmessage.Byte00 = 0x00;
txmessage.Byte01 = 1 << 7 | (raw_data1 >> 4);
raw_data2 = map(inputs.yaw, 0, 4096, 2000, 1000);
txmessage.Byte02 = (raw_data1 & 0x000F) << 4 | raw_data2 >> 8;
txmessage.Byte03 = (raw_data2 & 0x00FF);
raw_data1 = map(inputs.pitch, 0, 4096, 2000, 1000);
txmessage.Byte04 = raw_data1 >> 4;
raw_data2 = map(inputs.roll, 0, 4096, 1000, 2000);
txmessage.Byte05 = (raw_data1 & 0x000F) << 4 | raw_data2 >> 8;
txmessage.Byte06 = (raw_data2 & 0x00FF);
// Byte00 ~ Byte06 - END
// Sending Data over NRF
tx.write(&txmessage, sizeof(txmessage));
// Profiling..
nrf_profiling_raw++;
}
void RCController::show(TFT_eSprite *m) {
if(!nrf_begin) {
nrf_begin = true;
////////////////////ble.begin();
tx.begin();
tx.setAutoAck(false);
tx.setPayloadSize(sizeof(TxMessage));
tx.setChannel(8); ///// Temporary hard code first
tx.setDataRate(RF24_250KBPS); // Lowest.. at 250kbps
switch(txpower) { // Set the transmit power
case 0:
tx.setPALevel(RF24_PA_MIN);
break;
case 1:
tx.setPALevel(RF24_PA_LOW);
break;
case 2:
tx.setPALevel(RF24_PA_HIGH);
break;
case 3:
tx.setPALevel(RF24_PA_MAX);
break;
}
tx.openWritingPipe(pipeIn);
// tx.printDetails();
Serial.println("nrf tx started.");
}
if((uint32_t) (millis() - last_updated) > 50) {
updated = true;
}
if((uint32_t) (millis() - last_voltage_read) > 500) {
voltage = M5.Axp.GetBatVoltage();
current = M5.Axp.GetBatCurrent();
@ -45,13 +133,19 @@ void RCController::show(TFT_eSprite *m) {
last_voltage_read = millis();
}
if((uint32_t) (millis() - last_profile_update) > 1000) {
nrf_profiling = nrf_profiling_raw;
nrf_profiling_raw = 0;
last_profile_update = millis();
}
if(updated) {
m->fillRect(0, 0, 320, 240, BLACK);
m->setTextColor(WHITE, BLACK);
m->setTextSize(2);
m->setCursor(0, 0);
m->print("Bat: ");
m->print(String(voltage) + "V " + String(current) + "mA");
m->print(String(voltage) + "V " + String(current) + "mA " + String(nrf_profiling));
// Draw the gimbal box
draw_gimbal(m, 69, 150);
@ -115,7 +209,7 @@ void RCController::show(TFT_eSprite *m) {
// Key Input Handler
if(M5.BtnC.wasPressed()) {
if(ble_begin) {
if(nrf_begin) {
nrf_begin = false;
//////// ble.end();
}

13
src/RCController.h

@ -6,16 +6,25 @@
#include "Inputs.h"
#include "Screen.h"
#include "Speak.h"
#include "nrf24l01.h"
class RCController {
public:
const uint64_t pipeIn = 0xE8E8F0F0E1LL; // Must be same as the transmission
TouchPoint_t coordinate;
uint32_t touch_time = 0;
bool ble_begin = false;
bool nrf_begin = false;
bool updated = false;
float voltage = 0.00;
float current = 0.00;
uint16_t nrf_profiling_raw = 0;
uint16_t nrf_profiling = 0;
uint8_t txpower = 0;
uint32_t last_button1 = 0;
uint32_t last_button2 = 0;
uint32_t last_button3 = 0;
@ -26,10 +35,12 @@ class RCController {
void begin(void);
void show(TFT_eSprite *m);
void draw_gimbal(TFT_eSprite *m, uint16_t x, uint16_t y);
void update(void); // Sending NRF signals
private:
uint32_t last_updated = 0;
uint32_t last_voltage_read = 0;
uint32_t last_profile_update = 0;
};
extern RCController rccontroller;

20
src/main.cpp

@ -24,6 +24,7 @@ void setup() {
storage.begin();
customwifi.begin();
inputs.begin();
tx.begin();
web.begin();
speak.welcome();
@ -80,6 +81,15 @@ void setup() {
CPU_0
);
xTaskCreatePinnedToCore(
taskNRF,
"TaskNRF", // Name of the process
8192, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
4, // Priority
NULL,
CPU_0
);
}
@ -150,3 +160,13 @@ void taskBLE(void *pvParameters) {
vTaskDelay(5);
}
}
void taskNRF(void *pvParameters) {
(void) pvParameters;
for(;;) {
rccontroller.update();
vTaskDelay(5);
}
}

3
src/main.h

@ -12,7 +12,9 @@
#include "Screen.h"
#include "Storage.h"
#include "CustomWiFi.h"
#include "RCController.h"
#include "Web.h"
#include "nrf24l01.h"
// Task Handler
void taskScreen(void *pvParameters);
@ -20,5 +22,6 @@ void taskSpeak(void *pvParameters);
void taskWeb(void *pvParameters);
void taskInput(void *pvParameters);
void taskBLE(void *pvParameters);
void taskNRF(void *pvParameters);
#endif

2
src/nrf24l01.cpp

@ -1315,4 +1315,4 @@ void NRF24L01::stopConstCarrier() {
}
NRF24L01 rx(RX_CE_PIN, RX_CSN_PIN); // Setting up receiver module
NRF24L01 tx(TX_CE_PIN, TX_CSN_PIN); // Setting up receiver module

15
src/nrf24l01.h

@ -25,10 +25,16 @@
#ifndef _NRF24L01_H
#define _NRF24L01_H
// For NRF
#define _SPI SPI
#define ESP32
#define MAX_CHANNELS 125
#define TX_CE_PIN 19
#define TX_CSN_PIN 27
#include <Arduino.h>
#include <SPI.h>
#include <pgmspace.h>
#include "main.h"
/* Memory Map */
#define NRF_CONFIG 0x00
@ -166,6 +172,11 @@ enum rc {
* Power Amplifier level.
*
* For use with setPALevel()
*
* MIN: -18dB
* LOW: -12dB
* HIGH: -6dB
* MAX: 0dB
*/
typedef enum {
RF24_PA_MIN = 0,
@ -1356,5 +1367,5 @@ class NRF24L01 {
}; // End of class RF24
extern NRF24L01 rx;
extern NRF24L01 tx;
#endif // _NRF24L01_H
Loading…
Cancel
Save