Skip to content

Commit 10068e8

Browse files
committed
added PWM dithering support
1 parent b219de9 commit 10068e8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Inc/eeprom.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ typedef union EEprom_u {
4747
uint8_t sine_mode_power; // 45
4848
uint8_t input_type; // 46
4949
uint8_t auto_advance; // 47
50-
uint8_t reserved_2[4]; //48-51
50+
uint8_t pwm_dithering; // 48
51+
uint8_t reserved_2[3]; //49-51
5152
uint8_t tune[124]; // 52-175
5253
struct {
5354
uint8_t can_node; // 176

Src/DroneCAN/DroneCAN.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static const struct parameter {
152152
{ "BEEP_VOLUME", T_UINT8, 0, 11, 5, &eepromBuffer.beep_volume},
153153
{ "VARIABLE_PWM", T_BOOL, 0, 1, 1, &eepromBuffer.variable_pwm},
154154
{ "PWM_FREQUENCY", T_UINT8, 8, 48, 24, &eepromBuffer.pwm_frequency},
155+
{ "PWM_DITHERING", T_UINT8, 0, 200, 0, &eepromBuffer.pwm_dithering},
155156
{ "USE_SIN_START", T_BOOL, 0, 1, 0, &eepromBuffer.use_sine_start},
156157
{ "COMP_PWM", T_BOOL, 0, 1, 1, &eepromBuffer.comp_pwm},
157158
{ "STUCK_ROTOR_PROTECTION", T_BOOL, 0, 1, 1, &eepromBuffer.stuck_rotor_protection},

Src/main.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ void loadEEpromSettings()
750750
// if (!eepromBuffer.comp_pwm) {
751751
// eepromBuffer.bi_direction = 0;
752752
// }
753+
if (eepromBuffer.pwm_dithering > 128) {
754+
eepromBuffer.pwm_dithering = 0;
755+
}
753756
}
754757

755758
void saveEEpromSettings()
@@ -1240,6 +1243,18 @@ if (!stepper_sine && armed) {
12401243
#endif
12411244
}
12421245

1246+
/*
1247+
calculate a PWM dithering value
1248+
*/
1249+
static uint16_t pwmDither(uint16_t reload, uint16_t dithering)
1250+
{
1251+
// simple LCG random number generator
1252+
static uint8_t seed;
1253+
seed = (((uint32_t)seed) * 1103515245U + 12345U);
1254+
int16_t r = (seed % (dithering * 2)) - dithering;
1255+
return reload + r;
1256+
}
1257+
12431258
void tenKhzRoutine()
12441259
{ // 20khz as of 2.00 to be renamed
12451260
duty_cycle = duty_cycle_setpoint;
@@ -1408,10 +1423,19 @@ void tenKhzRoutine()
14081423
fast_accel = 0;
14091424
}
14101425
}
1426+
1427+
/*
1428+
possibly add PWM dithering to reduce impact of harmonics
1429+
*/
1430+
uint16_t tim1_arr_used = tim1_arr;
1431+
if (eepromBuffer.pwm_dithering > 0) {
1432+
tim1_arr_used = pwmDither(tim1_arr, eepromBuffer.pwm_dithering);
1433+
}
1434+
14111435
if ((armed && running) && input > 47) {
14121436
if (eepromBuffer.variable_pwm) {
14131437
}
1414-
adjusted_duty_cycle = ((duty_cycle * tim1_arr) / 2000) + 1;
1438+
adjusted_duty_cycle = ((duty_cycle * tim1_arr_used) / 2000) + 1;
14151439

14161440
} else {
14171441

@@ -1422,7 +1446,7 @@ void tenKhzRoutine()
14221446
}
14231447
}
14241448
last_duty_cycle = duty_cycle;
1425-
SET_AUTO_RELOAD_PWM(tim1_arr);
1449+
SET_AUTO_RELOAD_PWM(tim1_arr_used);
14261450
SET_DUTY_CYCLE_ALL(adjusted_duty_cycle);
14271451
}
14281452
#endif // ndef brushed_mode

0 commit comments

Comments
 (0)