From a7940c887b654fff3606e8292617a09d3999d3a0 Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Mon, 13 Dec 2021 16:44:53 +0100 Subject: [PATCH] comments for Doxygen all private Libs --- lib/Debug/MQTT/debugMqtt.cpp | 96 +++++----- lib/Debug/MQTT/debugMqtt.h | 135 ++++++++++++-- lib/Debug/Times/debugTimes.cpp | 20 ++- lib/Debug/Times/debugTimes.h | 47 ++++- .../Illumination/underfloorLighting.cpp | 0 .../Illumination/underfloorLighting.h | 0 .../MotorControl/motorControl.cpp | 50 +++++- lib/Hardware/MotorControl/motorControl.h | 164 ++++++++++++++++++ .../Speedometer/speedometer.cpp | 11 ++ lib/{ => Hardware}/Speedometer/speedometer.h | 2 +- lib/MotorControl/motorControl.h | 86 --------- 11 files changed, 448 insertions(+), 163 deletions(-) rename lib/{ => Hardware}/Illumination/underfloorLighting.cpp (100%) rename lib/{ => Hardware}/Illumination/underfloorLighting.h (100%) rename lib/{ => Hardware}/MotorControl/motorControl.cpp (77%) create mode 100644 lib/Hardware/MotorControl/motorControl.h rename lib/{ => Hardware}/Speedometer/speedometer.cpp (91%) rename lib/{ => Hardware}/Speedometer/speedometer.h (97%) delete mode 100644 lib/MotorControl/motorControl.h diff --git a/lib/Debug/MQTT/debugMqtt.cpp b/lib/Debug/MQTT/debugMqtt.cpp index fb81f8c..e198c14 100644 --- a/lib/Debug/MQTT/debugMqtt.cpp +++ b/lib/Debug/MQTT/debugMqtt.cpp @@ -1,12 +1,21 @@ +/** + * @file debugMqtt.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief Implemention of the class debugMqtt.h + * @see debugMqtt.h + * @version 0.1 + * @date 2021-12-13 + * + * @copyright Copyright (c) 2021 + * + */ #include "debugMqtt.h" PubSubClient* DebugMqtt::client; Loglevel DebugMqtt::loglevel; bool DebugMqtt::isInit = false; -char DebugMqtt::msg[MQTT_BUFFER_SITE]; -char DebugMqtt::topic[MQTT_BUFFER_SITE]; -unsigned long long int DebugMqtt::real_millis = 0; -unsigned long int DebugMqtt::last_millis = 0; +char DebugMqtt::msg[MQTT_BUFFER_SIZE]; +char DebugMqtt::topic[MQTT_BUFFER_SIZE]; DebugMqtt::DebugMqtt(const char* name) { @@ -14,7 +23,7 @@ DebugMqtt::DebugMqtt(const char* name) { } void DebugMqtt::sendMsg(Loglevel loglevel, String topic, String msg) { - snprintf (DebugMqtt::msg, MQTT_BUFFER_SITE, "%s: %s",this->name ,msg.c_str()); + snprintf (DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s: %s",this->name ,msg.c_str()); this->sendData(loglevel, topic, DebugMqtt::msg); } @@ -26,8 +35,8 @@ void DebugMqtt::sendData(Loglevel loglevel, String topic, String data) { if (!DebugMqtt::isInit) {return;} if (loglevel <= DebugMqtt::loglevel && loglevel > Loglevel::none) { - snprintf (DebugMqtt::topic, MQTT_BUFFER_SITE, "%s%s", DebugMqtt::enum_to_string(loglevel).c_str(), topic.c_str()); - snprintf (DebugMqtt::msg, MQTT_BUFFER_SITE, "%s", data.c_str()); + snprintf (DebugMqtt::topic, MQTT_BUFFER_SIZE, "%s%s", DebugMqtt::enum_to_string(loglevel).c_str(), topic.c_str()); + snprintf (DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s", data.c_str()); client->publish(DebugMqtt::topic, DebugMqtt::msg); } } @@ -36,24 +45,14 @@ void DebugMqtt::sendData(Loglevel loglevel, String data){ this->sendData(loglevel, "", data); } -void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement) { +void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos) { // Example String: "weather temperature=82 1465839830100400200"; - unsigned long long int nanos = DebugMqtt::getUpdatedRealMillis() * 1000000; - snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%s %s=%f %llu", measurement_name.c_str(), field_set.c_str(), measurement, nanos); + snprintf(DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s %s=%f %llu", measurement_name.c_str(), field_set.c_str(), measurement, nanos); this->sendData(Loglevel::influx, DebugMqtt::msg); } -void DebugMqtt::sendTime() { - struct tm timeinfo; - if(!getLocalTime(&timeinfo)){ - snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "Failed to obtain time"); - } - snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%d %d %d %d %d %d %llu", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, DebugMqtt::getUpdatedRealMillis()); - this->sendMsg(Loglevel::info, DebugMqtt::msg); -} - -void DebugMqtt::init(PubSubClient *client, Loglevel loglevel) { +void DebugMqtt::init(PubSubClient *client, Loglevel max_loglevel) { DebugMqtt::client = client; DebugMqtt::loglevel = loglevel; DebugMqtt::isInit = true; @@ -63,40 +62,29 @@ void DebugMqtt::changeLoglevel(Loglevel loglevel) { DebugMqtt::loglevel = loglevel; } -void DebugMqtt::initRealMillis() { - time_t now; - struct tm timeinfo; - if(!getLocalTime(&timeinfo)){ - snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "Failed to obtain time in initRealMillis()"); - DebugMqtt::client->publish(DebugMqtt::enum_to_string(Loglevel::error).c_str(), DebugMqtt::msg); - return; - } - time(&now); - DebugMqtt::last_millis = millis(); - DebugMqtt::real_millis = now; - DebugMqtt::real_millis = DebugMqtt::real_millis * 1000 + DebugMqtt::last_millis; -} - -unsigned long long int DebugMqtt::getUpdatedRealMillis() { - DebugMqtt::real_millis = DebugMqtt::real_millis + (millis() - DebugMqtt::last_millis); - DebugMqtt::last_millis = millis(); - return DebugMqtt::real_millis; -} - String DebugMqtt::enum_to_string(Loglevel loglevel) { - switch(loglevel){ - case Loglevel::error : - return "Rover/Error"; - case Loglevel::warn : - return "Rover/Warn"; - case Loglevel::info : - return "Rover/Info"; - case Loglevel::debug : - return "Rover/Debug"; - case Loglevel::influx : - return "Rover/Influx"; - default: - return "INVALID ENUM"; - } + String topic = ""; + topic += MQTT_DEBUG_TOPIC; + switch(loglevel){ + case Loglevel::error : + topic += "/Error"; + break; + case Loglevel::warn : + topic += "/Warn"; + break; + case Loglevel::info : + topic += "/Info"; + break; + case Loglevel::debug : + topic += "/Debug"; + break; + case Loglevel::influx : + topic += "/Influx"; + break; + default: + topic += "/NoLoglovel"; + break; + } + return topic; } diff --git a/lib/Debug/MQTT/debugMqtt.h b/lib/Debug/MQTT/debugMqtt.h index 316be20..2027f2a 100644 --- a/lib/Debug/MQTT/debugMqtt.h +++ b/lib/Debug/MQTT/debugMqtt.h @@ -1,10 +1,51 @@ +/** + * @file debugMqtt.h + * @author Alexander Klein (alex@kleiax.de) + * @brief Inherits a class to send debug messages over MQTT + * @version 0.1 + * @date 2021-12-13 + * + * @copyright Copyright (c) 2021 + * + */ #ifndef DEBUG_MQTT_H #define DEBUG_MQTT_H #include -#include "config.h" +/** + * @brief + * + * If you want to change the default topic + * to an other value, than you have to define this define + * in your code befor you include this File. + */ +#ifndef MQTT_DEBUG_TOPIC +#define MQTT_DEBUG_TOPIC "Rover" +#endif // MQTT_DEBUG_TOPIC +/** + * @brief Defualt for max message size + * + * If you want to change the default size of 128 Byte + * to an other value, than you have to define this define + * in your code befor you include this File. + */ +#ifndef MQTT_BUFFER_SIZE +#define MQTT_BUFFER_SIZE 128 +#endif //MQTT_BUFFER_SIZE + +/** + * @brief An enum to set the log level + * + * The log level is the last part of the MQTT topic. + * Unless you give sendMsg() or sendData() a additional + * topic as String. + * + * @see sendMsg() + * @see sendData() + * + */ enum Loglevel { none, error, warn, @@ -12,21 +53,95 @@ enum Loglevel { none, debug, influx}; +/** + * @brief A class to send debug messages over MQTT + * + * This class send debug messages over MQTT with different topics + * by the enum Loglevel. Besides that this class supports to send + * data via Telegraf into Grafana. + * + * @see Loglevel + */ class DebugMqtt { public: + /** + * @brief Construct a new Debug Mqtt object. + * + * @param name A String with send with every Message. + */ DebugMqtt(const char* name); + + /** + * @brief Send a Message via MQTT + * + * This funtion uses sendData to send the given string and + * add the name to the message given by the constructer. + * + * @see sendData() + * @see Loglevel + * + * @param loglevel Loglevel given by the enum Loglevel + * @param topic Additional topic behind loglevel + * @param msg The message to send as String + */ void sendMsg(Loglevel loglevel, String topic, String msg); void sendMsg(Loglevel loglevel, String msg); + + /** + * @brief Send a Message via MQTT + * + * This funtion sends the Data via MQTT with the given topic + * from Loglevel or followed by given String topic. + * Normaly this function is called by sendMsg() or by + * writeToInflux() + * + * @see sendData() + * @see writeToInflux() + * @see Loglevel + * + * @param loglevel Loglevel given by the enum Loglevel + * @param topic Additional topic behind loglevel + * @param data The message to send as String + */ void sendData(Loglevel loglevel, String topic, String data); void sendData(Loglevel loglevel, String data); - void writeToInflux(String measurement_name, String field_set, float measurement); - void sendTime(); + /** + * @brief Send a Message via MQTT for InfluxDB + * + * This funtion sends a MQTT message which is intended for + * Telegraf. Telegraf can listen on MQTT messages and put + * them in an Influx Database. + * + * @param measurement_name like a category + * @param field_set the name of the value e.g temperature + * @param measurement the real value + */ + void writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos); - static void init(PubSubClient *client, Loglevel loglevel); + /** + * @brief Initalize debugMQTT for all instances + * + * You only have to call this function once for your project. + * If you call this funtion again you overwrite the client and + * the loglevel. If you only eant du overwrite the max_loglevel + * use changeLoglevel() + * + * @see changeLoglevel() + * + * @param client PubSubClient + * @param max_loglevel Max loglevel to send. + */ + static void init(PubSubClient *client, Loglevel max_loglevel); + + /** + * @brief Change loglevel + * + * This function changes the maximum loglevel which be send. + * + * @param loglevel + */ static void changeLoglevel(Loglevel loglevel); - static void initRealMillis(); - static unsigned long long int getUpdatedRealMillis(); private: const char* name; @@ -36,12 +151,8 @@ class DebugMqtt { static PubSubClient* client; static Loglevel loglevel; static bool isInit; - static char msg[MQTT_BUFFER_SITE]; - static char topic[MQTT_BUFFER_SITE]; - - static unsigned long long int real_millis; - static unsigned long int last_millis; - + static char msg[MQTT_BUFFER_SIZE]; + static char topic[MQTT_BUFFER_SIZE]; }; #endif // DEBUG_MQTT_H \ No newline at end of file diff --git a/lib/Debug/Times/debugTimes.cpp b/lib/Debug/Times/debugTimes.cpp index ffa189b..c1f3518 100644 --- a/lib/Debug/Times/debugTimes.cpp +++ b/lib/Debug/Times/debugTimes.cpp @@ -1,10 +1,28 @@ +/** + * @file debugTimes.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief Implemention of the class debugTimes.h. + * @version 0.1 + * @date 2021-12-13 + * + * @copyright Copyright (c) 2021 + * + */ #include "debugTimes.h" DebugTimes::DebugTimes() { this->startTime = millis(); } -void DebugTimes::stop(const char* name, uint16_t minTime) { +void DebugTimes::restart() { + this->startTime = millis(); +} + +uint16_t DebugTimes::stop() { + return millis() - this->startTime; +} + +void DebugTimes::stopConsol(const char* name, uint16_t minTime) { uint64_t time = millis() - this->startTime; if (time > minTime) Serial.printf("%s needs %llu ms\n", name, time); diff --git a/lib/Debug/Times/debugTimes.h b/lib/Debug/Times/debugTimes.h index 9aca030..50e97da 100644 --- a/lib/Debug/Times/debugTimes.h +++ b/lib/Debug/Times/debugTimes.h @@ -1,3 +1,14 @@ +/** + * @file debugTimes.h + * @author Alexander Klein (alex@kleiax.de) + * @brief Inherits a class to measure times of functions. + * @version 0.1 + * @date 2021-12-13 + * + * @copyright Copyright (c) 2021 + * + */ + #ifndef DEBUG_TIMES_H #define DEBUG_TIMES_H @@ -6,10 +17,44 @@ #include +/** + * @brief A class to measure times of functions. + * + * This simple class only save the value of the millis() + * function when you call the constructor or restart(). + * To get the elapsed time call stop() or stopConsol(). + * + * @warning This class is not very accurate + * It only give you the time in milliseconds. + */ class DebugTimes { public: + /** + * @brief Construct a new Debug Times object + * Starts to count milliseconds + */ + DebugTimes(); - void stop(const char* name, uint16_t minTime = 0); + /** + * @brief Set the counter to 0 + */ + + void restart(); + + /** + * @brief Give the elapsed time + * + * @return uint16_t elapsed milliseconds + */ + uint16_t stop(); + + /** + * @brief Print the elapsed time to consol + * + * @param name Functionname to print + * @param minTime A minimum time before printing + */ + void stopConsol(const char* name, uint16_t minTime = 0); private: uint64_t startTime; diff --git a/lib/Illumination/underfloorLighting.cpp b/lib/Hardware/Illumination/underfloorLighting.cpp similarity index 100% rename from lib/Illumination/underfloorLighting.cpp rename to lib/Hardware/Illumination/underfloorLighting.cpp diff --git a/lib/Illumination/underfloorLighting.h b/lib/Hardware/Illumination/underfloorLighting.h similarity index 100% rename from lib/Illumination/underfloorLighting.h rename to lib/Hardware/Illumination/underfloorLighting.h diff --git a/lib/MotorControl/motorControl.cpp b/lib/Hardware/MotorControl/motorControl.cpp similarity index 77% rename from lib/MotorControl/motorControl.cpp rename to lib/Hardware/MotorControl/motorControl.cpp index d71d43c..399455f 100644 --- a/lib/MotorControl/motorControl.cpp +++ b/lib/Hardware/MotorControl/motorControl.cpp @@ -1,6 +1,21 @@ +/** + * @file motorControl.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief Implemention of the class motorControl.h. + * @see motorControl.h + * @version 0.1 + * @date 2021-12-13 + * + * @copyright Copyright (c) 2021 + * + */ + #include "motorControl.h" -MotorControl::MotorControl() {} +MotorControl::MotorControl() { + this->setMinPwm(PWMMIN); + 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; @@ -14,7 +29,7 @@ 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, PWMRES); + ledcSetup(this->pwm_channel, PWMFREQ, this->pwm_res); ledcAttachPin(this->pwm_pin, this->pwm_channel); ledcWrite(this->pwm_channel, 0); } @@ -76,6 +91,25 @@ 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)); + this->dutycycle_min = 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)); + this->dutycycle_max = max; +} + +uint16_t MotorControl::setPowerSteps(uint8_t increment) { + this->powersteps = increment; + return (uint16_t) (delay * ( 100 / powersteps )); +} + void MotorControl::setTargetPower(int8_t power) { if (power <= 100 && power >= -100) { this->target_power = power; @@ -85,17 +119,17 @@ void MotorControl::setTargetPower(int8_t power) { } } +uint16_t MotorControl::setDelay(uint8_t delay) { + this->delay = delay; + return (uint16_t) (delay * ( 100 / powersteps )); +} + void MotorControl::stop() { this->target_power = 0; } void MotorControl::emergencyStop() { setRealPower(0); - while(1) -} - -void MotorControl::toString() { - Serial.printf("Target power: %d, power: %d, direction: %d \n", this->target_power, this->power, this->direction); } int8_t MotorControl::getPower() { @@ -140,7 +174,7 @@ void MotorControl::setRealPower(int8_t power) { return; } - uint8_t pwm_val = map(abs(power), 0, 100, this->pwm_min, this->pwm_max); + uint8_t pwm_val = map(abs(power), 0, 100, this->dutycycle_min, this->dutycycle_max); if ((this->direction == 1 || this->direction == 0) && power < 0){ // new direction backward this->direction = 2; diff --git a/lib/Hardware/MotorControl/motorControl.h b/lib/Hardware/MotorControl/motorControl.h new file mode 100644 index 0000000..e64766e --- /dev/null +++ b/lib/Hardware/MotorControl/motorControl.h @@ -0,0 +1,164 @@ +/** + * @file motorControl.h + * @author Alexander Klein (alex@kleiax.de) + * @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 + +#include +#include +#include + +#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 50 +#define PWMMAX 95 // Max 98% of 2^PWM_RES + +/** + * @brief A class which use PWM to control the power of DC Motor + * You can control the acceleration of the motor, for example to + * prevent a damage on your H-Bridge. + */ +class MotorControl { + public: + MotorControl(); + + /** + * @brief Initalize the motorcontroler + * + * @param pwm_pin The output pin for the signal on the esp. + * @param pwm_channel 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); + + /** + * @brief Calls runMotorControl() to update the pwm signal + * + * This function should be called every mainloop. If the delay is not reached, than the + * functions returns immediately. + * @see runMotorControl() + * @see DELAY + */ + void loop(); + + /** + * @brief Normaly 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 + * + * @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 Power Steps + * + * Set the increment of the steps with which the dutycycle is + * increased or decreased. Note the dependency between the increment + * and delay(). + * + * The formula for the time between 0% and 100% power is: + * time[ms] = delay * ( 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 + * + * @param power power in percent + */ + void setTargetPower(int8_t power); + + /** + * @brief Set the min delay between each loop + * + * Note the dependency between delay and + * setPowerSteps(). + * + * @see setPowerSteps() + * + * @param delay time in Milliseconds + * @return time from 0% power to 100% power in Milliseconds + */ + uint16_t setDelay(uint8_t delay); + + /** + * @brief Stops the motor like setTargetPower() to 0 + * + */ + void stop(); + + /** + * @brief Stops the motor immediately + * + */ + void emergencyStop(); + + /** + * @brief Get the current power + * + * @return int8_t percent of power (-100 to 100) + */ + int8_t getPower(); + + /** + * @brief Get the target power + * + * @return int8_t percent of power (-100 to 100) + */ + int8_t getTargetPower(); + bool isTargetPowerReached(); + bool isAccelerationPositive(); + bool isAccelerationNegative(); + + private: + void setRealPower(int8_t power); + void increasePower(int8_t power); + + int8_t target_power = 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 dutycycle_min; + uint8_t dutycycle_max; + uint8_t dir_1; + uint8_t dir_2; + uint8_t delay = DELAY; + uint8_t powersteps = POWERSTEPS; + +}; + +#endif // MOTOR_CONTROL_H \ No newline at end of file diff --git a/lib/Speedometer/speedometer.cpp b/lib/Hardware/Speedometer/speedometer.cpp similarity index 91% rename from lib/Speedometer/speedometer.cpp rename to lib/Hardware/Speedometer/speedometer.cpp index 2bdd66e..a5da650 100644 --- a/lib/Speedometer/speedometer.cpp +++ b/lib/Hardware/Speedometer/speedometer.cpp @@ -1,3 +1,14 @@ +/** + * @file speedometer.cpp + * @author Alexander Klein (alex@kleiax.de) + * @brief Implemention of the class speedometer.h + * @see speedometer.h + * @version 0.1 + * @date 2021-12-13 + * + * @copyright Copyright (c) 2021 + * + */ #include "speedometer.h" Speedometer::Speedometer() {} diff --git a/lib/Speedometer/speedometer.h b/lib/Hardware/Speedometer/speedometer.h similarity index 97% rename from lib/Speedometer/speedometer.h rename to lib/Hardware/Speedometer/speedometer.h index 264ee22..901651f 100644 --- a/lib/Speedometer/speedometer.h +++ b/lib/Hardware/Speedometer/speedometer.h @@ -62,7 +62,7 @@ class Speedometer { void loop(); /** - * @brief Noramly called repeatedly by loop to calcluate new values. + * @brief Noramly called repeatedly by loop() to calcluate new values. * * Add a new Value to the average and update the speed. */ diff --git a/lib/MotorControl/motorControl.h b/lib/MotorControl/motorControl.h deleted file mode 100644 index d6d5ac1..0000000 --- a/lib/MotorControl/motorControl.h +++ /dev/null @@ -1,86 +0,0 @@ -/** - * @file motorControl.h - * @author Alexander Klein (alex@kleiax.de) - * @brief - * @version 0.1 - * @date 2021-12-09 - * - * @copyright Copyright (c) 2021 - * - */ -#ifndef MOTOR_CONTROL_H -#define MOTOR_CONTROL_H - -#include -#include -#include - -#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 50 -#define PWMMAX 95 // Max 98% of 2^PWM_RES - -/** - * @brief - * - */ -class MotorControl { - public: - MotorControl(); - void init(uint8_t pwm_pin, uint8_t pwm_channel, uint8_t dir_1, uint8_t dir_2); - void loop(); - void runMotorControl(); - - /** - * @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); - - void setPowerSteps(uint8_t steps); // Min anfahrkurve einbauen - void setTargetPower(int8_t power); - void setDelay(uint8_t delay); // Min anfahrkurve einbauen - void stop(); - - /** - * @brief - * - */ - void emergencyStop(); - - int8_t getPower(); - int8_t getTargetPower(); - bool isTargetPowerReached(); - bool isAccelerationPositive(); - bool isAccelerationNegative(); - - private: - void setRealPower(int8_t power); - void increasePower(int8_t power); - - int8_t target_power = 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_min = PWMMIN; - uint8_t pwm_max = PWMMAX; - uint8_t dir_1; - uint8_t dir_2; - uint8_t delay = DELAY; - uint8_t powersteps = POWERSTEPS; - -}; - -#endif // MOTOR_CONTROL_H \ No newline at end of file