customized_OBD.cpp
3.5 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "simple_obd_test.h"
#include "customized_DateAndTime.h"
#include "customized_OBD.h"
COBD obd;
extern FreematicsESP32 sys;
extern void print_data(char* data, uint32_t len);
extern void clr_buff(char* buf,uint32_t len);
char vin[18] = {0};
uint16_t dtc[6] = {0};
static SemaphoreHandle_t xSemaphore_err=NULL;
//Semaphore to synchronize access to ODB
static SemaphoreHandle_t xSemaphore_OBD=NULL;
void init_OBD_dev(void){
xSemaphore_err= xSemaphoreCreateMutex();
//creating mutex to synchronze the access to OBD
xSemaphore_OBD= xSemaphoreCreateMutex();
obd.begin(sys.link);
int cnt=0;
while(!set_obd()){
cnt++;
delay(200);
#if USE_OBD==0
break;
#endif
if (cnt==5){
esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
}
}
int GetOBDInfo(uint8_t pid, int* val){
int aux;
if (obd.isValidPID(pid)){
if (obd.readPID(pid,aux)){
*val=aux;
return 1;
}
}
return 0;
}
void ErrorOBDCheck(void){
xSemaphoreTake(xSemaphore_err,portMAX_DELAY);
if (obd.errors >= MAX_OBD_ERRORS) {
#if USE_SERIAL==1
Serial.println("ECU OFF");
#endif
obd.reset();
int cnt=0;
while(!set_obd()){
#if USE_OBD==0
break;
#endif
if (cnt==5){
esp_sleep_enable_timer_wakeup(2 * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
cnt++;
}
}
xSemaphoreGive(xSemaphore_err);
}
int set_obd(void){
int res=0;
if (!obd.init()) {
#if USE_SERIAL==1
Serial.println("OBD is not detected");
#endif
} else {
#if USE_SERIAL==1
Serial.println("OBD was successfully set !");
#endif
res=1;
}
char buf[128];
if (obd.getVIN(buf, sizeof(buf))) {
strncpy(vin, buf, sizeof(vin) - 1);
#if USE_SERIAL==1
Serial.print("VIN:");
Serial.println(vin);
#endif
}
int dtcCount = obd.readDTC(dtc, sizeof(dtc) / sizeof(dtc[0]));
if (dtcCount > 0) {
#if USE_SERIAL==1
Serial.print("DTC:");
Serial.println(dtcCount);
#endif
}
return res;
}
int OBD_looping(ObdInfoPid* PIDs, int nbr_elem,int fr_max){
char buff[buff_SIZE];
char* p;
uint32_t nbr;
char isoTime[time_size]={0};
int i;
int j;
int val;
int OBD_flag;
int res=90;
if (!fr_max)
res=60000;
clr_buff(buff,buff_SIZE);
p=buff;
OBD_flag=0;
nbr=0;
clr_buff(isoTime,time_size);
xSemaphoreTake(xSemaphore_OBD,portMAX_DELAY);
GetCurrentTime(isoTime);
j=sprintf(p,"%s,,,,,,,,,,,,,,,",isoTime);
nbr+=j;
p+=j;
for(i=0; i<nbr_elem;i++){
if (PIDs[i].fr_max==fr_max){
if(GetOBDInfo(PIDs[i].pid, &val)){
OBD_flag=1;
j=sprintf(p,",%d",val);
nbr+=j;
p+=j;
CheckObdSpeed(PIDs[i].label,isoTime,val, &res);
}else{
j=sprintf(p,",");
nbr+=j;
p+=j;
}
}else{
j=sprintf(p,",");
nbr+=j;
p+=j;
}
}
xSemaphoreGive(xSemaphore_OBD);
if (OBD_flag){
j=sprintf(p,"\n");
nbr+=j;
p+=j;
if (res!=100)
print_data(buff,nbr);
}
return res;
}
void CheckObdSpeed(char* label,char* isoTime,int val, int* res){
static char lastTimeStamp[time_size]={0};
static int meanSpeed=0;
int j;
if (!strcmp(label,"PID_SPEED")){
if (lastTimeStamp[0]){
if (GetShiftTime(lastTimeStamp,isoTime)>3){
if (abs(val-meanSpeed)<5){
if (val<2){
#if STOP_SLEEP==1
esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR);
esp_deep_sleep_start();
#endif
}
*res=100;
}else{
meanSpeed=val;
for (j=0;j<time_size;j++){
lastTimeStamp[j]=isoTime[j];
}
}
}else{
meanSpeed+=val;
meanSpeed/=2;
}
}else{
meanSpeed=val;
for (j=0;j<time_size;j++){
lastTimeStamp[j]=isoTime[j];
}
}
}
}