2018-07-19 06:26:31 -07:00
|
|
|
#ifndef CRANKMATHS_H
|
|
|
|
#define CRANKMATHS_H
|
|
|
|
|
2023-06-25 19:13:53 -07:00
|
|
|
#include "maths.h"
|
2023-10-06 00:10:20 -07:00
|
|
|
#include "globals.h"
|
2023-06-25 19:13:53 -07:00
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/**
|
|
|
|
* @brief Makes one pass at nudging the angle to within [0,CRANK_ANGLE_MAX_IGN]
|
|
|
|
*
|
|
|
|
* @param angle A crank angle in degrees
|
|
|
|
* @return int16_t
|
|
|
|
*/
|
2024-01-21 22:20:06 -08:00
|
|
|
static inline int16_t ignitionLimits(int16_t angle)
|
|
|
|
{
|
2023-11-05 14:10:08 -08:00
|
|
|
return nudge(0, CRANK_ANGLE_MAX_IGN, angle, CRANK_ANGLE_MAX_IGN);
|
|
|
|
}
|
2018-06-29 16:21:13 -07:00
|
|
|
|
2024-01-21 22:20:06 -08:00
|
|
|
/**
|
|
|
|
* @brief Makes one pass at nudging the angle to within [0,CRANK_ANGLE_MAX_INJ]
|
|
|
|
*
|
|
|
|
* @param angle A crank angle in degrees
|
|
|
|
* @return int16_t
|
|
|
|
*/
|
|
|
|
static inline int16_t injectorLimits(int16_t angle)
|
|
|
|
{
|
|
|
|
int16_t tempAngle = angle;
|
|
|
|
if(tempAngle < 0) { tempAngle = tempAngle + CRANK_ANGLE_MAX_INJ; }
|
|
|
|
while(tempAngle > CRANK_ANGLE_MAX_INJ ) { tempAngle -= CRANK_ANGLE_MAX_INJ; }
|
|
|
|
return tempAngle;
|
|
|
|
}
|
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/** @brief At 1 RPM, each degree of angular rotation takes this many microseconds */
|
|
|
|
#define MICROS_PER_DEG_1_RPM INT32_C(166667)
|
2022-11-21 20:53:35 -08:00
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/** @brief 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
|
|
|
|
**/
|
|
|
|
#define MAX_RPM INT16_C(18000)
|
2018-08-07 22:17:09 -07:00
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/** @brief 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
|
|
|
|
* angle<->time conversion factor (degreesPerMicro)
|
|
|
|
*/
|
|
|
|
#define MIN_RPM ((MICROS_PER_DEG_1_RPM/(UINT16_MAX/16UL))+1UL)
|
2018-09-03 22:13:14 -07:00
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/**
|
|
|
|
* @name 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)
|
|
|
|
*
|
|
|
|
* @param angle Angle in degrees
|
|
|
|
* @return Time interval in uS
|
|
|
|
*/
|
|
|
|
///@{
|
|
|
|
/** @brief Converts based on the time one degree of rotation takes
|
|
|
|
*
|
|
|
|
* Inverse of timeToAngleDegPerMicroSec
|
|
|
|
*/
|
|
|
|
uint32_t angleToTimeMicroSecPerDegree(uint16_t angle);
|
2018-07-12 03:45:27 -07:00
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/** @brief Converts based on the time interval between the 2 most recently detected decoder teeth
|
|
|
|
*
|
|
|
|
* Inverse of timeToAngleIntervalTooth
|
|
|
|
*/
|
|
|
|
uint32_t angleToTimeIntervalTooth(uint16_t angle);
|
|
|
|
///@}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Converts a time interval in microsecods to the equivalent degrees of angular (crank)
|
|
|
|
* rotation at current RPM.
|
|
|
|
*
|
|
|
|
* @param time Time interval in uS
|
|
|
|
* @return Angle in degrees
|
|
|
|
*/
|
|
|
|
///@{
|
|
|
|
/** @brief Converts based on the the interval on time one degree of rotation takes
|
|
|
|
*
|
|
|
|
* Inverse of angleToTimeMicroSecPerDegree
|
|
|
|
*/
|
|
|
|
uint16_t timeToAngleDegPerMicroSec(uint32_t time);
|
2018-07-12 03:45:27 -07:00
|
|
|
|
2023-11-05 14:10:08 -08:00
|
|
|
/** @brief Converts based on the time interval between the 2 most recently detected decoder teeth
|
|
|
|
*
|
|
|
|
* Inverse of angleToTimeIntervalTooth
|
|
|
|
*/
|
|
|
|
uint16_t timeToAngleIntervalTooth(uint32_t time);
|
|
|
|
///@}
|
2018-08-31 00:35:29 -07:00
|
|
|
|
2018-07-19 06:26:31 -07:00
|
|
|
#endif
|