more refactor bug fixes
This commit is contained in:
@@ -33,6 +33,7 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
|
||||
ledcSetup(this->pwmChannel, MotorControl::pwmFreq, MotorControl::pwmRes);
|
||||
ledcAttachPin(this->pwmPin, this->pwmChannel);
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
std::cout << "Init pwm" << std::endl;
|
||||
}
|
||||
|
||||
void MotorControl::run()
|
||||
@@ -56,7 +57,7 @@ void MotorControl::run()
|
||||
return;
|
||||
}
|
||||
|
||||
// Positive or negative tagret speed
|
||||
// Positive or negative target speed
|
||||
if (this->targetPower >= 0)
|
||||
{
|
||||
// Positive or negative speed
|
||||
@@ -97,28 +98,6 @@ void MotorControl::run()
|
||||
}
|
||||
}
|
||||
|
||||
void MotorControl::setMinPwm(uint8_t min)
|
||||
{
|
||||
if (min > MotorControl::maxPwmMin)
|
||||
{
|
||||
min = MotorControl::maxPwmMin;
|
||||
}
|
||||
// transform percentage to real pwm value
|
||||
min = static_cast<uint8_t>(((static_cast<uint8_t>(1) << pwmRes) - 1) * (min / 100.0));
|
||||
this->dutycycleMin = min;
|
||||
}
|
||||
|
||||
void MotorControl::setMaxPwm(uint8_t max)
|
||||
{
|
||||
if (max > 100)
|
||||
{
|
||||
max = 100;
|
||||
}
|
||||
// transform percentage to real pwm value
|
||||
max = static_cast<uint8_t>(((static_cast<uint8_t>(1) << pwmRes) - 1) * (max / 100.0));
|
||||
this->dutycycleMax = max;
|
||||
}
|
||||
|
||||
void MotorControl::setTargetPower(int8_t power)
|
||||
{
|
||||
if (power <= 100 && power >= -100)
|
||||
@@ -174,10 +153,11 @@ void MotorControl::setRealPower(int8_t power)
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
this->dutycycle = 0;
|
||||
std::cout << "abort" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t pwm_val = map(abs(power), 0, 100, this->dutycycleMin, this->dutycycleMax);
|
||||
const uint8_t pwm_val = map(abs(power), 0, 100, MotorControl::minPwmVal, MotorControl::maxPwmVal);
|
||||
|
||||
if ((this->direction == 1 || this->direction == 0) && power < 0)
|
||||
{ // new direction backward
|
||||
@@ -192,6 +172,7 @@ void MotorControl::setRealPower(int8_t power)
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
}
|
||||
|
||||
std::cout << "MotorControl::setRealPower pwm_val: " << (int) pwm_val << " channel:" << (int) this->pwmChannel << std::endl;
|
||||
ledcWrite(this->pwmChannel, pwm_val);
|
||||
this->dutycycle = pwm_val;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user