more refactor bug fixes

This commit is contained in:
2023-10-13 14:17:51 +02:00
parent f0474027be
commit a7930e2915
14 changed files with 21 additions and 655 deletions
Submodule
+1
Submodule lib/Menu added at 35b1478666
+5 -24
View File
@@ -33,6 +33,7 @@ void MotorControl::init(uint8_t pwmPin, uint8_t pwmChannel, uint8_t dir_1, uint8
ledcSetup(this->pwmChannel, MotorControl::pwmFreq, MotorControl::pwmRes);
ledcAttachPin(this->pwmPin, this->pwmChannel);
ledcWrite(this->pwmChannel, 0);
std::cout << "Init pwm" << std::endl;
}
void MotorControl::run()
@@ -56,7 +57,7 @@ void MotorControl::run()
return;
}
// Positive or negative tagret speed
// Positive or negative target speed
if (this->targetPower >= 0)
{
// Positive or negative speed
@@ -97,28 +98,6 @@ void MotorControl::run()
}
}
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 = static_cast<uint8_t>(((static_cast<uint8_t>(1) << pwmRes) - 1) * (max / 100.0));
this->dutycycleMax = max;
}
void MotorControl::setTargetPower(int8_t power)
{
if (power <= 100 && power >= -100)
@@ -174,10 +153,11 @@ void MotorControl::setRealPower(int8_t power)
digitalWrite(this->dir_2, LOW);
ledcWrite(this->pwmChannel, 0);
this->dutycycle = 0;
std::cout << "abort" << std::endl;
return;
}
const uint8_t pwm_val = map(abs(power), 0, 100, this->dutycycleMin, this->dutycycleMax);
const uint8_t pwm_val = map(abs(power), 0, 100, MotorControl::minPwmVal, MotorControl::maxPwmVal);
if ((this->direction == 1 || this->direction == 0) && power < 0)
{ // new direction backward
@@ -192,6 +172,7 @@ void MotorControl::setRealPower(int8_t power)
digitalWrite(this->dir_2, LOW);
}
std::cout << "MotorControl::setRealPower pwm_val: " << (int) pwm_val << " channel:" << (int) this->pwmChannel << std::endl;
ledcWrite(this->pwmChannel, pwm_val);
this->dutycycle = pwm_val;
}
+2 -16
View File
@@ -38,20 +38,6 @@ public:
*/
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 maximum duty cycle
*
* @param max duty cycle in percent
*/
void setMaxPwm(uint8_t max);
/**
* @brief Set the Target Power
*
@@ -103,6 +89,8 @@ private:
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;
static constexpr uint8_t maxPwmVal = 250;
static constexpr uint8_t minPwmVal = 55; // Max 98% of 2^PWM_RES
int8_t targetPower = 0;
int8_t power = 0;
@@ -111,8 +99,6 @@ private:
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;
};
-1
View File
@@ -13,7 +13,6 @@
#define SENSOR_DATA_H
#include <SPI.h>
#include <I2Cdev.h>
#include <iostream>
#include "component.h"