Speeduino
Loading...
Searching...
No Matches
crankMaths.h
Go to the documentation of this file.
1#ifndef CRANKMATHS_H
2#define CRANKMATHS_H
3
4#include "maths.h"
5#include "board_definition.h"
6
15static constexpr uint32_t MICROS_PER_DEG_1_RPM = UDIV_ROUND_CLOSEST(MICROS_PER_MIN, 360UL, uint32_t);
16
22static constexpr uint16_t MAX_RPM = 18000U;
23
29static constexpr uint16_t MIN_RPM = (uint16_t)UDIV_ROUND_UP(MICROS_PER_DEG_1_RPM, (uint32_t)UINT16_MAX/16UL, uint32_t);
30
36static constexpr uint16_t MIN_REVOLUTION_TIME = MICROS_PER_MIN/MAX_RPM;
37
43static constexpr uint32_t MAX_REVOLUTION_TIME = MICROS_PER_MIN/MIN_RPM;
44
51static inline uint16_t RpmFromRevolutionTimeUs(uint32_t revolutionTime) {
52 if (revolutionTime==0U) { return 0U; }
53 return (uint16_t)clamp(fast_div_closest(MICROS_PER_MIN, revolutionTime), (uint32_t)0UL, (uint32_t)MAX_RPM);
54}
55
56extern int16_t CRANK_ANGLE_MAX_IGN;
57extern int16_t CRANK_ANGLE_MAX_INJ;
58
65static inline int16_t ignitionLimits(int16_t angle) {
66 return nudge((int16_t)0, (int16_t)CRANK_ANGLE_MAX_IGN, angle);
67}
68
79
88angle_converter_factors_t calculateAngleConverterFactors(uint32_t revolutionTime) noexcept;
89
97void applyAngleConverterFactors(const angle_converter_factors_t &factors) noexcept;
98
106void setAngleConverterRevolutionTime(uint32_t revolutionTime) noexcept;
107
118uint32_t angleToTime(uint16_t angle) noexcept;
119
126COMPARE_TYPE angleToTimerTicks(uint16_t angle) noexcept;
127
137uint16_t timeToAngle(uint32_t time) noexcept;
138
139#endif
uint16_t COMPARE_TYPE
The timer overflow type.
Definition board_avr2560.h:32
Inclusion of board specific header files and board related definitions.
static constexpr uint32_t MICROS_PER_DEG_1_RPM
At 1 RPM, each degree of angular rotation takes this many microseconds.
Definition crankMaths.h:15
void applyAngleConverterFactors(const angle_converter_factors_t &factors) noexcept
Set the factors used by the degree<-->angle conversions.
Definition crankMaths.cpp:38
int16_t CRANK_ANGLE_MAX_IGN
The number of crank degrees that the system tracks ignition over.
Definition crankMaths.cpp:14
uint16_t timeToAngle(uint32_t time) noexcept
Converts a time interval in microsecods to the equivalent degrees of angular (crank) rotation at curr...
COMPARE_TYPE angleToTimerTicks(uint16_t angle) noexcept
Converts angular degrees to the equivalent timer ticks at current RPM.
static int16_t ignitionLimits(int16_t angle)
Makes one pass at nudging the angle to within [0,CRANK_ANGLE_MAX_IGN].
Definition crankMaths.h:65
angle_converter_factors_t calculateAngleConverterFactors(uint32_t revolutionTime) noexcept
Calculate the angle<-->time conversion factors for a crank revolution time.
Definition crankMaths.cpp:27
int16_t CRANK_ANGLE_MAX_INJ
The number of crank degrees that the system tracks fuel injection over.
Definition crankMaths.cpp:15
static constexpr uint32_t MAX_REVOLUTION_TIME
Maximum time in µS that one crank revolution can take.
Definition crankMaths.h:43
uint32_t angleToTime(uint16_t angle) noexcept
Converts angular degrees to the time interval that amount of rotation will take at current RPM.
static uint16_t RpmFromRevolutionTimeUs(uint32_t revolutionTime)
Converts a crank revolution time to engine speed.
Definition crankMaths.h:51
static constexpr uint16_t MAX_RPM
The maximum rpm that the ECU will attempt to run at.
Definition crankMaths.h:22
void setAngleConverterRevolutionTime(uint32_t revolutionTime) noexcept
Set the revolution time, from which some of the degree<-->angle conversions are derived.
Definition crankMaths.cpp:42
static constexpr uint16_t MIN_REVOLUTION_TIME
Minimum time in µS that one crank revolution can take.
Definition crankMaths.h:36
static constexpr uint16_t MIN_RPM
Absolute minimum RPM that the crank math (& therefore all of Speeduino) can be used with.
Definition crankMaths.h:29
#define UDIV_ROUND_UP(n, d, t)
Rounding up unsigned integer division.
Definition maths.h:127
#define UDIV_ROUND_CLOSEST(n, d, t)
Rounded unsigned integer division.
Definition maths.h:108
static constexpr uint32_t MICROS_PER_MIN
Self-explanatory.
Definition maths.h:23
TESTABLE_STATIC_CONSTEXPR TDividend fast_div_closest(TDividend dividend, TDivisor divisor)
Same as fast_div(), except this will round to nearest integer instead of truncating.
Definition maths.h:390
TESTABLE_STATIC_CONSTEXPR const T & clamp(const T &v, const T &lo, const T &hi)
clamps a given value between the minimum and maximum thresholds.
Definition maths.h:407
static T nudge(T min, T max, T value)
Make **one* pass at correcting the value into the range [ min, max)
Definition maths.h:351
The factors used to convert between crank angle & time. These are derived from the crank revolution t...
Definition crankMaths.h:70
uint32_t microsPerDegree
uS per degree in UQ24.8 fixed point
Definition crankMaths.h:72
uint16_t degreesPerMicro
Degrees per uS in UQ1.15 fixed point.
Definition crankMaths.h:77