| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /** | |||
| 2 | * @file fuel_schedule.cpp | |||
| 3 | * | |||
| 4 | * Handles injection scheduling | |||
| 5 | */ | |||
| 6 | ||||
| 7 | #include "pch.h" | |||
| 8 | ||||
| 9 | #if EFI_ENGINE_CONTROL | |||
| 10 | ||||
| 11 | 681 | void turnInjectionPinHigh(scheduler_arg_t const arg) { | ||
| 12 |
1/1✓ Branch 1 taken 681 times.
|
681 | auto const nowNt{ getTimeNowNt() }; | |
| 13 | ||||
| 14 | 681 | auto const taggedPointer{ TaggedPointer<InjectionEvent>::fromRaw(arg) }; | ||
| 15 | 681 | auto const event{ taggedPointer.getOriginalPointer() }; | ||
| 16 | 681 | auto const hasStage2Injection{ taggedPointer.getFlag() }; | ||
| 17 | ||||
| 18 |
2/2✓ Branch 0 taken 1362 times.
✓ Branch 1 taken 681 times.
|
2/2✓ Decision 'true' taken 1362 times.
✓ Decision 'false' taken 681 times.
|
2043 | for (auto const& output: event->outputs) { |
| 19 |
2/2✓ Branch 0 taken 891 times.
✓ Branch 1 taken 471 times.
|
2/2✓ Decision 'true' taken 891 times.
✓ Decision 'false' taken 471 times.
|
1362 | if (output) { |
| 20 |
1/1✓ Branch 1 taken 891 times.
|
891 | output->open(nowNt); | |
| 21 | } | |||
| 22 | } | |||
| 23 | ||||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 681 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 681 times.
|
681 | if (hasStage2Injection) { |
| 25 | ✗ | for (auto const& output: event->outputsStage2) { | ||
| 26 | ✗ | if (output) { | ||
| 27 | ✗ | output->open(nowNt); | ||
| 28 | } | |||
| 29 | } | |||
| 30 | } | |||
| 31 | 681 | } | ||
| 32 | ||||
| 33 | 678 | FuelSchedule::FuelSchedule() { | ||
| 34 |
2/2✓ Branch 0 taken 8136 times.
✓ Branch 1 taken 678 times.
|
2/2✓ Decision 'true' taken 8136 times.
✓ Decision 'false' taken 678 times.
|
8814 | for (int cylinderIndex = 0; cylinderIndex < MAX_CYLINDER_COUNT; cylinderIndex++) { |
| 35 | 8136 | elements[cylinderIndex].setIndex(cylinderIndex); | ||
| 36 | } | |||
| 37 | 678 | } | ||
| 38 | ||||
| 39 | 531088 | WallFuel& InjectionEvent::getWallFuel() { | ||
| 40 | 531088 | return wallFuel; | ||
| 41 | } | |||
| 42 | ||||
| 43 | 2138 | void FuelSchedule::invalidate() { | ||
| 44 | 2138 | isReady = false; | ||
| 45 | 2138 | } | ||
| 46 | ||||
| 47 | 257 | void FuelSchedule::resetOverlapping() { | ||
| 48 |
2/2✓ Branch 0 taken 3084 times.
✓ Branch 1 taken 257 times.
|
2/2✓ Decision 'true' taken 3084 times.
✓ Decision 'false' taken 257 times.
|
3341 | for (auto& inj : enginePins.injectors) { |
| 49 | 3084 | inj.reset(); | ||
| 50 | } | |||
| 51 | 257 | } | ||
| 52 | ||||
| 53 | // Determines how much to adjust injection opening angle based on the injection's duration and the current phasing mode | |||
| 54 | 2530 | static float getInjectionAngleCorrection(float fuelMs, float oneDegreeUs) { | ||
| 55 | 2530 | auto mode = engineConfiguration->injectionTimingMode; | ||
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2530 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2530 times.
|
2530 | if (mode == InjectionTimingMode::Start) { |
| 57 | // Start of injection gets no correction for duration | |||
| 58 | ✗ | return 0; | ||
| 59 | } | |||
| 60 | ||||
| 61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2530 times.
|
2530 | efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(fuelMs), "NaN fuelMs", false); | |
| 62 | ||||
| 63 | 2530 | angle_t injectionDurationAngle = MS2US(fuelMs) / oneDegreeUs; | ||
| 64 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2530 times.
|
2530 | efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(injectionDurationAngle), "NaN injectionDurationAngle", false); | |
| 65 |
2/4✓ Branch 0 taken 2530 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2530 times.
|
2530 | assertAngleRange(injectionDurationAngle, "injectionDuration_r", ObdCode::CUSTOM_INJ_DURATION); | |
| 66 | ||||
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2530 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2530 times.
|
2530 | if (mode == InjectionTimingMode::Center) { |
| 68 | // Center of injection is half-corrected for duration | |||
| 69 | ✗ | return injectionDurationAngle * 0.5f; | ||
| 70 | } else { | |||
| 71 | // End of injection gets "full correction" so we advance opening by the full duration | |||
| 72 | 2530 | return injectionDurationAngle; | ||
| 73 | } | |||
| 74 | } | |||
| 75 | ||||
| 76 | // Returns the start angle of this injector in engine coordinates (0-720 for a 4 stroke), | |||
| 77 | // or unexpected if unable to calculate the start angle due to missing information. | |||
| 78 | 3174 | expected<float> InjectionEvent::computeInjectionAngle() const { | ||
| 79 |
2/2✓ Branch 1 taken 3174 times.
✓ Branch 4 taken 3174 times.
|
3174 | floatus_t oneDegreeUs = getEngineRotationState()->getOneDegreeUs(); // local copy | |
| 80 |
2/2✓ Branch 1 taken 644 times.
✓ Branch 2 taken 2530 times.
|
2/2✓ Decision 'true' taken 644 times.
✓ Decision 'false' taken 2530 times.
|
3174 | if (std::isnan(oneDegreeUs)) { |
| 81 | // in order to have fuel schedule we need to have current RPM | |||
| 82 | // wonder if this line slows engine startup? | |||
| 83 | 644 | return unexpected; | ||
| 84 | } | |||
| 85 | ||||
| 86 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | float fuelMs = getEngineState()->injectionDuration; | |
| 87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2530 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2530 times.
|
2530 | if (std::isnan(fuelMs)) { |
| 88 | ✗ | return unexpected; | ||
| 89 | } | |||
| 90 | ||||
| 91 | // injection phase may be scheduled by injection end, so we need to step the angle back | |||
| 92 | // for the duration of the injection | |||
| 93 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | angle_t injectionDurationAngle = getInjectionAngleCorrection(fuelMs, oneDegreeUs); | |
| 94 | ||||
| 95 | // User configured offset - degrees after TDC combustion | |||
| 96 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | floatus_t injectionOffset = getEngineState()->injectionOffset; | |
| 97 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2530 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2530 times.
|
2530 | if (std::isnan(injectionOffset)) { |
| 98 | // injection offset map not ready - we are not ready to schedule fuel events | |||
| 99 | ✗ | return unexpected; | ||
| 100 | } | |||
| 101 | ||||
| 102 | 2530 | angle_t openingAngle = injectionOffset - injectionDurationAngle; | ||
| 103 |
2/5✓ Branch 0 taken 2530 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2530 times.
✗ Branch 5 not taken.
|
2530 | assertAngleRange(openingAngle, "openingAngle_r", ObdCode::CUSTOM_ERR_6554); | |
| 104 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | wrapAngle(openingAngle, "addFuel#1", ObdCode::CUSTOM_ERR_6555); | |
| 105 | // TODO: should we log per-cylinder injection timing? #76 | |||
| 106 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | getTunerStudioOutputChannels()->injectionOffset = openingAngle; | |
| 107 | ||||
| 108 | // Convert from cylinder-relative to cylinder-1-relative | |||
| 109 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | openingAngle += getPerCylinderFiringOrderOffset(ownIndex, cylinderNumber); | |
| 110 | ||||
| 111 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 2530 times.
✗ Branch 4 not taken.
|
2530 | efiAssert(ObdCode::CUSTOM_ERR_ASSERT, !std::isnan(openingAngle), "findAngle#3", false); | |
| 112 |
2/5✓ Branch 0 taken 2530 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2530 times.
✗ Branch 5 not taken.
|
2530 | assertAngleRange(openingAngle, "findAngle#a33", ObdCode::CUSTOM_ERR_6544); | |
| 113 | ||||
| 114 |
1/1✓ Branch 1 taken 2530 times.
|
2530 | wrapAngle(openingAngle, "addFuel#2", ObdCode::CUSTOM_ERR_6555); | |
| 115 | ||||
| 116 | #if EFI_UNIT_TEST | |||
| 117 | // printf("registerInjectionEvent openingAngle=%.2f inj %d\r\n", openingAngle, cylinderNumber); | |||
| 118 | #endif | |||
| 119 | ||||
| 120 | 2530 | return openingAngle; | ||
| 121 | } | |||
| 122 | ||||
| 123 | 3174 | bool InjectionEvent::updateInjectionAngle() { | ||
| 124 |
1/1✓ Branch 2 taken 3174 times.
|
3174 | auto result = computeInjectionAngle(); | |
| 125 | ||||
| 126 |
2/2✓ Branch 1 taken 2530 times.
✓ Branch 2 taken 644 times.
|
2/2✓ Decision 'true' taken 2530 times.
✓ Decision 'false' taken 644 times.
|
3174 | if (result) { |
| 127 | // If injector duty cycle is high, lock injection SOI so that we | |||
| 128 | // don't miss injections at or above 100% duty | |||
| 129 |
2/3✓ Branch 1 taken 2530 times.
✓ Branch 3 taken 2530 times.
✗ Branch 4 not taken.
|
1/2✓ Decision 'true' taken 2530 times.
✗ Decision 'false' not taken.
|
2530 | if (getEngineState()->shouldUpdateInjectionTiming) { |
| 130 | 2530 | injectionStartAngle = result.Value; | ||
| 131 | } | |||
| 132 | ||||
| 133 | 2530 | return true; | ||
| 134 | } else { | |||
| 135 | 644 | return false; | ||
| 136 | } | |||
| 137 | } | |||
| 138 | ||||
| 139 | /** | |||
| 140 | * @returns false in case of error, true if success | |||
| 141 | */ | |||
| 142 |
1/1✓ Decision 'true' taken 3174 times.
|
3174 | bool InjectionEvent::update() { | |
| 143 | 3174 | bool updatedAngle = updateInjectionAngle(); | ||
| 144 | ||||
| 145 |
2/2✓ Branch 0 taken 644 times.
✓ Branch 1 taken 2530 times.
|
2/2✓ Decision 'true' taken 644 times.
✓ Decision 'false' taken 2530 times.
|
3174 | if (!updatedAngle) { |
| 146 | 644 | return false; | ||
| 147 | } | |||
| 148 | ||||
| 149 | 2530 | injection_mode_e mode = getCurrentInjectionMode(); | ||
| 150 | 2530 | engine->outputChannels.currentInjectionMode = static_cast<uint8_t>(mode); | ||
| 151 | ||||
| 152 | int injectorIndex; | |||
| 153 |
4/4✓ Branch 0 taken 1324 times.
✓ Branch 1 taken 1206 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 1312 times.
|
2/2✓ Decision 'true' taken 1218 times.
✓ Decision 'false' taken 1312 times.
|
2530 | if (mode == IM_SIMULTANEOUS || mode == IM_SINGLE_POINT) { |
| 154 | // These modes only have one injector | |||
| 155 | 1218 | injectorIndex = 0; | ||
| 156 |
3/4✓ Branch 0 taken 307 times.
✓ Branch 1 taken 1005 times.
✓ Branch 2 taken 307 times.
✗ Branch 3 not taken.
|
1/2✓ Decision 'true' taken 1312 times.
✗ Decision 'false' not taken.
|
1312 | } else if (mode == IM_SEQUENTIAL || mode == IM_BATCH) { |
| 157 | // Map order index -> cylinder index (firing order) | |||
| 158 | 1312 | injectorIndex = getCylinderNumberAtIndex(ownIndex); | ||
| 159 | } else { | |||
| 160 | ✗ | firmwareError(ObdCode::CUSTOM_OBD_UNEXPECTED_INJECTION_MODE, "Unexpected injection mode %d", mode); | ||
| 161 | ✗ | injectorIndex = 0; | ||
| 162 | } | |||
| 163 | ||||
| 164 | InjectorOutputPin *secondOutput; | |||
| 165 | InjectorOutputPin* secondOutputStage2; | |||
| 166 | ||||
| 167 |
2/2✓ Branch 0 taken 307 times.
✓ Branch 1 taken 2223 times.
|
2/2✓ Decision 'true' taken 307 times.
✓ Decision 'false' taken 2223 times.
|
2530 | if (mode == IM_BATCH) { |
| 168 | /** | |||
| 169 | * also fire the 2nd half of the injectors so that we can implement a batch mode on individual wires | |||
| 170 | */ | |||
| 171 | // Compute the position of this cylinder's twin in the firing order | |||
| 172 | // Each injector gets fired as a primary (the same as sequential), but also | |||
| 173 | // fires the injector 360 degrees later in the firing order. | |||
| 174 | 307 | int secondOrder = (ownIndex + (engineConfiguration->cylindersCount / 2)) % engineConfiguration->cylindersCount; | ||
| 175 | 307 | int secondIndex = getCylinderNumberAtIndex(secondOrder); | ||
| 176 | 307 | secondOutput = &enginePins.injectors[secondIndex]; | ||
| 177 | 307 | secondOutputStage2 = &enginePins.injectorsStage2[secondIndex]; | ||
| 178 | } else { | |||
| 179 | 2223 | secondOutput = nullptr; | ||
| 180 | 2223 | secondOutputStage2 = nullptr; | ||
| 181 | } | |||
| 182 | ||||
| 183 | 2530 | InjectorOutputPin *output = &enginePins.injectors[injectorIndex]; | ||
| 184 | ||||
| 185 | 2530 | outputs[0] = output; | ||
| 186 | 2530 | outputs[1] = secondOutput; | ||
| 187 | 2530 | isSimultaneous = mode == IM_SIMULTANEOUS; | ||
| 188 | // Stash the cylinder number so we can select the correct fueling bank later | |||
| 189 | 2530 | cylinderNumber = injectorIndex; | ||
| 190 | ||||
| 191 | 2530 | outputsStage2[0] = &enginePins.injectorsStage2[injectorIndex]; | ||
| 192 | 2530 | outputsStage2[1] = secondOutputStage2; | ||
| 193 | ||||
| 194 |
4/6✓ Branch 0 taken 1324 times.
✓ Branch 1 taken 1206 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1324 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2530 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2530 times.
|
2530 | if (!isSimultaneous && !output->isInitialized()) { |
| 195 | // todo: extract method for this index math | |||
| 196 | ✗ | warning(ObdCode::CUSTOM_OBD_INJECTION_NO_PIN_ASSIGNED, "no_pin_inj #%s", output->getName()); | ||
| 197 | } | |||
| 198 | ||||
| 199 | 2530 | return true; | ||
| 200 | } | |||
| 201 | ||||
| 202 | 941 | void FuelSchedule::addFuelEvents() { | ||
| 203 |
2/2✓ Branch 0 taken 1845 times.
✓ Branch 1 taken 297 times.
|
2/2✓ Decision 'true' taken 1845 times.
✓ Decision 'false' taken 297 times.
|
2142 | for (size_t cylinderIndex = 0; cylinderIndex < engineConfiguration->cylindersCount; cylinderIndex++) { |
| 204 | 1845 | bool result = elements[cylinderIndex].update(); | ||
| 205 | ||||
| 206 |
2/2✓ Branch 0 taken 644 times.
✓ Branch 1 taken 1201 times.
|
2/2✓ Decision 'true' taken 644 times.
✓ Decision 'false' taken 1201 times.
|
1845 | if (!result) { |
| 207 | 644 | invalidate(); | ||
| 208 | 644 | return; | ||
| 209 | } | |||
| 210 | } | |||
| 211 | ||||
| 212 | // We made it through all cylinders, mark the schedule as ready so it can be used | |||
| 213 | 297 | isReady = true; | ||
| 214 | } | |||
| 215 | ||||
| 216 | 31177 | void FuelSchedule::onTriggerTooth(efitick_t nowNt, float currentPhase, float nextPhase) { | ||
| 217 | // Wait for schedule to be built - this happens the first time we get RPM | |||
| 218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31177 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 31177 times.
|
31177 | if (!isReady) { |
| 219 | ✗ | return; | ||
| 220 | } | |||
| 221 | ||||
| 222 |
2/2✓ Branch 0 taken 121238 times.
✓ Branch 1 taken 31177 times.
|
2/2✓ Decision 'true' taken 121238 times.
✓ Decision 'false' taken 31177 times.
|
152415 | for (size_t i = 0; i < engineConfiguration->cylindersCount; i++) { |
| 223 | 121238 | elements[i].onTriggerTooth(nowNt, currentPhase, nextPhase); | ||
| 224 | } | |||
| 225 | } | |||
| 226 | ||||
| 227 | #endif // EFI_ENGINE_CONTROL | |||
| 228 |