CAN implementation started

This commit is contained in:
2021-05-16 21:51:31 +02:00
parent 873f076f93
commit e5871d339e
9 changed files with 54 additions and 4 deletions

View File

@ -162,6 +162,7 @@ build_flags =
; -DDEFAULT_GAMETRAKDISTMIN=0
; -DDEFAULT_GAMETRAKDISTMAX=4095
-DFEATURE_CLOUD
-DFEATURE_CAN
[env:feedc0de_usb]
platform = ${feedc0de.platform}

View File

@ -4,20 +4,33 @@
#include "bobbycar-protocol/protocol.h"
#ifndef FEATURE_CAN
#include "feedbackparser.h"
#endif
#ifndef FEATURE_CAN
class HardwareSerial;
#endif
namespace {
struct Controller {
Controller(HardwareSerial &serial, bool &enableLeft, bool &enableRight, bool &invertLeft, bool &invertRight) :
serial{serial}, enableLeft{enableLeft}, enableRight{enableRight}, invertLeft{invertLeft}, invertRight{invertRight}
Controller(
#ifndef FEATURE_CAN
HardwareSerial &serial,
#endif
bool &enableLeft, bool &enableRight, bool &invertLeft, bool &invertRight) :
#ifndef FEATURE_CAN
serial{serial},
#endif
enableLeft{enableLeft}, enableRight{enableRight}, invertLeft{invertLeft}, invertRight{invertRight}
{
}
// Controller(const Controller &) = delete;
// Controller &operator=(const Controller &) = delete;
#ifndef FEATURE_CAN
std::reference_wrapper<HardwareSerial> serial;
#endif
bool &enableLeft, &enableRight, &invertLeft, &invertRight;
Command command{};
@ -25,6 +38,8 @@ struct Controller {
bool feedbackValid{};
Feedback feedback{};
#ifndef FEATURE_CAN
FeedbackParser parser{serial, feedbackValid, feedback};
#endif
};
}

View File

@ -60,7 +60,9 @@ public:
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERMM>, SwitchScreenAction<WheelDiameterMmChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_WHEELDIAMETERINCH>, SwitchScreenAction<WheelDiameterInchChangeScreen>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_NUMMAGNETPOLES>, SwitchScreenAction<NumMagnetPolesChangeScreen>>>();
#ifndef FEATURE_CAN
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_SWAPFRONTBACK>, ToggleBoolAction, CheckboxIcon, SwapFrontBackAccessor>>();
#endif
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&icons::back>>>();
}
};

View File

@ -41,8 +41,18 @@ class Controllers : public std::array<Controller, 2>
public:
explicit Controllers() :
std::array<Controller, 2>{{
Controller{Serial1, settings.controllerHardware.enableFrontLeft, settings.controllerHardware.enableFrontRight, settings.controllerHardware.invertFrontLeft, settings.controllerHardware.invertFrontRight},
Controller{Serial2, settings.controllerHardware.enableBackLeft, settings.controllerHardware.enableBackRight, settings.controllerHardware.invertBackLeft, settings.controllerHardware.invertBackRight}
Controller {
#ifndef FEATURE_CAN
Serial1,
#endif
settings.controllerHardware.enableFrontLeft, settings.controllerHardware.enableFrontRight, settings.controllerHardware.invertFrontLeft, settings.controllerHardware.invertFrontRight
},
Controller {
#ifndef FEATURE_CAN
Serial2,
#endif
settings.controllerHardware.enableBackLeft, settings.controllerHardware.enableBackRight, settings.controllerHardware.invertBackLeft, settings.controllerHardware.invertBackRight
}
}}
{}

View File

@ -233,9 +233,11 @@ void setup()
}
printMemoryStats("loadSettings()");
#ifndef FEATURE_CAN
bootLabel.redraw("swap front back");
updateSwapFrontBack();
printMemoryStats("swapFronBack()");
#endif
bootLabel.redraw("deviceName");
{
@ -295,11 +297,13 @@ void setup()
}
#endif
#ifndef FEATURE_CAN
bootLabel.redraw("front Serial begin");
controllers.front.serial.get().begin(38400, SERIAL_8N1, PINS_RX1, PINS_TX1);
bootLabel.redraw("back Serial begin");
controllers.back.serial.get().begin(38400, SERIAL_8N1, PINS_RX2, PINS_TX2);
#endif
raw_gas = 0;
raw_brems = 0;
@ -418,8 +422,10 @@ void loop()
performance.lastTime = now;
}
#ifndef FEATURE_CAN
for (Controller &controller : controllers)
controller.parser.update();
#endif
handleSerial();

View File

@ -32,7 +32,9 @@ constexpr Settings::ControllerHardware defaultControllerHardware {
.wheelDiameter = DEFAULT_WHEELDIAMETER,
.numMagnetPoles = 15,
#ifndef FEATURE_CAN
.swapFrontBack = false
#endif
};
constexpr Settings::ControllerHardware mosfetsOffControllerHardware {
@ -48,7 +50,9 @@ constexpr Settings::ControllerHardware mosfetsOffControllerHardware {
.wheelDiameter = 165,
.numMagnetPoles = 15,
#ifndef FEATURE_CAN
.swapFrontBack = false
#endif
};
constexpr Settings::WifiSettings defaultWifiSettings {
@ -75,7 +79,9 @@ constexpr Settings::ControllerHardware spinnerControllerHardware {
.wheelDiameter = 165,
.numMagnetPoles = 15,
#ifndef FEATURE_CAN
.swapFrontBack = false
#endif
};
constexpr Settings::BoardcomputerHardware::TimersSettings defaultTimersSettings {

View File

@ -51,7 +51,9 @@ struct Settings
int16_t wheelDiameter; // in mm
int16_t numMagnetPoles; // virtual RPM per one real RPM
#ifndef FEATURE_CAN
bool swapFrontBack;
#endif
} controllerHardware;
struct BoardcomputerHardware {
@ -142,7 +144,9 @@ void Settings::executeForEverySetting(T &&callable)
callable("wheelDiameter", controllerHardware.wheelDiameter);
callable("numMagnetPoles", controllerHardware.numMagnetPoles);
#ifndef FEATURE_CAN
callable("swapFrontBack", controllerHardware.swapFrontBack);
#endif
callable("sampleCount", boardcomputerHardware.sampleCount);
callable("gasMin", boardcomputerHardware.gasMin);

View File

@ -56,10 +56,12 @@ struct WheelDiameterInchAccessor : public virtual AccessorInterface<float>
void setValue(float value) override { settings.controllerHardware.wheelDiameter = convertFromInch(value); saveSettings(); }
};
struct NumMagnetPolesAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.controllerHardware.numMagnetPoles; } };
#ifndef FEATURE_CAN
struct SwapFrontBackAccessor : public RefAccessorSaveSettings<bool> {
bool &getRef() const override { return settings.controllerHardware.swapFrontBack; }
void setValue(bool value) override { RefAccessorSaveSettings<bool>::setValue(value); updateSwapFrontBack(); };
};
#endif
struct SampleCountAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.boardcomputerHardware.sampleCount; } };
struct GasMinAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.boardcomputerHardware.gasMin; } };

View File

@ -251,18 +251,22 @@ void sendCommands()
{
controller.command.start = Command::VALID_HEADER;
controller.command.checksum = calculateChecksum(controller.command);
#ifndef FEATURE_CAN
controller.serial.get().write((uint8_t *) &controller.command, sizeof(controller.command));
#endif
}
}
template<typename T, typename... Args>
void switchScreen(Args&&... args);
#ifndef FEATURE_CAN
void updateSwapFrontBack()
{
controllers.front.serial = settings.controllerHardware.swapFrontBack ? Serial2 : Serial1;
controllers.back.serial = settings.controllerHardware.swapFrontBack ? Serial1 : Serial2;
}
#endif
void loadSettings()
{