PWM-Signal for H-bridge bldc motor
Posted: Fri Apr 19, 2024 11:58 am
Hi,
i would like to write a progam for NodeMCU 32 that drives my motor via sinus- pwm-signal.
The code I have written so far looks like this.
The code outputs pwm-signals, but sometimes the ouput is not very nice.
In the oscilloscope(pico) I can see that the PWM-voltage sometimes is lower than 3,3V and sometimes higher, but I don't know why.
Maybe you can give me some help and also tell me what I could improve on my code.
i would like to write a progam for NodeMCU 32 that drives my motor via sinus- pwm-signal.
The code I have written so far looks like this.
The code outputs pwm-signals, but sometimes the ouput is not very nice.
In the oscilloscope(pico) I can see that the PWM-voltage sometimes is lower than 3,3V and sometimes higher, but I don't know why.
Maybe you can give me some help and also tell me what I could improve on my code.
Code: Select all
const int pwmPin_U = 4;
const int pwmPin_U_inv = 14;
const int pwmPin_V = 2;
const int pwmPin_V_inv = 12;
const int pwmPin_W = 15;
const int pwmPin_W_inv = 13;
int pwmIndex = 0;
unsigned long ts;
const uint8_t sinTable_U[] = {
128, 131, 134, 137, 140, 143, 146, 149, 152, 156, 159, 162, 165, 168, 171, 174, 176, 179, 182, 185, 188, 191, 193, 196, 199, 201, 204, 206, 209, 211, 213, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 239, 240, 242, 243, 244, 246, 247, 248, 249, 250, 251, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 244, 242, 241, 239, 238, 236, 235, 233, 231, 229, 227, 225, 223, 221, 219, 217, 215, 212, 210, 207, 205, 202, 200, 197, 195, 192, 189, 186, 184, 181, 178, 175, 172, 169, 166, 163, 160, 157, 154, 151, 148, 145, 142, 138, 135, 132, 129, 126, 123,
120, 117, 113, 110, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 69, 66, 63, 60, 58, 55, 53, 50, 48, 45, 43, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 19, 17, 16, 14, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9, 11, 12, 13, 15, 16, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76, 79, 81, 84, 87, 90, 93, 96, 99, 103, 106, 109, 112, 115, 118, 121, 124};
const uint8_t sinTable_V[] = {
207, 210, 212, 214, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 238, 240, 241, 243, 244, 245, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 240, 239, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198, 195, 193, 190, 187, 184, 181, 178, 176, 173, 170, 167, 164, 161, 158, 154, 151, 148, 145, 142, 139, 136, 133, 129, 126, 123, 120, 117, 114, 111, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 69, 66, 63, 60, 58, 55, 53, 50, 48, 45, 43, 40, 38, 36, 34, 32, 30, 28, 26, 24,
22, 20, 19, 17, 16, 14, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 68, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 112, 116, 119, 122, 125, 128, 131, 134, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 191, 194, 197, 200, 202, 205 };
const uint8_t sinTable_W[] = {
252, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 251, 250, 249, 248, 247, 246, 244, 243, 242, 240, 239, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220, 217, 215, 213, 210, 208, 205, 203, 200, 198, 195, 192, 190, 187, 184, 181, 178, 175, 172, 169, 166, 163, 160, 157, 154, 151, 148, 145, 142, 139, 136, 132, 129, 126, 123, 120, 117, 113, 110, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 69, 66, 63, 60, 58, 55, 52, 50, 47, 45, 43, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 19, 17, 15, 14, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 40, 42, 44, 47, 49, 52, 54, 57, 60, 62, 65, 68, 71, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 113, 116, 119, 122, 125, 128, 132, 135, 138, 141, 144, 147, 150, 153, 156, 160, 163, 166, 169, 172, 175, 177, 180, 183, 186, 189, 192, 194, 197, 200, 202, 205, 207, 210, 212, 214, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 238, 240, 241, 243, 244, 245, 247, 248, 249, 250, 251, 252 };
void pwmInterrupt();
void checkAnalog();
hw_timer_t *timer = NULL;
hw_timer_t *timer_analog = NULL;
bool t_flag = false;
bool analog_flag = false;
int sensorwert;
void setupPWM(){
int freq = 20000;
ledcSetup(0, freq, 8);
ledcSetup(1, freq, 8);
ledcSetup(2, freq, 8);
ledcAttachPin(pwmPin_U, 0);
ledcAttachPin(pwmPin_U_inv, 0);
GPIO.func_out_sel_cfg[pwmPin_U_inv].inv_sel = 1;
ledcAttachPin(pwmPin_V, 1);
ledcAttachPin(pwmPin_V_inv, 1);
GPIO.func_out_sel_cfg[pwmPin_V_inv].inv_sel = 1;
ledcAttachPin(pwmPin_W, 2);
ledcAttachPin(pwmPin_W_inv, 2);
GPIO.func_out_sel_cfg[pwmPin_W_inv].inv_sel = 1;
}
void setupTimers(){
timer = timerBegin(2, 80, true);
timerAttachInterrupt(timer, &pwmInterrupt, true);
timerAlarmWrite(timer, 10000, true);
timerAlarmEnable(timer);
timer_analog = timerBegin(3, 80, true);
timerAttachInterrupt(timer_analog, &checkAnalog, true);
timerAlarmWrite(timer_analog, 100000, true);
timerAlarmEnable(timer_analog);
}
void setup() {
Serial.begin(115200);
setupTimers();
setupPWM();
ts = micros();
}
void loop() {
if(t_flag)
{
Serial.println(micros() - ts); //Zeit pro Periodendauer ausgeben
ts = micros();
t_flag = false;
}
if(analog_flag)
{
sensorwert = analogRead(27);
if(sensorwert < 30)
sensorwert = 30;
timerAlarmWrite(timer, sensorwert, true);
analog_flag = false;
}
}
void IRAM_ATTR pwmInterrupt() {
if(pwmIndex == 0)
t_flag = true;
ledcWrite(0, sinTable_U[pwmIndex]);
ledcWrite(1, sinTable_V[pwmIndex]);
ledcWrite(2, sinTable_W[pwmIndex]);
pwmIndex = (pwmIndex + 1) % 255;
}
void IRAM_ATTR checkAnalog()
{
analog_flag = true;
}