Speeduino
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1
41#ifndef SCHEDULER_H
42#define SCHEDULER_H
43
44#include "board_definition.h"
45#include "crankMaths.h"
46#include "preprocessor.h"
47
52 // We use powers of 2 so we can check multiple states with a single bitwise AND
53
55 OFF = 0b00000000U,
57 PENDING = 0b00000001U,
59 RUNNING = 0b00000010U,
61 RUNNING_WITHNEXT = 0b00000100U,
62};
63
65void nullCallback(void);
66
87struct Schedule {
88 // Deduce the real types of the counter and compare registers.
89 // COMPARE_TYPE is NOT the same - it's just an integer type wide enough to
90 // store 16-bit counter/compare calculation results.
92 using counter_t = decltype(FUEL1_COUNTER /* <-- Arbitrary choice of macro, assumes all have the same type */);
94 using compare_t = decltype(FUEL1_COMPARE /* <-- Arbitrary choice of macro, assumes all have the same type */);
95
102 constexpr Schedule(counter_t &counter, compare_t &compare)
103 : _counter(counter)
104 , _compare(compare)
105 {
106 }
107
108 using callback = void(*)(void);
109
117 volatile COMPARE_TYPE _duration = 0U;
122
125
126protected:
127 virtual void reset(void);
128};
129
134static inline bool isRunning(const Schedule &schedule) noexcept {
135 // Using flags and bitwise AND (&) to check multiple states is much quicker
136 // than a logical or (||) (one less branch & 30% less instructions)
137 static constexpr uint8_t flags = RUNNING | RUNNING_WITHNEXT;
138 return ((uint8_t)schedule._status & flags)!=0U;
139}
140
149void setCallbacks(Schedule &schedule, Schedule::callback pStartCallback, Schedule::callback pEndCallback) noexcept;
150
159void setSchedule(Schedule &schedule, uint32_t delay, uint16_t duration, bool allowQueuedSchedule);
160
178struct IgnitionSchedule : public Schedule {
179
180 using Schedule::Schedule;
181
182 volatile uint32_t _startTime = 0U;
183 int16_t chargeAngle = 0U;
184 int16_t dischargeAngle = 0U;
185 uint16_t channelDegrees = 0U;
186
187 void reset(void) override;
188};
189
196void moveToNextState(IgnitionSchedule &schedule) noexcept;
197
209void adjustCrankAngle(const statuses &current, IgnitionSchedule &schedule, int16_t crankAngle);
210
221struct FuelSchedule : public Schedule {
222
223 using Schedule::Schedule;
224
225 uint16_t channelDegrees = 0U;
226 uint16_t pw = 0U;
227
228 void reset(void) override;
229};
230
237void moveToNextState(FuelSchedule &schedule) noexcept;
238
240static FORCE_INLINE uint32_t _calculateAngularTime(const Schedule &schedule, uint16_t eventAngle, uint16_t crankAngle, uint16_t maxAngle) {
241 int16_t delta = eventAngle - crankAngle;
242 if ( (isRunning(schedule)) || (schedule._status == OFF)) {
243 while(delta < 0) { delta += (int16_t)maxAngle; }
244 }
245
246 return delta > 0 ? angleToTime((uint16_t)delta) : 0U;
247}
248
249static FORCE_INLINE uint16_t _adjustToTDC(int16_t angle, uint16_t angleOffset, uint16_t maxAngle) {
250 angle = angle - (int16_t)angleOffset;
251 if( angle < 0) { return angle + (int16_t)maxAngle; }
252 return angle;
253}
254
255static FORCE_INLINE uint32_t _calculateAngularTime(const Schedule &schedule, uint16_t angleOffset, uint16_t eventAngle, uint16_t crankAngle, uint16_t maxAngle) {
256 if (angleOffset==0U) { // Optimize for zero channel angle - no need to adjust start & crank angles
257 return _calculateAngularTime(schedule, eventAngle, crankAngle, maxAngle);
258 }
259 // Realign the current crank angle and the desired start angle around 0 degrees for the given cylinder/output
260 // Eg: If cylinder 2 TDC is 180 degrees after cylinder 1 (E.g. a standard 4 cylinder engine), then
261 // adjusted crank angle is 180* less than the current crank angle and adjusted start angle is the desired open angle less 180*.
262 // Thus the cylinder is being treated relative to its own TDC, regardless of its offset
263 //
264 // This is done to avoid very small or very large deltas between crank angle and start angle.
265 return _calculateAngularTime(schedule,
266 _adjustToTDC(eventAngle, angleOffset, maxAngle),
267 _adjustToTDC(crankAngle, angleOffset, maxAngle),
268 maxAngle);
269}
270
272
273#endif // SCHEDULER_H
#define FUEL1_COUNTER
Definition board_avr2560.h:69
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:32
#define FUEL1_COMPARE
Definition board_avr2560.h:87
Inclusion of board specific header files and board related definitions.
Crank revolution based mathematical functions.
uint32_t angleToTime(uint16_t angle) noexcept
Converts angular degrees to the time interval that amount of rotation will take at current RPM.
return flags
Definition engineProtection.cpp:170
void moveToNextState(IgnitionSchedule &schedule) noexcept
Shared ignition schedule timer ISR implementation. Should be called by the actual ignition timer ISRs...
Definition scheduler.cpp:181
#define FORCE_INLINE
Definition preprocessor.h:82
void adjustCrankAngle(const statuses &current, IgnitionSchedule &schedule, int16_t crankAngle)
Adjust the crank angle used to originally set the schedule.
Definition scheduler.cpp:188
void nullCallback(void)
A scheduler callback that does nothing.
Definition scheduler.cpp:35
void setCallbacks(Schedule &schedule, Schedule::callback pStartCallback, Schedule::callback pEndCallback) noexcept
Set the schedule callbacks. I.e the functions called when the action needs to start & stop.
Definition scheduler.cpp:57
ScheduleStatus
The current state of a schedule.
Definition scheduler.h:51
@ RUNNING
Definition scheduler.h:59
@ PENDING
Definition scheduler.h:57
@ RUNNING_WITHNEXT
Definition scheduler.h:61
@ OFF
Definition scheduler.h:55
void setSchedule(Schedule &schedule, uint32_t delay, uint16_t duration, bool allowQueuedSchedule)
Set the schedule action to run for a certain duration in the future.
Definition scheduler.cpp:93
static bool isRunning(const Schedule &schedule) noexcept
Is the schedule running? I.e. the action has started, but not finished. E.g. injector is open.
Definition scheduler.h:134
const config2 const config4 const config6 statuses & current
Definition scheduler_fuel_controller.cpp:500
uint16_t crankAngle
Definition scheduler_ignition_controller.cpp:695
A fuel injection schedule.
Definition scheduler.h:221
void reset(void) override
Definition scheduler.cpp:51
uint16_t pw
Pulse width in uS.
Definition scheduler.h:226
uint16_t channelDegrees
The number of crank degrees until cylinder is at TDC
Definition scheduler.h:225
An ignition schedule.
Definition scheduler.h:178
void reset(void) override
Definition scheduler.cpp:43
uint16_t channelDegrees
The number of crank degrees until cylinder is at TDC
Definition scheduler.h:185
int16_t dischargeAngle
Angle the coil should discharge at. I.e. spark.
Definition scheduler.h:184
volatile uint32_t _startTime
The system time (in uS) that the schedule started, used by the overdwell protection in timers....
Definition scheduler.h:182
int16_t chargeAngle
Angle the coil should begin charging.
Definition scheduler.h:183
A schedule for a single output channel.
Definition scheduler.h:87
COMPARE_TYPE _nextStartCompare
Planned start of next schedule (when current schedule is RUNNING)
Definition scheduler.h:121
counter_t & _counter
Reference to the counter register. E.g. TCNT3
Definition scheduler.h:123
callback _pEndCallback
End Callback function for schedule.
Definition scheduler.h:120
void(*)(void) callback
Definition scheduler.h:108
constexpr Schedule(counter_t &counter, compare_t &compare)
Construct a new Schedule object.
Definition scheduler.h:102
decltype(FUEL1_COUNTER) counter_t
The type of a timer counter register (this varies between platforms)
Definition scheduler.h:92
volatile ScheduleStatus _status
Schedule status: OFF, PENDING, STAGED, RUNNING.
Definition scheduler.h:118
compare_t & _compare
**Reference**to the compare register. E.g. OCR3A
Definition scheduler.h:124
volatile COMPARE_TYPE _duration
Scheduled duration (timer ticks)
Definition scheduler.h:117
callback _pStartCallback
Start Callback function for schedule.
Definition scheduler.h:119
virtual void reset(void)
Definition scheduler.cpp:37
decltype(FUEL1_COMPARE) compare_t
The type of a timer compare register (this varies between platforms)
Definition scheduler.h:94
The status struct with current values for all 'live' variables.
Definition statuses.h:69