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
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
/*
2013-02-05 15:43:17 -08:00
This functino retuns a pulsewidth time ( in us ) 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-07-03 03:27:46 -07:00
injOpen : Injector open time . The time the injector take to open in uS
2013-02-04 03:43:38 -08:00
*/
2013-07-09 17:26:16 -07:00
int PW ( int REQ_FUEL , byte VE , byte MAP , int GammaE , int injOpen )
2013-02-04 03:43:38 -08:00
{
2013-07-03 03:56:33 -07:00
//Standard float version of the calculation
//return (REQ_FUEL * (float)(VE/100.0) * (float)(MAP/100.0) * (float)(GammaE/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 ;
2013-07-03 03:56:33 -07:00
int iGammaE = ( GammaE < < 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 ;
intermediate = ( intermediate * iGammaE ) > > 7 ;
return ( int ) intermediate + injOpen ;
2013-02-04 03:43:38 -08:00
}
2013-02-04 19:05:03 -08:00
/* Determine the Gamma Enrichment number. Forumla borrowed from MS2 manual... may be skipped/simplified for arduino!
WARMUP : Warmup Correction
O2_CLOSED : Feedback from Closed Loop Operation
AIRTEMP : Air Temp correction < - - Skip ?
BARO : Barometric Correction < - - Skip ?
*/
int GammaE ( int WARMUP , int O2_CLOSED , int AIRTEMP , int BARO )
{
2013-02-05 00:27:06 -08:00
return ( WARMUP / 100 ) * ( O2_CLOSED / 100 ) * ( AIRTEMP / 100 ) * ( BARO / 100 ) ;
2013-02-04 19:05:03 -08:00
}