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 "dc_motors.h"
|
|
|
|
|
2023-02-13 15:26:22 -08:00
|
|
|
void DcHardware::start(bool useTwoWires,
|
2020-03-03 14:56:50 -08:00
|
|
|
brain_pin_e pinEnable,
|
|
|
|
brain_pin_e pinDir1,
|
|
|
|
brain_pin_e pinDir2,
|
2023-03-27 08:00:01 -07:00
|
|
|
const char *disPinMsg,
|
2020-04-10 14:27:13 -07:00
|
|
|
brain_pin_e pinDisable,
|
2021-10-06 10:57:04 -07:00
|
|
|
bool isInverted,
|
2024-07-11 17:03:13 -07:00
|
|
|
Scheduler* executor,
|
2020-03-03 14:56:50 -08:00
|
|
|
int frequency) {
|
2022-11-03 06:45:57 -07:00
|
|
|
|
|
|
|
if (isStarted) {
|
|
|
|
// actually implement stop()
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isStarted = true;
|
|
|
|
|
2020-03-03 14:56:50 -08:00
|
|
|
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
|
2023-03-27 08:37:31 -07:00
|
|
|
m_disablePin.initPin(disPinMsg, 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-10-25 12:35:23 -07:00
|
|
|
if (clampedFrequency > ETB_HW_MAX_FREQUENCY) {
|
2023-08-20 19:23:44 -07:00
|
|
|
criticalError("Electronic throttle frequency too high, maximum %d hz", ETB_HW_MAX_FREQUENCY);
|
2021-10-25 12:35:23 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
2023-10-29 14:13:01 -07:00
|
|
|
/*dutyCycle*/0
|
2021-08-18 22:33:33 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
startSimplePwmHard(&m_pwm2, "ETB Dir 2",
|
|
|
|
executor,
|
|
|
|
pinDir2,
|
|
|
|
&m_pinDir2,
|
|
|
|
clampedFrequency,
|
2023-10-29 14:13:01 -07:00
|
|
|
/*dutyCycle*/0
|
2021-08-18 22:33:33 -07:00
|
|
|
);
|
|
|
|
#endif // EFI_UNIT_TEST
|
|
|
|
|
2021-10-06 10:57:04 -07:00
|
|
|
dcMotor.configure(wrappedEnable, m_pwm1, m_pwm2, isInverted);
|
2021-08-18 22:33:33 -07:00
|
|
|
} 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,
|
2023-10-29 14:13:01 -07:00
|
|
|
/*dutyCycle*/0
|
2021-08-18 22:33:33 -07:00
|
|
|
);
|
|
|
|
#endif // EFI_UNIT_TEST
|
|
|
|
|
2021-10-06 10:57:04 -07:00
|
|
|
dcMotor.configure(m_pwm1, wrappedDir1, wrappedDir2, isInverted);
|
2021-08-18 22:33:33 -07:00
|
|
|
}
|
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
|
|
|
|
2023-11-22 20:58:30 -08:00
|
|
|
DcHardware *getPrimaryDCHardwareForLogging() {
|
2023-11-01 15:51:44 -07:00
|
|
|
return &dcHardware[0];
|
2023-02-13 15:26:22 -08:00
|
|
|
}
|
|
|
|
|
2023-03-27 08:00:01 -07:00
|
|
|
DcMotor* initDcMotor(const char *disPinMsg,const dc_io& io, size_t index, bool useTwoWires) {
|
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,
|
2023-03-27 08:00:01 -07:00
|
|
|
disPinMsg,
|
2020-04-10 14:27:13 -07:00
|
|
|
io.disablePin,
|
2022-09-13 18:03:47 -07:00
|
|
|
// todo You would not believe how you invert TLE9201 #4579
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->stepperDcInvertedPins,
|
2024-07-11 17:03:13 -07:00
|
|
|
&engine->scheduler,
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->etbFreq
|
2020-03-03 14:56:50 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
return &hw.dcMotor;
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:38:48 -08:00
|
|
|
DcMotor* initDcMotor(brain_pin_e coil_p, brain_pin_e coil_m, size_t index) {
|
|
|
|
auto& hw = dcHardware[index];
|
|
|
|
|
|
|
|
hw.start(
|
|
|
|
true, /* useTwoWires */
|
2022-04-28 14:32:39 -07:00
|
|
|
Gpio::Unassigned, /* pinEnable */
|
2022-01-07 08:38:48 -08:00
|
|
|
coil_p,
|
|
|
|
coil_m,
|
2023-03-27 08:00:01 -07:00
|
|
|
nullptr,
|
2022-04-28 14:32:39 -07:00
|
|
|
Gpio::Unassigned, /* pinDisable */
|
2022-01-07 08:38:48 -08:00
|
|
|
engineConfiguration->stepperDcInvertedPins,
|
2024-07-11 17:03:13 -07:00
|
|
|
&engine->scheduler,
|
2022-01-07 08:38:48 -08:00
|
|
|
engineConfiguration->etbFreq /* same in case of stepper? */
|
|
|
|
);
|
|
|
|
|
|
|
|
return &hw.dcMotor;
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:56:50 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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());
|
2023-01-17 10:46:54 -08:00
|
|
|
const char *disableMsg = dc->msg();
|
|
|
|
if (disableMsg != nullptr) {
|
|
|
|
efiPrintf("disabled [%s]", disableMsg);
|
|
|
|
}
|
2020-03-03 14:56:50 -08:00
|
|
|
}
|
2020-05-08 18:39:35 -07:00
|
|
|
|