RC ESC modification part5: Simple FOC with arduino Mega2560/1280 + IR 2101S gate drives

 Goal is to drive a hall sensored BLDC with simple FOC and RC ESC.

328P has only 2 interrupt pins, I have tested hall sensor reading with simple FOC.

But the result is not looking too good, I start to look into the arduino boards I have on hand, ESP32 can also be good, but it took quite long time to upload codes, not my favorite in development. 

PWMHW InterruptADC
Uno, Nano, Mini, other 328-based6 PWM 5/6,9/10,3/112,36
Mega 1280/256015 PWM
D2~D13,D44~D46
2, 3, 18, 19, 20, 21 (pins 20 & 21 are not available to use for interrupts while they are used for I2C communication)16
Micro, Leonardo, other 32u4-based11,3(SCL)
9,10
6,13
0, 1, 2, 3, 7A0-A5

32U4 could be next to test, I first test my mega1280/2560...

I attached E44 hall sensors on 19,20,21, the testing program of simple FOC runs well.

But to drive the gatedrive, I met problem of the pairs of timer.

Base on this post, find this file under lib: src\drivers\hardware_specific\atmega2560_mcu.cpp

the modify the  function _configureComplementaryPair :

int _configureComplementaryPair(int pinH, int pinL) {
if( (pinH == 4 && pinL == 13 ) || (pinH == 13 && pinL == 4 ) ){
// configure the pwm phase-corrected mode
TCCR0A = ((TCCR0A & 0b11111100) | 0x01);
// configure complementary pwm on low side
if(pinH == 13 ) TCCR0A = 0b10110000 | (TCCR0A & 0b00001111) ;
else TCCR0A = 0b11100000 | (TCCR0A & 0b00001111) ;
// set prescaler to 1 - 32kHz freq
TCCR0B = ((TCCR0B & 0b11110000) | 0x01); }
}else if( (pinH == 11 && pinL == 12 ) || (pinH == 12 && pinL == 11 ) ){
// set prescaler to 1 - 32kHz freq
TCCR1B = ((TCCR1B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 11 ) TCCR1A = 0b10110000 | (TCCR1A & 0b00001111) ;
else TCCR1A = 0b11100000 | (TCCR1A & 0b00001111) ; }
}else if((pinH == 10 && pinL == 9 ) || (pinH == 9 && pinL == 10 ) ){
// set prescaler to 1 - 32kHz freq
TCCR2B = ((TCCR2B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 10 ) TCCR2A = 0b10110000 | (TCCR2A & 0b00001111) ;
else TCCR2A = 0b11100000 | (TCCR2A & 0b00001111) ; }
}else if((pinH == 5 && pinL == 2 ) || (pinH == 2 && pinL == 5 ) ){
// set prescaler to 1 - 32kHz freq
TCCR3B = ((TCCR3B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 5 ) TCCR3A = 0b10110000 | (TCCR3A & 0b00001111) ;
else TCCR3A = 0b11100000 | (TCCR3A & 0b00001111) ; }
}else if((pinH == 6 && pinL == 7 ) || (pinH == 7 && pinL == 6 ) ){
// set prescaler to 1 - 32kHz freq
TCCR4B = ((TCCR4B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 6 ) TCCR4A = 0b10110000 | (TCCR4A & 0b00001111) ;
else TCCR4A = 0b11100000 | (TCCR4A & 0b00001111) ; }
}else if((pinH == 46 && pinL == 45 ) || (pinH == 45 && pinL == 46 ) ){
// set prescaler to 1 - 32kHz freq
TCCR5B = ((TCCR5B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 46 ) TCCR5A = 0b10110000 | (TCCR5A & 0b00001111) ;
else TCCR5A = 0b11100000 | (TCCR5A & 0b00001111) ; }
}else{
return -1; }
}
return 0; }
}


then avoid pin 11/12, it should work.

this one works for me: 

BLDCDriver6PWM driver = BLDCDriver6PWM(9,10,2,5,4,13);

But only on mega2560 not 1280.


留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

Install Network Time Protocol(NTP) on BeagleBone with Angstrom linux and set local time zone