Commit f45c121f8f4a96ad134369836a3015a348014d69
1 parent
e6a956d3
26-01-2021
Showing
6 changed files
with
48 additions
and
85 deletions
Show diff stats
FreematicsOBD/datalogger/customized_GPS.cpp
@@ -5,6 +5,7 @@ | @@ -5,6 +5,7 @@ | ||
5 | 5 | ||
6 | extern void clr_buff(char*,uint32_t); | 6 | extern void clr_buff(char*,uint32_t); |
7 | extern FreematicsESP32 sys; | 7 | extern FreematicsESP32 sys; |
8 | +int init_gps=0; | ||
8 | GPS_DATA* gd=0; | 9 | GPS_DATA* gd=0; |
9 | 10 | ||
10 | void init_GPS_dev(void){ | 11 | void init_GPS_dev(void){ |
@@ -23,6 +24,7 @@ void init_GPS_dev(void){ | @@ -23,6 +24,7 @@ void init_GPS_dev(void){ | ||
23 | pinMode(PIN_GPS_POWER, OUTPUT); | 24 | pinMode(PIN_GPS_POWER, OUTPUT); |
24 | digitalWrite(PIN_GPS_POWER, HIGH); | 25 | digitalWrite(PIN_GPS_POWER, HIGH); |
25 | int cnt=0; | 26 | int cnt=0; |
27 | + init_gps=0; | ||
26 | while(!isDataGPSReady(10000)){ | 28 | while(!isDataGPSReady(10000)){ |
27 | delay(100); | 29 | delay(100); |
28 | cnt++; | 30 | cnt++; |
@@ -32,15 +34,27 @@ void init_GPS_dev(void){ | @@ -32,15 +34,27 @@ void init_GPS_dev(void){ | ||
32 | esp_deep_sleep_start(); | 34 | esp_deep_sleep_start(); |
33 | } | 35 | } |
34 | } | 36 | } |
37 | + init_gps=1; | ||
35 | UpdateTime(); | 38 | UpdateTime(); |
36 | SetSysInstance(&sys); | 39 | SetSysInstance(&sys); |
37 | } | 40 | } |
38 | 41 | ||
39 | int isDataGPSReady(int wait_MS){ | 42 | int isDataGPSReady(int wait_MS){ |
40 | for (uint32_t t=millis();millis()-t<wait_MS;){ | 43 | for (uint32_t t=millis();millis()-t<wait_MS;){ |
41 | - digitalWrite(26, digitalRead(34)); | 44 | + if (!init_gps){ |
45 | + digitalWrite(26, digitalRead(34)); | ||
46 | + }else{ | ||
47 | + vTaskSuspendAll(); | ||
48 | + digitalWrite(26, digitalRead(34)); | ||
49 | + xTaskResumeAll(); | ||
50 | + } | ||
42 | if (sys.gpsGetData(&gd)) | 51 | if (sys.gpsGetData(&gd)) |
43 | return 1; | 52 | return 1; |
53 | + if (!init_gps){ | ||
54 | + delay(5); | ||
55 | + }else{ | ||
56 | + vTaskDelay(pdMS_TO_TICKS(5)); | ||
57 | + } | ||
44 | } | 58 | } |
45 | return 0; | 59 | return 0; |
46 | } | 60 | } |
@@ -111,13 +125,11 @@ int FilterGpsData(char* isoTime, double* res){ | @@ -111,13 +125,11 @@ int FilterGpsData(char* isoTime, double* res){ | ||
111 | 125 | ||
112 | if (slope_flag){ | 126 | if (slope_flag){ |
113 | double dist = (double) TinyGPS::distance_between((float) meanGPSCoord[prev][2],(float) meanGPSCoord[prev][1],(float) res[2],(float) res[1]); | 127 | double dist = (double) TinyGPS::distance_between((float) meanGPSCoord[prev][2],(float) meanGPSCoord[prev][1],(float) res[2],(float) res[1]); |
114 | - #if USE_OBD==0 | ||
115 | - double dist_max = gd->speed*shiftTime/3.6*1.25; | ||
116 | - #else | ||
117 | - double dist_max = GetOBDSpeed()*shiftTime/3.6*1.2; | ||
118 | - #endif | ||
119 | - if ((dist>dist_max) || (dist>(85*shiftTime))) | 128 | + //double dist_max = gd->speed*shiftTime/3.6*1.25; |
129 | + | ||
130 | + if (dist>(85*shiftTime)) | ||
120 | return -1; | 131 | return -1; |
132 | + | ||
121 | res[3]=(res[0]-meanGPSCoord[prev][0])/(dist+10)*100; | 133 | res[3]=(res[0]-meanGPSCoord[prev][0])/(dist+10)*100; |
122 | meanGPSCoord[n][0]=res[0]; | 134 | meanGPSCoord[n][0]=res[0]; |
123 | meanGPSCoord[n][1]=res[1]; | 135 | meanGPSCoord[n][1]=res[1]; |
@@ -129,40 +141,9 @@ int FilterGpsData(char* isoTime, double* res){ | @@ -129,40 +141,9 @@ int FilterGpsData(char* isoTime, double* res){ | ||
129 | for (j=0;j<time_size;j++){ | 141 | for (j=0;j<time_size;j++){ |
130 | lastTimeStamp2[j]=isoTime[j]; | 142 | lastTimeStamp2[j]=isoTime[j]; |
131 | } | 143 | } |
132 | - #if USE_OBD==0 | ||
133 | - isVehicleStopped(isoTime,res[4]); | ||
134 | - #endif | ||
135 | return slope_flag; | 144 | return slope_flag; |
136 | } | 145 | } |
137 | 146 | ||
138 | -void isVehicleStopped(char* isoTime,double val){ | ||
139 | - static char lastTimeStamp[time_size]={0}; | ||
140 | - static float meanSpeed=0; | ||
141 | - int j; | ||
142 | - | ||
143 | - if (lastTimeStamp[0]){ | ||
144 | - if (GetShiftTime(lastTimeStamp,isoTime)>NBR_GPS_POINTS){ | ||
145 | - if ((meanSpeed<2)&&(abs(val-meanSpeed)<0.5)){ | ||
146 | - esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR); | ||
147 | - esp_deep_sleep_start(); | ||
148 | - }else{ | ||
149 | - meanSpeed=val; | ||
150 | - for (j=0;j<time_size;j++){ | ||
151 | - lastTimeStamp[j]=isoTime[j]; | ||
152 | - } | ||
153 | - } | ||
154 | - }else{ | ||
155 | - meanSpeed+=val; | ||
156 | - meanSpeed/=2; | ||
157 | - } | ||
158 | - }else{ | ||
159 | - meanSpeed=val; | ||
160 | - for (j=0;j<time_size;j++){ | ||
161 | - lastTimeStamp[j]=isoTime[j]; | ||
162 | - } | ||
163 | - } | ||
164 | -} | ||
165 | - | ||
166 | int AddGPSData(char* isoTime,char* p, int* nbr){ | 147 | int AddGPSData(char* isoTime,char* p, int* nbr){ |
167 | 148 | ||
168 | int GPS_Stat=0; | 149 | int GPS_Stat=0; |
FreematicsOBD/datalogger/customized_GPS.h
@@ -10,9 +10,6 @@ int isDataGPSReady(int); | @@ -10,9 +10,6 @@ int isDataGPSReady(int); | ||
10 | //return -1 if GPS coordinates are incorrect accroding to speed | 10 | //return -1 if GPS coordinates are incorrect accroding to speed |
11 | int FilterGpsData(char*, double*); | 11 | int FilterGpsData(char*, double*); |
12 | 12 | ||
13 | -//check if the vehicle has stopped | ||
14 | -void isVehicleStopped(char*,double); | ||
15 | - | ||
16 | int AddGPSData(char*,char*,int*); | 13 | int AddGPSData(char*,char*,int*); |
17 | 14 | ||
18 | #endif /* __customized_GPS_H */ | 15 | #endif /* __customized_GPS_H */ |
FreematicsOBD/datalogger/customized_OBD.cpp
@@ -15,36 +15,21 @@ static SemaphoreHandle_t xSemaphore_err=NULL; | @@ -15,36 +15,21 @@ static SemaphoreHandle_t xSemaphore_err=NULL; | ||
15 | //Semaphore to synchronize access to ODB | 15 | //Semaphore to synchronize access to ODB |
16 | static SemaphoreHandle_t xSemaphore_OBD=NULL; | 16 | static SemaphoreHandle_t xSemaphore_OBD=NULL; |
17 | 17 | ||
18 | -static SemaphoreHandle_t xSemaphore_Speed=NULL; | ||
19 | - | ||
20 | -int OBD_Speed = 0; | ||
21 | - | ||
22 | - | ||
23 | -int GetOBDSpeed(void){ | ||
24 | - int res=0; | ||
25 | - xSemaphoreTake(xSemaphore_Speed,portMAX_DELAY); | ||
26 | - res = OBD_Speed; | ||
27 | - xSemaphoreGive(xSemaphore_Speed); | ||
28 | - return res; | ||
29 | -} | ||
30 | -void setOBDSpeed(int val){ | ||
31 | - xSemaphoreTake(xSemaphore_Speed,portMAX_DELAY); | ||
32 | - OBD_Speed = val; | ||
33 | - xSemaphoreGive(xSemaphore_Speed); | ||
34 | -} | ||
35 | - | ||
36 | 18 | ||
37 | void init_OBD_dev(void){ | 19 | void init_OBD_dev(void){ |
38 | xSemaphore_err= xSemaphoreCreateMutex(); | 20 | xSemaphore_err= xSemaphoreCreateMutex(); |
39 | //creating mutex to synchronze the access to OBD | 21 | //creating mutex to synchronze the access to OBD |
40 | xSemaphore_OBD= xSemaphoreCreateMutex(); | 22 | xSemaphore_OBD= xSemaphoreCreateMutex(); |
41 | - xSemaphore_Speed= xSemaphoreCreateMutex(); | ||
42 | obd.begin(sys.link); | 23 | obd.begin(sys.link); |
43 | int cnt=0; | 24 | int cnt=0; |
44 | while(!set_obd()){ | 25 | while(!set_obd()){ |
45 | cnt++; | 26 | cnt++; |
46 | delay(200); | 27 | delay(200); |
47 | - if (cnt==10){ | 28 | + #if USE_OBD==0 |
29 | + break; | ||
30 | + #endif | ||
31 | + | ||
32 | + if (cnt==5){ | ||
48 | esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR); | 33 | esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR); |
49 | esp_deep_sleep_start(); | 34 | esp_deep_sleep_start(); |
50 | } | 35 | } |
@@ -72,11 +57,14 @@ void ErrorOBDCheck(void){ | @@ -72,11 +57,14 @@ void ErrorOBDCheck(void){ | ||
72 | obd.reset(); | 57 | obd.reset(); |
73 | int cnt=0; | 58 | int cnt=0; |
74 | while(!set_obd()){ | 59 | while(!set_obd()){ |
75 | - delay(200); | 60 | + #if USE_OBD==0 |
61 | + break; | ||
62 | + #endif | ||
76 | if (cnt==5){ | 63 | if (cnt==5){ |
77 | esp_sleep_enable_timer_wakeup(2 * uS_TO_S_FACTOR); | 64 | esp_sleep_enable_timer_wakeup(2 * uS_TO_S_FACTOR); |
78 | esp_deep_sleep_start(); | 65 | esp_deep_sleep_start(); |
79 | } | 66 | } |
67 | + cnt++; | ||
80 | } | 68 | } |
81 | } | 69 | } |
82 | xSemaphoreGive(xSemaphore_err); | 70 | xSemaphoreGive(xSemaphore_err); |
@@ -126,7 +114,7 @@ int OBD_looping(ObdInfoPid* PIDs, int nbr_elem,int fr_max){ | @@ -126,7 +114,7 @@ int OBD_looping(ObdInfoPid* PIDs, int nbr_elem,int fr_max){ | ||
126 | int j; | 114 | int j; |
127 | int val; | 115 | int val; |
128 | int OBD_flag; | 116 | int OBD_flag; |
129 | - int res=5; | 117 | + int res=90; |
130 | 118 | ||
131 | if (!fr_max) | 119 | if (!fr_max) |
132 | res=60000; | 120 | res=60000; |
@@ -184,13 +172,14 @@ void CheckObdSpeed(char* label,char* isoTime,int val, int* res){ | @@ -184,13 +172,14 @@ void CheckObdSpeed(char* label,char* isoTime,int val, int* res){ | ||
184 | int j; | 172 | int j; |
185 | 173 | ||
186 | if (!strcmp(label,"PID_SPEED")){ | 174 | if (!strcmp(label,"PID_SPEED")){ |
187 | - setOBDSpeed(val); | ||
188 | if (lastTimeStamp[0]){ | 175 | if (lastTimeStamp[0]){ |
189 | if (GetShiftTime(lastTimeStamp,isoTime)>3){ | 176 | if (GetShiftTime(lastTimeStamp,isoTime)>3){ |
190 | if (abs(val-meanSpeed)<5){ | 177 | if (abs(val-meanSpeed)<5){ |
191 | if (val<2){ | 178 | if (val<2){ |
192 | - esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR); | ||
193 | - esp_deep_sleep_start(); | 179 | + #if STOP_SLEEP==1 |
180 | + esp_sleep_enable_timer_wakeup(5 * uS_TO_S_FACTOR); | ||
181 | + esp_deep_sleep_start(); | ||
182 | + #endif | ||
194 | } | 183 | } |
195 | *res=100; | 184 | *res=100; |
196 | }else{ | 185 | }else{ |
@@ -210,4 +199,4 @@ void CheckObdSpeed(char* label,char* isoTime,int val, int* res){ | @@ -210,4 +199,4 @@ void CheckObdSpeed(char* label,char* isoTime,int val, int* res){ | ||
210 | } | 199 | } |
211 | } | 200 | } |
212 | } | 201 | } |
213 | -} | ||
214 | \ No newline at end of file | 202 | \ No newline at end of file |
203 | +} |
FreematicsOBD/datalogger/simple_obd_test.bin
No preview for this file type
FreematicsOBD/datalogger/simple_obd_test.h
@@ -38,13 +38,13 @@ | @@ -38,13 +38,13 @@ | ||
38 | #include <freertos/portmacro.h> | 38 | #include <freertos/portmacro.h> |
39 | #include <freertos/semphr.h> | 39 | #include <freertos/semphr.h> |
40 | 40 | ||
41 | -#define USE_SERIAL 0 | 41 | +#define USE_SERIAL 1 |
42 | #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ | 42 | #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ |
43 | #define STACK_SIZE 1024 // in words (= 4*STACK_SIZE bytes) | 43 | #define STACK_SIZE 1024 // in words (= 4*STACK_SIZE bytes) |
44 | #define buff_SIZE 500 | 44 | #define buff_SIZE 500 |
45 | #define time_size 100 | 45 | #define time_size 100 |
46 | #define USE_OBD 1 | 46 | #define USE_OBD 1 |
47 | -#define NBR_GPS_POINTS 10 | ||
48 | - | 47 | +#define NBR_GPS_POINTS 1 |
48 | +#define SLEEP_STOP 0 | ||
49 | 49 | ||
50 | #endif /* __simple_obd_test_H */ | 50 | #endif /* __simple_obd_test_H */ |
FreematicsOBD/datalogger/simple_obd_test.ino
@@ -70,9 +70,8 @@ void setup(){ | @@ -70,9 +70,8 @@ void setup(){ | ||
70 | //initialize MEMS device | 70 | //initialize MEMS device |
71 | init_MEMS_dev(); | 71 | init_MEMS_dev(); |
72 | 72 | ||
73 | - #if USE_OBD==1 | ||
74 | - init_OBD_dev(); | ||
75 | - #endif | 73 | + |
74 | + init_OBD_dev(); | ||
76 | 75 | ||
77 | // turn on buzzer at 2000Hz frequency | 76 | // turn on buzzer at 2000Hz frequency |
78 | sys.buzzer(2000); | 77 | sys.buzzer(2000); |
@@ -81,20 +80,16 @@ void setup(){ | @@ -81,20 +80,16 @@ void setup(){ | ||
81 | sys.buzzer(0); | 80 | sys.buzzer(0); |
82 | 81 | ||
83 | //creating queue with a size (15*sizeof(Irp*)) | 82 | //creating queue with a size (15*sizeof(Irp*)) |
84 | - xQueue_print = xQueueCreate(15,sizeof(Irp*)); | 83 | + xQueue_print = xQueueCreate(25,sizeof(Irp*)); |
85 | 84 | ||
86 | - #if USE_OBD==1 | ||
87 | - //f=fmax | ||
88 | - xTaskCreate(write_task_1,"write_task_1",STACK_SIZE*3,(void*) 0,tskIDLE_PRIORITY+1,NULL); | ||
89 | - #endif | 85 | + //f=fmax |
86 | + xTaskCreate(write_task_1,"write_task_1",STACK_SIZE*3,(void*) 0,tskIDLE_PRIORITY+1,NULL); | ||
90 | 87 | ||
91 | //f=10Hz | 88 | //f=10Hz |
92 | - xTaskCreate(write_task_2,"write_task_2",STACK_SIZE*5,(void*) 0,tskIDLE_PRIORITY+4,NULL); | 89 | + xTaskCreate(write_task_2,"write_task_2",STACK_SIZE*5,(void*) 0,tskIDLE_PRIORITY+1,NULL); |
93 | 90 | ||
94 | - #if USE_OBD==1 | ||
95 | - //f=1/60 Hz | ||
96 | - xTaskCreate(write_task_3,"write_task_3",STACK_SIZE*3,(void*) 0,tskIDLE_PRIORITY+1,NULL); | ||
97 | - #endif | 91 | + //f=1/60 Hz |
92 | + xTaskCreate(write_task_3,"write_task_3",STACK_SIZE*3,(void*) 0,tskIDLE_PRIORITY+1,NULL); | ||
98 | 93 | ||
99 | //create daemon task | 94 | //create daemon task |
100 | xTaskCreate(vTask_Transmit,"vTask_Transmit",STACK_SIZE*40,(void*) 0,tskIDLE_PRIORITY+3,NULL); | 95 | xTaskCreate(vTask_Transmit,"vTask_Transmit",STACK_SIZE*40,(void*) 0,tskIDLE_PRIORITY+3,NULL); |
@@ -110,6 +105,7 @@ void write_task_1(void* parameter) | @@ -110,6 +105,7 @@ void write_task_1(void* parameter) | ||
110 | sleep_tme=OBD_looping(PIDs,nbr_elem,1); | 105 | sleep_tme=OBD_looping(PIDs,nbr_elem,1); |
111 | taskYIELD(); | 106 | taskYIELD(); |
112 | ErrorOBDCheck(); | 107 | ErrorOBDCheck(); |
108 | + Serial.println("OBD signal lost !"); | ||
113 | vTaskDelay(pdMS_TO_TICKS(sleep_tme)); | 109 | vTaskDelay(pdMS_TO_TICKS(sleep_tme)); |
114 | } | 110 | } |
115 | vTaskDelete(NULL); | 111 | vTaskDelete(NULL); |
@@ -139,7 +135,7 @@ void write_task_2(void* parameter) | @@ -139,7 +135,7 @@ void write_task_2(void* parameter) | ||
139 | sprintf(p,"\n"); | 135 | sprintf(p,"\n"); |
140 | print_data(buff,nbr+1); | 136 | print_data(buff,nbr+1); |
141 | } | 137 | } |
142 | - vTaskDelay(pdMS_TO_TICKS(100)); | 138 | + vTaskDelay(pdMS_TO_TICKS(25)); |
143 | } | 139 | } |
144 | vTaskDelete(NULL); | 140 | vTaskDelete(NULL); |
145 | } | 141 | } |
@@ -180,7 +176,7 @@ void vTask_Transmit(void *pvParameters){ | @@ -180,7 +176,7 @@ void vTask_Transmit(void *pvParameters){ | ||
180 | appendFile(SD, getFileName(),buff); | 176 | appendFile(SD, getFileName(),buff); |
181 | //free the semaphore | 177 | //free the semaphore |
182 | xSemaphoreGive(ReceiveItem->completion); | 178 | xSemaphoreGive(ReceiveItem->completion); |
183 | - taskYIELD(); | 179 | + vTaskDelay(pdMS_TO_TICKS(20)); |
184 | } | 180 | } |
185 | 181 | ||
186 | } | 182 | } |