refactore motorControl
This commit is contained in:
@@ -17,9 +17,9 @@ MotorControl::MotorControl() {
|
||||
this->setMaxPwm(PWMMAX);
|
||||
}
|
||||
|
||||
void MotorControl::init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uint8_t dir_2) {
|
||||
this->pwm_pin = pwm_pin;
|
||||
this->pwm_channel = pwm_channel;
|
||||
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2) {
|
||||
this->pwmPin = pwmPin;
|
||||
this->pwmChannel = pwmChannel;
|
||||
this->dir_1 = dir_1;
|
||||
this->dir_2 = dir_2;
|
||||
|
||||
@@ -29,17 +29,17 @@ void MotorControl::init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uin
|
||||
digitalWrite(this->dir_1, LOW);
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
|
||||
ledcSetup(this->pwm_channel, PWMFREQ, this->pwm_res);
|
||||
ledcAttachPin(this->pwm_pin, this->pwm_channel);
|
||||
ledcWrite(this->pwm_channel, 0);
|
||||
ledcSetup(this->pwmChannel, PWMFREQ, this->pwmRes);
|
||||
ledcAttachPin(this->pwmPin, this->pwmChannel);
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
}
|
||||
|
||||
uint16_t MotorControl::loop() {
|
||||
uint32_t time = millis();
|
||||
uint16_t elapsed_time = time - this->lastMillis;
|
||||
|
||||
//Cancel if delay is not reached
|
||||
if (elapsed_time < delay)
|
||||
//Cancel if delayLoop is not reached
|
||||
if (elapsed_time < delayLoop)
|
||||
return elapsed_time;
|
||||
|
||||
runMotorControl();
|
||||
@@ -48,14 +48,14 @@ uint16_t MotorControl::loop() {
|
||||
}
|
||||
|
||||
void MotorControl::runMotorControl() {
|
||||
// Absolute difference between target_power and power
|
||||
uint8_t abs_difference = abs(this->target_power - this->power);
|
||||
// Absolute difference between targetPower and power
|
||||
uint8_t abs_difference = abs(this->targetPower - this->power);
|
||||
|
||||
// Difference between target_power and power
|
||||
int16_t difference = this->target_power - this->power;
|
||||
// Difference between targetPower and power
|
||||
int16_t difference = this->targetPower - this->power;
|
||||
|
||||
// Check that the target speed is close to 0 and that the abs_difference is lower than powersteps
|
||||
if (abs(this->target_power) < powersteps && abs_difference < powersteps) {
|
||||
if (abs(this->targetPower) < powersteps && abs_difference < powersteps) {
|
||||
this->setRealPower(0);
|
||||
return;
|
||||
}
|
||||
@@ -66,7 +66,7 @@ void MotorControl::runMotorControl() {
|
||||
}
|
||||
|
||||
// Positive or negative tagret speed
|
||||
if (this->target_power >= 0) {
|
||||
if (this->targetPower >= 0) {
|
||||
// Positive or negative speed
|
||||
if (this->power >= 0) {
|
||||
if (difference > 0) {
|
||||
@@ -95,36 +95,36 @@ void MotorControl::runMotorControl() {
|
||||
void MotorControl::setMinPwm(uint8_t min) {
|
||||
if (min > 80) min = 80;
|
||||
//transform percentage to real pwm value
|
||||
min = (uint8_t) (((1 << pwm_res) - 1) * (min / 100.0));
|
||||
this->dutycycle_min = min;
|
||||
min = (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 = (uint8_t) (((1 << pwm_res) - 1) * (max / 100.0));
|
||||
this->dutycycle_max = max;
|
||||
max = (uint8_t) (((1 << pwmRes) - 1) * (max / 100.0));
|
||||
this->dutycycleMax = max;
|
||||
}
|
||||
|
||||
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
|
||||
this->powersteps = increment;
|
||||
return (uint16_t) (delay * ( 100 / powersteps ));
|
||||
return (uint16_t) (delayLoop * ( 100 / powersteps ));
|
||||
}
|
||||
|
||||
void MotorControl::setTargetPower(int8_t power) {
|
||||
if (power <= 100 && power >= -100)
|
||||
this->target_power = power;
|
||||
this->targetPower = power;
|
||||
else
|
||||
std::cout << " MotorControl::setTargetPower: Invalid Argument - Power: " << power << std::endl;
|
||||
}
|
||||
|
||||
uint16_t MotorControl::setDelay(uint8_t delay) {
|
||||
this->delay = delay;
|
||||
return (uint16_t) (delay * ( 100 / powersteps ));
|
||||
uint16_t MotorControl::setDelay(uint8_t delayLoop) {
|
||||
this->delayLoop = delayLoop;
|
||||
return (uint16_t) (delayLoop * ( 100 / powersteps ));
|
||||
}
|
||||
|
||||
void MotorControl::stop() {
|
||||
this->target_power = 0;
|
||||
this->targetPower = 0;
|
||||
}
|
||||
|
||||
void MotorControl::emergencyStop() {
|
||||
@@ -136,23 +136,23 @@ int8_t MotorControl::getPower() {
|
||||
}
|
||||
|
||||
int8_t MotorControl::getTargetPower() {
|
||||
return this->target_power;
|
||||
return this->targetPower;
|
||||
}
|
||||
|
||||
bool MotorControl::isTargetPowerReached() {
|
||||
if (this->target_power == this->power)
|
||||
if (this->targetPower == this->power)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MotorControl::isAccelerationPositive() {
|
||||
if (power < target_power)
|
||||
if (power < targetPower)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MotorControl::isAccelerationNegative() {
|
||||
if (power > target_power)
|
||||
if (power > targetPower)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@@ -169,12 +169,12 @@ void MotorControl::setRealPower(int8_t power) {
|
||||
this->direction = 0;
|
||||
digitalWrite(this->dir_1, LOW);
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
ledcWrite(this->pwm_channel, 0);
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
this->dutycycle = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t pwm_val = map(abs(power), 0, 100, this->dutycycle_min, this->dutycycle_max);
|
||||
uint8_t pwm_val = map(abs(power), 0, 100, this->dutycycleMin, this->dutycycleMax);
|
||||
|
||||
if ((this->direction == 1 || this->direction == 0) && power < 0){ // new direction backward
|
||||
this->direction = 2;
|
||||
@@ -186,7 +186,7 @@ void MotorControl::setRealPower(int8_t power) {
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
}
|
||||
|
||||
ledcWrite(this->pwm_channel, pwm_val);
|
||||
ledcWrite(this->pwmChannel, pwm_val);
|
||||
this->dutycycle = pwm_val;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user