forked from ugracing/datalogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datalogger.cpp
157 lines (135 loc) · 3.19 KB
/
datalogger.cpp
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
#include "cmath"
#include "mbed.h"
#include <stdio.h>
#include <string>
#define FSNAME "msc"
Serial pc(USBTX, USBRX); // tx, rx
Serial msquirt(p9, p10); // tx, rx
DigitalOut myled(LED1);
AnalogOut LED_driver(p18);
DigitalOut LED_CLT(p21);
//USB:
//MSCFileSystem msc("msc"); //USB file
//AnalogIn Tin(p16);
//AnalogIn Tout(p17);
Timer t;
int byte_count;
string str;
bool fetch;
int count;
struct car_datastruct{
float RPM;
float map;
float mat;
float coolant;
float throttle;
float battery;
float air_fuel_1;
float air_fuel_2;
bool engine_status[8];
bool injectors_status[7];
};
car_datastruct car_data;
void drive_dash(float RPM, float CLT){
for(int i = 0; i< 10000; i = i+ 100){
RPM = i;
wait(0.2);
LED_driver = RPM/10000;
}
if(CLT>20){
LED_CLT = 1;
}
else LED_CLT = 0;
}
void rx_data(){
__disable_irq(); // Disable Interrupts
count++;
str = "";
byte_count=0;
while (1){
if (msquirt.readable())
{
//printf("got byte - %d\r\n", byte_count);
str += msquirt.getc();
byte_count++;
}
if (byte_count >= 209)
{
break;
}
}
union
{
uint8_t b[2];
int i;
};
b[1] = str[22]; b[0] = str[23];
car_data.coolant = (i-320) * 0.05555;
pc.printf("CLT = %f \r\n",car_data.coolant);
b[1] = str[6]; b[0] = str[7];
car_data.RPM = i;
pc.printf("rpm %f \n\r",car_data.RPM);
pc.printf("coolant %f \n\r",car_data.coolant);
b[1] = str[28];b[0] = str[29];
car_data.air_fuel_1 = i*0.1;
pc.printf("data %f \n\r",car_data.air_fuel_1);
b[1] = str[18];b[0] = str[19];
car_data.map = i*0.1;
pc.printf("data %f \n\r",car_data.map);
b[1] = str[20];b[0] = str[21];
car_data.mat = (i-320)*0.05555;
b[1] = str[24];b[0] = str[25];
car_data.throttle = i*0.1;
b[1] = str[26];b[0] = str[27];
car_data.battery = i*0.1;
b[1] = str[28];b[0] = str[29];
car_data.air_fuel_1 = i*0.1;
b[1] = str[28];b[0] = str[29];
car_data.air_fuel_2 = i*0.1;
int offset;
b[0] = str[10];
for(offset = 0; offset<7; offset++){
car_data.injectors_status[offset] = ((b[0] >> offset) & 0x01);
pc.printf("%d", car_data.injectors_status[offset]);
}
pc.printf("got %d bytes\r\n", byte_count);
pc.printf("MAT %f \r\n", car_data.mat);
str = "";
byte_count = 0;
//pc.printf("set to zer mofofoker");
fetch = true;
// __enable_irq(); // Enable Interrupts
//pc.printf("fetch set true");
//wait(0.2);
}
void flush()
{
char obl[8];
while(msquirt.readable())
{
obl[0] = msquirt.getc();
}
}
void log_data()
{
//fprintf(fp,"%.1f\370C\t%.1f\370C\n\r",temp(Tin), temp(Tout));
}
int main(){
//FILE *fp = fopen( "/msc/data.txt", "w");
msquirt.baud(38400);
pc.baud(57600);
pc.printf("main\r\n");
//msquirt.attach(&rx_data, Serial::RxIrq);
fetch = true;
flush();
while(1){
//wait_us(100);
if(fetch)
{
flush();
msquirt.putc('A');
wait(0.1);
rx_data();
}
}
}