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
21
23 return static_cast<SensorType>(zeroBasedSensorIndex + static_cast<int>(SensorType::RangeInput1));
24}
25
26bool 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
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.
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
56 }
58 // If we've locked out this range with 3 in a cell
59 if (cellState == 3) {
61 break;
62 }
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
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) {
86 break;
89 break;
92 break;
95 break;
99 break;
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 :
110 break;
111 case GEAR_2 :
113 break;
114 case GEAR_3 :
116 break;
117 default:
118 break;
119 }
120 break;
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 :
131 break;
132 case GEAR_3 :
134 break;
135 case GEAR_4 :
137 break;
138 default:
139 break;
140 }
141 break;
143 // If the gear selector is in drive, let AutomaticGearController,
144 // which this class inherits from, decide what gear the transmission should be in.
146 return;
147 default:
148 break;
149 }
150
151 lastRange = gear;
152 }
153
155}
156
160#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