rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stepper_dual_hbridge.cpp
Go to the documentation of this file.
1
2#include "pch.h"
3
4#if !EFI_UNIT_TEST
5
6#include "stepper.h"
7#include "dc_motor.h"
8
9static const int8_t phaseA[] =
10{
11 1,
12 1,
13 -1,
14 -1
15};
16
17static const int8_t phaseB[] =
18{
19 -1,
20 1,
21 1,
22 -1
23};
24
25static const int8_t microPhases[] = {
26 0, 20, 38, 56, 71, 83, 92, 98,
27 100, 98, 92, 83, 71, 56, 38, 20,
28 0, -20, -38, -56, -71, -83, -92, -98,
29 -100, -98, -92, -83, -71, -56, -38, -20
30};
31
32static const int maxNumSteps = 8;
33static constexpr int tableSizeMask = efi::size(microPhases) - 1;
34static constexpr float phaseDutyCycleDivisor = 1.0f / (100.0f * 100.0f); // both the phase degrees and duty cycle settings are in %
35
36
37void DualHBridgeStepper::initialize(DcMotor* motorPhaseA, DcMotor* motorPhaseB, float reactionTime)
38{
39 setReactionTime(reactionTime);
40
41 m_motorPhaseA = motorPhaseA;
42 m_motorPhaseB = motorPhaseB;
43
44 efiAssertVoid(ObdCode::CUSTOM_ERR_ASSERT, engineConfiguration->stepperNumMicroSteps <= maxNumSteps, "stepperNumMicroSteps");
45}
46
47bool DualHBridgeStepper::step(bool positive) {
48 // Check that we've been initialized
50 return false;
51 }
52
56 if (!positive)
57 numStepIncr = -numStepIncr;
58 for (int i = engineConfiguration->stepperNumMicroSteps; i > 0; i--) {
59 m_phase = (m_phase + numStepIncr) & tableSizeMask;
60 update(dutyMult);
61 // sleep 1/Nth of the pause time
63 }
64 return true;
65 }
66
67 // For the full-stepping mode, we use a traditional "two phase on" drive model
68 // because "wave drive" (one phase on) method has less torque.
69 // For explanation, pls see: https://github.com/rusefi/rusefi/pull/3213#discussion_r700746453
70
71 // step phase, wrapping
72 if (positive) {
73 m_phase = (m_phase + 1) & 0x03;
74 } else {
75 m_phase = (m_phase - 1) & 0x03;
76 }
77
78 update(1.0f);
79 pause();
80
81 return true;
82}
83
84bool DualHBridgeStepper::update(float dutyMult) {
86 return false;
87 }
88
90 // phase B is 90 degrees shifted
92
93 // Set phases according to the table
95 m_motorPhaseB->set(dutyMult * microPhases[m_phaseB]);
96 return true;
97 }
98 // Set phases according to the table
101 return true;
102}
103
109
110#endif
Brushed or brushless DC motor interface.
Definition dc_motor.h:20
virtual bool set(float duty)=0
Sets the motor duty cycle.
bool step(bool positive) override
void initialize(DcMotor *motorPhaseA, DcMotor *motorPhaseB, float reactionTime)
DcMotor * m_motorPhaseA
Definition stepper.h:60
bool update(float dutyMult)
DcMotor * m_motorPhaseB
Definition stepper.h:61
uint8_t m_phase
Definition stepper.h:63
void setReactionTime(float ms)
Definition stepper.cpp:202
void pause(int divisor=1) const
Definition stepper.cpp:197
static constexpr engine_configuration_s * engineConfiguration
@ CUSTOM_ERR_ASSERT
static constexpr float phaseDutyCycleDivisor
static const int8_t microPhases[]
static const int8_t phaseA[]
static const int maxNumSteps
static constexpr int tableSizeMask
static const int8_t phaseB[]