Open zhuwei opened 2 years ago
Also just FYI I mostly lifted the code for the CRSF parsing from Pixhawk (left their license in place and not trying to take credit for that part). Basically what is being done is the ESP32 is just using a wired serial connection to the ELRS radio receiver and then parses the data and uses the relevant RC channel signals to make PWM pulses to control servos or ESCs.
Hi
It is working!! I found out I had included extra serial library that make it fail to read serial (#include
My 2.4G ELRS EP2 Nano Receiver was working fine with drone previously also I had swap TX-RX wire to make it is not wiring problem. ELRS's LED is solid mean it is paired to my TX16s too. I do modify some of your code to fit LOLIN S2 mini. Here is my code. I have to use Serial1 as 2 is not declared, UART speed 420000 and the wiring is TX (ESP32) to RX (receiver)
uint8_t _rcs_buf[25] {}; uint16_t _raw_rc_values[RC_INPUT_MAX_CHANNELS] {}; uint16_t _raw_rc_count{};
int aileronsPin = 12; int elevatorPin = 13; int throttlePin = 14; int rudderPin = 15;
int aileronsPWMChannel = 1; int elevatorPWMChannel = 2; int throttlePWMChannel = 3; int rudderPWMChannel = 4;
void SetServoPos(float percent, int pwmChannel) { // 50 cycles per second 1,000ms / 50 = 100 /5 = 20ms per cycle // 1ms / 20ms = 1/20 duty cycle // 2ms / 20ms = 2/20 = 1/10 duty cycle // using 16 bit resolution for PWM signal convert to range of 0-65536 (0-100% duty/on time) // 1/20th of 65536 = 3276.8 // 1/10th of 65536 = 6553.6
uint32_t duty = map(percent, 0, 100, 3276.8, 6553.6);
ledcWrite(pwmChannel, duty);
}
void setup() { // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin); Serial.begin(115200); Serial1.begin(420000, SERIAL_8N1, RXD2, TXD2); //Serial2.begin(420000, SERIAL_8N1, RXD2, TXD2); Serial.println("Serial Txd is on pin: "+String(TX)); Serial.println("Serial Rxd is on pin: "+String(RX));
ledcSetup(aileronsPWMChannel,50,16); ledcSetup(elevatorPWMChannel,50,16); ledcSetup(throttlePWMChannel,50,16); ledcSetup(rudderPWMChannel,50,16);
ledcAttachPin(aileronsPin, aileronsPWMChannel); ledcAttachPin(elevatorPin, elevatorPWMChannel); ledcAttachPin(throttlePin, throttlePWMChannel); ledcAttachPin(rudderPin, rudderPWMChannel); }
void loop() { //Choose Serial1 or Serial2 as required //Serial.println("looping"); while (Serial1.available()) { size_t numBytesRead = Serial1.readBytes(_rcs_buf, SBUS_BUFFER_SIZE); if(numBytesRead > 0) { crsf_parse(&_rcs_buf[0], SBUS_BUFFER_SIZE, &_raw_rc_values[0], &_raw_rc_count, RC_INPUT_MAX_CHANNELS ); Serial.print("Channel 1: "); Serial.print(_raw_rc_values[0]); Serial.print("\tChannel 2: "); Serial.print(_raw_rc_values[1]); Serial.print("\tChannel 3: "); Serial.print(_raw_rc_values[2]); Serial.print("\tChannel 4: "); Serial.print(_raw_rc_values[3]); Serial.print("\tChannel 5: "); Serial.println(_raw_rc_values[4]);
int aileronsMapped = map(_raw_rc_values[0], 1000, 2000, 0, 100);
int elevatorMapped = map(_raw_rc_values[1], 1000, 2000, 0, 100);
int throttleMapped = map(_raw_rc_values[2], 1000, 2000, 0, 100);
int rudderMapped = map(_raw_rc_values[3], 1000, 2000, 0, 100);
int switchMapped = map(_raw_rc_values[4], 1000, 2000, 0, 100);
SetServoPos(aileronsMapped, aileronsPWMChannel);
SetServoPos(elevatorMapped, elevatorPWMChannel);
SetServoPos(throttleMapped, throttlePWMChannel);
SetServoPos(rudderMapped, rudderPWMChannel);
}
} }
It is working, sorry my mistake
Hi How do you get this terminal with all output values?
When I upload the code the terminal ends from here. I tried to read in Serial monitor also but can't get any sensible output. I'm sorta lost here please help!
Sorry been buried with work so didn't have chance to respond here, but if can open a new issue or include some more details here with how you're wiring things and which serial monitor you're using that might help. After upload the success message about flashing is right but would need to serial monitor to see any output. The baud rate in the main.cpp is set to 460800 for the primary serial connection to the computer and 420000 for the secondary serial connection used to read data from the ELRS/CRSF receiver.
Hiya @zhuwei sorry I'm not entirely sure what issue you're running into there.
Generally speaking when working with embedded programming I'll enable a Serial output in the firmware being written to device and will Serial.println(); or similar to write logs back to the machine I'm debugging/writing firmware with (can use any serial monitor to then see what is happening on the MCU code if don't have proper remote debugging tools/capability). Will try to address your other issue about the schematic here and hopefully helps get it resolved with the wiring of things.