Merge remote-tracking branch 'origin/projektarbeit' into testProjekttoBachelor but only motor things

This commit is contained in:
2024-07-19 15:57:54 +02:00
4 changed files with 9 additions and 44 deletions
+1
View File
@@ -87,6 +87,7 @@ public:
* @param status
*/
void setDrivingStatus(Status status);
Status getDrivingStatus() const { return this->driving_status; }
/**
* @brief Stops the engine immediately
+5 -27
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)
@@ -158,7 +137,6 @@ bool MotorControl::isAccelerationNegative() const
void MotorControl::setRealPower(int8_t power)
{
// TODO: Exceptionhandling
if (power <= 100 && power >= -100)
{
this->power = power;
@@ -175,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
@@ -193,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);
std::cout << "MotorControl::serRealPower - pwm: " << static_cast<int>(pwm_val) << std::endl;
this->dutycycle = pwm_val;
@@ -200,8 +180,6 @@ 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 * MotorControl::powerSteps)
{
Serial.println("Invalid Argument in MotorControl::increasePower");
+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;
};
@@ -47,7 +47,7 @@ void ManualControl::analogControl()
void ManualControl::digitalControl()
{
static constexpr uint8_t deadzone = 120;
static constexpr uint8_t deadzone = 110;
const int16_t yAxis = this->input->x - 127;
const int16_t xAxis = this->input->y - 127;