Merge pull request #389 from bobbycar-graz/accel-brake-lights

This commit is contained in:
CommanderRedYT
2022-12-18 20:32:29 +01:00
committed by GitHub
11 changed files with 121 additions and 99 deletions

View File

@ -205,6 +205,9 @@ struct LedstripEnableBlinkAnimationAccessor : public NewSettingsAccessor<bool> {
struct LedstripOtaAnimationAccessor : public NewSettingsAccessor<OtaAnimationModes> { ConfigWrapper<OtaAnimationModes> &getConfig() const override { return configs.ledstrip.otaMode; } };
struct LedstripEnableVisualizeBlinkAnimationAccessor : public NewSettingsAccessor<bool> { ConfigWrapper<bool> &getConfig() const override { return configs.ledstrip.enableVisualizeBlink; } };
struct LedstripAutomaticLightAccessor : public NewSettingsAccessor<bool> { ConfigWrapper<bool> &getConfig() const override { return configs.ledstrip.automaticLight; } };
struct LedstripBrakeLightUseAccelAccessor : public NewSettingsAccessor<bool> { ConfigWrapper<bool> &getConfig() const override { return configs.ledstrip.brakeLights_useAccel; } };
struct LedstripBrakeLightUsePowerAccessor : public NewSettingsAccessor<bool> { ConfigWrapper<bool> &getConfig() const override { return configs.ledstrip.brakeLights_usePower; } };
// Battery
struct BatterySeriesCellsAccessor : public NewSettingsAccessor<uint8_t> { ConfigWrapper<uint8_t> &getConfig() const override { return configs.battery.cellsSeries; } };

View File

@ -60,23 +60,18 @@ namespace bobbyblinker {
}
if (configs.ledstrip.enableBrakeLights.value() && espchrono::ago(*brake_last_time_sent) > 500ms)
{
float avgPwm{};
for (const Controller &controller: controllers) {
avgPwm +=
controller.command.left.pwm * (controller.invertLeft ? -1 : 1) +
controller.command.right.pwm * (controller.invertRight ? -1 : 1);
}
avgPwm /= 4;
if (avgPwm < -1.f)
if (brakeLightsStatus == brakeLightsOffSent)
{
sendState("BRAKELIGHTSON");
brakeLightsOffSent = false;
}
else if (!brakeLightsOffSent && avgPwm > -1.f)
{
sendState("BRAKELIGHTSOFF");
brakeLightsOffSent = true;
if (brakeLightsStatus)
{
sendState("BRAKELIGHTSON");
brakeLightsOffSent = false;
}
else if (!brakeLightsOffSent)
{
sendState("BRAKELIGHTSOFF");
brakeLightsOffSent = true;
}
}
}
}

View File

@ -62,6 +62,15 @@ constexpr char TEXT_ERASENVS[] = "Erase NVS (old)";
constexpr char TEXT_RESET_NVS_NEW[] = "Reset NVS (new)";
constexpr char TEXT_DYNAMICMENU[] = "GUI experiments";
constexpr char TEXT_BACK[] = "Back";
class AccelerationText : public virtual espgui::TextInterface
{
public:
std::string text() const override
{
return fmt::format("Accel: {:.6f}", avgAccel);
}
};
} // namespace
DebugMenu::DebugMenu()
@ -82,6 +91,7 @@ DebugMenu::DebugMenu()
#ifdef FEATURE_CAN
constructMenuItem<makeComponent<MenuItem, CanIcCrashText, StaticFont<2>, DisabledColor, DummyAction>>();
#endif
constructMenuItem<makeComponent<MenuItem, AccelerationText, StaticFont<2>, DisabledColor, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, EmptyText, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_FRONTCOMMAND>, PushScreenAction<FrontCommandDebugMenu>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACKCOMMAND>, PushScreenAction<BackCommandDebugMenu>>>();

View File

@ -48,6 +48,8 @@ constexpr char TEXT_ANIMATION_MULTIPLIER[] = "Animation Multiplier";
constexpr char TEXT_LEDSTRIP_BRIGHTNESS[] = "Ledstrip Brightness";
constexpr char TEXT_LEDSTRIP_ALLCUSTOMOFF[] = "All custom off";
constexpr char TEXT_LEDSTRIP_CHANGE_OTA_ANIM[] = "Change Ota animation";
constexpr char TEXT_LEDSTRIP_BRAKE_USE_ACCEL[] = "Brakelight use acceleration";
constexpr char TEXT_LEDSTRIP_BRAKE_USE_POWER[] = "Brakelight use motor power";
constexpr char TEXT_BACK[] = "Back";
using LedsCountChangeScreen = espgui::makeComponent<
@ -179,6 +181,8 @@ LedstripMenu::LedstripMenu()
if (!simplified) { constructMenuItem<makeComponent<MenuItem, TextWithValueHelper<TEXT_BIGOFFSET, BigOffsetAccessor>, espgui::PushScreenAction<BigOffsetChangeScreen>>>(); }
if (!simplified) { constructMenuItem<makeComponent<MenuItem, espgui::StaticText<TEXT_LEDSTRIP_BRIGHTNESS>, espgui::PushScreenAction<LedStripBrightnessChangeScreen>>>(); }
if (!simplified) { constructMenuItem<makeComponent<MenuItem, LedStripMaxCurrentText, espgui::PushScreenAction<LedStripMaxAmpereChangeScreen>>>(); }
constructMenuItem<makeComponent<MenuItem, espgui::StaticText<TEXT_LEDSTRIP_BRAKE_USE_ACCEL>, BobbyCheckbox, LedstripBrakeLightUseAccelAccessor>>();
constructMenuItem<makeComponent<MenuItem, espgui::StaticText<TEXT_LEDSTRIP_BRAKE_USE_POWER>, BobbyCheckbox, LedstripBrakeLightUsePowerAccessor>>();
constructMenuItem<makeComponent<MenuItem, espgui::StaticText<TEXT_BACK>, espgui::PushScreenAction<MainMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}

View File

@ -1,72 +0,0 @@
#include "ledstripselectanimationmenu.h"
// Local includes
#include "actions/dummyaction.h"
#include "actions/ledstripanimationactions.h"
#include "actions/popscreenaction.h"
#include "icons/back.h"
#include "ledstripdefines.h"
#include "ledstripmenu.h"
#include "newsettings.h"
#ifdef FEATURE_LEDSTRIP
namespace {
constexpr char TEXT_SELECTANIMATION[] = "Select Animation";
constexpr char TEXT_ANIMATION_DEFAULTRAINBOW[] = "Default Rainbow";
constexpr char TEXT_ANIMATION_BETTERRAINBOW[] = "Better Rainbow";
constexpr char TEXT_ANIMATION_SPEEDSYNCANIMATION[] = "Speed Sync";
constexpr char TEXT_ANIMATION_CUSTOMCOLOR[] = "Custom Color";
constexpr char TEXT_BACK[] = "Back";
class CurrentSelectedAnimationText : public virtual espgui::TextInterface
{
public:
std::string text() const override;
};
} // namespace
LedstripSelectAnimationMenu::LedstripSelectAnimationMenu()
{
using namespace espgui;
constructMenuItem<makeComponent<MenuItem, CurrentSelectedAnimationText, DisabledColor, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, EmptyText, DummyAction>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_DEFAULTRAINBOW>, LedStripSetAnimationAction<LedstripAnimation::DefaultRainbow>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_BETTERRAINBOW>, LedStripSetAnimationAction<LedstripAnimation::BetterRainbow>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_SPEEDSYNCANIMATION>, LedStripSetAnimationAction<LedstripAnimation::SpeedSync>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ANIMATION_CUSTOMCOLOR>, LedStripSetAnimationAction<LedstripAnimation::CustomColor>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, PopScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>();
}
std::string LedstripSelectAnimationMenu::text() const
{
return TEXT_SELECTANIMATION;
}
void LedstripSelectAnimationMenu::back()
{
espgui::popScreen();
}
namespace {
std::string CurrentSelectedAnimationText::text() const
{
switch (configs.ledstrip.animationType.value)
{
case LedstripAnimation::DefaultRainbow:
return TEXT_ANIMATION_DEFAULTRAINBOW;
case LedstripAnimation::BetterRainbow:
return TEXT_ANIMATION_BETTERRAINBOW;
case LedstripAnimation::SpeedSync:
return TEXT_ANIMATION_SPEEDSYNCANIMATION;
case LedstripAnimation::CustomColor:
return TEXT_ANIMATION_CUSTOMCOLOR;
default:
return "Animation Unkown";
}
}
} // namespace
#endif

View File

@ -15,8 +15,12 @@ float gametrakDist;
float avgSpeed{};
float avgSpeedKmh{};
float sumCurrent{};
float lastAvgSpeedKmh{};
float avgAccel{};
bool isLocked{};
espchrono::millis_clock::time_point lastAvgSpeedKmhTs{};
std::optional<int> sunrise;
std::optional<int> sunset;
std::optional<espchrono::DateTime> sunrise_dt;

View File

@ -53,8 +53,12 @@ extern esp_pm_config_esp32_t pm_config;
extern float avgSpeed;
extern float avgSpeedKmh;
extern float lastAvgSpeedKmh;
extern float avgAccel;
extern float sumCurrent;
extern espchrono::millis_clock::time_point lastAvgSpeedKmhTs;
extern bool isLocked;
#ifdef GLOBALS_PLUGIN

View File

@ -19,10 +19,53 @@ std::vector<CRGB> leds;
uint8_t gHue = 0;
float gLedPosition = 0; // yes, this is intendet as a float value! Do NOT change!
bool brakeLightsStatus;
espchrono::millis_clock::time_point brakeLightTimer;
uint16_t blinkAnimation = LEDSTRIP_OVERWRITE_NONE;
namespace {
bool initialized{false};
bool initialized{false};
bool brakeLights()
{
float avgPwm{};
for (const Controller &controller : controllers)
{
avgPwm +=
controller.command.left.pwm * (controller.invertLeft ? -1 : 1) +
controller.command.right.pwm * (controller.invertRight ? -1 : 1);
}
avgPwm /= 4;
if (avgPwm < -1.f)
{
return true;
}
if (configs.ledstrip.brakeLights_useAccel.value() && (avgAccel < -0.001f && avgSpeedKmh > 2.f))
{
return true;
}
if (espchrono::ago(brakeLightTimer) < 200ms)
{
return true;
}
if (configs.ledstrip.brakeLights_usePower.value() && brakeLightsStatus)
{
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
{
const auto watt = sumCurrent * *avgVoltage;
return watt < -1;
}
}
return false;
}
} // namespace
void initLedStrip()
@ -140,20 +183,17 @@ void updateLedStrip()
{
if (configs.ledstrip.enableBrakeLights.value())
{
float avgPwm{};
for (const Controller &controller : controllers)
// avgAccel in m/s/s
if (brakeLights())
{
avgPwm +=
controller.command.left.pwm * (controller.invertLeft ? -1 : 1) +
controller.command.right.pwm * (controller.invertRight ? -1 : 1);
}
avgPwm /= 4;
if (!(espchrono::ago(brakeLightTimer) < 200ms))
{
brakeLightTimer = espchrono::millis_clock::now();
}
if (avgPwm < -1.f)
{
auto color = avgSpeedKmh < -0.1f ? CRGB{255, 255, 255} : CRGB{255, 0, 0};
const auto center = (std::begin(leds) + (leds.size() / 2) + configs.ledstrip.centerOffset.value());
brakeLightsStatus = true;
std::fill(std::begin(leds), std::end(leds), CRGB{0, 0, 0});
if (configs.ledstrip.enableFullBlink.value())
@ -162,12 +202,15 @@ void updateLedStrip()
}
else if(!configs.ledstrip.enableAnimBlink.value())
{
const auto center = (std::begin(leds) + (leds.size() / 2) + configs.ledstrip.centerOffset.value());
std::fill(center - configs.ledstrip.bigOffset.value() - 2, center - configs.ledstrip.smallOffset.value() + 2, color);
std::fill(center + configs.ledstrip.smallOffset.value() - 2, center + configs.ledstrip.bigOffset.value() + 2, color);
}
}
else
{
brakeLightsStatus = false;
showAnimation();
}
}

View File

@ -4,6 +4,7 @@
#include <vector>
// 3rdparty lib includes
#include <espchrono.h>
#include <FastLED.h>
// local includes
@ -44,6 +45,10 @@ extern float gLedPosition; // yes, this is intendet as a float value! Do NOT cha
extern uint16_t blinkAnimation;
extern bool brakeLightsStatus;
extern espchrono::millis_clock::time_point brakeLightTimer;
void showDefaultLedstrip();
void showAnimation();
void showBetterRainbow();

View File

@ -415,6 +415,8 @@ public:
};
ConfigWrapperLegacy<uint8_t> leds_per_meter {144, DoReset, {}, "ledsPerMeter" };
ConfigWrapperLegacy<bool> automaticLight {false, DoReset, {}, "nightLights" };
ConfigWrapperLegacy<bool> brakeLights_useAccel {false, DoReset, {}, "brakeLightsA" };
ConfigWrapperLegacy<bool> brakeLights_usePower {false, DoReset, {}, "brakeLightsP" };
} ledstrip;
struct {
@ -749,6 +751,8 @@ public:
x(ledstrip.maxMilliamps) \
x(ledstrip.enableVisualizeBlink)\
x(ledstrip.automaticLight) \
x(ledstrip.brakeLights_useAccel) \
x(ledstrip.brakeLights_usePower) \
\
x(ledstrip.custom_color[0]) \
x(ledstrip.custom_color[1]) \

View File

@ -248,6 +248,28 @@ void updateAccumulators()
sumCurrent = fixCurrent(sumCurrent);
avgSpeedKmh = convertToKmh(avgSpeed);
// accel
EVERY_N_MILLIS(100)
{
const auto now = espchrono::millis_clock::now();
const auto delta = now - lastAvgSpeedKmhTs;
const auto deltaSpeedKmh = avgSpeedKmh - lastAvgSpeedKmh;
// ESP_LOGI("utils.cpp", "delta: %lli ms, deltaSpeedKmh: %f", delta.count() / 1000, deltaSpeedKmh);
const auto deltaMilliseconds = delta.count() / 1000.f;
const auto kmh_s = deltaSpeedKmh / deltaMilliseconds; // km/h / s
const auto m_s2 = kmh_s * 1000.f / 3600.f; // m/s^2
// ESP_LOGI("utils.cpp", "m_s2: %f", m_s2);
avgAccel = avgAccel * 0.3f + m_s2 * 0.7f;
lastAvgSpeedKmh = avgSpeedKmh;
lastAvgSpeedKmhTs = espchrono::millis_clock::now();
}
}
float wattToAmpere(float watt) {