rusefi-1/firmware/controllers/algo/signal_executor.cpp

223 lines
7.4 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file signal_executor.cpp
*
* todo: we should split this file into two:
* one for pure scheduling and another one for signal output which would
* use the scheduling
*
* @date Dec 4, 2013
2015-12-31 13:02:30 -08:00
* @author Andrey Belomutskiy, (c) 2012-2016
2015-07-10 06:01:56 -07:00
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "signal_executor.h"
#include "efiGpio.h"
#include "engine.h"
/**
* Signal executors feed digital events right into WaveChart used by Sniffer tab of Dev Console
*/
#include "rpm_calculator.h"
EXTERN_ENGINE;
2016-01-24 23:03:01 -08:00
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
2015-07-15 18:01:45 -07:00
#include "engine_sniffer.h"
2015-07-10 06:01:56 -07:00
extern WaveChart waveChart;
2016-08-26 14:02:37 -07:00
#endif /* EFI_ENGINE_SNIFFER */
2015-07-10 06:01:56 -07:00
#include "efiGpio.h"
extern engine_pins_s enginePins;
static const char *sparkNames[IGNITION_PIN_COUNT] = { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8",
"c9", "cA", "cB", "cD"};
static const char *injectorNames[INJECTION_PIN_COUNT] = { "i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8",
"j9", "iA", "iB", "iC"};
2016-09-03 16:02:42 -07:00
void initEnginePinsNames(void) {
// todo: make engine_pins_s a class and move this to constructor?
2015-07-10 06:01:56 -07:00
for (int i = 0; i < IGNITION_PIN_COUNT;i++) {
enginePins.coils[i].name = sparkNames[i];
}
for (int i = 0; i < INJECTION_PIN_COUNT;i++) {
enginePins.injectors[i].name = injectorNames[i];
}
}
2016-09-03 16:02:42 -07:00
void initSignalExecutor(void) {
initSignalExecutorImpl();
initEnginePinsNames();
}
2015-07-10 06:01:56 -07:00
void turnPinHigh(NamedOutputPin *output) {
efiAssertVoid(output!=NULL, "NULL @ turnPinHigh");
2016-01-24 23:03:01 -08:00
#if EFI_DEFAILED_LOGGING || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
// signal->hi_time = hTimeNow();
#endif /* EFI_DEFAILED_LOGGING */
// turn the output level ACTIVE
// todo: this XOR should go inside the setOutputPinValue method
doSetOutputPinValue2(output, true);
2016-09-03 21:03:27 -07:00
2015-07-10 06:01:56 -07:00
// sleep for the needed duration
2016-01-24 23:03:01 -08:00
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
// explicit check here is a performance optimization to speed up no-chart mode
2016-01-30 19:03:36 -08:00
if (ENGINE(isEngineChartEnabled)) {
2015-07-10 06:01:56 -07:00
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
const char *pinName = output->name;
// dbgDurr = hal_lld_get_counter_value() - dbgStart;
2016-01-30 19:03:36 -08:00
addEngineSniffferEvent(pinName, WC_UP);
2015-07-10 06:01:56 -07:00
}
2016-01-24 23:03:01 -08:00
#endif /* EFI_ENGINE_SNIFFER */
2015-07-10 06:01:56 -07:00
// dbgDurr = hal_lld_get_counter_value() - dbgStart;
}
void turnPinLow(NamedOutputPin *output) {
efiAssertVoid(output!=NULL, "NULL turnPinLow");
// turn off the output
doSetOutputPinValue2(output, false);
2016-01-24 23:03:01 -08:00
#if EFI_DEFAILED_LOGGING || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
systime_t after = hTimeNow();
debugInt(&signal->logging, "a_time", after - signal->hi_time);
scheduleLogging(&signal->logging);
#endif /* EFI_DEFAILED_LOGGING */
2016-01-24 23:03:01 -08:00
#if EFI_ENGINE_SNIFFER || defined(__DOXYGEN__)
2016-01-30 19:03:36 -08:00
if (ENGINE(isEngineChartEnabled)) {
2015-07-10 06:01:56 -07:00
// this is a performance optimization - array index is cheaper then invoking a method with 'switch'
const char *pinName = output->name;
2016-01-30 19:03:36 -08:00
addEngineSniffferEvent(pinName, WC_DOWN);
2015-07-10 06:01:56 -07:00
}
2016-01-24 23:03:01 -08:00
#endif /* EFI_ENGINE_SNIFFER */
2015-07-10 06:01:56 -07:00
}
2016-08-26 14:02:37 -07:00
#if FUEL_MATH_EXTREME_LOGGING
extern LoggingWithStorage sharedLogger;
#endif /* FUEL_MATH_EXTREME_LOGGING */
// todo: make these macro? kind of a penny optimization if compiler is not smart to inline
2016-09-12 17:02:56 -07:00
void seTurnPinHigh(InjectorOutputPin *output) {
2016-09-22 06:03:20 -07:00
if (output->currentLogicValue == 1) {
2016-09-13 06:01:44 -07:00
// if (output->cancelNextTurningInjectorOff) {
// // how comes AutoTest.testFordAspire ends up here?
// } else {
// /**
// * #299
// * this is another kind of overlap which happens in case of a small duty cycle after a large duty cycle
// */
2016-09-22 06:03:20 -07:00
#if EFI_SIMULATOR || defined(__DOXYGEN__)
printf("cancelNextTurningInjectorOff %s %d\r\n", output->name, (int)getTimeNowUs());
#endif /* EFI_SIMULATOR */
2016-09-13 06:01:44 -07:00
// output->cancelNextTurningInjectorOff = true;
// return;
// }
2016-09-22 06:03:20 -07:00
}
2016-08-26 14:02:37 -07:00
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
const char * w = output->currentLogicValue == true ? "err" : "";
2016-09-04 21:02:31 -07:00
scheduleMsg(&sharedLogger, "^ %spin=%s eventIndex %d %d", w, output->name,
getRevolutionCounter(), getTimeNowUs());
2016-08-26 14:02:37 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2016-09-04 21:02:31 -07:00
#if EFI_UNIT_TEST
2016-09-13 06:01:44 -07:00
// if (output->currentLogicValue == 1)
// firmwareError("Already high");
2016-09-04 21:02:31 -07:00
#endif
2016-09-22 06:03:20 -07:00
#if EFI_SIMULATOR || defined(__DOXYGEN__)
printf("seTurnPinHigh %s %d\r\n", output->name, (int)getTimeNowUs());
#endif /* EFI_SIMULATOR */
2016-08-26 14:02:37 -07:00
turnPinHigh(output);
}
2016-09-03 22:01:54 -07:00
void seTurnPinLow(InjectorOutputPin *output) {
if (output->cancelNextTurningInjectorOff) {
/**
* in case of fuel schedule overlap between engine cycles,
* and if engine cycle is above say 75% for batch mode on 4 cylinders,
* we will get a secondary overlap between the special injection and a normal injection on the same injector.
* In such a case want to combine these two injection into one continues injection.
* Unneeded turn of injector on is handle while scheduling that second injection, but cancellation
* of special injection end has to be taken care of dynamically
*
*/
output->cancelNextTurningInjectorOff = false;
2016-09-22 06:03:20 -07:00
#if EFI_SIMULATOR || defined(__DOXYGEN__)
printf("was cancelled %s %d\r\n", output->name, (int)getTimeNowUs());
#endif /* EFI_SIMULATOR */
2016-09-03 22:01:54 -07:00
return;
}
2016-09-22 06:03:20 -07:00
#if EFI_SIMULATOR || defined(__DOXYGEN__)
printf("seTurnPinLow %s %d\r\n", output->name, (int)getTimeNowUs());
#endif /* EFI_SIMULATOR */
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
2016-08-26 14:02:37 -07:00
const char * w = output->currentLogicValue == false ? "err" : "";
2016-09-04 21:02:31 -07:00
scheduleMsg(&sharedLogger, "- %spin=%s eventIndex %d %d", w, output->name,
getRevolutionCounter(), getTimeNowUs());
2016-08-26 14:02:37 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2016-09-04 21:02:31 -07:00
#if EFI_UNIT_TEST
2016-09-13 06:01:44 -07:00
// if (output->currentLogicValue == 0)
// firmwareError("Already low");
2016-09-04 21:02:31 -07:00
#endif
2016-08-26 14:02:37 -07:00
turnPinLow(output);
}
void seScheduleByTime(const char *prefix, scheduling_s *scheduling, efitimeus_t time, schfunc_t callback, NamedOutputPin *param) {
#if FUEL_MATH_EXTREME_LOGGING || defined(__DOXYGEN__)
2016-09-04 21:02:31 -07:00
scheduleMsg(&sharedLogger, "schX %s %x %d", prefix, scheduling, time);
scheduleMsg(&sharedLogger, "schX %s", param->name);
2016-08-26 14:02:37 -07:00
#endif /* FUEL_MATH_EXTREME_LOGGING */
2016-09-03 22:01:54 -07:00
2016-09-22 06:03:20 -07:00
#if EFI_SIMULATOR || EFI_UNIT_TEST || defined(__DOXYGEN__)
printf("schB %s %d\r\n", param->name, (int)time);
#endif /* EFI_SIMULATOR || EFI_UNIT_TEST */
2016-08-26 14:02:37 -07:00
scheduleByTime(prefix, scheduling, time, callback, param);
}
2015-07-10 06:01:56 -07:00
/**
*
* @param delay the number of ticks before the output signal
* immediate output if delay is zero
* @param dwell the number of ticks of output duration
*
*/
2016-09-03 22:01:54 -07:00
void scheduleOutput2(scheduling_s * sUp, scheduling_s * sDown, efitimeus_t nowUs, float delayUs, float durationUs, InjectorOutputPin *output) {
2016-01-30 19:03:36 -08:00
#if EFI_GPIO || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
2016-01-30 19:03:36 -08:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
printf("scheduling output %s\r\n", output->name);
2016-09-03 22:01:54 -07:00
#endif /* EFI_UNIT_TEST */
2015-07-10 06:01:56 -07:00
2016-09-03 22:01:54 -07:00
efitimeus_t turnOnTime = nowUs + (int) delayUs;
seScheduleByTime("out up", sUp, turnOnTime, (schfunc_t) &seTurnPinHigh, output);
efitimeus_t turnOffTime = nowUs + (int) (delayUs + durationUs);
seScheduleByTime("out down", sDown, turnOffTime, (schfunc_t) &seTurnPinLow, output);
#endif /* EFI_GPIO */
2015-07-10 06:01:56 -07:00
}
2016-09-03 22:01:54 -07:00