remove most of the defines

This commit is contained in:
2023-08-18 15:34:56 +02:00
parent 4cd2b9368d
commit a82b32087c
21 changed files with 155 additions and 285 deletions
+14 -19
View File
@@ -13,9 +13,9 @@
#include "motorControl.h"
MotorControl::MotorControl() {
this->setMinPwm(PWMMIN);
this->setMaxPwm(PWMMAX);
this->loopDelay = DELAY;
this->setMinPwm(MotorControl::pwmMin);
this->setMaxPwm(MotorControl::pwmMax);
Component::loopDelay = MotorControl::loopDelay;
}
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2) {
@@ -30,7 +30,7 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
digitalWrite(this->dir_1, LOW);
digitalWrite(this->dir_2, LOW);
ledcSetup(this->pwmChannel, PWMFREQ, this->pwmRes);
ledcSetup(this->pwmChannel, MotorControl::pwmFreq, MotorControl::pwmRes);
ledcAttachPin(this->pwmPin, this->pwmChannel);
ledcWrite(this->pwmChannel, 0);
}
@@ -42,14 +42,14 @@ void MotorControl::run() {
// 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->targetPower) < powersteps && abs_difference < powersteps) {
// 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) {
this->setRealPower(0);
return;
}
// Correct speed
if (abs_difference < powersteps) {
if (abs_difference < MotorControl::powerSteps) {
return;
}
@@ -58,23 +58,23 @@ void MotorControl::run() {
// Positive or negative speed
if (this->power >= 0) {
if (difference > 0) {
this->increasePower(powersteps);
this->increasePower(MotorControl::powerSteps);
} else {
this->increasePower(-powersteps);
this->increasePower(-MotorControl::powerSteps);
}
} else {
this->increasePower(powersteps);
this->increasePower(MotorControl::powerSteps);
}
} else {
// Positive or negative speed
if (this->power >= 0) {
this->increasePower(-powersteps);
this->increasePower(-MotorControl::powerSteps);
} else {
if (difference > 0) {
this->increasePower(powersteps);
this->increasePower(MotorControl::powerSteps);
} else {
this->increasePower(-powersteps);
this->increasePower(-MotorControl::powerSteps);
}
}
}
@@ -94,11 +94,6 @@ void MotorControl::setMaxPwm(uint8_t max) {
this->dutycycleMax = max;
}
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
this->powersteps = increment;
return (uint16_t) (this->loopDelay * ( 100 / powersteps ));
}
void MotorControl::setTargetPower(int8_t power) {
if (power <= 100 && power >= -100)
this->targetPower = power;
@@ -168,7 +163,7 @@ void MotorControl::setRealPower(int8_t power) {
void MotorControl::increasePower(int8_t power) {
//TODO: Exceptionhandling
//TODO: make a stop befor a direction change
if (abs(power) > 2 * powersteps) {
if (abs(power) > 2 * MotorControl::powerSteps) {
Serial.println("Invalid Argument in MotorControl::increasePower");
return;
}
+9 -30
View File
@@ -16,14 +16,7 @@
#include <iostream>
#include <Arduino.h>
#include <component.h>
#define DELAY 10
#define PWMFREQ 16000
#define PWMRES 8
#define POWERSTEPS 2 // A total of 20 levels ( 100 / SPEED_STEPS ) * RUN_MOTOR_CONTROL_DELAY = 500ms
#define PWMMIN 55
#define PWMMAX 94 // Max 98% of 2^PWM_RES
#include "component.h"
/**
* @brief A class which use PWM to control the power of DC Motor
@@ -58,24 +51,6 @@ class MotorControl : public Component {
*/
void setMaxPwm(uint8_t max);
/**
* @brief Set the Power Steps
*
* Set the increment of the steps with which the dutycycle is
* increased or decreased. Note the dependency between the increment
* and delayLoop().
*
* The formula for the time between 0% and 100% power is:
* time[ms] = delayLoop * ( 100 / increment )
* 500 ms are recommended
*
* @see setDelay()
*
* @param increment
* @return time from 0% power to 100% power in Milliseconds
*/
uint16_t setPowerSteps(uint8_t increment);
/**
* @brief Set the Target Power
*
@@ -122,20 +97,24 @@ class MotorControl : public Component {
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
int8_t targetPower = 0;
int8_t power = 0;
uint8_t direction = 0; // 0 = stop, 1 = forward, 2 = backward
uint8_t pwmPin;
uint8_t pwmChannel;
uint8_t pwmRes = PWMRES;
uint16_t dutycycle = 0;
uint8_t dutycycleMin;
uint8_t dutycycleMax;
uint8_t dir_1;
uint8_t dir_2;
uint8_t powersteps = POWERSTEPS;
uint8_t dir_2;
};
#endif // MOTOR_CONTROL_H