Browse Source

Final version

master
Englebert 4 years ago
parent
commit
d0e73d17c7
  1. 3
      OpenFlightTX.h
  2. 278
      OpenFlightTX.ino

3
OpenFlightTX.h

@ -30,12 +30,11 @@ void reboot(void);
void stick_position(void);
void rc_controller(void);
void rx_checker(void);
void frequency_settings(void);
void rf_settings(void);
void setup_webserver(void);
void stick_trims(void);
void wifi_enabler(void);
void unload_webserver(void);
void debug_me(void);
void rf_scanners(void);
void setup_nrf_scanner(void);
void setRegister(byte r, byte v);

278
OpenFlightTX.ino

@ -59,7 +59,8 @@ bool nrf_enable = false;
bool nrf_scanner = false;
bool channel_used[MAX_CHANNELS];
uint8_t graph_type = 0;
uint32_t delay_maxrf_time = 0;
bool delay_maxrf = false;
TaskHandle_t SignalTask;
@ -504,10 +505,8 @@ struct menu_entry_type menu_entry_alpha_list[] = {
{ u8g2_font_open_iconic_all_2x_t, 242, "Stick Positions"},
{ u8g2_font_open_iconic_all_2x_t, 246, "Trims"},
{ u8g2_font_open_iconic_all_2x_t, 281, "WiFi"},
{ u8g2_font_open_iconic_all_2x_t, 141, "Frequency"},
{ u8g2_font_open_iconic_all_2x_t, 253, "RF Modules"},
{ u8g2_font_open_iconic_all_2x_t, 253, "RF Settings"},
{ u8g2_font_open_iconic_all_2x_t, 207, "RF Scanners"},
{ u8g2_font_open_iconic_all_2x_t, 104, "Debug"},
{ u8g2_font_open_iconic_all_2x_t, 90, "Battery"},
{ u8g2_font_open_iconic_all_2x_t, 142, "Save Settings"},
{ u8g2_font_open_iconic_all_2x_t, 205, "Reset Default"},
@ -648,7 +647,12 @@ uint16_t throttle_value = 0;
uint16_t yaw_value = 0;
uint16_t pitch_value = 0;
uint16_t roll_value = 0;
/*
uint16_t tx_throttle = 0;
uint16_t tx_yaw = 0;
uint16_t tx_pitch = 0;
uint16_t tx_roll = 0;
*/
// For Center sticks trim
uint16_t throttle_max = 0;
uint16_t throttle_min = 4096;
@ -659,6 +663,10 @@ uint16_t pitch_min = 4096;
uint16_t roll_max = 0;
uint16_t roll_min = 4096;
// For NRF24L01 frequency channels + TxPower
uint8_t freq_channel = 0;
uint8_t freq_txpower = 0; // 0: LOW, 1: MIN, 2: HIGH, 3: MAX
uint64_t vbat_value = 0;
bool sw1 = 0;
@ -1153,10 +1161,20 @@ void SignalHandler(void *pvParameters) {
nrf_scanner = false;
// Assign values...
txmessage.throttle = throttle_value;
txmessage.yaw = yaw_value;
txmessage.pitch = pitch_value;
txmessage.roll = roll_value;
throttle_value = (throttle_value > throttle_max) ? throttle_max : throttle_value; // Play safe... before mapping
throttle_value = (throttle_value < throttle_min) ? throttle_min : throttle_value;
yaw_value = (yaw_value > yaw_max) ? yaw_max : yaw_value; // Play safe... before mapping
yaw_value = (yaw_value < yaw_min) ? yaw_min : yaw_value;
pitch_value = (pitch_value > pitch_max) ? pitch_max : pitch_value; // Play safe... before mapping
pitch_value = (pitch_value < pitch_min) ? pitch_min : pitch_value;
roll_value = (roll_value > roll_max) ? roll_max : roll_value; // Play safe... before mapping
roll_value = (roll_value < roll_min) ? roll_min : roll_value;
// Mapping before sending...
txmessage.throttle = map(throttle_value, throttle_min, throttle_max, 1000, 2000);
txmessage.yaw = map(yaw_value, yaw_min, yaw_max, 2000, 1000);
txmessage.pitch = map(pitch_value, pitch_min, pitch_max, 2000, 1000);
txmessage.roll = map(roll_value, roll_min, roll_max, 1000, 2000);
txmessage.switches = switches_value;
// Sending Data ove NRF
@ -1164,6 +1182,7 @@ void SignalHandler(void *pvParameters) {
// Profiling..
nrf_profiling_raw++;
// delay(1);
} else {
delay(10);
} // End of if(nrf_scanner)
@ -1188,6 +1207,17 @@ void loop() {
last_seconds = millis() + 1000;
// For maximize the RF
/*
if(delay_maxrf) {
if(millis() > delay_maxrf_time) {
radio.setPALevel(RF24_PA_MAX); // Set the transmit power to Maximum.
delay_maxrf = false; // Only once.
}
}
*/
// This is the way to reduce CPU cycles. Only enable it when require
if(wifi_enable) {
if(!webserver_setup) {
@ -1242,9 +1272,15 @@ void loop() {
} else if(pitch_value < MENU_STICK_PITCH_MIN) {
stick_navigation_position = MENU_BACK;
} else if(yaw_value > MENU_STICK_YAW_MAX) {
stick_navigation_position = MENU_EXTEND_RIGHT;
if(!invert_yaw)
stick_navigation_position = MENU_EXTEND_RIGHT;
else
stick_navigation_position = MENU_EXTEND_LEFT;
} else if(yaw_value < MENU_STICK_YAW_MIN) {
stick_navigation_position = MENU_EXTEND_LEFT;
if(!invert_yaw)
stick_navigation_position = MENU_EXTEND_LEFT;
else
stick_navigation_position = MENU_EXTEND_RIGHT;
} else {
stick_navigation_position = MENU_NONE;
}
@ -1347,12 +1383,9 @@ void loop() {
} else if(menu_entry_alpha_list[destination_alpha_state.position].name == "WiFi") {
require_unlock = false;
wifi_enabler();
} else if(menu_entry_alpha_list[destination_alpha_state.position].name == "Frequency") {
require_unlock = false;
frequency_settings();
} else if(menu_entry_alpha_list[destination_alpha_state.position].name == "Debug") {
} else if(menu_entry_alpha_list[destination_alpha_state.position].name == "RF Settings") {
require_unlock = false;
debug_me();
rf_settings();
} else if(menu_entry_alpha_list[destination_alpha_state.position].name == "RF Scanners") {
require_unlock = true;
rf_scanners();
@ -1423,13 +1456,13 @@ void rc_controller(void) {
u8g2_left.clearBuffer();
u8g2_left.firstPage();
u8g2_left.setFont(u8g2_font_ImpactBits_tr);
u8g2_left.setCursor(0, 15);
u8g2_left.print(F("RC Mode"));
u8g2_left.setCursor(0, 13);
u8g2_left.print(F("OpenFlightTX RC"));
show_battery_level(0, 56);
draw_gimbalbox(53, 25, 32);
draw_gimbalbox(90, 25, 32);
draw_gimbalbox(53, 31, 32);
draw_gimbalbox(90, 31, 32);
uint16_t gimbal_y_raw = throttle_value;
uint16_t gimbal_x_raw = yaw_value;
@ -1451,7 +1484,7 @@ void rc_controller(void) {
gimbal_x = map(gimbal_x_raw, yaw_max, yaw_min, 0, 31); // Yaw
// u8g2_left.drawFilledEllipse(64 + gimbal_x, gimbal_y, 2, 2, U8G2_DRAW_ALL);
u8g2_left.drawFilledEllipse(53 + gimbal_x, 25 + gimbal_y, 2, 2, U8G2_DRAW_ALL);
u8g2_left.drawFilledEllipse(53 + gimbal_x, 31 + gimbal_y, 2, 2, U8G2_DRAW_ALL);
// Mapping based on the live values
@ -1473,15 +1506,62 @@ void rc_controller(void) {
gimbal_x = map(gimbal_x_raw, roll_max, roll_min, 0, 31); // Roll
// u8g2_right.drawFilledEllipse(gimbal_x, gimbal_y, 3, 3, U8G2_DRAW_ALL);
u8g2_left.drawFilledEllipse(90 + gimbal_x, 25 + gimbal_y, 2, 2, U8G2_DRAW_ALL);
u8g2_left.drawFilledEllipse(90 + gimbal_x, 31 + gimbal_y, 2, 2, U8G2_DRAW_ALL);
u8g2_left.setCursor(0, 25);
u8g2_left.setFont(u8g2_font_5x7_tf);
u8g2_left.print(F("Rate:"));
sprintf(buf, "%d", nrf_profiling);
u8g2_left.drawStr(25, 25, buf);
sprintf(buf, "%d packets/s", nrf_profiling);
u8g2_left.drawStr(0, 24, buf);
sprintf(buf, "%d MHz", freq_channel + 2400);
u8g2_left.drawStr(0, 32, buf);
switch(freq_txpower) {
case 0:
sprintf(buf, "TX MIN");
break;
case 1:
sprintf(buf, "TX LOW");
break;
case 2:
sprintf(buf, "TX HIGH");
break;
case 3:
sprintf(buf, "TX MAX");
break;
}
u8g2_left.drawStr(0, 40, buf);
if(!sw6) {
u8g2_left.setCursor(20, 63);
u8g2_left.print(F("LOCKED"));
}
if(sw1) {
u8g2_left.setCursor(0, 48);
u8g2_left.print(F("A"));
}
if(sw2) {
u8g2_left.setCursor(5, 48);
u8g2_left.print(F("B"));
}
if(sw3) {
u8g2_left.setCursor(10, 48);
u8g2_left.print(F("C"));
}
if(sw4) {
u8g2_left.setCursor(15, 48);
u8g2_left.print(F("D"));
}
if(sw5) {
u8g2_left.setCursor(20, 48);
u8g2_left.print(F("E"));
}
u8g2_left.nextPage();
}
@ -1876,13 +1956,21 @@ void read_settings(void) {
// Roll Trim
addr = 20;
trim_roll = EEPROM.read(addr);
// Frequency Channel
addr = 21;
freq_channel = EEPROM.read(addr);
// Frequency TxPower
addr = 22;
freq_txpower = EEPROM.read(addr);
}
void commit_all(void) {
/*
* Memory Format:
*
* TTttYYyyPPppRRrrPABCDFFFFS
* TTttYYyyPPppRRrrPABCDFf
*
* TT: Throttle Max
* tt: Throttle Min
@ -1897,8 +1985,8 @@ void commit_all(void) {
* B : Yaw Trim
* C : Pitch Trim
* D : Roll Trim
* FFFF: LoRA Frequency
* S : SyncWord
* F : NRF Channels (0 ~ 124) Total 125 channels
* f : TX Power (MIN, LOW, HIGH and MAX)
*/
uint16_t addr = 0x00;
@ -1952,6 +2040,12 @@ void commit_all(void) {
// Roll Trim
write_data(addr++, trim_roll);
// Frequency Channel
write_data(addr++, freq_channel);
// Frequency Tx Power
write_data(addr++, freq_txpower);
}
void write_data(uint16_t addr, uint8_t val) {
@ -2346,19 +2440,69 @@ void bluetooth_gamepad() {
}
}
void frequency_settings(void) {
void rf_settings(void) {
// Data processing
if(stick_navigation_position == MENU_LEFT) {
if(freq_txpower > 0) {
freq_txpower--;
radio.setPALevel(freq_txpower);
}
} else if(stick_navigation_position == MENU_RIGHT) {
if(freq_txpower < 3) {
freq_txpower++;
radio.setPALevel(freq_txpower);
}
} else if(stick_navigation_position == MENU_EXTEND_LEFT) {
if(freq_channel > 0) {
freq_channel--;
radio.setChannel(freq_channel);
}
} else if(stick_navigation_position == MENU_EXTEND_RIGHT) {
if(freq_channel < MAX_CHANNELS) {
freq_channel++;
radio.setChannel(freq_channel);
}
}
u8g2_left.clearBuffer();
u8g2_left.firstPage();
u8g2_left.setFont(u8g2_font_ImpactBits_tr);
u8g2_left.setCursor(0, 15);
u8g2_left.print(F("Frequency"));
u8g2_left.nextPage();
u8g2_left.print(F("RF Settings"));
/*
u8g2_right.clearBuffer();
u8g2_right.firstPage();
u8g2_right.nextPage();
*/
// To List the current frequency... and also current power. Using the sticks to change it. Once back, it will commit the setting by pushing down the stick. If pulling up...nothing change.
u8g2_left.setFont(u8g2_font_5x7_tf);
u8g2_left.setCursor(0, 32);
u8g2_left.print(F("Frequency:"));
u8g2_left.setCursor(64, 32);
u8g2_left.print(F("TX Power:"));
// Getting the common variables
static char buf[10];
int frequency_mhz = 2400 + freq_channel;
sprintf(buf, "%d MHz", frequency_mhz);
u8g2_left.drawStr(0, 40, buf);
switch(freq_txpower) {
case 0:
sprintf(buf, "LOW");
break;
case 1:
sprintf(buf, "MIN");
break;
case 2:
sprintf(buf, "HIGH");
break;
case 3:
sprintf(buf, "MAX");
break;
default:
sprintf(buf, "UNKNOWN");
break;
}
u8g2_left.drawStr(64, 40, buf);
u8g2_left.nextPage();
}
void show_battery_level(uint8_t x, uint8_t y) {
@ -2432,47 +2576,16 @@ void wifi_enabler(void) {
}
void debug_me(void){
/*
u8g2_left.clearBuffer();
u8g2_left.firstPage();
u8g2_left.setFont(menu_entry_alpha_huge_list[destination_alpha_state.position].font);
u8g2_left.drawGlyph(40, ICONX_Y, menu_entry_alpha_huge_list[destination_alpha_state.position].icon);
u8g2_left.nextPage();
*/
u8g2_left.clearBuffer();
u8g2_left.firstPage();
u8g2_left.setFontMode(1);
u8g2_left.setFont(u8g2_font_VCR_OSD_tr);
static char buf[10];
u8g2_left.setFont(u8g2_font_ImpactBits_tr);
u8g2_left.setCursor(0,12);
u8g2_left.print(F("DEBUG ME"));
// u8g2_left.drawStr(45, 15, buf);
// strcpy(buf, ltoa(vbat_value, buf, 10));
// u8g2_left.drawStr(0, 30, buf);
//strcpy(buf, ltoa(vbat_real_value, buf, 10));
//sprintf(buf, "RATE:%i", lora_receive_profiling);
//u8g2_left.setFont(u8g2_font_5x7_tf);
//u8g2_left.drawStr(0, 32, buf);
u8g2_left.nextPage();
}
void setup_nrf_scanner() {
// Setting NRF24L01
printf_begin();
// printf_begin();
radio.begin();
radio.setAutoAck(false);
// Get into standby mode
radio.startListening();
radio.stopListening();
radio.printDetails();
// radio.printDetails();
// Initialize variables...
for(int i = 0; i < MAX_CHANNELS; i++) {
@ -2485,19 +2598,36 @@ void setup_nrf_scanner() {
void setup_nrf_tx(void) {
// Setting NRF24L01
printf_begin();
// printf_begin();
radio.begin();
// radio.setAutoAck(true);
radio.setAutoAck(false); // Seems like i am doing this on the last version...
radio.setPayloadSize(sizeof(TxMessage));
radio.setChannel(99); // This will need to set from EEPROM settings
radio.setDataRate(RF24_250KBPS); // Lowest.. at 250kbps
radio.setPALevel(RF24_PA_MAX); // Set the transmit power to Maximum... Later make it configurable.
radio.setChannel(freq_channel); // This will need to set from EEPROM settings
radio.setDataRate(RF24_250KBPS); // Lowest.. at 250kbps
switch(freq_txpower) { // Set the transmit power
case 0:
radio.setPALevel(RF24_PA_MIN);
break;
case 1:
radio.setPALevel(RF24_PA_LOW);
break;
case 2:
radio.setPALevel(RF24_PA_HIGH);
break;
case 3:
radio.setPALevel(RF24_PA_MAX);
break;
}
// delay_maxrf_time = millis() + 2000; // Delay 2 seconds later
// delay_maxrf = true;
// radio.setPALevel(RF24_PA_MAX); // Set the transmit power to Maximum... Later make it configurable.
const uint64_t pipe = 0xE8E8F0F0E1LL;
radio.openWritingPipe(pipe);
// radio.printDetails();
Serial.println("SET nrf_scanner = TRUE");
nrf_enable = true;

Loading…
Cancel
Save