speeduino/utils.h

71 lines
2.8 KiB
C
Raw Normal View History

2013-02-04 03:43:38 -08:00
/*
These are some utility functions and variables used through the main code
2013-02-13 03:49:36 -08:00
*/
2013-07-09 17:26:16 -07:00
#include <Arduino.h>
2013-02-04 13:05:35 -08:00
#define MS_IN_MINUTE 60000
#define US_IN_MINUTE 60000000
/*
Returns how much free dynamic memory exists (between heap and stack)
*/
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
2013-02-04 13:05:35 -08:00
2013-02-04 17:39:20 -08:00
/* The following functions help determine the required fuel constant. For more information about these calculations, please refer to http://www.megamanual.com/v22manual/mfuel.htm
Calc below are for metric inputs of temp (degrees C) and MAP (kPa) to produce kg/m3.
*/
2013-02-04 03:43:38 -08:00
int AIRDEN(int MAP, int temp)
{
2013-02-04 19:05:03 -08:00
return (1.2929 * 273.13/(temp+273.13) * MAP/101.325);
2013-02-04 03:43:38 -08:00
}
2013-02-04 19:05:03 -08:00
2013-02-04 03:43:38 -08:00
/*
This function retuns a pulsewidth time (in us) using a either Alpha-N or Speed Density algorithms, given the following:
2013-02-04 03:43:38 -08:00
REQ_FUEL
VE: Lookup from the main MAP vs RPM fuel table
MAP: In KPa, read from the sensor
GammaE: Sum of Enrichment factors (Cold start, acceleration). This is a multiplication factor (Eg to add 10%, this should be 110)
2013-12-22 04:44:25 -08:00
injDT: Injector dead time. The time the injector take to open minus the time it takes to close (Both in uS)
TPS: Throttle position (0% to 100%)
This function is called by PW_SD and PW_AN for speed0density and pure Alpha-N calculations respectively.
2013-02-04 03:43:38 -08:00
*/
2014-09-21 12:52:32 -07:00
unsigned int PW(int REQ_FUEL, byte VE, byte MAP, int corrections, int injOpen, byte TPS)
2013-02-04 03:43:38 -08:00
{
//Standard float version of the calculation
2014-09-20 15:46:25 -07:00
//return (REQ_FUEL * (float)(VE/100.0) * (float)(MAP/100.0) * (float)(TPS/100.0) * (float)(corrections/100.0) + injOpen);
//100% float free version, does sacrifice a little bit of accuracy. Accuracy loss is in the order of 0.1ms (100uS)
2013-07-09 17:26:16 -07:00
int iVE = ((int)VE << 7) / 100;
int iMAP = ((int)MAP << 7) / 100;
2014-01-29 17:17:50 -08:00
int iCorrections = (corrections << 7) / 100;
2013-12-22 04:44:25 -08:00
int iTPS = ((int)TPS << 7) / 100;
2013-07-09 17:26:16 -07:00
unsigned long intermediate = ((long)REQ_FUEL * (long)iVE) >>7; //Need to use an intermediate value to avoid overflowing the long
intermediate = (intermediate * iMAP) >> 7;
2014-01-29 17:17:50 -08:00
intermediate = (intermediate * iCorrections) >> 7;
2013-12-22 04:44:25 -08:00
intermediate = (intermediate * iTPS) >> 7;
2014-09-21 12:52:32 -07:00
unsigned int final = (unsigned int)(intermediate + injOpen);
//if (final > 65535) { final = 65535; } //Make sure this won't overflow. This means the maximum pulsewidth possible is 65.535mS
return final;
2013-07-09 17:26:16 -07:00
2013-02-04 03:43:38 -08:00
}
2013-12-22 04:44:25 -08:00
//Convenience functions for Speed Density and Alpha-N
2014-09-21 12:52:32 -07:00
unsigned int PW_SD(int REQ_FUEL, byte VE, byte MAP, int corrections, int injOpen)
2013-12-22 04:44:25 -08:00
{
2014-01-29 17:17:50 -08:00
return PW(REQ_FUEL, VE, MAP, corrections, injOpen, 100); //Just use 1 in place of the TPS
2013-12-22 04:44:25 -08:00
}
2014-09-21 12:52:32 -07:00
unsigned int PW_AN(int REQ_FUEL, byte VE, byte TPS, int corrections, int injOpen)
2013-12-22 04:44:25 -08:00
{
//Sanity check
if(TPS > 100) { TPS = 100; }
2014-01-29 17:17:50 -08:00
return PW(REQ_FUEL, VE, 100, corrections, injOpen, TPS); //Just use 1 in place of the MAP
2013-12-22 04:44:25 -08:00
}