diff --git a/firmware/console/status_loop.cpp b/firmware/console/status_loop.cpp index 42b221032a..3cf6096b6a 100644 --- a/firmware/console/status_loop.cpp +++ b/firmware/console/status_loop.cpp @@ -55,6 +55,7 @@ #include "dynoview.h" #include "frequency_sensor.h" #include "digital_input_exti.h" +#include "dc_motors.h" extern bool main_loop_started; @@ -662,6 +663,11 @@ void updateTunerStudioState() { tsOutputChannels->tsConfigVersion = TS_FILE_VERSION; static_assert(offsetof (TunerStudioOutputChannels, tsConfigVersion) == TS_FILE_VERSION_OFFSET); +DcHardware *getdcHardware(); + + DcHardware *dc = getdcHardware(); + engine->dc_motors.dcOutput0 = dc->dcMotor.get(); + #if EFI_SHAFT_POSITION_INPUT // offset 0 diff --git a/firmware/controllers/actuators/dc_motors.cpp b/firmware/controllers/actuators/dc_motors.cpp index 32bc0f064a..8c68404f6f 100644 --- a/firmware/controllers/actuators/dc_motors.cpp +++ b/firmware/controllers/actuators/dc_motors.cpp @@ -10,54 +10,8 @@ #include "periodic_task.h" #include "dc_motors.h" -#include "dc_motor.h" -// Simple wrapper to use an OutputPin as "PWM" that can only do 0 or 1 -struct PwmWrapper : public IPwm { - OutputPin& m_pin; - - PwmWrapper(OutputPin& pin) : m_pin(pin) { } - - void setSimplePwmDutyCycle(float dutyCycle) override { - m_pin.setValue(dutyCycle > 0.5f); - } -}; - -class DcHardware { -private: - OutputPin m_pinEnable; - OutputPin m_pinDir1; - OutputPin m_pinDir2; - OutputPin m_disablePin; - - PwmWrapper wrappedEnable{m_pinEnable}; - PwmWrapper wrappedDir1{m_pinDir1}; - PwmWrapper wrappedDir2{m_pinDir2}; - - SimplePwm m_pwm1; - SimplePwm m_pwm2; - - bool isStarted = false; - -public: - DcHardware() : dcMotor(m_disablePin) {} - - TwoPinDcMotor dcMotor; - - void setFrequency(int frequency) { - m_pwm1.setFrequency(frequency); - m_pwm2.setFrequency(frequency); - } - - const char *msg() { - return dcMotor.msg; - } - - void stop() { - // todo: replace 'isStarted' with 'stop' - } - - void start(bool useTwoWires, + void DcHardware::start(bool useTwoWires, brain_pin_e pinEnable, brain_pin_e pinDir1, brain_pin_e pinDir2, @@ -127,10 +81,13 @@ public: dcMotor.configure(m_pwm1, wrappedDir1, wrappedDir2, isInverted); } } -}; static DcHardware dcHardware[ETB_COUNT + DC_PER_STEPPER]; +DcHardware *getdcHardware() { + return &dcHardware[0]; +} + DcMotor* initDcMotor(const dc_io& io, size_t index, bool useTwoWires) { auto& hw = dcHardware[index]; diff --git a/firmware/controllers/actuators/dc_motors.h b/firmware/controllers/actuators/dc_motors.h index 5adf042b69..1c20cdef9b 100644 --- a/firmware/controllers/actuators/dc_motors.h +++ b/firmware/controllers/actuators/dc_motors.h @@ -7,9 +7,9 @@ #pragma once -#include +#include "dc_motor.h" -class DcMotor; +#include DcMotor* initDcMotor(const dc_io& io, size_t index, bool useTwoWires); DcMotor* initDcMotor(brain_pin_e coil_p, brain_pin_e coil_m, size_t index); @@ -20,3 +20,58 @@ void setDcMotorDuty(size_t index, float duty); void showDcMotorInfo(int i); +// Simple wrapper to use an OutputPin as "PWM" that can only do 0 or 1 +struct PwmWrapper : public IPwm { + OutputPin& m_pin; + + PwmWrapper(OutputPin& pin) : m_pin(pin) { } + + void setSimplePwmDutyCycle(float dutyCycle) override { + m_pin.setValue(dutyCycle > 0.5f); + } +}; + +class DcHardware { +private: + OutputPin m_pinEnable; + OutputPin m_pinDir1; + OutputPin m_pinDir2; + OutputPin m_disablePin; + + PwmWrapper wrappedEnable{m_pinEnable}; + PwmWrapper wrappedDir1{m_pinDir1}; + PwmWrapper wrappedDir2{m_pinDir2}; + + SimplePwm m_pwm1; + SimplePwm m_pwm2; + + bool isStarted = false; +public: + + DcHardware() : dcMotor(m_disablePin) {} + + void start(bool useTwoWires, + brain_pin_e pinEnable, + brain_pin_e pinDir1, + brain_pin_e pinDir2, + brain_pin_e pinDisable, + bool isInverted, + ExecutorInterface* executor, + int frequency); + + TwoPinDcMotor dcMotor; + + void setFrequency(int frequency) { + m_pwm1.setFrequency(frequency); + m_pwm2.setFrequency(frequency); + } + + const char *msg() { + return dcMotor.msg; + } + + void stop() { + // todo: replace 'isStarted' with 'stop' + } + +};