Config: Replace abInverted with a- and bEnabledPinState

Changed config and code for motor driver:
Now its possible to configure whether a and b pins are inverted separately
e.g. a connected to mosfets (inverted) and b connected directly
This commit is contained in:
jonny_l480 2023-08-13 10:31:51 +02:00
parent 007b76b3a3
commit 9550b387c9
3 changed files with 14 additions and 19 deletions

View File

@ -10,7 +10,8 @@ single100a_config_t configDriverLeft = {
.gpio_b = GPIO_NUM_4,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.abInverted = true,
.aEnabledPinState = false, //-> pins inverted (mosfets)
.bEnabledPinState = false,
.resolution = LEDC_TIMER_11_BIT,
.pwmFreq = 10000
};
@ -22,7 +23,8 @@ single100a_config_t configDriverRight = {
.gpio_b = GPIO_NUM_14,
.ledc_timer = LEDC_TIMER_1,
.ledc_channel = LEDC_CHANNEL_1,
.abInverted = false,
.aEnabledPinState = true, //-> pins not inverted
.bEnabledPinState = true,
.resolution = LEDC_TIMER_11_BIT,
.pwmFreq = 10000
};

View File

@ -79,14 +79,6 @@ void single100a::init(){
//function to put the h-bridge module in the desired state and duty cycle
void single100a::set(motorstate_t state_f, float duty_f){
//define enabled signal state (gpio high/low) TODO: move this to constructor?
bool enabled;
if (config.abInverted) {
enabled = false;
} else {
enabled = true;
}
//scale provided target duty in percent to available resolution for ledc
uint32_t dutyScaled;
if (duty_f > 100) { //target duty above 100%
@ -105,29 +97,29 @@ void single100a::set(motorstate_t state_f, float duty_f){
ledc_update_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel);
//TODO: to fix bugged state of h-bridge module when idle and start again, maybe try to leave pwm signal on for some time before updating a/b pins?
//no brake: (freewheel)
gpio_set_level(config.gpio_a, enabled);
gpio_set_level(config.gpio_b, enabled);
gpio_set_level(config.gpio_a, config.aEnabledPinState);
gpio_set_level(config.gpio_b, config.bEnabledPinState);
break;
case motorstate_t::BRAKE:
ledc_set_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel, 0);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel);
//brake:
gpio_set_level(config.gpio_a, !enabled);
gpio_set_level(config.gpio_b, !enabled);
gpio_set_level(config.gpio_a, !config.aEnabledPinState);
gpio_set_level(config.gpio_b, !config.bEnabledPinState);
break;
case motorstate_t::FWD:
ledc_set_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel, dutyScaled);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel);
//forward:
gpio_set_level(config.gpio_a, enabled);
gpio_set_level(config.gpio_b, !enabled);
gpio_set_level(config.gpio_a, config.aEnabledPinState);
gpio_set_level(config.gpio_b, !config.bEnabledPinState);
break;
case motorstate_t::REV:
ledc_set_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel, dutyScaled);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, config.ledc_channel);
//reverse:
gpio_set_level(config.gpio_a, !enabled);
gpio_set_level(config.gpio_b, enabled);
gpio_set_level(config.gpio_a, !config.aEnabledPinState);
gpio_set_level(config.gpio_b, config.bEnabledPinState);
break;
}
ESP_LOGD(TAG, "set module to state=%s, duty=%d/%d, duty_input=%.3f%%", motorstateStr[(int)state_f], dutyScaled, dutyMax, duty_f);

View File

@ -34,7 +34,8 @@ typedef struct single100a_config_t {
gpio_num_t gpio_b;
ledc_timer_t ledc_timer;
ledc_channel_t ledc_channel;
bool abInverted;
bool aEnabledPinState;
bool bEnabledPinState;
ledc_timer_bit_t resolution;
int pwmFreq;
} single100a_config_t;