-
Notifications
You must be signed in to change notification settings - Fork 2
/
trip.ino
170 lines (148 loc) · 4.61 KB
/
trip.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "trip.h"
#include "mathfuncs.h"
#include "parms.h"
Trip::Trip() {
}
unsigned long Trip::miles(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
init64(tmp1,0,var[Trip::vssPulses]);
init64(tmp2,0,1000);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[vssPulsesPerMileIdx]);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long Trip::eocMiles(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
init64(tmp1,0,var[Trip::vssEOCPulses]);
init64(tmp2,0,1000);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[vssPulsesPerMileIdx]);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long Trip::mph(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
if(var[Trip::loopCount] == 0)
return 0;
init64(tmp1,0,loopsPerSecond);
init64(tmp2,0,var[Trip::vssPulses]);
mul64(tmp1,tmp2);
init64(tmp2,0,3600000);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[vssPulsesPerMileIdx]);
div64(tmp1,tmp2);
init64(tmp2,0,var[Trip::loopCount]);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long Trip::gallons(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
init64(tmp1,0,var[Trip::injHiSec]);
init64(tmp2,0,1000000);
mul64(tmp1,tmp2);
init64(tmp2,0,var[Trip::injHius]);
add64(tmp1,tmp2);
init64(tmp2,0,1000);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[microSecondsPerGallonIdx]);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long Trip::idleGallons(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
init64(tmp1,0,var[Trip::injIdleHiSec]);
init64(tmp2,0,1000000);
mul64(tmp1,tmp2);
init64(tmp2,0,var[Trip::injIdleHius]);
add64(tmp1,tmp2);
init64(tmp2,0,1000);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[microSecondsPerGallonIdx]);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long Trip::fuelCost(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
init64(tmp1,0,(Trip::gallons())); /* 0.001 gal/bit */
init64(tmp2,0,parms[fuelcostIdx]); /* 0.01 dollars/bit */
mul64(tmp1,tmp2);
init64(tmp2,0,100);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long Trip::mpg(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long tmp3[2];
if(var[Trip::vssPulses]==0) return 0;
if(var[Trip::injPulses]==0) return 999999000; //who doesn't like to see 999999? :)
init64(tmp1,0,var[Trip::injHiSec]);
init64(tmp3,0,1000000);
mul64(tmp3,tmp1);
init64(tmp1,0,var[Trip::injHius]);
add64(tmp3,tmp1);
init64(tmp1,0,parms[vssPulsesPerMileIdx]);
mul64(tmp3,tmp1);
init64(tmp1,0,parms[microSecondsPerGallonIdx]);
init64(tmp2,0,1000);
mul64(tmp1,tmp2);
init64(tmp2,0,var[Trip::vssPulses]);
mul64(tmp1,tmp2);
div64(tmp1,tmp3);
return tmp1[1];
}
//return the seconds as a time mmm.ss, eventually hhh:mm too
unsigned long Trip::time(){
// return seconds*1000;
unsigned char d = 60;
unsigned long seconds = var[Trip::loopCount]/loopsPerSecond;
return ((seconds/d)*1000) + ((seconds%d) * 10);
}
void Trip::reset(){
var[Trip::loopCount]=0;
var[Trip::injPulses]=0;
var[Trip::injHius]=0;
var[Trip::injHiSec]=0;
var[Trip::vssPulses]=0;
var[Trip::vssPulseLength]=0;
var[Trip::injIdleHiSec]=0;
var[Trip::injIdleHius]=0;
var[Trip::vssEOCPulses]=0;
}
void Trip::update(Trip t) {
var[Trip::loopCount]++; //we call update once per loop
var[Trip::vssPulses]+=t.var[Trip::vssPulses];
var[Trip::vssPulseLength]+=t.var[Trip::vssPulseLength];
if ( t.var[Trip::injPulses] == 0 ) //track distance traveled with engine off
var[Trip::vssEOCPulses]+=t.var[Trip::vssPulses];
if ( t.var[Trip::injPulses] > 2 && t.var[Trip::injHius]<500000 ) {// chasing ghosts
var[Trip::injPulses]+=t.var[Trip::injPulses];
var[Trip::injHius]+=t.var[Trip::injHius];
if (var[Trip::injHius]>=1000000){
// rollover into the var[Trip::injHiSec] counter
var[Trip::injHiSec]++;
var[Trip::injHius]-=1000000;
}
if(t.var[Trip::vssPulses] == 0) {
// track gallons spent sitting still
var[Trip::injIdleHius]+=t.var[Trip::injHius];
if (var[Trip::injIdleHius]>=1000000) { //r
var[Trip::injIdleHiSec]++;
var[Trip::injIdleHius]-=1000000;
}
}
}
}