2020-03-03 14:56:50 -08:00
|
|
|
/**
|
|
|
|
* @file dc_motors.cpp
|
|
|
|
*
|
|
|
|
* @date March 3, 2020
|
|
|
|
* @author Matthew Kennedy (c) 2020
|
|
|
|
*/
|
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2020-03-03 14:56:50 -08:00
|
|
|
#include "periodic_task.h"
|
|
|
|
|
|
|
|
#include "dc_motors.h"
|
|
|
|
#include "dc_motor.h"
|
|
|
|
|
2021-08-18 22:33:33 -07:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-16 16:28:30 -08:00
|
|
|
class DcHardware {
|
2020-03-03 14:56:50 -08:00
|
|
|
private:
|
|
|
|
OutputPin m_pinEnable;
|
|
|
|
OutputPin m_pinDir1;
|
|
|
|
OutputPin m_pinDir2;
|
2020-04-10 14:27:13 -07:00
|
|
|
OutputPin m_disablePin;
|
2020-03-03 14:56:50 -08:00
|
|
|
|
2021-08-18 22:33:33 -07:00
|
|
|
PwmWrapper wrappedEnable{m_pinEnable};
|
|
|
|
PwmWrapper wrappedDir1{m_pinDir1};
|
|
|
|
PwmWrapper wrappedDir2{m_pinDir2};
|
|
|
|
|
|
|
|
SimplePwm m_pwm1;
|
|
|
|
SimplePwm m_pwm2;
|
2020-03-03 14:56:50 -08:00
|
|
|
|
|
|
|
public:
|
2021-06-26 18:42:40 -07:00
|
|
|
DcHardware() : dcMotor(m_disablePin) {}
|
2020-03-03 14:56:50 -08:00
|
|
|
|
|
|
|
TwoPinDcMotor dcMotor;
|
|
|
|
|
|
|
|
void setFrequency(int frequency) {
|
2021-08-18 22:33:33 -07:00
|
|
|
m_pwm1.setFrequency(frequency);
|
|
|
|
m_pwm2.setFrequency(frequency);
|
2020-03-03 14:56:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void start(bool useTwoWires,
|
|
|
|
brain_pin_e pinEnable,
|
|
|
|
brain_pin_e pinDir1,
|
|
|
|
brain_pin_e pinDir2,
|
2020-04-10 14:27:13 -07:00
|
|
|
brain_pin_e pinDisable,
|
2020-03-03 14:56:50 -08:00
|
|
|
ExecutorInterface* executor,
|
|
|
|
int frequency) {
|
|
|
|
dcMotor.setType(useTwoWires ? TwoPinDcMotor::ControlType::PwmDirectionPins : TwoPinDcMotor::ControlType::PwmEnablePin);
|
|
|
|
|
2020-04-10 14:27:13 -07:00
|
|
|
// Configure the disable pin first - ensure things are in a safe state
|
|
|
|
m_disablePin.initPin("ETB Disable", pinDisable);
|
2020-11-15 21:06:11 -08:00
|
|
|
m_disablePin.setValue(0);
|
2020-04-10 14:27:13 -07:00
|
|
|
|
2020-03-03 14:56:50 -08:00
|
|
|
// Clamp to >100hz
|
|
|
|
int clampedFrequency = maxI(100, frequency);
|
|
|
|
|
2021-08-18 22:33:33 -07:00
|
|
|
if (useTwoWires) {
|
|
|
|
m_pinEnable.initPin("ETB Enable", pinEnable);
|
|
|
|
|
|
|
|
// no need to complicate event queue with ETB PWM in unit tests
|
|
|
|
#if ! EFI_UNIT_TEST
|
|
|
|
startSimplePwmHard(&m_pwm1, "ETB Dir 1",
|
|
|
|
executor,
|
|
|
|
pinDir1,
|
|
|
|
&m_pinDir1,
|
|
|
|
clampedFrequency,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
startSimplePwmHard(&m_pwm2, "ETB Dir 2",
|
|
|
|
executor,
|
|
|
|
pinDir2,
|
|
|
|
&m_pinDir2,
|
|
|
|
clampedFrequency,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
#endif // EFI_UNIT_TEST
|
|
|
|
|
|
|
|
dcMotor.configure(wrappedEnable, m_pwm1, m_pwm2);
|
|
|
|
} else {
|
|
|
|
m_pinDir1.initPin("ETB Dir 1", pinDir1);
|
|
|
|
m_pinDir2.initPin("ETB Dir 2", pinDir2);
|
|
|
|
|
2020-03-03 14:56:50 -08:00
|
|
|
// no need to complicate event queue with ETB PWM in unit tests
|
|
|
|
#if ! EFI_UNIT_TEST
|
2021-08-18 22:33:33 -07:00
|
|
|
startSimplePwmHard(&m_pwm1, "ETB Enable",
|
|
|
|
executor,
|
|
|
|
pinEnable,
|
|
|
|
&m_pinEnable,
|
|
|
|
clampedFrequency,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
#endif // EFI_UNIT_TEST
|
|
|
|
|
|
|
|
dcMotor.configure(m_pwm1, wrappedDir1, wrappedDir2);
|
|
|
|
}
|
2020-03-03 14:56:50 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-16 17:07:12 -08:00
|
|
|
static DcHardware dcHardware[ETB_COUNT + DC_PER_STEPPER];
|
2020-05-02 14:54:28 -07:00
|
|
|
|
2020-11-16 17:07:12 -08:00
|
|
|
DcMotor* initDcMotor(const dc_io& io, size_t index, bool useTwoWires DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
2020-11-16 16:28:30 -08:00
|
|
|
auto& hw = dcHardware[index];
|
2020-03-03 14:56:50 -08:00
|
|
|
|
|
|
|
hw.start(
|
2020-05-02 14:54:28 -07:00
|
|
|
useTwoWires,
|
2021-06-05 13:19:01 -07:00
|
|
|
io.controlPin,
|
2020-03-03 14:56:50 -08:00
|
|
|
io.directionPin1,
|
|
|
|
io.directionPin2,
|
2020-04-10 14:27:13 -07:00
|
|
|
io.disablePin,
|
2020-03-03 14:56:50 -08:00
|
|
|
&ENGINE(executor),
|
|
|
|
CONFIG(etbFreq)
|
|
|
|
);
|
|
|
|
|
|
|
|
return &hw.dcMotor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDcMotorFrequency(size_t index, int hz) {
|
2020-11-16 16:28:30 -08:00
|
|
|
dcHardware[index].setFrequency(hz);
|
2020-03-03 14:56:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setDcMotorDuty(size_t index, float duty) {
|
2020-11-16 16:28:30 -08:00
|
|
|
dcHardware[index].dcMotor.set(duty);
|
2020-03-03 14:56:50 -08:00
|
|
|
}
|
|
|
|
|
2020-05-08 18:39:35 -07:00
|
|
|
|
2021-04-18 17:02:32 -07:00
|
|
|
void showDcMotorInfo(int i) {
|
2020-11-16 16:28:30 -08:00
|
|
|
DcHardware *dc = &dcHardware[i];
|
2020-03-03 14:56:50 -08:00
|
|
|
|
2021-04-18 17:02:32 -07:00
|
|
|
efiPrintf(" motor: dir=%d DC=%f", dc->dcMotor.isOpenDirection(), dc->dcMotor.get());
|
2020-03-03 14:56:50 -08:00
|
|
|
}
|
2020-05-08 18:39:35 -07:00
|
|
|
|