rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
controllers
tcu
gc_generic.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
3
#include "math.h"
4
#include "
gc_generic.h
"
5
6
#if EFI_TCU
7
GenericGearController
genericGearController
;
8
9
GenericGearController::GenericGearController
() {
10
}
11
12
void
GenericGearController::init
() {
13
for
(
size_t
i = 0; i < efi::size(
engineConfiguration
->
tcu_rangeInput
); i++) {
14
if
(
isBrainPinValid
(
engineConfiguration
->
tcu_rangeInput
[i])) {
15
efiSetPadMode
(
"Range Input"
,
engineConfiguration
->
tcu_rangeInput
[i],
getInputMode
(
engineConfiguration
->
tcu_rangeInputMode
[i]));
16
}
17
}
18
19
GearControllerBase::init
();
20
}
21
22
SensorType
GenericGearController::getAnalogSensorType
(
int
zeroBasedSensorIndex) {
23
return
static_cast<
SensorType
>
(zeroBasedSensorIndex +
static_cast<
int
>
(
SensorType::RangeInput1
));
24
}
25
26
bool
GenericGearController::isNearest
(
float
value,
int
pinIndex,
float
* rangeStates) {
27
float
distance = fabs(rangeStates[pinIndex] - value);
28
for
(
int
i = 1; i <= TCU_RANGE_COUNT; i++) {
29
float
pinDistance = fabs(
getRangeStateArray
(i)[pinIndex] - value);
30
if
(pinDistance < distance) {
31
return
false
;
32
}
33
}
34
return
true
;
35
}
36
37
void
GenericGearController::update
() {
38
SelectedGear
gear =
SelectedGear::Invalid
;
39
// Loop through possible range states
40
// 1 based because 0 is SelectedGear::Invalid
41
for
(
int
i = 1; i <= TCU_RANGE_COUNT; i++) {
42
float
*rangeStates =
getRangeStateArray
(i);
43
// Loop through inputs
44
for
(
size_t
p = 0; p < efi::size(
engineConfiguration
->
tcu_rangeInput
); p++) {
45
float
cellState = rangeStates[p];
46
// We allow the user to configure either a digital input or an analog input for each pin,
47
// so we need to check which is valid.
48
if
(
isAdcChannelValid
(
engineConfiguration
->
tcu_rangeAnalogInput
[p])) {
49
float
pinState =
Sensor::getOrZero
(
getAnalogSensorType
(p));
50
if
(
isNearest
(pinState, p, rangeStates)) {
51
// Set the gear to the one we're checking, and continue to the next pin
52
gear =
static_cast<
SelectedGear
>
(i);
53
}
else
{
54
// This possibility doesn't match, set to invalid
55
gear =
SelectedGear::Invalid
;
56
}
57
}
else
if
(
isBrainPinValid
(
engineConfiguration
->
tcu_rangeInput
[p])) {
58
// If we've locked out this range with 3 in a cell
59
if
(cellState == 3) {
60
gear =
SelectedGear::Invalid
;
61
break
;
62
}
63
bool
pinState =
efiReadPin
(
engineConfiguration
->
tcu_rangeInput
[p]);
64
// If the pin doesn't matter, or if it matches the cellState
65
if
(cellState == 2 || (pinState && cellState == 1) || (!pinState && cellState == 0)) {
66
// Set the gear to the one we're checking, and continue to the next pin
67
gear =
static_cast<
SelectedGear
>
(i);
68
}
else
{
69
// This possibility doesn't match, set to invalid
70
gear =
SelectedGear::Invalid
;
71
}
72
}
73
}
74
// If we didn't find it, try the next range
75
if
(gear ==
SelectedGear::Invalid
) {
76
continue
;
77
// We found a match
78
}
else
{
79
break
;
80
}
81
}
82
if
(gear !=
SelectedGear::Invalid
) {
83
switch
(gear) {
84
case
SelectedGear::Manual3
:
85
setDesiredGear
(
GEAR_3
);
86
break
;
87
case
SelectedGear::Manual2
:
88
setDesiredGear
(
GEAR_2
);
89
break
;
90
case
SelectedGear::Manual1
:
91
setDesiredGear
(
GEAR_1
);
92
break
;
93
case
SelectedGear::Reverse
:
94
setDesiredGear
(
REVERSE
);
95
break
;
96
case
SelectedGear::Park
:
97
case
SelectedGear::Neutral
:
98
setDesiredGear
(
NEUTRAL
);
99
break
;
100
case
SelectedGear::ManualPlus
:
101
// Only allow manual shift once per 500 ms,
102
// and if the selected range was Manual prior to this update
103
if
(!
shiftTimer
.hasElapsedMs(500) ||
lastRange
!=
SelectedGear::Manual
) {
104
break
;
105
}
106
shiftTimer
.reset();
107
switch
(
getDesiredGear
()) {
108
case
GEAR_1
:
109
setDesiredGear
(
GEAR_2
);
110
break
;
111
case
GEAR_2
:
112
setDesiredGear
(
GEAR_3
);
113
break
;
114
case
GEAR_3
:
115
setDesiredGear
(
GEAR_4
);
116
break
;
117
default
:
118
break
;
119
}
120
break
;
121
case
SelectedGear::ManualMinus
:
122
// Only allow manual shift once per 500 ms,
123
// and if the selected range was Manual prior to this update
124
if
(!
shiftTimer
.hasElapsedMs(500) ||
lastRange
!=
SelectedGear::Manual
) {
125
break
;
126
}
127
shiftTimer
.reset();
128
switch
(
getDesiredGear
()) {
129
case
GEAR_2
:
130
setDesiredGear
(
GEAR_1
);
131
break
;
132
case
GEAR_3
:
133
setDesiredGear
(
GEAR_2
);
134
break
;
135
case
GEAR_4
:
136
setDesiredGear
(
GEAR_3
);
137
break
;
138
default
:
139
break
;
140
}
141
break
;
142
case
SelectedGear::Drive
:
143
// If the gear selector is in drive, let AutomaticGearController,
144
// which this class inherits from, decide what gear the transmission should be in.
145
AutomaticGearController::update
();
146
return
;
147
default
:
148
break
;
149
}
150
151
lastRange
= gear;
152
}
153
154
GearControllerBase::update
();
155
}
156
157
GenericGearController
*
getGenericGearController
() {
158
return
&
genericGearController
;
159
}
160
#endif
// EFI_TCU
isAdcChannelValid
bool isAdcChannelValid(adc_channel_e hwChannel)
Definition
adc_inputs.h:23
efiSetPadMode
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
Definition
bootloader_main.cpp:207
AutomaticGearController::update
void update()
Definition
gc_auto.cpp:11
GearControllerBase::init
virtual void init()
Definition
gear_controller.cpp:6
GearControllerBase::update
virtual void update()
Definition
gear_controller.cpp:28
GearControllerBase::getRangeStateArray
float * getRangeStateArray(int)
Definition
gear_controller.cpp:77
GearControllerBase::getDesiredGear
virtual gear_e getDesiredGear() const
Definition
gear_controller.cpp:47
GearControllerBase::setDesiredGear
virtual gear_e setDesiredGear(gear_e)
Definition
gear_controller.cpp:51
GenericGearController
Definition
gc_generic.h:7
GenericGearController::isNearest
bool isNearest(float value, int pinIndex, float *rangeStates)
Definition
gc_generic.cpp:26
GenericGearController::lastRange
SelectedGear lastRange
Definition
gc_generic.h:18
GenericGearController::getAnalogSensorType
SensorType getAnalogSensorType(int zeroBasedSensorIndex)
Definition
gc_generic.cpp:22
GenericGearController::GenericGearController
GenericGearController()
Definition
gc_generic.cpp:9
GenericGearController::shiftTimer
Timer shiftTimer
Definition
gc_generic.h:17
GenericGearController::update
void update()
Definition
gc_generic.cpp:37
GenericGearController::init
void init()
Definition
gc_generic.cpp:12
Sensor::getOrZero
static float getOrZero(SensorType type)
Definition
sensor.h:83
engineConfiguration
static constexpr engine_configuration_s * engineConfiguration
Definition
engine_configuration.h:80
genericGearController
GenericGearController genericGearController
Definition
gc_generic.cpp:7
getGenericGearController
GenericGearController * getGenericGearController()
Definition
gc_generic.cpp:157
gc_generic.h
getInputMode
iomode_t getInputMode(pin_input_mode_e mode)
Definition
io_pins.cpp:103
efiReadPin
bool efiReadPin(brain_pin_e pin)
Definition
io_pins.cpp:89
pch.h
isBrainPinValid
bool isBrainPinValid(brain_pin_e brainPin)
Definition
pin_repository.cpp:25
GEAR_2
@ GEAR_2
Definition
rusefi_enums.h:498
REVERSE
@ REVERSE
Definition
rusefi_enums.h:495
GEAR_1
@ GEAR_1
Definition
rusefi_enums.h:497
GEAR_4
@ GEAR_4
Definition
rusefi_enums.h:500
NEUTRAL
@ NEUTRAL
Definition
rusefi_enums.h:496
GEAR_3
@ GEAR_3
Definition
rusefi_enums.h:499
SelectedGear
SelectedGear
Definition
rusefi_enums.h:844
SelectedGear::Invalid
@ Invalid
SelectedGear::Reverse
@ Reverse
SelectedGear::Park
@ Park
SelectedGear::Manual2
@ Manual2
SelectedGear::ManualMinus
@ ManualMinus
SelectedGear::ManualPlus
@ ManualPlus
SelectedGear::Manual
@ Manual
SelectedGear::Manual1
@ Manual1
SelectedGear::Neutral
@ Neutral
SelectedGear::Drive
@ Drive
SelectedGear::Manual3
@ Manual3
SensorType
SensorType
Definition
sensor_type.h:18
SensorType::RangeInput1
@ RangeInput1
engine_configuration_s::tcu_rangeAnalogInput
adc_channel_e tcu_rangeAnalogInput[RANGE_INPUT_COUNT]
Definition
engine_configuration_generated_structures_alphax-2chan.h:5137
engine_configuration_s::tcu_rangeInputMode
pin_input_mode_e tcu_rangeInputMode[RANGE_INPUT_COUNT]
Definition
engine_configuration_generated_structures_alphax-2chan.h:4891
engine_configuration_s::tcu_rangeInput
switch_input_pin_e tcu_rangeInput[RANGE_INPUT_COUNT]
Definition
engine_configuration_generated_structures_alphax-2chan.h:4887
Generated on Sat Sep 27 2025 00:10:06 for rusEFI by
1.9.8