FreeEMS  0.2.0-SNAPSHOT-282-g9efc524
scalerDefines.h
Go to the documentation of this file.
1 /* FreeEMS - the open source engine management system
2  *
3  * Copyright 2012-2013 Fred Cooke
4  *
5  * This file is part of the FreeEMS project.
6  *
7  * FreeEMS software is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FreeEMS software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
19  *
20  * We ask that if you make any changes to this file you email them upstream to
21  * us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
22  *
23  * Thank you for choosing FreeEMS to run your engine!
24  */
25 
26 
27 /** @file
28  *
29  * @ingroup allHeaders
30  * @ingroup globalHeaders
31  *
32  * @brief Global scaling values and macros
33  *
34  * A single source of internally scaled fixed point numbers.
35  */
36 
37 
38 /* Header file multiple inclusion protection courtesy eclipse Header Template */
39 /* and http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/ C pre processor manual */
40 #ifndef FILE_SCALERDEFINES_H_SEEN
41 #define FILE_SCALERDEFINES_H_SEEN
42 
43 
44 /// Ensures that configuration values are properly rounded and not truncated.
45 #define ROUND(FLOATING_POINT_VALUE) ((FLOATING_POINT_VALUE) + 0.5) // Note, if integer math is used to generate the argument, it won't work.
46 
47 
48 // All fixed point configuration values
49 #define IGNITION_TIMING_FACTOR 1024 ///< Temporary ignition timing table scaling factor. Warning, to be changed to 8 bit!
50 #define TEMPERATURE_K_FACTOR 100 ///< Scaling factor for temperature fixed point arithmetic
51 #define PRESSURE_KPA_FACTOR 100 ///< Scaling factor for pressure fixed point arithmetic
52 #define VOLTAGE_FACTOR 1000 ///< Scaling factor for Voltage fixed point arithmetic
53 #define LAMBDA_FACTOR 32768 ///< Scaling factor for EGO and Lambda fixed point arithmetic
54 #define RPM_FACTOR 2 ///< Scaling factor for RPM fixed point arithmetic
55 #define ANGLE_FACTOR 50U ///< Scaling factor for engine angle scheduling fixed point arithmetic. Suffix is necessary otherwise 8 bit is assumed.
56 #define VE_FACTOR 512 ///< Scaling factor for Volumetric Efficiency fixed point arithmetic
57 #define AFR_FACTOR 1024 ///< Scaling factor for Air:Fuel Ratio fixed point arithmetic
58 #define FUEL_DENSITY_FACTOR 32 ///< Scaling factor for fuel density (grams per litre) fixed point arithmetic
59 #define PW_TICK_FACTOR 1250 ///< Scaling factor for raw pulse width fixed point arithmetic;
60 #define PERCENT_FACTOR 640 ///< Scaling factor for percentage fixed point arithmetic
61 #define CYLINDER_VOLUME_LIMIT 2000 ///< Cylinder volume, in cc, can be anything less than this
62 #define INJECTOR_FLOW_LIMIT 3840 ///< Injector flow, in cc/min can be anything less than this
63 #define WARM_UP_LIMIT 400 ///< Warm up enrichment percentage, can be anything less than this
64 
65 // Convenience values for convenience wrappers
66 #define TEMPERATURE_C_TO_K_OFFSET 273.15 ///< Offset for human degrees Celsius configuration items
67 #define FUEL_DENSITY_UNIT_FACTOR 1000 ///< Part of the original speed density mathematics, still in use
68 
69 
70 // All scaler macros!
71 // Warning: Surround your argument with brackets to ensure order of operations!
72 // For table use, choose very short, for config use, choose very clear.
73 // For configuration only, live code will be bloated by cast to long which is required to prevent configuration overflows wrapping to zero.
74 #define KPA(PRESSURE) (signed long)ROUND((PRESSURE) * (double)PRESSURE_KPA_FACTOR)
75 #define DEGREES_K(TEMPERATURE) (unsigned long)ROUND((TEMPERATURE) * (double)TEMPERATURE_K_FACTOR)
76 #define CC_PER_MINUTE(FLOW_RATE) (unsigned long)ROUND(((FLOW_RATE) / (double)INJECTOR_FLOW_LIMIT) * 65536)
77 #define CYLINDER_VOLUME(VOLUME_IN_CC) (unsigned long)ROUND(((VOLUME_IN_CC) / (double)CYLINDER_VOLUME_LIMIT) * 65536)
78 #define VOLTS(VOLTAGE) (unsigned long)ROUND((VOLTAGE) * (double)VOLTAGE_FACTOR)
79 #define LAMBDA(LAMBDA_VALUE) (unsigned long)ROUND((LAMBDA_VALUE) * (double)LAMBDA_FACTOR)
80 #define RPM(ENGINE_SPEED) (unsigned long)ROUND((ENGINE_SPEED) * (double)RPM_FACTOR)
81 #define ANGLE(CRANK_DEGREES) (unsigned long)ROUND((CRANK_DEGREES) * (double)ANGLE_FACTOR)
82 #define STOICH_AFR(AIR_FUEL_RATIO) (unsigned long)ROUND((AIR_FUEL_RATIO) * (double)AFR_FACTOR)
83 #define FUEL_DENSITY(DENSITY) (unsigned long)ROUND((DENSITY) * (double)FUEL_DENSITY_FACTOR)
84 #define PW_MS(PW_MILLISECONDS) (unsigned long)ROUND((PW_MILLISECONDS) * (double)PW_TICK_FACTOR)
85 #define PERCENT(PERCENTAGE) (unsigned long)ROUND((PERCENTAGE) * (double)PERCENT_FACTOR)
86 #define ACCEL_TIME_TOL(PERCENTAGE) (unsigned long)((100/(100 + (double)PERCENTAGE)) * 1000) // This code is getting changed a lot, hence literals
87 #define DECEL_TIME_TOL(PERCENTAGE) (unsigned long)(((100 + (double)PERCENTAGE)/100) * 1000) // This code is getting changed a lot, hence literals
88 
89 // One-off configuration stuff
90 #define SCI_BAUD_DIVISOR(BAUD) (unsigned long)ROUND(40000000 / ((double)(BAUD) * 16)) // 40MHz / (16*115.2kHz) TODO pull 40MHz out of clock rate stuff
91 
92 // For table data
93 #define IT(TIMING_BTDC) (unsigned long)ROUND((TIMING_BTDC) * (double)IGNITION_TIMING_FACTOR)
94 #define VE(VOLUMETRIC_EFFICIENCY) (unsigned long)ROUND((VOLUMETRIC_EFFICIENCY) * (double)VE_FACTOR)
95 #define AF(PERCENTAGE) PERCENT(PERCENTAGE) // Shortcut for table use, same as above
96 #define LR(LAMBDA_RATIO) LAMBDA(LAMBDA_RATIO) // Shortcut for table use, same as above
97 #define AP(AFR) LR((double)AFR / 14.7) // AFR Petrol, alternative unit for table use
98 #define PC(PERCENTAGE) PERCENT(PERCENTAGE) // Shortcut for table use, same as above
99 #define C(TEMPERATURE) DEGREES_C(TEMPERATURE) // Shortcut for table use, same as below
100 #define V(VOLTAGE) VOLTS(VOLTAGE) // Shortcut for table use, same as above
101 #define W(PERCENTAGE) (unsigned long)ROUND(((PERCENTAGE) / (double)WARM_UP_LIMIT) * 65536)
102 #define T(PW_MILLISECONDS) PW_MS(PW_MILLISECONDS) // Shortcut for table use, same as above
103 
104 // Convenience wrappers for various non-native units
105 #define CC_PER_MINUTE_85(FLOW_RATE) CC_PER_MINUTE((FLOW_RATE) * (100 / 85.0))
106 #define DEGREES_C(TEMPERATURE) DEGREES_K((TEMPERATURE) + TEMPERATURE_C_TO_K_OFFSET)
107 
108 
109 #else
110  /* let us know if we are being untidy with headers */
111  #warning "Header file SCALERDEFINES_H seen before, sort it out!"
112 /* end of the wrapper ifdef from the very top */
113 #endif