| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /** | |||
| 2 | * @file dc_motors.cpp | |||
| 3 | * | |||
| 4 | * @date March 3, 2020 | |||
| 5 | * @author Matthew Kennedy (c) 2020 | |||
| 6 | */ | |||
| 7 | ||||
| 8 | #include "pch.h" | |||
| 9 | ||||
| 10 | #include "dc_motors.h" | |||
| 11 | ||||
| 12 | 2068 | void DcHardware::start(bool useTwoWires, | ||
| 13 | brain_pin_e pinEnable, | |||
| 14 | brain_pin_e pinDir1, | |||
| 15 | brain_pin_e pinDir2, | |||
| 16 | const char *disPinMsg, | |||
| 17 | brain_pin_e pinDisable, | |||
| 18 | bool isInverted, | |||
| 19 | Scheduler* executor, | |||
| 20 | int frequency) { | |||
| 21 | ||||
| 22 |
2/2✓ Branch 0 taken 2066 times.
✓ Branch 1 taken 2 times.
|
2/2✓ Decision 'true' taken 2066 times.
✓ Decision 'false' taken 2 times.
|
2068 | if (isStarted) { |
| 23 | // actually implement stop() | |||
| 24 | 2066 | return; | ||
| 25 | } | |||
| 26 | 2 | isStarted = true; | ||
| 27 | ||||
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | dcMotor.setType(useTwoWires ? TwoPinDcMotor::ControlType::PwmDirectionPins : TwoPinDcMotor::ControlType::PwmEnablePin); | |
| 29 | ||||
| 30 | // Configure the disable pin first - ensure things are in a safe state | |||
| 31 | 2 | m_disablePin.initPin(disPinMsg, pinDisable); | ||
| 32 | 2 | m_disablePin.setValue(0); | ||
| 33 | ||||
| 34 | // Clamp to >100hz | |||
| 35 | 2 | int clampedFrequency = maxI(100, frequency); | ||
| 36 | ||||
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2 times.
|
2 | if (clampedFrequency > ETB_HW_MAX_FREQUENCY) { |
| 38 | ✗ | criticalError("Electronic throttle frequency too high, maximum %d hz", ETB_HW_MAX_FREQUENCY); | ||
| 39 | ✗ | return; | ||
| 40 | } | |||
| 41 | ||||
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2 times.
|
2 | if (useTwoWires) { |
| 43 | ✗ | m_pinEnable.initPin("ETB Enable", pinEnable); | ||
| 44 | ||||
| 45 | // no need to complicate event queue with ETB PWM in unit tests | |||
| 46 | #if ! EFI_UNIT_TEST | |||
| 47 | startSimplePwmHard(&m_pwm1, "ETB Dir 1", | |||
| 48 | executor, | |||
| 49 | pinDir1, | |||
| 50 | &m_pinDir1, | |||
| 51 | clampedFrequency, | |||
| 52 | /*dutyCycle*/0 | |||
| 53 | ); | |||
| 54 | ||||
| 55 | startSimplePwmHard(&m_pwm2, "ETB Dir 2", | |||
| 56 | executor, | |||
| 57 | pinDir2, | |||
| 58 | &m_pinDir2, | |||
| 59 | clampedFrequency, | |||
| 60 | /*dutyCycle*/0 | |||
| 61 | ); | |||
| 62 | #endif // EFI_UNIT_TEST | |||
| 63 | ||||
| 64 | ✗ | dcMotor.configure(wrappedEnable, m_pwm1, m_pwm2, isInverted); | ||
| 65 | } else { | |||
| 66 | 2 | m_pinDir1.initPin("ETB Dir 1", pinDir1); | ||
| 67 | 2 | m_pinDir2.initPin("ETB Dir 2", pinDir2); | ||
| 68 | ||||
| 69 | // no need to complicate event queue with ETB PWM in unit tests | |||
| 70 | #if ! EFI_UNIT_TEST | |||
| 71 | startSimplePwmHard(&m_pwm1, "ETB Enable", | |||
| 72 | executor, | |||
| 73 | pinEnable, | |||
| 74 | &m_pinEnable, | |||
| 75 | clampedFrequency, | |||
| 76 | /*dutyCycle*/0 | |||
| 77 | ); | |||
| 78 | #endif // EFI_UNIT_TEST | |||
| 79 | ||||
| 80 | 2 | dcMotor.configure(m_pwm1, wrappedDir1, wrappedDir2, isInverted); | ||
| 81 | } | |||
| 82 | } | |||
| 83 | ||||
| 84 | static DcHardware dcHardware[ETB_COUNT + DC_PER_STEPPER]; | |||
| 85 | ||||
| 86 | 531078 | DcHardware *getPrimaryDCHardwareForLogging() { | ||
| 87 | 531078 | return &dcHardware[0]; | ||
| 88 | } | |||
| 89 | ||||
| 90 | 2068 | DcMotor* initDcMotor(const char *disPinMsg,const dc_io& io, size_t index, bool useTwoWires) { | ||
| 91 | 2068 | auto& hw = dcHardware[index]; | ||
| 92 | ||||
| 93 | 2068 | hw.start( | ||
| 94 | useTwoWires, | |||
| 95 | 2068 | io.controlPin, | ||
| 96 | 2068 | io.directionPin1, | ||
| 97 | 2068 | io.directionPin2, | ||
| 98 | disPinMsg, | |||
| 99 | 2068 | io.disablePin, | ||
| 100 | // todo You would not believe how you invert TLE9201 #4579 | |||
| 101 | 2068 | engineConfiguration->stepperDcInvertedPins, | ||
| 102 | &engine->scheduler, | |||
| 103 | 2068 | engineConfiguration->etbFreq | ||
| 104 | ); | |||
| 105 | ||||
| 106 | 2068 | return &hw.dcMotor; | ||
| 107 | } | |||
| 108 | ||||
| 109 | ✗ | DcMotor* initDcMotor(brain_pin_e coil_p, brain_pin_e coil_m, size_t index) { | ||
| 110 | ✗ | auto& hw = dcHardware[index]; | ||
| 111 | ||||
| 112 | ✗ | hw.start( | ||
| 113 | true, /* useTwoWires */ | |||
| 114 | Gpio::Unassigned, /* pinEnable */ | |||
| 115 | coil_p, | |||
| 116 | coil_m, | |||
| 117 | nullptr, | |||
| 118 | Gpio::Unassigned, /* pinDisable */ | |||
| 119 | ✗ | engineConfiguration->stepperDcInvertedPins, | ||
| 120 | &engine->scheduler, | |||
| 121 | ✗ | engineConfiguration->etbFreq /* same in case of stepper? */ | ||
| 122 | ); | |||
| 123 | ||||
| 124 | ✗ | return &hw.dcMotor; | ||
| 125 | } | |||
| 126 | ||||
| 127 | ✗ | void setDcMotorFrequency(size_t index, int hz) { | ||
| 128 | ✗ | dcHardware[index].setFrequency(hz); | ||
| 129 | ✗ | } | ||
| 130 | ||||
| 131 | ✗ | void setDcMotorDuty(size_t index, float duty) { | ||
| 132 | ✗ | dcHardware[index].dcMotor.set(duty); | ||
| 133 | ✗ | } | ||
| 134 | ||||
| 135 | ✗ | void showDcMotorInfo(int i) { | ||
| 136 | ✗ | DcHardware *dc = &dcHardware[i]; | ||
| 137 | ||||
| 138 | ✗ | efiPrintf(" motor: dir=%d DC=%f", dc->dcMotor.isOpenDirection(), dc->dcMotor.get()); | ||
| 139 | ✗ | const char *disableMsg = dc->msg(); | ||
| 140 | ✗ | if (disableMsg != nullptr) { | ||
| 141 | ✗ | efiPrintf("disabled [%s]", disableMsg); | ||
| 142 | } | |||
| 143 | ✗ | } | ||
| 144 | ||||
| 145 |