qqqlab / madflight

Flight Controller for Arduino ESP32 / RP2350 / RP2040 / STM32
https://madflight.com
GNU General Public License v3.0
98 stars 17 forks source link

trouble of setting PWM on ESP32 #22

Closed umeshwalkar closed 3 months ago

umeshwalkar commented 3 months ago

Hello,

I was trying to imitate your work on ESP32 hardware with default settings and pin configs. I cloned the git repository, made a new project using VS Code ,PlatformIO and Arduino. with some modification in Quadcopter.ino I able to read MPU 6050 (GY87/HW290 module) + QMC5883L and BMP180.

but, while PWM initializing I am getting following runtime errors. It is related with pwm init. function with pwm_frequnecy and duty resolution.

E (6289) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=5 [ 6300][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed!

When further debugging, I found the motors initialization is failing I tried both standard PWM and OneShot125 statements.

//Motors for(int i=0;i<out_MOTOR_COUNT;i++) { //uncomment one line - sets pin, frequency (Hz), minimum (us), maximum (us) out[i].begin(HW_PIN_OUT[i], 400, 950, 2000); //Standard PWM: 400Hz, 950-2000 us //out[i].begin(HW_PIN_OUT[i], 2000, 125, 250); //Oneshot125: 2000Hz, 125-250 us out_command[i] = 0; //set output to 0 for motors out[i].writeFactor(out_command[i]); //start the PWM output to the motors }

added a printf statement in ledc-setup() to check error generated for which channels (w.r.t. motor) while(1) { act_freq = ledcSetup(ch, freq, bits); Serial.printf("ch=%d,freq=%d,bits=%d\n",ch,freq,bits); if(act_freq > 0) break; bits--; if(bits < 7) return false; }

Below is the complete trace.

`rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0030,len:1184 load:0x40078000,len:13232 load:0x40080400,len:3028 entry 0x400805e4 [ 3][E][Wire.cpp:381] setClock(): could not acquire lock E (191) esp_core_dump_fl␆�\K␂J����ɕ�сsize of core dump image: 65537 madflight v1.0.4-DEV starting 20 ... madflight v1.0.4-DEV starting 19 ... madflight v1.0.4-DEV starting 18 ... madflight v1.0.4-DEV starting 17 ... madflight v1.0.4-DEV starting 16 ... madflight v1.0.4-DEV starting 15 ... madflight v1.0.4-DEV starting 14 ... madflight v1.0.4-DEV starting 13 ... madflight v1.0.4-DEV starting 12 ... madflight v1.0.4-DEV starting 11 ... madflight v1.0.4-DEV starting 10 ... madflight v1.0.4-DEV starting 9 ... madflight v1.0.4-DEV starting 8 ... madflight v1.0.4-DEV starting 7 ... madflight v1.0.4-DEV starting 6 ... madflight v1.0.4-DEV starting 5 ... madflight v1.0.4-DEV starting 4 ... madflight v1.0.4-DEV starting 3 ... madflight v1.0.4-DEV starting 2 ... madflight v1.0.4-DEV starting 1 ... DEFAULT ESP32 BOARD - Espressif ESP32-DevKitC 38 pin eeprom_read[64]:FF FF FF FF FF 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF CFG: EEPROM Config invalid, using defaults. len=65280 crc=00FF crc_expected=10000 HW_BOARD_NAME=DEFAULT ESP32 BOARD - Espressif ESP32-DevKitC 38 pin HW_MCU=ESP32 USE_IMU_POLLING HW_PIN_LED=16 HW_PIN_SPI_MOSI=21 MISO=36 SCLK=19 HW_PIN_IMU_CS=18 HW_PIN_IMU_EXTI=39 HW_PIN_I2C_SDA=23 SCL=22 HW_PIN_OUT[11]=33,25,26,27,14,12,13,15,0,4,2 HW_PIN_RCIN_RX=35 TX=32 HW_PIN_GPS_RX=17 TX=5 I2C: Scanning ... I2C: Found address: 0x0D (13) I2C: Found address: 0x68 (104) I2C: Found address: 0x77 (119) I2C: Found 3 device(s) RCIN_USE_CRSF MPU60X0 revision:2.8 IMU_USE_I2C_MPU6050 sample_rate=1000Hz rv=0

BARO_USE_BMP180 BARO_I2C_ADR: 0x77 SensorID: 0x55 MAG_USE_QMC5883L detect=1 ch=0,freq=50,bits=20 ch=1,freq=50,bits=20 ch=2,freq=50,bits=20 ch=3,freq=50,bits=20 ch=4,freq=50,bits=20 ch=5,freq=50,bits=20 ch=6,freq=50,bits=20 E (6269) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=10 [ 6284][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=8,freq=200,bits=20 E (6290) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=21 [ 6303][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=8,freq=200,bits=19 ch=8,freq=200,bits=18 E (6309) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=10 [ 6324][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=9,freq=200,bits=20 E (6330) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=21 [ 6343][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=9,freq=200,bits=19 ch=9,freq=200,bits=18 E (6349) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=10 [ 6364][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=10,freq=200,bits=20 E (6370) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=21 [ 6383][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=10,freq=200,bits=19 ch=10,freq=200,bits=18 E (6390) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=10 [ 6405][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=11,freq=200,bits=20 E (6411) ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=21 [ 6424][E][esp32-hal-ledc.c:75] ledcSetup(): ledc setup failed! ch=11,freq=200,bits=19 ch=11,freq=200,bits=18 Calibrating gyro, this takes a couple of seconds... set imu_cal_gx -7.312554 #config was +0.000000 set imu_cal_gy +0.983968 #config was +0.000000 set imu_cal_gz -0.470907 #config was +0.000000 Use CLI 'cwrite' to write these values to flash ahrs_Setup() Estimated yaw:-101.17 CLI Command Line Interface Started - Type help for help`

I wanted to drive 720 or 8520 coreless motors. Can you guide me what settings I should use.

Chip is ESP32-D0WDQ6 (revision v0.0) Features: WiFi, BT, Dual Core, Coding Scheme None Crystal is 40MHz Arduino v2.0.16 PlatformIO v6.7.0

qqqlab commented 3 months ago

Hi @umeshwalkar , thanks for trying madflight.

The "ledc setup failed" error can be ignored. The ESP32 PWM driver (ESP32_PWM.h) starts trying from 20 bits resolution, and reduces the number of bits until a working resolution is found. (I was to lazy to create code that figures out what the max working resolution is, but I agree that it is annoying and confusing that the ESP-IDF outputs these errors.)

So, your startup dump looks fine to me.

Some thoughts: What ESCs are you using? Did you arm the quad? Maybe the output of CLI 'pmot' helps? Maybe create a small test program to only set the PWM, in order to isolate the problem.

umeshwalkar commented 3 months ago

Hi @qqqlab , Thanks for reply.

Ok. your suggestion to ignore error is noted. I want to bring into your noticed that LEDC_TIMER_BIT_MAX is 21 which is out of limits of ESP32 as per v2.0.16

` LEDC_TIMER_19_BIT, /!< LEDC PWM duty resolution of 19 bits / LEDC_TIMER_20_BIT, /!< LEDC PWM duty resolution of 20 bits /

endif

LEDC_TIMER_BIT_MAX,

} ledc_timer_bit_t;`

int act_freq; int bits = LEDC_TIMER_BIT_MAX-1; // -1 added by umesh while(1) { act_freq = ledcSetup(ch, freq, bits);

So, in ESP32_PWM.h I made a small change just to avoid unnecessary error printing at terminal.

I am planning to use 8520 or 720 core-less motors for first experiment ( I guess you have used them too). for this I will use SI2302N mosfet for each motor. I haven't setup the hardware yet. Some components are still in transit, not received.

I am planning for Q100 or QX95 drone-frame and some custom circuit on GPP. 3.7 Volt / 1S/ 1000 mAH battery to power all this.

if all goes well and if I able to make stable flight with current configuration then I am willing to introduce the swarming and autonomous waypoint navigation feature. I want to add either mavlink or esp-now for drone-to-drone communication for swarming. currently, I am testing swarming algorithms on simulator and seems working, i want to test it on real drone.

I planned to make a PCB for drone FCC like "ESP32-S2-Drone v1.2" from espressif. all in one PCB. but before that i want make sure all peripherals needed.

qqqlab commented 3 months ago

Thanks for the info on LEDC_TIMER_BIT_MAX, I've updated the v1.1.4 code accordingly.

I never used core-less motors with mosfets, only brushless motors with ESCs. For core-less motors you probably need a higher PWM frequency and full range duty cycle, something like:

out[i].begin(HW_PIN_OUT[i], 1000, 0, 1000); //1kHz PWM 0-1000 us duty cycle or even: out[i].begin(HW_PIN_OUT[i], 10000, 0, 100); //10kHz PWM 0-100 us duty cycle

Good luck with building and flying, please post some updates!

I'm closing this issue now. Hint: a star is always welcome :-)