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;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,17 +35,17 @@ class MotorControl {
|
||||
/**
|
||||
* @brief Initialize the motorController
|
||||
*
|
||||
* @param pwm_pin The output pin for the signal on the esp.
|
||||
* @param pwm_channel One of the pwm channels from the esp.
|
||||
* @param pwmPin The output pin for the signal on the esp.
|
||||
* @param pwmChannel One of the pwm channels from the esp.
|
||||
* @param dir_1 First direction pin for the H-Bridge.
|
||||
* @param dir_2 Second direction pin for the H-Bridge.
|
||||
*/
|
||||
void init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uint8_t dir_2);
|
||||
void init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2);
|
||||
|
||||
/**
|
||||
* @brief Calls runMotorControl() to update the pwm signal
|
||||
*
|
||||
* This function should be called every mainloop. If the delay is not reached, than the
|
||||
* This function should be called every mainloop. If the delayLoop is not reached, than the
|
||||
* functions returns immediately.
|
||||
* @see runMotorControl()
|
||||
* @see DELAY
|
||||
@@ -81,10 +81,10 @@ class MotorControl {
|
||||
*
|
||||
* Set the increment of the steps with which the dutycycle is
|
||||
* increased or decreased. Note the dependency between the increment
|
||||
* and delay().
|
||||
* and delayLoop().
|
||||
*
|
||||
* The formula for the time between 0% and 100% power is:
|
||||
* time[ms] = delay * ( 100 / increment )
|
||||
* time[ms] = delayLoop * ( 100 / increment )
|
||||
* 500 ms are recommended
|
||||
*
|
||||
* @see setDelay()
|
||||
@@ -105,17 +105,17 @@ class MotorControl {
|
||||
void setTargetPower(int8_t power);
|
||||
|
||||
/**
|
||||
* @brief Set the min delay between each loop
|
||||
* @brief Set the min delayLoop between each loop
|
||||
*
|
||||
* Note the dependency between delay and
|
||||
* Note the dependency between delayLoop and
|
||||
* setPowerSteps().
|
||||
*
|
||||
* @see setPowerSteps()
|
||||
*
|
||||
* @param delay time in Milliseconds
|
||||
* @param delayLoop time in Milliseconds
|
||||
* @return time from 0% power to 100% power in Milliseconds
|
||||
*/
|
||||
uint16_t setDelay(uint8_t delay);
|
||||
uint16_t setDelay(uint8_t delayLoop);
|
||||
|
||||
/**
|
||||
* @brief Stops the motor like setTargetPower() to 0
|
||||
@@ -152,19 +152,19 @@ class MotorControl {
|
||||
void setRealPower(int8_t power);
|
||||
void increasePower(int8_t power);
|
||||
|
||||
int8_t target_power = 0;
|
||||
int8_t targetPower = 0;
|
||||
int8_t power = 0;
|
||||
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
|
||||
|
||||
uint8_t pwm_pin;
|
||||
uint8_t pwm_channel;
|
||||
uint8_t pwm_res = PWMRES;
|
||||
uint8_t pwmPin;
|
||||
uint8_t pwmChannel;
|
||||
uint8_t pwmRes = PWMRES;
|
||||
uint16_t dutycycle = 0;
|
||||
uint8_t dutycycle_min;
|
||||
uint8_t dutycycle_max;
|
||||
uint8_t dutycycleMin;
|
||||
uint8_t dutycycleMax;
|
||||
uint8_t dir_1;
|
||||
uint8_t dir_2;
|
||||
uint8_t delay = DELAY;
|
||||
uint8_t delayLoop = DELAY;
|
||||
uint8_t powersteps = POWERSTEPS;
|
||||
|
||||
uint32_t lastMillis = 0;
|
||||
|
||||
Reference in New Issue
Block a user