2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file event_registry.h
|
|
|
|
*
|
|
|
|
* @date Nov 27, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2019-10-07 22:49:42 -07:00
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "global.h"
|
2019-10-08 00:14:21 -07:00
|
|
|
#include "efi_gpio.h"
|
|
|
|
#include "scheduler.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "fl_stack.h"
|
2016-12-18 19:03:00 -08:00
|
|
|
#include "trigger_structure.h"
|
2019-10-07 23:13:52 -07:00
|
|
|
|
2022-12-10 14:07:02 -08:00
|
|
|
struct AngleBasedEvent {
|
2024-05-10 20:08:11 -07:00
|
|
|
scheduling_s eventScheduling;
|
2019-11-23 16:47:53 -08:00
|
|
|
action_s action;
|
2019-11-23 17:36:40 -08:00
|
|
|
/**
|
|
|
|
* Trigger-based scheduler maintains a linked list of all pending tooth-based events.
|
|
|
|
*/
|
2022-12-10 14:07:02 -08:00
|
|
|
AngleBasedEvent *nextToothEvent = nullptr;
|
2022-11-05 22:11:33 -07:00
|
|
|
|
2024-02-28 18:44:02 -08:00
|
|
|
// angular position of this event
|
|
|
|
angle_t getAngle() const {
|
|
|
|
return enginePhase;
|
|
|
|
}
|
2022-11-05 22:11:33 -07:00
|
|
|
|
2023-11-01 07:09:04 -07:00
|
|
|
void setAngle(angle_t p_enginePhase) {
|
|
|
|
enginePhase = p_enginePhase;
|
2023-10-05 19:42:00 -07:00
|
|
|
}
|
2022-09-20 02:28:23 -07:00
|
|
|
|
2022-11-28 05:55:38 -08:00
|
|
|
bool shouldSchedule(float currentPhase, float nextPhase) const;
|
2022-12-10 14:07:02 -08:00
|
|
|
float getAngleFromNow(float currentPhase) const;
|
2024-02-28 18:44:02 -08:00
|
|
|
private:
|
|
|
|
angle_t enginePhase;
|
2019-11-23 16:47:53 -08:00
|
|
|
};
|
|
|
|
|
2024-04-23 12:55:12 -07:00
|
|
|
// this is related to wasted spark idea where engines fire each spark twice per 4 stroke 720 degree cycle of operations
|
|
|
|
// first spark is happens on intake stroke and actually ignites fuel mixture, that's the useful one
|
|
|
|
// the other spark 360 degrees later happens during exhaust stroke meaning there is nothing to ignite, that spark is known as "wasted" spark
|
|
|
|
// historically this was about sharing ignition coils between opposite cylinders and having two high voltage wire coming from one physical coil
|
|
|
|
// more recently same idea happens with two individual physical coils (meaning two outputs) since wasted spark of operation is useful
|
|
|
|
// while exact engine phase is either not known YET (cranking) or just not known (broken cam sensor)
|
|
|
|
// so, while in wasted spark we manage half of cylinder count _events_ potentially with each event having two outputs
|
|
|
|
//
|
|
|
|
// an interesting corner case is when we transition from wasted spark mode into individual/sequential mode
|
2016-11-26 21:01:22 -08:00
|
|
|
#define MAX_OUTPUTS_FOR_IGNITION 2
|
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
class IgnitionEvent {
|
2015-07-10 06:01:56 -07:00
|
|
|
public:
|
|
|
|
IgnitionEvent();
|
2024-04-23 12:55:12 -07:00
|
|
|
// IgnitionEvent to IgnitionOutputPin is either 1 to 1 or 1 to 2 relationship, see large comment at 'MAX_OUTPUTS_FOR_IGNITION'
|
2016-11-27 18:04:45 -08:00
|
|
|
IgnitionOutputPin *outputs[MAX_OUTPUTS_FOR_IGNITION];
|
2019-10-07 23:01:41 -07:00
|
|
|
scheduling_s dwellStartTimer;
|
2022-12-10 14:07:02 -08:00
|
|
|
AngleBasedEvent sparkEvent;
|
2020-03-25 22:49:36 -07:00
|
|
|
|
2021-07-09 05:37:46 -07:00
|
|
|
scheduling_s trailingSparkCharge;
|
|
|
|
scheduling_s trailingSparkFire;
|
|
|
|
|
2020-03-25 22:49:36 -07:00
|
|
|
// How many additional sparks should we fire after the first one?
|
|
|
|
// For single sparks, this should be zero.
|
|
|
|
uint8_t sparksRemaining = 0;
|
|
|
|
|
2023-01-10 16:07:44 -08:00
|
|
|
// Track whether coil charge was intentionally skipped (spark limiter)
|
|
|
|
bool wasSparkLimited = false;
|
|
|
|
|
2019-10-07 23:01:41 -07:00
|
|
|
/**
|
|
|
|
* Desired timing advance
|
|
|
|
*/
|
2019-11-23 20:49:39 -08:00
|
|
|
angle_t sparkAngle = NAN;
|
2020-07-29 02:22:54 -07:00
|
|
|
floatms_t sparkDwell = 0;
|
2024-04-25 15:06:11 -07:00
|
|
|
|
|
|
|
// this timer allows us to measure actual dwell time
|
|
|
|
Timer actualDwellTimer;
|
2022-09-25 08:00:55 -07:00
|
|
|
|
|
|
|
float dwellAngle = 0;
|
|
|
|
|
2016-11-28 11:01:52 -08:00
|
|
|
/**
|
2019-10-07 23:01:41 -07:00
|
|
|
* Sequential number of currently processed spark event
|
2024-02-28 21:29:48 -08:00
|
|
|
* @see engineState.globalSparkCounter
|
2016-11-28 11:01:52 -08:00
|
|
|
*/
|
2024-02-28 21:34:26 -08:00
|
|
|
int sparkCounter = 0;
|
2017-03-11 19:22:10 -08:00
|
|
|
/**
|
2023-03-27 00:58:18 -07:00
|
|
|
* [0, cylindersCount)
|
2017-03-11 19:22:10 -08:00
|
|
|
*/
|
2019-10-07 21:27:49 -07:00
|
|
|
int cylinderIndex = 0;
|
2024-04-12 13:48:55 -07:00
|
|
|
// previously known as cylinderNumber
|
2024-02-28 21:41:12 -08:00
|
|
|
int8_t coilIndex = 0;
|
2019-10-07 22:26:35 -07:00
|
|
|
char *name = nullptr;
|
2016-11-27 18:04:45 -08:00
|
|
|
IgnitionOutputPin *getOutputForLoggins();
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2016-11-28 10:02:24 -08:00
|
|
|
class IgnitionEventList {
|
2016-11-28 09:03:02 -08:00
|
|
|
public:
|
2019-10-07 23:13:52 -07:00
|
|
|
/**
|
|
|
|
* ignition events, per cylinder
|
|
|
|
*/
|
2021-07-06 17:14:08 -07:00
|
|
|
IgnitionEvent elements[MAX_CYLINDER_COUNT];
|
2019-10-07 23:01:41 -07:00
|
|
|
bool isReady = false;
|
2016-11-28 09:03:02 -08:00
|
|
|
};
|
2019-12-02 21:29:12 -08:00
|
|
|
|
2021-11-16 13:52:11 -08:00
|
|
|
class AuxActor {
|
2019-12-02 21:29:12 -08:00
|
|
|
public:
|
|
|
|
int phaseIndex;
|
|
|
|
int valveIndex;
|
|
|
|
angle_t extra;
|
|
|
|
|
2022-12-10 14:07:02 -08:00
|
|
|
AngleBasedEvent open;
|
|
|
|
AngleBasedEvent close;
|
2019-12-02 21:29:12 -08:00
|
|
|
};
|
2022-09-15 20:41:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
IgnitionEventList *getIgnitionEvents();
|