cleaning
refactore all components now have a base class compnent for loop functions
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
MotorControl::MotorControl() {
|
||||
this->setMinPwm(PWMMIN);
|
||||
this->setMaxPwm(PWMMAX);
|
||||
this->loopDelay = DELAY;
|
||||
}
|
||||
|
||||
void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8_t dir_2) {
|
||||
@@ -34,20 +35,7 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
|
||||
ledcWrite(this->pwmChannel, 0);
|
||||
}
|
||||
|
||||
uint16_t MotorControl::loop() {
|
||||
uint32_t time = millis();
|
||||
uint16_t elapsed_time = time - this->lastMillis;
|
||||
|
||||
//Cancel if delayLoop is not reached
|
||||
if (elapsed_time < delayLoop)
|
||||
return elapsed_time;
|
||||
|
||||
runMotorControl();
|
||||
this->lastMillis = time;
|
||||
return elapsed_time;
|
||||
}
|
||||
|
||||
void MotorControl::runMotorControl() {
|
||||
void MotorControl::run() {
|
||||
// Absolute difference between targetPower and power
|
||||
uint8_t abs_difference = abs(this->targetPower - this->power);
|
||||
|
||||
@@ -108,7 +96,7 @@ void MotorControl::setMaxPwm(uint8_t max) {
|
||||
|
||||
uint16_t MotorControl::setPowerSteps(uint8_t increment) {
|
||||
this->powersteps = increment;
|
||||
return (uint16_t) (delayLoop * ( 100 / powersteps ));
|
||||
return (uint16_t) (this->loopDelay * ( 100 / powersteps ));
|
||||
}
|
||||
|
||||
void MotorControl::setTargetPower(int8_t power) {
|
||||
@@ -118,11 +106,6 @@ void MotorControl::setTargetPower(int8_t power) {
|
||||
std::cout << " MotorControl::setTargetPower: Invalid Argument - Power: " << power << std::endl;
|
||||
}
|
||||
|
||||
uint16_t MotorControl::setDelay(uint8_t delayLoop) {
|
||||
this->delayLoop = delayLoop;
|
||||
return (uint16_t) (delayLoop * ( 100 / powersteps ));
|
||||
}
|
||||
|
||||
void MotorControl::stop() {
|
||||
this->targetPower = 0;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <iostream>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <component.h>
|
||||
|
||||
#define DELAY 10
|
||||
#define PWMFREQ 16000
|
||||
#define PWMRES 8
|
||||
@@ -28,7 +30,7 @@
|
||||
* You can control the acceleration of the motor, for example to
|
||||
* prevent a damage on your H-Bridge.
|
||||
*/
|
||||
class MotorControl {
|
||||
class MotorControl : public Component {
|
||||
public:
|
||||
MotorControl();
|
||||
|
||||
@@ -42,26 +44,6 @@ class MotorControl {
|
||||
*/
|
||||
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 delayLoop is not reached, than the
|
||||
* functions returns immediately.
|
||||
* @see runMotorControl()
|
||||
* @see DELAY
|
||||
* @return time since the last call in Milliseconds
|
||||
*/
|
||||
uint16_t loop();
|
||||
|
||||
/**
|
||||
* @brief Normally called repeatedly by loop() to update the pwm signal.
|
||||
*
|
||||
* Checks the difference between target power and current power to
|
||||
* increase or decrease the duty cycle. The amount of decrease or increase
|
||||
* is set by setPowerSetps() (default = 2).
|
||||
*/
|
||||
void runMotorControl();
|
||||
|
||||
/**
|
||||
* @brief Set the minimum duty cycle
|
||||
*
|
||||
@@ -104,19 +86,6 @@ class MotorControl {
|
||||
*/
|
||||
void setTargetPower(int8_t power);
|
||||
|
||||
/**
|
||||
* @brief Set the min delayLoop between each loop
|
||||
*
|
||||
* Note the dependency between delayLoop and
|
||||
* setPowerSteps().
|
||||
*
|
||||
* @see setPowerSteps()
|
||||
*
|
||||
* @param delayLoop time in Milliseconds
|
||||
* @return time from 0% power to 100% power in Milliseconds
|
||||
*/
|
||||
uint16_t setDelay(uint8_t delayLoop);
|
||||
|
||||
/**
|
||||
* @brief Stops the motor like setTargetPower() to 0
|
||||
*
|
||||
@@ -149,6 +118,7 @@ class MotorControl {
|
||||
bool isAccelerationNegative();
|
||||
|
||||
private:
|
||||
void run() override;
|
||||
void setRealPower(int8_t power);
|
||||
void increasePower(int8_t power);
|
||||
|
||||
@@ -164,10 +134,7 @@ class MotorControl {
|
||||
uint8_t dutycycleMax;
|
||||
uint8_t dir_1;
|
||||
uint8_t dir_2;
|
||||
uint8_t delayLoop = DELAY;
|
||||
uint8_t powersteps = POWERSTEPS;
|
||||
|
||||
uint32_t lastMillis = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user