bug fix static in motor und speed

This commit is contained in:
2022-01-29 12:25:06 +01:00
parent 1265cfda5c
commit 9c4c82f8b3
18 changed files with 197 additions and 99 deletions
+9 -8
View File
@@ -34,23 +34,24 @@ void MotorControl::init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uin
ledcWrite(this->pwm_channel, 0);
}
void MotorControl::loop() {
static uint32_t last_millis = 0;
uint16_t MotorControl::loop() {
uint32_t time = millis();
uint16_t elapsed_time = time - this->last_millis;
//Cancel if delay is not reached
if (time - last_millis < delay) {
return;
}
if (elapsed_time < delay)
return elapsed_time;
runMotorControl();
last_millis = time;
this->last_millis = time;
return elapsed_time;
}
void MotorControl::runMotorControl() {
// Absolute difference between target_power and speed
// Absolute difference between target_power and power
uint8_t abs_difference = abs(this->target_power - this->power);
// Difference between target_power and speed
// Difference between target_power and power
int16_t difference = this->target_power - this->power;
// Check that the target speed is close to 0 and that the abs_difference is lower than powersteps