GCC Code Coverage Report


Directory: ./
File: firmware/controllers/algo/shift_torque_reduction_controller.cpp
Date: 2025-12-08 15:38:50
Coverage Exec Excl Total
Lines: 95.6% 87 0 91
Functions: 100.0% 9 0 9
Branches: 92.7% 38 0 41
Decisions: 85.7% 18 - 21

Line Branch Decision Exec Source
1 //
2 // Created by kifir on 9/27/24.
3 //
4
5 #include "pch.h"
6
7 #if EFI_LAUNCH_CONTROL
8 #include "shift_torque_reduction_controller.h"
9 #include "boost_control.h"
10 #include "launch_control.h"
11 #include "advance_map.h"
12 #include "engine_state.h"
13 #include "advance_map.h"
14 #include "tinymt32.h"
15 #include "gppwm_channel_reader.h"
16
17 90815 void ShiftTorqueReductionController::update() {
18
2/2
✓ Branch 0 taken 529 times.
✓ Branch 1 taken 90286 times.
2/2
✓ Decision 'true' taken 529 times.
✓ Decision 'false' taken 90286 times.
90815 if (engineConfiguration->torqueReductionEnabled) {
19 529 updateTriggerPinState();
20 529 updateTimeConditionSatisfied();
21 529 updateRpmConditionSatisfied();
22 529 updateAppConditionSatisfied();
23
24
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 9 times.
799 isFlatShiftConditionSatisfied = torqueReductionTriggerPinState && isTimeConditionSatisfied
25
6/6
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 243 times.
799 && isRpmConditionSatisfied && isAppConditionSatisfied;
26 }
27 90815 }
28
29 81322 float ShiftTorqueReductionController::getSparkSkipRatio() {
30 81322 float result = 0.0f;
31 81322 auto torqueReductionXAxis = readGppwmChannel(config->torqueReductionCutXaxis);
32 81322 trqRedCutXaxisValue = torqueReductionXAxis.Value;
33 81322 int8_t currentGear = Sensor::getOrZero(SensorType::DetectedGear);
34
35
4/4
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 80787 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 524 times.
2/2
✓ Decision 'true' taken 11 times.
✓ Decision 'false' taken 81311 times.
81322 if (engineConfiguration->torqueReductionEnabled && isFlatShiftConditionSatisfied) {
36 11 result = interpolate3d(
37 11 config->torqueReductionIgnitionCutTable,
38 11 config->torqueReductionCutGearBins, currentGear,
39 11 config->torqueReductionCutXBins, torqueReductionXAxis.Value
40 ) / 100.0f;
41 }
42 81322 return result;
43 }
44
45 529 void ShiftTorqueReductionController::updateTriggerPinState() {
46
4/5
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 138 times.
✓ Branch 3 taken 114 times.
✗ Branch 4 not taken.
529 switch (engineConfiguration->torqueReductionActivationMode) {
47
1/1
✓ Decision 'true' taken 220 times.
220 case TORQUE_REDUCTION_BUTTON: {
48 220 updateTriggerPinState(
49 engineConfiguration->torqueReductionTriggerPin,
50 220 engineConfiguration->torqueReductionTriggerPinMode,
51 /*invertPhysicalPin*/false,
52 220 engine->engineState.lua.torqueReductionState
53 );
54 220 break;
55 }
56
1/1
✓ Decision 'true' taken 57 times.
57 case LAUNCH_BUTTON: {
57 57 updateTriggerPinState(
58 engineConfiguration->launchActivatePin,
59 57 engineConfiguration->launchActivatePinMode,
60 /*invertPhysicalPin*/false,
61 false
62 );
63 57 break;
64 }
65
1/1
✓ Decision 'true' taken 138 times.
138 case TORQUE_REDUCTION_CLUTCH_DOWN_SWITCH: {
66 138 updateTriggerPinState(
67 engineConfiguration->clutchDownPin,
68 138 engineConfiguration->clutchDownPinMode,
69 /*invertPhysicalPin*/false,
70 138 engine->engineState.lua.clutchDownState
71 );
72 138 break;
73 }
74
1/1
✓ Decision 'true' taken 114 times.
114 case TORQUE_REDUCTION_CLUTCH_UP_SWITCH: {
75 114 updateTriggerPinState(
76 engineConfiguration->clutchUpPin,
77 114 engineConfiguration->clutchUpPinMode,
78 /*invertPhysicalPin*/true,
79 114 engine->engineState.lua.clutchUpState
80 );
81 114 break;
82 }
83 default: {
84 break; // we shouldn't be here!
85 }
86 }
87 529 }
88
89 272 static bool isShiftTorqueBelowTemperatureThreshold() {
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 272 times.
272 if (engineConfiguration->torqueReductionActivationTemperature == 0) {
91 return false;
92 }
93 272 return Sensor::getOrZero(SensorType::Clt) < engineConfiguration->torqueReductionActivationTemperature;
94 }
95
96 // todo: rename method since we now check temperature not just pin state
97 529 void ShiftTorqueReductionController::updateTriggerPinState(
98 const switch_input_pin_e pin,
99 const pin_input_mode_e mode,
100 const bool invertPhysicalPin,
101 const bool invalidPinState
102 ) {
103
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 257 times.
2/2
✓ Decision 'true' taken 272 times.
✓ Decision 'false' taken 257 times.
529 if (!torqueReductionTriggerPinState) {
104 272 isBelowTemperatureThreshold = isShiftTorqueBelowTemperatureThreshold();
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 272 times.
272 if (isBelowTemperatureThreshold) {
106 // disable by not even reading control pin below threshold temperature, unless already active
107 return;
108 }
109 }
110
111 #if !EFI_SIMULATOR
112 529 isTorqueReductionTriggerPinValid = isBrainPinValid(pin);
113 529 const bool previousTorqueReductionTriggerPinState = torqueReductionTriggerPinState;
114
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 265 times.
2/2
✓ Decision 'true' taken 264 times.
✓ Decision 'false' taken 265 times.
529 if (isTorqueReductionTriggerPinValid) {
115 264 torqueReductionTriggerPinState = efiReadPin(pin, mode) ^ invertPhysicalPin;
116 } else {
117 265 torqueReductionTriggerPinState = invalidPinState;
118 }
119
4/4
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 257 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 236 times.
2/2
✓ Decision 'true' taken 36 times.
✓ Decision 'false' taken 493 times.
529 if (!previousTorqueReductionTriggerPinState && torqueReductionTriggerPinState) {
120 36 m_pinTriggeredTimer.reset();
121 }
122 #endif // !EFI_SIMULATOR
123 }
124
125 529 void ShiftTorqueReductionController::updateTimeConditionSatisfied() {
126 529 auto torqueReductionTimeXaxis = readGppwmChannel(config->torqueReductionTimeXaxis);
127 529 trqRedTimeXaxisValue = torqueReductionTimeXaxis.Value;
128 529 int8_t currentGear = Sensor::getOrZero(SensorType::DetectedGear);
129
130 1058 auto torqueReductionTime = interpolate3d(
131 529 config->torqueReductionTimeTable,
132 529 config->torqueReductionTimeGearBins, currentGear,
133 529 config->torqueReductionTimeXBins, torqueReductionTimeXaxis.Value
134 );
135
136 1058 isTimeConditionSatisfied = torqueReductionTriggerPinState
137
6/6
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 259 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 232 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 5 times.
562 ? !engineConfiguration->limitTorqueReductionTime ||
138 ((0.0f < torqueReductionTime)
139
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 4 times.
33 && !m_pinTriggeredTimer.hasElapsedMs(torqueReductionTime)
140 )
141 : false;
142 529 }
143
144 529 void ShiftTorqueReductionController::updateRpmConditionSatisfied() {
145 529 const float currentRpm = Sensor::getOrZero(SensorType::Rpm);
146 529 isRpmConditionSatisfied = (engineConfiguration->torqueReductionArmingRpm <= currentRpm);
147 529 }
148
149 529 void ShiftTorqueReductionController::updateAppConditionSatisfied() {
150 529 const SensorResult currentApp = Sensor::get(SensorType::DriverThrottleIntent);
151
152
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 503 times.
2/2
✓ Decision 'true' taken 26 times.
✓ Decision 'false' taken 503 times.
529 if (currentApp.Valid) {
153 26 isAppConditionSatisfied = (engineConfiguration->torqueReductionArmingApp <= currentApp.Value);
154 } else {
155 503 isAppConditionSatisfied = false;
156 }
157 529 }
158
159 11 float ShiftTorqueReductionController::getTorqueReductionIgnitionRetard() {
160 11 auto torqueReductionXAxis = readGppwmChannel(config->torqueReductionIgnitionRetardXaxis);
161 11 trqRedIgnRetXaxisValue = torqueReductionXAxis.Value;
162 11 int8_t currentGear = Sensor::getOrZero(SensorType::DetectedGear);
163
164 22 return interpolate3d(
165 11 config->torqueReductionIgnitionRetardTable,
166 11 config->torqueReductionIgnitionRetardGearBins, currentGear,
167 11 config->torqueReductionIgnitionRetardXBins, torqueReductionXAxis.Value
168 11 );
169 }
170
171 #endif // EFI_LAUNCH_CONTROL
172