comments for Doxygen all private Libs

This commit is contained in:
2021-12-13 16:44:53 +01:00
parent a1d59c18ad
commit a7940c887b
11 changed files with 448 additions and 163 deletions
+42 -54
View File
@@ -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;
}
+123 -12
View File
@@ -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 <PubSubClient.h>
#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
+19 -1
View File
@@ -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);
+46 -1
View File
@@ -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 <Arduino.h>
/**
* @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;
@@ -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;
+164
View File
@@ -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 <cstdint>
#include <string>
#include <Arduino.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 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
@@ -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() {}
@@ -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.
*/
-86
View File
@@ -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 <cstdint>
#include <string>
#include <Arduino.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 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