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

234 lines
9.3 KiB
C++
Raw Normal View History

2021-11-01 20:44:57 +01:00
#include "defaultmode.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()
{
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;
}
2021-12-06 00:36:24 +01:00
auto gas_processed = settings.defaultMode.squareGas ? (local_gas * local_gas) / 1000.f : local_gas;
auto brems_processed = settings.defaultMode.squareBrems ? (local_brems * local_brems) / 1000 : local_brems;
const auto c_gas_processed = gas_processed;
const auto c_brems_processed = brems_processed;
2021-11-01 20:44:57 +01:00
const auto now = espchrono::millis_clock::now();
2021-12-05 23:08:56 +01:00
if (!settings.handbremse.enable)
goto hell;
if (settings.handbremse.automatic && settings.handbremse.enable && settings.handbremse.mode == HandbremseMode::SPEED_0 && (abs(gas_processed) > 0 || abs(brems_processed) > 0))
2021-12-06 00:36:24 +01:00
{
if (handbremse::handbremseAngezogen && !m_handbrems_timer)
m_handbrems_timer = now;
2021-12-05 23:08:56 +01:00
goto hell;
2021-12-06 00:36:24 +01:00
}
2021-12-05 23:08:56 +01:00
if ((abs(gas_processed) > 10 || abs(brems_processed) > 10) || (!overrideHandbremse && (abs(avgSpeedKmh) > 2 || !controllers.front.feedbackValid || !controllers.back.feedbackValid || settings.handbremse.triggerTimeout < 1 || !settings.handbremse.automatic)))
2021-11-01 20:44:57 +01:00
{
2021-12-05 01:22:24 +01:00
m_stillSince = std::nullopt;
overrideHandbremse = false;
goto hell;
}
else if (!overrideHandbremse && !m_stillSince)
{
m_stillSince = now;
goto hell;
}
2021-12-06 00:36:24 +01:00
else if (((settings.handbremse.mode != HandbremseMode::SPEED_0 && abs(avgSpeedKmh) <= 2) || (settings.handbremse.mode == HandbremseMode::SPEED_0 && abs(avgSpeedKmh) <= 1)) && (overrideHandbremse || espchrono::ago(*m_stillSince) >= espchrono::milliseconds32(settings.handbremse.triggerTimeout * 100)))
2021-12-05 01:22:24 +01:00
{
handbremse::handbremseAngezogen = true;
2021-12-05 01:22:24 +01:00
fixCommonParams();
2021-11-01 20:44:57 +01:00
2021-12-05 01:22:24 +01:00
for (bobbycar::protocol::serial::MotorState &motor : motors())
2021-11-01 20:44:57 +01:00
{
2021-12-05 01:22:24 +01:00
motor.ctrlTyp = bobbycar::protocol::ControlType::FieldOrientedControl;
2021-12-05 23:08:56 +01:00
switch (settings.handbremse.mode)
{
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;
}
2021-12-05 01:22:24 +01:00
motor.pwm = 0;
motor.cruiseCtrlEna = false;
2021-12-05 23:08:56 +01:00
motor.nCruiseMotTgt = 0;
2021-11-01 20:44:57 +01:00
}
}
else
{
2021-12-05 01:22:24 +01:00
hell:
handbremse::handbremseAngezogen = false;
2021-12-05 01:22:24 +01:00
float pwm;
2021-12-06 00:36:24 +01:00
if (m_handbrems_timer && settings.handbremse.automatic && settings.handbremse.enable && settings.handbremse.mode == HandbremseMode::SPEED_0 && (abs(c_gas_processed) > 0 || abs(c_brems_processed) > 0))
{
2021-12-07 23:11:55 +01:00
if (espchrono::ago(*m_handbrems_timer) < 300ms)
2021-12-06 00:36:24 +01:00
{
gas_processed = 0;
brems_processed = 0;
2021-12-07 23:11:55 +01:00
m_lastPwm = 0;
2021-12-06 00:36:24 +01:00
}
}
2021-12-05 01:22:24 +01:00
if (gas_processed >= settings.defaultMode.add_schwelle)
2021-11-01 20:44:57 +01:00
{
2021-12-05 01:22:24 +01:00
pwm = (gas_processed/1000.*settings.defaultMode.gas1_wert) + (brems_processed/1000.*settings.defaultMode.brems1_wert);
2021-11-01 20:44:57 +01:00
2021-12-05 01:22:24 +01:00
if ((settings.defaultMode.enableSmoothingUp || settings.defaultMode.enableSmoothingDown) && (pwm > 1000. || m_lastPwm > 1000.))
2021-11-01 20:44:57 +01:00
{
2021-12-05 01:22:24 +01:00
if (m_lastPwm < pwm && settings.defaultMode.enableSmoothingUp)
{
pwm = std::min(pwm, m_lastPwm + (settings.defaultMode.smoothing * std::chrono::milliseconds{now - m_lastTime}.count() / 100.f));
if (pwm < 1000.)
pwm = 1000.;
}
else if (m_lastPwm > pwm && settings.defaultMode.enableSmoothingDown)
{
pwm = std::max(pwm, m_lastPwm - (settings.defaultMode.smoothing * std::chrono::milliseconds{now - m_lastTime}.count() / 100.f));
}
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
{
2021-12-05 01:22:24 +01:00
pwm = (gas_processed/1000.*settings.defaultMode.gas2_wert) - (brems_processed/1000.*settings.defaultMode.brems2_wert);
if (
(settings.defaultMode.enableFieldWeakSmoothingUp || settings.defaultMode.enableFieldWeakSmoothingDown) &&
(m_lastPwm > settings.defaultMode.fwSmoothLowerLimit) &&
brems_processed > 0)
{
if (m_lastPwm < pwm && settings.defaultMode.enableFieldWeakSmoothingUp)
{
auto effective_smoothing = settings.defaultMode.smoothing;
auto difference_to_target = std::abs(pwm-m_lastPwm);
effective_smoothing *= std::max((difference_to_target / 500),0.5f);
pwm = std::min(pwm, m_lastPwm + (effective_smoothing * std::chrono::milliseconds{now - m_lastTime}.count() / 100.f));
}
else if (m_lastPwm > pwm && settings.defaultMode.enableFieldWeakSmoothingDown)
{
auto effective_smoothing = settings.defaultMode.smoothing;
auto difference_to_target = std::abs(pwm-m_lastPwm);
effective_smoothing *= std::max((difference_to_target / 500),0.5f);
pwm = std::max(pwm, m_lastPwm - (effective_smoothing * std::chrono::milliseconds{now - m_lastTime}.count() / 100.f));
}
}
2021-11-01 20:44:57 +01:00
}
2021-12-05 01:22:24 +01:00
m_lastPwm = pwm;
m_lastTime = now;
auto pair = split(settings.defaultMode.modelMode);
if (settings.hybrid.enable)
2021-11-01 20:44:57 +01:00
{
2021-12-05 01:22:24 +01:00
auto activationLimit = settings.hybrid.activationLimit;
auto deactivationLimit = settings.hybrid.deactivationLimit;
auto diff = std::abs(activationLimit - deactivationLimit);
if (diff < 20)
{
int half = (diff / 2) + 0.5;
deactivationLimit -= half;
activationLimit += half;
}
if (!hybridModeActivated && (pwm > activationLimit))
{
hybridModeActivated = true;
}
else if (hybridModeActivated && (pwm < deactivationLimit))
{
hybridModeActivated = false;
}
if (hybridModeActivated)
{
pair = split(settings.hybrid.hybridMode);
}
2021-11-01 20:44:57 +01:00
}
2021-12-05 01:22:24 +01:00
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;
motor.pwm = pwm / 100. * settings.defaultMode.frontPercentage;
motor.cruiseCtrlEna = false;
motor.nCruiseMotTgt = 0;
}
for (bobbycar::protocol::serial::MotorState &motor : motorsInController(controllers.back))
{
motor.ctrlTyp = pair.first;
motor.ctrlMod = pair.second;
motor.pwm = pwm / 100. * settings.defaultMode.backPercentage;
motor.cruiseCtrlEna = false;
motor.nCruiseMotTgt = 0;
2021-11-01 20:44:57 +01:00
}
2021-12-05 23:08:56 +01:00
fixCommonParams();
2021-12-06 00:36:24 +01:00
if (m_handbrems_timer && settings.handbremse.automatic && settings.handbremse.enable && settings.handbremse.mode == HandbremseMode::SPEED_0 && (abs(c_gas_processed) > 0 || abs(c_brems_processed) > 0))
{
2021-12-07 23:11:55 +01:00
if (espchrono::ago(*m_handbrems_timer) < 100ms)
2021-12-06 00:36:24 +01:00
{
for (bobbycar::protocol::serial::MotorState &motor : motors())
{
motor.pwm = 0;
motor.ctrlTyp = pair.first;
motor.ctrlMod = pair.second;
}
}
else
m_handbrems_timer = std::nullopt;
}
2021-11-01 20:44:57 +01:00
}
}
sendCommands();
}