Files
bobbycar-boardcomputer-firm…/main/modes/defaultmode.cpp
T

265 lines
9.9 KiB
C++
Raw Normal View History

2021-11-01 20:44:57 +01:00
#include "defaultmode.h"
2022-02-14 22:43:32 +01:00
// local includes
#include "handbremse.h"
#include "tempomat.h"
2021-12-06 00:36:24 +01:00
using namespace std::chrono_literals;
2021-11-01 20:44:57 +01:00
namespace modes {
DefaultMode defaultMode;
} // namespace modes
void DefaultMode::start()
{
Base::start();
}
void DefaultMode::update()
{
2022-01-03 17:13:45 +01:00
auto pair = split(profileSettings.defaultMode.modelMode);
2021-11-01 20:44:57 +01:00
if (!gas || !brems)
{
start();
for (bobbycar::protocol::serial::MotorState &motor : motors())
{
motor.ctrlTyp = bobbycar::protocol::ControlType::FieldOrientedControl;
motor.ctrlMod = bobbycar::protocol::ControlMode::OpenMode;
motor.pwm = 0;
motor.cruiseCtrlEna = false;
motor.nCruiseMotTgt = 0;
}
}
else
{
float local_gas = *gas;
float local_brems = *brems;
if (waitForGasLoslass)
{
if (local_gas < 50)
waitForGasLoslass = false;
else
local_gas = 0;
}
if (waitForBremsLoslass)
{
if (local_brems < 50)
waitForBremsLoslass = false;
else
local_brems = 0;
}
2022-01-03 17:13:45 +01:00
auto gas_processed = profileSettings.defaultMode.squareGas ? (local_gas * local_gas) / 1000.f : local_gas;
auto brems_processed = profileSettings.defaultMode.squareBrems ? (local_brems * local_brems) / 1000 : local_brems;
2021-11-01 20:44:57 +01:00
const auto now = espchrono::millis_clock::now();
float pwm;
2021-12-05 23:08:56 +01:00
if (configs.handbremse.enable.value && handbremse::stateWish == handbremse::StateWish::brake)
2021-12-06 00:36:24 +01:00
{
using namespace handbremse;
const auto speed = abs(avgSpeedKmh);
const bool gas_und_brems_ist_null = (gas_processed < 1 && brems_processed < 1);
if (speed < 2 && gas_und_brems_ist_null)
{
angezogen = true;
stateWish = StateWish::none;
}
2021-12-06 00:36:24 +01:00
}
2021-12-05 23:08:56 +01:00
if (configs.handbremse.enable.value && configs.handbremse.automatic.value && !handbremse::angezogen)
2021-11-01 20:44:57 +01:00
{
using namespace handbremse;
const auto speed = abs(avgSpeedKmh);
if (speed < 1)
{
if (!standStillFirstDetected)
{
standStillFirstDetected = now;
}
}
else
standStillFirstDetected = std::nullopt;
if (standStillFirstDetected && lastAutoRelease)
if (espchrono::ago(*standStillFirstDetected) > 100ms * configs.handbremse.triggerTimeout.value && espchrono::ago(*lastAutoRelease) > 5s )
{
angezogen = true;
}
2021-12-05 01:22:24 +01:00
}
2021-12-11 03:43:24 +01:00
if (handbremse::wishTimer)
{
using namespace handbremse;
if (espchrono::ago(*wishTimer) > 5s)
{
stateWish = StateWish::none;
wishTimer = std::nullopt;
}
}
if (handbremse::angezogen)
2021-12-05 01:22:24 +01:00
{
using namespace handbremse;
standStillFirstDetected = std::nullopt;
const bool valid = (controllers.front.feedbackValid && controllers.back.feedbackValid);
const bool gas_oder_brems = (gas_processed > 10 || brems_processed > 10);
2021-12-05 01:22:24 +01:00
fixCommonParams();
2021-11-01 20:44:57 +01:00
if (stateWish == StateWish::release)
{
lastAutoRelease = now;
releaseTimer = now;
stateWish = StateWish::none;
finishedMotorUpdate = false;
}
if (valid && gas_oder_brems)
{
if (!releaseTimer)
{
lastAutoRelease = now;
releaseTimer = now;
finishedMotorUpdate = false;
}
}
if (releaseTimer)
{
for (bobbycar::protocol::serial::MotorState &motor : motors())
{
motor.pwm = 0;
motor.ctrlTyp = pair.first;
motor.ctrlMod = pair.second;
}
if (espchrono::ago(*releaseTimer) > 1s || finishedMotorUpdate)
{
releaseTimer = std::nullopt;
angezogen = false;
}
}
else
{
for (bobbycar::protocol::serial::MotorState &motor : motors())
{
motor.ctrlTyp = bobbycar::protocol::ControlType::FieldOrientedControl;
switch (configs.handbremse.mode.value)
{
case HandbremseMode::MOSFETS_OFF:
motor.ctrlMod = bobbycar::protocol::ControlMode::Torque;
motor.enable = false;
break;
case HandbremseMode::OPENMODE:
motor.ctrlMod = bobbycar::protocol::ControlMode::OpenMode;
break;
case HandbremseMode::SPEED_0:
motor.ctrlMod = bobbycar::protocol::ControlMode::Speed;
break;
}
motor.pwm = 0;
motor.cruiseCtrlEna = false;
motor.nCruiseMotTgt = 0;
2021-12-05 23:08:56 +01:00
}
2021-11-01 20:44:57 +01:00
}
}
else
{
2022-01-03 17:13:45 +01:00
if (gas_processed >= profileSettings.defaultMode.add_schwelle)
2021-11-01 20:44:57 +01:00
{
2022-01-03 17:13:45 +01:00
pwm = (gas_processed/1000.*profileSettings.defaultMode.gas1_wert) + (brems_processed/1000.*profileSettings.defaultMode.brems1_wert);
2021-11-01 20:44:57 +01:00
2022-01-03 17:13:45 +01:00
if ((profileSettings.defaultMode.enableSmoothingUp || profileSettings.defaultMode.enableSmoothingDown) && (pwm > 1000. || m_lastPwm > 1000.))
2021-11-01 20:44:57 +01:00
{
2022-01-03 17:13:45 +01:00
if (m_lastPwm < pwm && profileSettings.defaultMode.enableSmoothingUp)
2021-12-05 01:22:24 +01:00
{
2022-01-29 21:10:48 +01:00
pwm = std::min(pwm, m_lastPwm + (profileSettings.defaultMode.smoothing * std::chrono::floor<std::chrono::milliseconds>(now - m_lastTime).count() / 100.f));
2021-12-05 01:22:24 +01:00
if (pwm < 1000.)
pwm = 1000.;
}
2022-01-03 17:13:45 +01:00
else if (m_lastPwm > pwm && profileSettings.defaultMode.enableSmoothingDown)
2021-12-05 01:22:24 +01:00
{
2022-01-29 21:10:48 +01:00
pwm = std::max(pwm, m_lastPwm - (profileSettings.defaultMode.smoothing * std::chrono::floor<std::chrono::milliseconds>(now - m_lastTime).count() / 100.f));
2021-12-05 01:22:24 +01:00
}
2021-11-01 20:44:57 +01:00
}
}
2021-12-05 01:22:24 +01:00
else
2021-11-01 20:44:57 +01:00
{
2022-01-03 17:13:45 +01:00
pwm = (gas_processed/1000.*profileSettings.defaultMode.gas2_wert) - (brems_processed/1000.*profileSettings.defaultMode.brems2_wert);
2021-12-05 01:22:24 +01:00
if (
2022-01-03 17:13:45 +01:00
(profileSettings.defaultMode.enableFieldWeakSmoothingUp || profileSettings.defaultMode.enableFieldWeakSmoothingDown) &&
(m_lastPwm > profileSettings.defaultMode.fwSmoothLowerLimit) &&
2021-12-05 01:22:24 +01:00
brems_processed > 0)
{
2022-01-03 17:13:45 +01:00
if (m_lastPwm < pwm && profileSettings.defaultMode.enableFieldWeakSmoothingUp)
2021-12-05 01:22:24 +01:00
{
2022-01-03 17:13:45 +01:00
auto effective_smoothing = profileSettings.defaultMode.smoothing;
2021-12-05 01:22:24 +01:00
auto difference_to_target = std::abs(pwm-m_lastPwm);
effective_smoothing *= std::max((difference_to_target / 500),0.5f);
2022-01-29 21:10:48 +01:00
pwm = std::min(pwm, m_lastPwm + (effective_smoothing * std::chrono::floor<std::chrono::milliseconds>(now - m_lastTime).count() / 100.f));
2021-12-05 01:22:24 +01:00
}
2022-01-03 17:13:45 +01:00
else if (m_lastPwm > pwm && profileSettings.defaultMode.enableFieldWeakSmoothingDown)
2021-12-05 01:22:24 +01:00
{
2022-01-03 17:13:45 +01:00
auto effective_smoothing = profileSettings.defaultMode.smoothing;
2021-12-05 01:22:24 +01:00
auto difference_to_target = std::abs(pwm-m_lastPwm);
effective_smoothing *= std::max((difference_to_target / 500),0.5f);
2022-01-29 21:10:48 +01:00
pwm = std::max(pwm, m_lastPwm - (effective_smoothing * std::chrono::floor<std::chrono::milliseconds>(now - m_lastTime).count() / 100.f));
2021-12-05 01:22:24 +01:00
}
}
2021-11-01 20:44:57 +01:00
}
2022-02-14 22:43:32 +01:00
if (!pwmomat::tempomat_pwm && pwmomat::wish == pwmomat::WISH::WISH_ENABLE)
{
pwmomat::tempomat_pwm = pwm;
pwmomat::wish = pwmomat::WISH::NONE;
pwmomat::enabled_time = now;
}
else if ((pwmomat::tempomat_pwm && pwmomat::enabled_time) && (espchrono::ago(*pwmomat::enabled_time) > 500ms))
2022-02-14 22:43:32 +01:00
{
if ((*pwmomat::tempomat_pwm >= 0 && (pwm >= *pwmomat::tempomat_pwm + 10 || pwm < -10)) ||
(*pwmomat::tempomat_pwm < 0 && (pwm <= *pwmomat::tempomat_pwm - 10 || pwm > 10)) ||
(pwmomat::wish == pwmomat::WISH::WISH_DISABLE)
)
{
pwmomat::wish = pwmomat::WISH::NONE;
pwmomat::tempomat_pwm = std::nullopt;
}
2022-02-14 22:43:32 +01:00
}
if (pwmomat::tempomat_pwm)
pwm = *pwmomat::tempomat_pwm;
2021-12-05 01:22:24 +01:00
m_lastPwm = pwm;
m_lastTime = now;
for (bobbycar::protocol::serial::MotorState &motor : motorsInController(controllers.front))
2021-11-01 20:44:57 +01:00
{
2021-12-05 01:22:24 +01:00
motor.ctrlTyp = pair.first;
motor.ctrlMod = pair.second;
2022-01-03 17:13:45 +01:00
motor.pwm = pwm / 100. * profileSettings.defaultMode.frontPercentage;
2021-12-05 01:22:24 +01:00
motor.cruiseCtrlEna = false;
motor.nCruiseMotTgt = 0;
}
for (bobbycar::protocol::serial::MotorState &motor : motorsInController(controllers.back))
{
motor.ctrlTyp = pair.first;
motor.ctrlMod = pair.second;
2022-01-03 17:13:45 +01:00
motor.pwm = pwm / 100. * profileSettings.defaultMode.backPercentage;
2021-12-05 01:22:24 +01:00
motor.cruiseCtrlEna = false;
motor.nCruiseMotTgt = 0;
2021-11-01 20:44:57 +01:00
}
2021-12-05 23:08:56 +01:00
fixCommonParams();
2021-11-01 20:44:57 +01:00
}
}
sendCommands();
}