rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hw_layer
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
9
static
const
int8_t
phaseA
[] =
10
{
11
1,
12
1,
13
-1,
14
-1
15
};
16
17
static
const
int8_t
phaseB
[] =
18
{
19
-1,
20
1,
21
1,
22
-1
23
};
24
25
static
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
32
static
const
int
maxNumSteps
= 8;
33
static
constexpr
int
tableSizeMask
= efi::size(
microPhases
) - 1;
34
static
constexpr
float
phaseDutyCycleDivisor
= 1.0f / (100.0f * 100.0f);
// both the phase degrees and duty cycle settings are in %
35
36
37
void
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
47
bool
DualHBridgeStepper::step
(
bool
positive) {
48
// Check that we've been initialized
49
if
(!
m_motorPhaseA
|| !
m_motorPhaseB
) {
50
return
false
;
51
}
52
53
if
(
engineConfiguration
->
stepperNumMicroSteps
> 1) {
54
float
dutyMult =
engineConfiguration
->
stepperMaxDutyCycle
*
phaseDutyCycleDivisor
;
55
int
numStepIncr =
maxNumSteps
/
engineConfiguration
->
stepperNumMicroSteps
;
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
62
pause
(
engineConfiguration
->
stepperNumMicroSteps
);
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
84
bool
DualHBridgeStepper::update
(
float
dutyMult) {
85
if
(!
m_motorPhaseA
|| !
m_motorPhaseB
) {
86
return
false
;
87
}
88
89
if
(
engineConfiguration
->
stepperNumMicroSteps
> 1) {
90
// phase B is 90 degrees shifted
91
uint8_t m_phaseB = (
m_phase
+
engineConfiguration
->
stepperNumMicroSteps
) &
tableSizeMask
;
92
93
// Set phases according to the table
94
m_motorPhaseA
->
set
(dutyMult *
microPhases
[
m_phase
]);
95
m_motorPhaseB
->
set
(dutyMult *
microPhases
[m_phaseB]);
96
return
true
;
97
}
98
// Set phases according to the table
99
m_motorPhaseA
->
set
(
phaseA
[
m_phase
]);
100
m_motorPhaseB
->
set
(
phaseB
[
m_phase
]);
101
return
true
;
102
}
103
104
void
DualHBridgeStepper::sleep
() {
105
float
sleepingCoef = minI(
engineConfiguration
->
stepperMinDutyCycle
,
engineConfiguration
->
stepperMaxDutyCycle
) *
phaseDutyCycleDivisor
;
106
update
(sleepingCoef);
107
pause
();
108
}
109
110
#endif
DcMotor
Brushed or brushless DC motor interface.
Definition
dc_motor.h:20
DcMotor::set
virtual bool set(float duty)=0
Sets the motor duty cycle.
DualHBridgeStepper::step
bool step(bool positive) override
Definition
stepper_dual_hbridge.cpp:47
DualHBridgeStepper::initialize
void initialize(DcMotor *motorPhaseA, DcMotor *motorPhaseB, float reactionTime)
Definition
stepper_dual_hbridge.cpp:37
DualHBridgeStepper::m_motorPhaseA
DcMotor * m_motorPhaseA
Definition
stepper.h:60
DualHBridgeStepper::update
bool update(float dutyMult)
Definition
stepper_dual_hbridge.cpp:84
DualHBridgeStepper::m_motorPhaseB
DcMotor * m_motorPhaseB
Definition
stepper.h:61
DualHBridgeStepper::sleep
void sleep() override
Definition
stepper_dual_hbridge.cpp:104
DualHBridgeStepper::m_phase
uint8_t m_phase
Definition
stepper.h:63
StepperHw::setReactionTime
void setReactionTime(float ms)
Definition
stepper.cpp:202
StepperHw::pause
void pause(int divisor=1) const
Definition
stepper.cpp:197
dc_motor.h
engineConfiguration
static constexpr engine_configuration_s * engineConfiguration
Definition
engine_configuration.h:80
ObdCode::CUSTOM_ERR_ASSERT
@ CUSTOM_ERR_ASSERT
pch.h
stepper.h
phaseDutyCycleDivisor
static constexpr float phaseDutyCycleDivisor
Definition
stepper_dual_hbridge.cpp:34
microPhases
static const int8_t microPhases[]
Definition
stepper_dual_hbridge.cpp:25
phaseA
static const int8_t phaseA[]
Definition
stepper_dual_hbridge.cpp:9
maxNumSteps
static const int maxNumSteps
Definition
stepper_dual_hbridge.cpp:32
tableSizeMask
static constexpr int tableSizeMask
Definition
stepper_dual_hbridge.cpp:33
phaseB
static const int8_t phaseB[]
Definition
stepper_dual_hbridge.cpp:17
engine_configuration_s::stepperNumMicroSteps
stepper_num_micro_steps_e stepperNumMicroSteps
Definition
engine_configuration_generated_structures_alphax-2chan.h:3970
engine_configuration_s::stepperMinDutyCycle
uint8_t stepperMinDutyCycle
Definition
engine_configuration_generated_structures_alphax-2chan.h:3976
engine_configuration_s::stepperMaxDutyCycle
uint8_t stepperMaxDutyCycle
Definition
engine_configuration_generated_structures_alphax-2chan.h:3982
Generated on Sat Sep 27 2025 00:10:07 for rusEFI by
1.9.8