Speeduino
Loading...
Searching...
No Matches
Classes | Functions | Variables
crankMaths.h File Reference

Crank revolution based mathematical functions. More...

#include "maths.h"
#include "board_definition.h"

Go to the source code of this file.

Classes

struct  angle_converter_factors_t
 The factors used to convert between crank angle & time. These are derived from the crank revolution time. More...
 

Functions

static uint16_t RpmFromRevolutionTimeUs (uint32_t revolutionTime)
 Converts a crank revolution time to engine speed.
 
static int16_t ignitionLimits (int16_t angle)
 Makes one pass at nudging the angle to within [0,CRANK_ANGLE_MAX_IGN].
 
angle_converter_factors_t calculateAngleConverterFactors (uint32_t revolutionTime) noexcept
 Calculate the angle<-->time conversion factors for a crank revolution time.
 
void applyAngleConverterFactors (const angle_converter_factors_t &factors) noexcept
 Set the factors used by the degree<-->angle conversions.
 
void setAngleConverterRevolutionTime (uint32_t revolutionTime) noexcept
 Set the revolution time, from which some of the degree<-->angle conversions are derived.
 
uint32_t angleToTime (uint16_t angle) noexcept
 Converts angular degrees to the time interval that amount of rotation will take at current RPM.
 
COMPARE_TYPE angleToTimerTicks (uint16_t angle) noexcept
 Converts angular degrees to the equivalent timer ticks at current RPM.
 
uint16_t timeToAngle (uint32_t time) noexcept
 Converts a time interval in microsecods to the equivalent degrees of angular (crank) rotation at current RPM.
 

Variables

static constexpr uint32_t MICROS_PER_DEG_1_RPM = UDIV_ROUND_CLOSEST(MICROS_PER_MIN, 360UL, uint32_t)
 At 1 RPM, each degree of angular rotation takes this many microseconds.
 
static constexpr uint16_t MAX_RPM = 18000U
 The maximum rpm that the ECU will attempt to run at.
 
static constexpr uint16_t MIN_RPM = (uint16_t)UDIV_ROUND_UP(MICROS_PER_DEG_1_RPM, (uint32_t)UINT16_MAX/16UL, uint32_t)
 Absolute minimum RPM that the crank math (& therefore all of Speeduino) can be used with.
 
static constexpr uint16_t MIN_REVOLUTION_TIME = MICROS_PER_MIN/MAX_RPM
 Minimum time in µS that one crank revolution can take.
 
static constexpr uint32_t MAX_REVOLUTION_TIME = MICROS_PER_MIN/MIN_RPM
 Maximum time in µS that one crank revolution can take.
 
int16_t CRANK_ANGLE_MAX_IGN
 The number of crank degrees that the system tracks ignition over.
 
int16_t CRANK_ANGLE_MAX_INJ
 The number of crank degrees that the system tracks fuel injection over.
 

Detailed Description

Crank revolution based mathematical functions.

Function Documentation

◆ angleToTime()

uint32_t angleToTime ( uint16_t  angle)
noexcept

Converts angular degrees to the time interval that amount of rotation will take at current RPM.

Based on angle of [0,720] and min/max RPM, result ranges from 9 (MAX_RPM, 1 deg) to 2926828 (MIN_RPM, 720 deg)

Parameters
angleAngle in degrees
Returns
Time interval in uS

◆ angleToTimerTicks()

COMPARE_TYPE angleToTimerTicks ( uint16_t  angle)
noexcept

Converts angular degrees to the equivalent timer ticks at current RPM.

Parameters
angleAngle in degrees
Returns
Number of timer ticks

◆ applyAngleConverterFactors()

void applyAngleConverterFactors ( const angle_converter_factors_t &  factors)
noexcept

Set the factors used by the degree<-->angle conversions.

Note
If an ISR could be converting angles, call this from within an ATOMIC() block.
Parameters
factorsThe factors to use. See calculateAngleConverterFactors()

◆ calculateAngleConverterFactors()

angle_converter_factors_t calculateAngleConverterFactors ( uint32_t  revolutionTime)
noexcept

Calculate the angle<-->time conversion factors for a crank revolution time.

This does not change the factors in use (see applyAngleConverterFactors()), so the (relatively slow) divisions can be done outside of a critical section.

Parameters
revolutionTimeThe crank revolution time in uS. Zero results in zero factors.

◆ ignitionLimits()

static int16_t ignitionLimits ( int16_t  angle)
inlinestatic

Makes one pass at nudging the angle to within [0,CRANK_ANGLE_MAX_IGN].

Parameters
angleA crank angle in degrees
Returns
int16_t

◆ RpmFromRevolutionTimeUs()

static uint16_t RpmFromRevolutionTimeUs ( uint32_t  revolutionTime)
inlinestatic

Converts a crank revolution time to engine speed.

Parameters
revolutionTimeThe time in µS that one crank revolution takes. Zero if the engine speed is unknown
Returns
The engine speed in RPM, rounded to the closest integer & limited to MAX_RPM. Zero if revolutionTime is zero

◆ setAngleConverterRevolutionTime()

void setAngleConverterRevolutionTime ( uint32_t  revolutionTime)
noexcept

Set the revolution time, from which some of the degree<-->angle conversions are derived.

Shorthand for applyAngleConverterFactors(calculateAngleConverterFactors(revolutionTime))

Parameters
revolutionTimeThe crank revolution time.

◆ timeToAngle()

uint16_t timeToAngle ( uint32_t  time)
noexcept

Converts a time interval in microsecods to the equivalent degrees of angular (crank) rotation at current RPM.

Inverse of angleToTime

Parameters
timeTime interval in uS
Returns
Angle in degrees

Variable Documentation

◆ CRANK_ANGLE_MAX_IGN

int16_t CRANK_ANGLE_MAX_IGN
extern

The number of crank degrees that the system tracks ignition over.

◆ CRANK_ANGLE_MAX_INJ

int16_t CRANK_ANGLE_MAX_INJ
extern

The number of crank degrees that the system tracks fuel injection over.

◆ MAX_REVOLUTION_TIME

constexpr uint32_t MAX_REVOLUTION_TIME = MICROS_PER_MIN/MIN_RPM
staticconstexpr

Maximum time in µS that one crank revolution can take.

Note
: many calculations are done over 2 revolutions (cycles), in which case this would be doubled

◆ MAX_RPM

constexpr uint16_t MAX_RPM = 18000U
staticconstexpr

The maximum rpm that the ECU will attempt to run at.

It is NOT related to the rev limiter, but is instead dictates how fast certain operations will be allowed to run. Lower number gives better performance

◆ MICROS_PER_DEG_1_RPM

constexpr uint32_t MICROS_PER_DEG_1_RPM = UDIV_ROUND_CLOSEST(MICROS_PER_MIN, 360UL, uint32_t)
staticconstexpr

At 1 RPM, each degree of angular rotation takes this many microseconds.

◆ MIN_REVOLUTION_TIME

constexpr uint16_t MIN_REVOLUTION_TIME = MICROS_PER_MIN/MAX_RPM
staticconstexpr

Minimum time in µS that one crank revolution can take.

Note
: many calculations are done over 2 revolutions (cycles), in which case this would be doubled

◆ MIN_RPM

constexpr uint16_t MIN_RPM = (uint16_t)UDIV_ROUND_UP(MICROS_PER_DEG_1_RPM, (uint32_t)UINT16_MAX/16UL, uint32_t)
staticconstexpr

Absolute minimum RPM that the crank math (& therefore all of Speeduino) can be used with.

This is dictated by the use of uint16_t as the base type for storing time --> angle conversion factor (degreesPerMicro)