clangtidy corrections part 2
This commit is contained in:
@@ -5,20 +5,20 @@
|
||||
* @see motorControl.h
|
||||
* @version 0.1
|
||||
* @date 2021-12-13
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include "motorControl.h"
|
||||
|
||||
MotorControl::MotorControl() {
|
||||
this->setMinPwm(MotorControl::pwmMin);
|
||||
this->setMaxPwm(MotorControl::pwmMax);
|
||||
MotorControl::MotorControl()
|
||||
{
|
||||
Component::loopDelay = MotorControl::loopDelay;
|
||||
}
|
||||
|
||||
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, 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->pwmPin = pwmPin;
|
||||
this->pwmChannel = pwmChannel;
|
||||
this->dir_1 = dir_1;
|
||||
@@ -35,107 +35,141 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
}
|
||||
|
||||
void MotorControl::run() {
|
||||
void MotorControl::run()
|
||||
{
|
||||
// Absolute difference between targetPower and power
|
||||
uint8_t abs_difference = abs(this->targetPower - this->power);
|
||||
const uint8_t abs_difference = abs(this->targetPower - this->power);
|
||||
|
||||
// Difference between targetPower and power
|
||||
int16_t difference = this->targetPower - this->power;
|
||||
const int16_t difference = this->targetPower - this->power;
|
||||
|
||||
// Check that the target speed is close to 0 and that the abs_difference is lower than MotorControl::powerSteps
|
||||
if (abs(this->targetPower) < MotorControl::powerSteps && abs_difference < MotorControl::powerSteps) {
|
||||
if (abs(this->targetPower) < MotorControl::powerSteps && abs_difference < MotorControl::powerSteps)
|
||||
{
|
||||
this->setRealPower(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Correct speed
|
||||
if (abs_difference < MotorControl::powerSteps) {
|
||||
if (abs_difference < MotorControl::powerSteps)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Positive or negative tagret speed
|
||||
if (this->targetPower >= 0) {
|
||||
if (this->targetPower >= 0)
|
||||
{
|
||||
// Positive or negative speed
|
||||
if (this->power >= 0) {
|
||||
if (difference > 0) {
|
||||
if (this->power >= 0)
|
||||
{
|
||||
if (difference > 0)
|
||||
{
|
||||
this->increasePower(MotorControl::powerSteps);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
this->increasePower(-MotorControl::powerSteps);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
this->increasePower(MotorControl::powerSteps);
|
||||
}
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Positive or negative speed
|
||||
if (this->power >= 0) {
|
||||
if (this->power >= 0)
|
||||
{
|
||||
this->increasePower(-MotorControl::powerSteps);
|
||||
} else {
|
||||
if (difference > 0) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (difference > 0)
|
||||
{
|
||||
this->increasePower(MotorControl::powerSteps);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
this->increasePower(-MotorControl::powerSteps);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MotorControl::setMinPwm(uint8_t min) {
|
||||
if (min > 80) min = 80;
|
||||
//transform percentage to real pwm value
|
||||
min = (uint8_t) (((1 << pwmRes) - 1) * (min / 100.0));
|
||||
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 = (uint8_t) (((1 << pwmRes) - 1) * (max / 100.0));
|
||||
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) {
|
||||
void MotorControl::setTargetPower(int8_t power)
|
||||
{
|
||||
if (power <= 100 && power >= -100)
|
||||
{
|
||||
this->targetPower = power;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << " MotorControl::setTargetPower: Invalid Argument - Power: " << power << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void MotorControl::stop() {
|
||||
void MotorControl::stop()
|
||||
{
|
||||
this->targetPower = 0;
|
||||
}
|
||||
|
||||
void MotorControl::emergencyStop() {
|
||||
void MotorControl::emergencyStop()
|
||||
{
|
||||
setRealPower(0);
|
||||
}
|
||||
|
||||
bool MotorControl::isTargetPowerReached() const {
|
||||
if (this->targetPower == this->power)
|
||||
return true;
|
||||
return false;
|
||||
bool MotorControl::isTargetPowerReached() const
|
||||
{
|
||||
return this->targetPower == this->power;
|
||||
}
|
||||
|
||||
bool MotorControl::isAccelerationPositive() const {
|
||||
if (power < targetPower)
|
||||
return true;
|
||||
return false;
|
||||
bool MotorControl::isAccelerationPositive() const
|
||||
{
|
||||
return power < targetPower;
|
||||
}
|
||||
|
||||
bool MotorControl::isAccelerationNegative() const {
|
||||
if (power > targetPower)
|
||||
return true;
|
||||
return false;
|
||||
bool MotorControl::isAccelerationNegative() const
|
||||
{
|
||||
return power > targetPower;
|
||||
}
|
||||
|
||||
void MotorControl::setRealPower(int8_t power) {
|
||||
//TODO: Exceptionhandling
|
||||
if (power <= 100 && power >= -100) {
|
||||
void MotorControl::setRealPower(int8_t power)
|
||||
{
|
||||
// TODO: Exceptionhandling
|
||||
if (power <= 100 && power >= -100)
|
||||
{
|
||||
this->power = power;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->power == 0) {
|
||||
if (this->power == 0)
|
||||
{
|
||||
this->direction = 0;
|
||||
digitalWrite(this->dir_1, LOW);
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
@@ -144,13 +178,16 @@ void MotorControl::setRealPower(int8_t power) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t pwm_val = map(abs(power), 0, 100, this->dutycycleMin, this->dutycycleMax);
|
||||
const 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;
|
||||
digitalWrite(this->dir_1, LOW);
|
||||
digitalWrite(this->dir_2, HIGH);
|
||||
} else if ((this->direction == 2 || this->direction == 0) && power > 0){ // new direction forward
|
||||
}
|
||||
else if ((this->direction == 2 || this->direction == 0) && power > 0)
|
||||
{ // new direction forward
|
||||
this->direction = 1;
|
||||
digitalWrite(this->dir_1, HIGH);
|
||||
digitalWrite(this->dir_2, LOW);
|
||||
@@ -160,10 +197,12 @@ void MotorControl::setRealPower(int8_t power) {
|
||||
this->dutycycle = pwm_val;
|
||||
}
|
||||
|
||||
void MotorControl::increasePower(int8_t power) {
|
||||
//TODO: Exceptionhandling
|
||||
//TODO: make a stop befor a direction change
|
||||
if (abs(power) > 2 * MotorControl::powerSteps) {
|
||||
void MotorControl::increasePower(int8_t power)
|
||||
{
|
||||
// TODO: Exceptionhandling
|
||||
// TODO: make a stop befor a direction change
|
||||
if (abs(power) > 2 * MotorControl::powerSteps)
|
||||
{
|
||||
Serial.println("Invalid Argument in MotorControl::increasePower");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
* @brief Inherits a class to control a motor with pwm signal.
|
||||
* @version 0.1
|
||||
* @date 2021-12-09
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*
|
||||
*/
|
||||
#ifndef MOTOR_CONTROL_H
|
||||
#define MOTOR_CONTROL_H
|
||||
@@ -23,98 +23,98 @@
|
||||
* You can control the acceleration of the motor, for example to
|
||||
* prevent a damage on your H-Bridge.
|
||||
*/
|
||||
class MotorControl : public Component {
|
||||
public:
|
||||
MotorControl();
|
||||
class MotorControl : public Component
|
||||
{
|
||||
public:
|
||||
MotorControl();
|
||||
|
||||
/**
|
||||
* @brief Initialize the motorController
|
||||
*
|
||||
* @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 pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2);
|
||||
/**
|
||||
* @brief Initialize the motorController
|
||||
*
|
||||
* @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 pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2);
|
||||
|
||||
/**
|
||||
* @brief Set the minimum duty cycle
|
||||
*
|
||||
* @param min duty cycle in percent
|
||||
*/
|
||||
void setMinPwm(uint8_t min);
|
||||
/**
|
||||
* @brief Set the minimum duty cycle
|
||||
*
|
||||
* @param min duty cycle in percent
|
||||
*/
|
||||
void setMinPwm(uint8_t min);
|
||||
|
||||
/**
|
||||
* @brief Set the maximum duty cycle
|
||||
*
|
||||
* @param max duty cycle in percent
|
||||
*/
|
||||
void setMaxPwm(uint8_t max);
|
||||
/**
|
||||
* @brief Set the maximum duty cycle
|
||||
*
|
||||
* @param max duty cycle in percent
|
||||
*/
|
||||
void setMaxPwm(uint8_t max);
|
||||
|
||||
/**
|
||||
* @brief Set the Target Power
|
||||
*
|
||||
* If the given power is greater than 100 or smaller than -100, then
|
||||
* this function only print an error to consol.
|
||||
*
|
||||
* @param power power in percent
|
||||
*/
|
||||
void setTargetPower(int8_t power);
|
||||
/**
|
||||
* @brief Set the Target Power
|
||||
*
|
||||
* If the given power is greater than 100 or smaller than -100, then
|
||||
* this function only print an error to consol.
|
||||
*
|
||||
* @param power power in percent
|
||||
*/
|
||||
void setTargetPower(int8_t power);
|
||||
|
||||
/**
|
||||
* @brief Stops the motor like setTargetPower() to 0
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Stops the motor like setTargetPower() to 0
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* @brief Stops the motor immediately
|
||||
*
|
||||
*/
|
||||
void emergencyStop();
|
||||
/**
|
||||
* @brief Stops the motor immediately
|
||||
*
|
||||
*/
|
||||
void emergencyStop();
|
||||
|
||||
/**
|
||||
* @brief Get the current power
|
||||
*
|
||||
* @return int8_t percent of power (-100 to 100)
|
||||
*/
|
||||
int8_t getPower() const { return this->power; };
|
||||
/**
|
||||
* @brief Get the current power
|
||||
*
|
||||
* @return int8_t percent of power (-100 to 100)
|
||||
*/
|
||||
int8_t getPower() const { return this->power; };
|
||||
|
||||
/**
|
||||
* @brief Get the target power
|
||||
*
|
||||
* @return int8_t percent of power (-100 to 100)
|
||||
*/
|
||||
int8_t getTargetPower() const { return this->targetPower; };
|
||||
/**
|
||||
* @brief Get the target power
|
||||
*
|
||||
* @return int8_t percent of power (-100 to 100)
|
||||
*/
|
||||
int8_t getTargetPower() const { return this->targetPower; };
|
||||
|
||||
uint16_t getDutycycle() const { return this->dutycycle; }
|
||||
bool isTargetPowerReached() const;
|
||||
bool isAccelerationPositive() const;
|
||||
bool isAccelerationNegative() const;
|
||||
uint16_t getDutycycle() const { return this->dutycycle; }
|
||||
bool isTargetPowerReached() const;
|
||||
bool isAccelerationPositive() const;
|
||||
bool isAccelerationNegative() const;
|
||||
|
||||
private:
|
||||
void run() override;
|
||||
void setRealPower(int8_t power);
|
||||
void increasePower(int8_t power);
|
||||
private:
|
||||
void run() override;
|
||||
void setRealPower(int8_t power);
|
||||
void increasePower(int8_t power);
|
||||
|
||||
static constexpr uint8_t loopDelay = 10;
|
||||
static constexpr uint16_t pwmFreq = 16000;
|
||||
static constexpr uint8_t pwmRes = 8;
|
||||
static constexpr uint8_t powerSteps = 2; // A total of 20 levels ( 100 / SPEED_STEPS ) * RUN_MOTOR_CONTROL_DELAY = 500ms
|
||||
static constexpr uint8_t pwmMin = 55;
|
||||
static constexpr uint8_t pwmMax = 98; // Max 98% of 2^PWM_RES
|
||||
static constexpr uint8_t loopDelay = 10;
|
||||
static constexpr uint16_t pwmFreq = 16000;
|
||||
static constexpr uint8_t pwmRes = 8;
|
||||
static constexpr uint8_t powerSteps = 2; // A total of 20 levels ( 100 / SPEED_STEPS ) * RUN_MOTOR_CONTROL_DELAY = 500ms
|
||||
static constexpr uint8_t maxPwmMin = 80;
|
||||
|
||||
int8_t targetPower = 0;
|
||||
int8_t power = 0;
|
||||
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
|
||||
int8_t targetPower = 0;
|
||||
int8_t power = 0;
|
||||
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
|
||||
|
||||
uint8_t pwmPin;
|
||||
uint8_t pwmChannel;
|
||||
uint16_t dutycycle = 0;
|
||||
uint8_t dutycycleMin;
|
||||
uint8_t dutycycleMax;
|
||||
uint8_t dir_1;
|
||||
uint8_t dir_2;
|
||||
uint8_t pwmPin = 0;
|
||||
uint8_t pwmChannel = 0;
|
||||
uint16_t dutycycle = 0;
|
||||
uint8_t dutycycleMin = 55;
|
||||
uint8_t dutycycleMax = 98; // Max 98% of 2^PWM_RES
|
||||
uint8_t dir_1 = 0;
|
||||
uint8_t dir_2 = 0;
|
||||
};
|
||||
|
||||
#endif // MOTOR_CONTROL_H
|
||||
|
||||
Reference in New Issue
Block a user