refactore motorControl
This commit is contained in:
@@ -17,9 +17,9 @@ MotorControl::MotorControl() {
|
|||||||
this->setMaxPwm(PWMMAX);
|
this->setMaxPwm(PWMMAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uint8_t dir_2) {
|
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2) {
|
||||||
this->pwm_pin = pwm_pin;
|
this->pwmPin = pwmPin;
|
||||||
this->pwm_channel = pwm_channel;
|
this->pwmChannel = pwmChannel;
|
||||||
this->dir_1 = dir_1;
|
this->dir_1 = dir_1;
|
||||||
this->dir_2 = dir_2;
|
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_1, LOW);
|
||||||
digitalWrite(this->dir_2, LOW);
|
digitalWrite(this->dir_2, LOW);
|
||||||
|
|
||||||
ledcSetup(this->pwm_channel, PWMFREQ, this->pwm_res);
|
ledcSetup(this->pwmChannel, PWMFREQ, this->pwmRes);
|
||||||
ledcAttachPin(this->pwm_pin, this->pwm_channel);
|
ledcAttachPin(this->pwmPin, this->pwmChannel);
|
||||||
ledcWrite(this->pwm_channel, 0);
|
ledcWrite(this->pwmChannel, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t MotorControl::loop() {
|
uint16_t MotorControl::loop() {
|
||||||
uint32_t time = millis();
|
uint32_t time = millis();
|
||||||
uint16_t elapsed_time = time - this->lastMillis;
|
uint16_t elapsed_time = time - this->lastMillis;
|
||||||
|
|
||||||
//Cancel if delay is not reached
|
//Cancel if delayLoop is not reached
|
||||||
if (elapsed_time < delay)
|
if (elapsed_time < delayLoop)
|
||||||
return elapsed_time;
|
return elapsed_time;
|
||||||
|
|
||||||
runMotorControl();
|
runMotorControl();
|
||||||
@@ -48,14 +48,14 @@ uint16_t MotorControl::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::runMotorControl() {
|
void MotorControl::runMotorControl() {
|
||||||
// Absolute difference between target_power and power
|
// Absolute difference between targetPower and power
|
||||||
uint8_t abs_difference = abs(this->target_power - this->power);
|
uint8_t abs_difference = abs(this->targetPower - this->power);
|
||||||
|
|
||||||
// Difference between target_power and power
|
// Difference between targetPower and power
|
||||||
int16_t difference = this->target_power - this->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
|
// 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);
|
this->setRealPower(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ void MotorControl::runMotorControl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Positive or negative tagret speed
|
// Positive or negative tagret speed
|
||||||
if (this->target_power >= 0) {
|
if (this->targetPower >= 0) {
|
||||||
// Positive or negative speed
|
// Positive or negative speed
|
||||||
if (this->power >= 0) {
|
if (this->power >= 0) {
|
||||||
if (difference > 0) {
|
if (difference > 0) {
|
||||||
@@ -95,36 +95,36 @@ void MotorControl::runMotorControl() {
|
|||||||
void MotorControl::setMinPwm(uint8_t min) {
|
void MotorControl::setMinPwm(uint8_t min) {
|
||||||
if (min > 80) min = 80;
|
if (min > 80) min = 80;
|
||||||
//transform percentage to real pwm value
|
//transform percentage to real pwm value
|
||||||
min = (uint8_t) (((1 << pwm_res) - 1) * (min / 100.0));
|
min = (uint8_t) (((1 << pwmRes) - 1) * (min / 100.0));
|
||||||
this->dutycycle_min = min;
|
this->dutycycleMin = min;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::setMaxPwm(uint8_t max) {
|
void MotorControl::setMaxPwm(uint8_t max) {
|
||||||
if (max > 100) max = 100;
|
if (max > 100) max = 100;
|
||||||
//transform percentage to real pwm value
|
//transform percentage to real pwm value
|
||||||
max = (uint8_t) (((1 << pwm_res) - 1) * (max / 100.0));
|
max = (uint8_t) (((1 << pwmRes) - 1) * (max / 100.0));
|
||||||
this->dutycycle_max = max;
|
this->dutycycleMax = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
|
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
|
||||||
this->powersteps = increment;
|
this->powersteps = increment;
|
||||||
return (uint16_t) (delay * ( 100 / powersteps ));
|
return (uint16_t) (delayLoop * ( 100 / powersteps ));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::setTargetPower(int8_t power) {
|
void MotorControl::setTargetPower(int8_t power) {
|
||||||
if (power <= 100 && power >= -100)
|
if (power <= 100 && power >= -100)
|
||||||
this->target_power = power;
|
this->targetPower = power;
|
||||||
else
|
else
|
||||||
std::cout << " MotorControl::setTargetPower: Invalid Argument - Power: " << power << std::endl;
|
std::cout << " MotorControl::setTargetPower: Invalid Argument - Power: " << power << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t MotorControl::setDelay(uint8_t delay) {
|
uint16_t MotorControl::setDelay(uint8_t delayLoop) {
|
||||||
this->delay = delay;
|
this->delayLoop = delayLoop;
|
||||||
return (uint16_t) (delay * ( 100 / powersteps ));
|
return (uint16_t) (delayLoop * ( 100 / powersteps ));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::stop() {
|
void MotorControl::stop() {
|
||||||
this->target_power = 0;
|
this->targetPower = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorControl::emergencyStop() {
|
void MotorControl::emergencyStop() {
|
||||||
@@ -136,23 +136,23 @@ int8_t MotorControl::getPower() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int8_t MotorControl::getTargetPower() {
|
int8_t MotorControl::getTargetPower() {
|
||||||
return this->target_power;
|
return this->targetPower;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MotorControl::isTargetPowerReached() {
|
bool MotorControl::isTargetPowerReached() {
|
||||||
if (this->target_power == this->power)
|
if (this->targetPower == this->power)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MotorControl::isAccelerationPositive() {
|
bool MotorControl::isAccelerationPositive() {
|
||||||
if (power < target_power)
|
if (power < targetPower)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MotorControl::isAccelerationNegative() {
|
bool MotorControl::isAccelerationNegative() {
|
||||||
if (power > target_power)
|
if (power > targetPower)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -169,12 +169,12 @@ void MotorControl::setRealPower(int8_t power) {
|
|||||||
this->direction = 0;
|
this->direction = 0;
|
||||||
digitalWrite(this->dir_1, LOW);
|
digitalWrite(this->dir_1, LOW);
|
||||||
digitalWrite(this->dir_2, LOW);
|
digitalWrite(this->dir_2, LOW);
|
||||||
ledcWrite(this->pwm_channel, 0);
|
ledcWrite(this->pwmChannel, 0);
|
||||||
this->dutycycle = 0;
|
this->dutycycle = 0;
|
||||||
return;
|
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
|
if ((this->direction == 1 || this->direction == 0) && power < 0){ // new direction backward
|
||||||
this->direction = 2;
|
this->direction = 2;
|
||||||
@@ -186,7 +186,7 @@ void MotorControl::setRealPower(int8_t power) {
|
|||||||
digitalWrite(this->dir_2, LOW);
|
digitalWrite(this->dir_2, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
ledcWrite(this->pwm_channel, pwm_val);
|
ledcWrite(this->pwmChannel, pwm_val);
|
||||||
this->dutycycle = pwm_val;
|
this->dutycycle = pwm_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,17 +35,17 @@ class MotorControl {
|
|||||||
/**
|
/**
|
||||||
* @brief Initialize the motorController
|
* @brief Initialize the motorController
|
||||||
*
|
*
|
||||||
* @param pwm_pin The output pin for the signal on the esp.
|
* @param pwmPin The output pin for the signal on the esp.
|
||||||
* @param pwm_channel One of the pwm channels from the esp.
|
* @param pwmChannel One of the pwm channels from the esp.
|
||||||
* @param dir_1 First direction pin for the H-Bridge.
|
* @param dir_1 First direction pin for the H-Bridge.
|
||||||
* @param dir_2 Second 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
|
* @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.
|
* functions returns immediately.
|
||||||
* @see runMotorControl()
|
* @see runMotorControl()
|
||||||
* @see DELAY
|
* @see DELAY
|
||||||
@@ -81,10 +81,10 @@ class MotorControl {
|
|||||||
*
|
*
|
||||||
* Set the increment of the steps with which the dutycycle is
|
* Set the increment of the steps with which the dutycycle is
|
||||||
* increased or decreased. Note the dependency between the increment
|
* increased or decreased. Note the dependency between the increment
|
||||||
* and delay().
|
* and delayLoop().
|
||||||
*
|
*
|
||||||
* The formula for the time between 0% and 100% power is:
|
* 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
|
* 500 ms are recommended
|
||||||
*
|
*
|
||||||
* @see setDelay()
|
* @see setDelay()
|
||||||
@@ -105,17 +105,17 @@ class MotorControl {
|
|||||||
void setTargetPower(int8_t power);
|
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().
|
* setPowerSteps().
|
||||||
*
|
*
|
||||||
* @see setPowerSteps()
|
* @see setPowerSteps()
|
||||||
*
|
*
|
||||||
* @param delay time in Milliseconds
|
* @param delayLoop time in Milliseconds
|
||||||
* @return time from 0% power to 100% power 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
|
* @brief Stops the motor like setTargetPower() to 0
|
||||||
@@ -152,19 +152,19 @@ class MotorControl {
|
|||||||
void setRealPower(int8_t power);
|
void setRealPower(int8_t power);
|
||||||
void increasePower(int8_t power);
|
void increasePower(int8_t power);
|
||||||
|
|
||||||
int8_t target_power = 0;
|
int8_t targetPower = 0;
|
||||||
int8_t power = 0;
|
int8_t power = 0;
|
||||||
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
|
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
|
||||||
|
|
||||||
uint8_t pwm_pin;
|
uint8_t pwmPin;
|
||||||
uint8_t pwm_channel;
|
uint8_t pwmChannel;
|
||||||
uint8_t pwm_res = PWMRES;
|
uint8_t pwmRes = PWMRES;
|
||||||
uint16_t dutycycle = 0;
|
uint16_t dutycycle = 0;
|
||||||
uint8_t dutycycle_min;
|
uint8_t dutycycleMin;
|
||||||
uint8_t dutycycle_max;
|
uint8_t dutycycleMax;
|
||||||
uint8_t dir_1;
|
uint8_t dir_1;
|
||||||
uint8_t dir_2;
|
uint8_t dir_2;
|
||||||
uint8_t delay = DELAY;
|
uint8_t delayLoop = DELAY;
|
||||||
uint8_t powersteps = POWERSTEPS;
|
uint8_t powersteps = POWERSTEPS;
|
||||||
|
|
||||||
uint32_t lastMillis = 0;
|
uint32_t lastMillis = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user