rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
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
8
11
13#if EFI_PROD_CODE
14 for (size_t i = 0; i < efi::size(engineConfiguration->tcu_rangeInput); i++) {
17 }
18 }
19#endif /* EFI_PROD_CODE */
20
22}
23
25 return static_cast<SensorType>(zeroBasedSensorIndex + static_cast<int>(SensorType::RangeInput1));
26}
27
28bool GenericGearController::isNearest(float value, int pinIndex, float* rangeStates) {
29 float distance = fabs(rangeStates[pinIndex] - value);
30 for (int i = 1; i <= TCU_RANGE_COUNT; i++) {
31 float pinDistance = fabs(getRangeStateArray(i)[pinIndex] - value);
32 if (pinDistance < distance) {
33 return false;
34 }
35 }
36 return true;
37}
38
41 // Loop through possible range states
42 // 1 based because 0 is SelectedGear::Invalid
43 for (int i = 1; i <= TCU_RANGE_COUNT; i++) {
44 float *rangeStates = getRangeStateArray(i);
45 // Loop through inputs
46 for (size_t p = 0; p < efi::size(engineConfiguration->tcu_rangeInput); p++) {
47 float cellState = rangeStates[p];
48 // We allow the user to configure either a digital input or an analog input for each pin,
49 // so we need to check which is valid.
51 float pinState = Sensor::getOrZero(getAnalogSensorType(p));
52 if (getRangeStateArray(i)[p] == 0 || isNearest(pinState, p, rangeStates)) {
53 // Set the gear to the one we're checking, and continue to the next pin
54 gear = static_cast<SelectedGear>(i);
55 } else {
56 // This possibility doesn't match, set to invalid
58 break;
59 }
62 // If the pin doesn't matter, or if it matches the cellState
63 if (cellState == 2 || (pinState && cellState == 1) || (!pinState && cellState == 0)) {
64 // Set the gear to the one we're checking, and continue to the next pin
65 gear = static_cast<SelectedGear>(i);
66 } else {
67 // This possibility doesn't match, set to invalid
69 break;
70 }
71 }
72 }
73 // If we didn't find it, try the next range
74 if (gear == SelectedGear::Invalid) {
75 continue;
76 // We found a match
77 } else {
78 break;
79 }
80 }
81 if (gear != SelectedGear::Invalid) {
82 switch (gear) {
85 break;
88 break;
91 break;
94 break;
98 break;
100 // Only allow manual shift once per 500 ms,
101 // and if the selected range was Manual prior to this update
102 if (!shiftTimer.hasElapsedMs(500) || lastRange != SelectedGear::Manual) {
103 break;
104 }
105 shiftTimer.reset();
106 switch (getDesiredGear()) {
107 case GEAR_1 :
109 break;
110 case GEAR_2 :
112 break;
113 case GEAR_3 :
115 break;
116 default:
117 break;
118 }
119 break;
121 // Only allow manual shift once per 500 ms,
122 // and if the selected range was Manual prior to this update
123 if (!shiftTimer.hasElapsedMs(500) || lastRange != SelectedGear::Manual) {
124 break;
125 }
126 shiftTimer.reset();
127 switch (getDesiredGear()) {
128 case GEAR_2 :
130 break;
131 case GEAR_3 :
133 break;
134 case GEAR_4 :
136 break;
137 default:
138 break;
139 }
140 break;
142 // If the gear selector is in drive, let AutomaticGearController,
143 // which this class inherits from, decide what gear the transmission should be in.
145 return;
146 default:
147 break;
148 }
149
150 lastRange = gear;
151 }
152
154}
155
159#endif // EFI_TCU
bool isAdcChannelValid(adc_channel_e hwChannel)
Definition adc_inputs.h:23
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
virtual void init()
virtual void update()
float * getRangeStateArray(int)
virtual gear_e getDesiredGear() const
virtual gear_e setDesiredGear(gear_e)
bool isNearest(float value, int pinIndex, float *rangeStates)
SelectedGear lastRange
Definition gc_generic.h:18
SensorType getAnalogSensorType(int zeroBasedSensorIndex)
static float getOrZero(SensorType type)
Definition sensor.h:83
static constexpr engine_configuration_s * engineConfiguration
GenericGearController genericGearController
Definition gc_generic.cpp:7
GenericGearController * getGenericGearController()
iomode_t getInputMode(pin_input_mode_e mode)
Definition io_pins.cpp:103
bool efiReadPin(brain_pin_e pin)
Definition io_pins.cpp:89
bool isBrainPinValid(brain_pin_e brainPin)
@ GEAR_2
@ REVERSE
@ GEAR_1
@ GEAR_4
@ NEUTRAL
@ GEAR_3
SelectedGear
SensorType
Definition sensor_type.h:18