Speeduino
Loading...
Searching...
No Matches
schedule_calcs.hpp
Go to the documentation of this file.
1// Note that all functions with an underscore prefix are NOT part
2// of the public API. They are only here so we can inline them.
3
4#include "scheduler.h"
5#include "crankMaths.h"
6#include "maths.h"
7#include "timers.h"
8
10{
11 // 0<=injAngle<=720°
12 // 0<=injChannelDegrees<=720°
13 // 0<pwDegrees<=??? (could be many crank rotations in the worst case!)
14 // 45<=CRANK_ANGLE_MAX_INJ<=720
15 // (CRANK_ANGLE_MAX_INJ can be as small as 360/nCylinders. E.g. 45° for 8 cylinder)
16
18
19 while (startAngle<pwDegrees) { startAngle = startAngle + (uint16_t)CRANK_ANGLE_MAX_INJ; } // Avoid underflow
20 startAngle = startAngle - pwDegrees; // startAngle guaranteed to be >=0.
21 while (startAngle>(uint16_t)CRANK_ANGLE_MAX_INJ) { startAngle = startAngle - (uint16_t)CRANK_ANGLE_MAX_INJ; } // Clamp to 0<=startAngle<=CRANK_ANGLE_MAX_INJ
22
23 return startAngle;
24}
25
34
40
42 if (angleOffset==0U) { // Optimize for zero channel angle - no need to adjust start & crank angles
44 }
45 // Realign the current crank angle and the desired start angle around 0 degrees for the given cylinder/output
46 // Eg: If cylinder 2 TDC is 180 degrees after cylinder 1 (E.g. a standard 4 cylinder engine), then
47 // adjusted crank angle is 180* less than the current crank angle and adjusted start angle is the desired open angle less 180*.
48 // Thus the cylinder is being treated relative to its own TDC, regardless of its offset
49 //
50 // This is done to avoid very small or very large deltas between crank angle and start angle.
54 maxAngle);
55}
56
58{
60
61 if (delta<0)
62 {
63 if (schedule.Status != PENDING)
64 {
65 while(delta < 0) { delta += CRANK_ANGLE_MAX_INJ; }
66 }
67 else
68 {
69 delta = 0;
70 }
71 }
73}
74
76 int16_t angle = (schedule.channelDegrees==0 ? CRANK_ANGLE_MAX_IGN : schedule.channelDegrees) - advance;
78 return angle;
79}
80
82 if (dischargeAngle>(int16_t)dwellAngle) {
83 return dischargeAngle - (int16_t)dwellAngle;
84 }
85 return dischargeAngle + CRANK_ANGLE_MAX_IGN - (int16_t)dwellAngle;
86}
87
93
94
100
105
106// The concept here is that we have a more accurate crank angle.
107// Ignition timing is driven by target spark angle relative to crank position.
108// So the timing to begin & end charging the coil is based on crank angle.
109// With a more accurate crank angle, we can increase the precision of the
110// spark timing.
112 constexpr uint8_t MIN_CYCLES_FOR_CORRECTION = 6U;
113
115 ATOMIC() { // Prevent race conditions with the timer interrupt.
116 // We only want to adjust the crank angle if we are running and the coil is charging or we are waiting for the timer to fire.
117 if( isRunning(schedule) ) {
118 if (schedule.dischargeAngle>crankAngle) {
119 // Coil is charging so change the charge time so the spark fires at
120 // the requested crank angle (this could reduce dwell time & potentially
121 // result in a weaker spark).
122 SET_COMPARE(schedule._compare, schedule._counter + angleToTimerTicks( schedule.dischargeAngle-crankAngle ));
123 }
124 }
125 else if( (schedule.Status==PENDING) ) {
126 if ((currentStatus.startRevolutions > MIN_CYCLES_FOR_CORRECTION) && (schedule.chargeAngle>crankAngle)) {
127 // We are waiting for the timer to fire & start charging the coil.
128 // Keep dwell (I.e. duration) constant (for better spark) - instead adjust the waiting period so
129 // the spark fires at the requested crank angle.
130 SET_COMPARE(schedule._compare, schedule._counter + angleToTimerTicks( schedule.chargeAngle-crankAngle ));
131 }
132 } else {
133 // Unknown state, so no adjustment possible
134 }
135 }
136}
static integerPID_ideal boostPID & currentStatus
Definition auxiliaries.cpp:187
#define SET_COMPARE(compare, value)
Definition board_definition.h:79
Crank revolution based mathematical functions.
static int16_t ignitionLimits(int16_t angle)
Makes one pass at nudging the angle to within [0,CRANK_ANGLE_MAX_IGN].
Definition crankMaths.h:51
uint32_t angleToTimeMicroSecPerDegree(uint16_t angle)
Converts angular degrees to the time interval that amount of rotation will take at current RPM.
COMPARE_TYPE angleToTimerTicks(uint16_t angle)
Converts angular degrees to the equivalent timer ticks at current RPM.
int16_t CRANK_ANGLE_MAX_IGN
Definition globals.cpp:40
int16_t CRANK_ANGLE_MAX_INJ
The number of crank degrees that the system track over. Typically 720 divided by the number of squirt...
Definition globals.cpp:41
static TIntegral readSerialIntegralTimeout(void)
Reads an integral type, timing out if necessary.
Definition comms.cpp:175
page_iterator_t advance(const page_iterator_t &iter)
Definition pages.cpp:508
static uint32_t _calculateIgnitionTimeout(const IgnitionSchedule &schedule, int16_t crankAngle)
Definition schedule_calcs.hpp:101
static uint32_t calculateInjectorTimeout(const FuelSchedule &schedule, int16_t openAngle, int16_t crankAngle)
Definition schedule_calcs.hpp:57
static int16_t _calculateSparkAngle(const IgnitionSchedule &schedule, int8_t advance)
Definition schedule_calcs.hpp:75
static int16_t _calculateCoilChargeAngle(uint16_t dwellAngle, int16_t dischargeAngle)
Definition schedule_calcs.hpp:81
static uint32_t _calculateAngularTime(const Schedule &schedule, uint16_t eventAngle, uint16_t crankAngle, uint16_t maxAngle)
Definition schedule_calcs.hpp:26
static void calculateIgnitionAngles(IgnitionSchedule &schedule, uint16_t dwellAngle, int8_t advance)
Definition schedule_calcs.hpp:88
static uint16_t calculateInjectorStartAngle(uint16_t pwDegrees, int16_t injChannelDegrees, uint16_t injAngle)
Definition schedule_calcs.hpp:9
static uint16_t _adjustToTDC(int16_t angle, uint16_t angleOffset, uint16_t maxAngle)
Definition schedule_calcs.hpp:35
static void adjustCrankAngle(IgnitionSchedule &schedule, int16_t crankAngle)
Definition schedule_calcs.hpp:111
static void calculateIgnitionTrailingRotary(IgnitionSchedule &leading, uint16_t dwellAngle, int16_t rotarySplitDegrees, IgnitionSchedule &trailing)
Definition schedule_calcs.hpp:95
static bool isRunning(const Schedule &schedule)
Is the schedule running? I.e. the action has started, but not finished. E.g. injector is open.
Definition scheduler.h:145
@ PENDING
Definition scheduler.h:68
@ OFF
Definition scheduler.h:66
uint16_t crankAngle
Definition scheduler_ignition_controller.cpp:399
uint16_t dwellAngle
Definition scheduler_ignition_controller.cpp:370
Definition scheduler.h:213
An ignition schedule.
Definition scheduler.h:189
A schedule for a single output channel.
Definition scheduler.h:98