forked from Openvario/sensord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensorcal.c
executable file
·236 lines (207 loc) · 5.89 KB
/
sensorcal.c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* sensorcal - Sensor Calibration for Openvario Sensorboard - http://www.openvario.org/
Copyright (C) 2014 The openvario project
A detailed list of copyright holders can be found in the file "AUTHORS"
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "24c16.h"
#include "ams5915.h"
#include "ms5611.h"
#include "sensorcal.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int g_debug=0;
FILE *fp_console=NULL;
int calibrate_ams5915(t_eeprom_data* data)
{
t_ams5915 dynamic_sensor;
float offset=0.0;
int i;
// open sensor for differential pressure
/// @todo remove hardcoded i2c address for differential pressure
printf("Open sensor ...");
if (ams5915_open(&dynamic_sensor, 0x28) != 0)
{
printf(" failed !!\n");
return 1;
}
else
{
printf(" success!\n");
}
dynamic_sensor.offset = 0.0;
dynamic_sensor.linearity = 1.0;
//initialize differential pressure sensor
ams5915_init(&dynamic_sensor);
clock_gettime(CLOCK_REALTIME,&sensor_prev);
sensor_wait(112500);
clock_gettime(CLOCK_REALTIME,&sensor_prev);
for (i=0;i<800;i++)
{
// read AMS5915
sensor_wait(12500);
ams5915_measure(&dynamic_sensor);
clock_gettime(CLOCK_REALTIME,&sensor_prev);
ams5915_calculate(&dynamic_sensor);
// wait some time ...
printf("Measured: %f\n",dynamic_sensor.p);
// calc offset
offset += dynamic_sensor.p;
}
data->zero_offset = -1*(offset/800);
return(0);
}
int main (int argc, char **argv) {
// local variables
t_24c16 eeprom;
t_eeprom_data data;
int exit_code=0;
int result;
int c;
int i;
char sn[7];
char zero[1]={0x00};
// usage message
const char* Usage = "\n"\
" -c calibrate sensors\n"\
" -s [serial] write serial number (6 characters)\n"\
" -i initialize EEPROM. All values will be cleared !!!\n"\
"\n";
// disable buffering
setbuf(stdout, NULL);
// print banner
printf("sensorcal V%c.%c RELEASE %c build: %s %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, __DATE__, __TIME__);
printf("sensorcal Copyright (C) 2014 see AUTHORS on www.openvario.org\n");
printf("This program comes with ABSOLUTELY NO WARRANTY;\n");
printf("This is free software, and you are welcome to redistribute it under certain conditions;\n");
// open eeprom object
result = eeprom_open(&eeprom, 0x50);
if (result != 0)
{
printf("No EEPROM found !!\n");
exit(1);
}
sn[6]=0;
// check commandline arguments
while ((c = getopt (argc, argv, "his:cde")) != -1)
{
switch (c) {
case 'h':
printf("Usage: sensorcal [OPTION]\n%s",Usage);
break;
case 'i':
printf("Initialize EEPROM ...\n");
for (i=0; i<128; i++)
{
result = eeprom_write(&eeprom, &zero[0], i, 1);
}
strcpy(data.header, "OV");
data.data_version = EEPROM_DATA_VERSION;
memset(data.serial,'0',6);
data.zero_offset=0.0;
update_checksum(&data);
printf("Writing data to EEPROM ...\n");
result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
break;
case 'c':
// read actual EEPROM values
printf("Reading EEPROM values ...\n\n");
if( eeprom_read_data(&eeprom, &data) == 0)
{
calibrate_ams5915(&data);
printf("New Offset: %f\n",(data.zero_offset));
update_checksum(&data);
printf("Writing data to EEPROM ...\n");
result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
}
else
{
printf("EEPROM content not valid !!\n");
printf("Please use -i to initialize EEPROM !!\n");
exit_code=2;
break;
}
break;
case 'e':
// delete complete EEPROM
printf("Delete whole EEPROM ...\n\n");
for (i=0; i< sizeof(data); i++)
{
result = eeprom_write(&eeprom, &zero[0], 0x00, 1);
}
printf("EEPROM cleared !!\n");
exit_code=3;
printf("End ...\n");
exit(exit_code);
break;
case 'd':
// read actual EEPROM values
printf("Reading EEPROM values ...\n\n");
if( eeprom_read_data(&eeprom, &data) == 0)
{
memcpy(sn,data.serial,6);
printf("Actual EEPROM values:\n");
printf("---------------------\n");
printf("Serial: \t\t\t%s\n", sn);
printf("Differential pressure offset:\t%f\n",data.zero_offset);
}
else
{
printf("EEPROM content not valid !!\n");
printf("Please use -i to initialize EEPROM !!\n");
exit_code=2;
break;
}
printf("End ...\n");
exit(exit_code);
break;
case 's':
if( strlen(optarg) == 6)
{
// read actual EEPROM values
if( eeprom_read_data(&eeprom, &data) == 0)
{
for(i=0; i<6;i++)
{
sn[i]=data.serial[i]=*optarg;
optarg++;
}
printf("New Serial number: %s\n",sn);
update_checksum(&data);
printf("Writing data to EEPROM ...\n");
result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
}
else
{
printf("EEPROM content not valid !!\n");
printf("Please use -i to initialize EEPROM !!\n");
exit_code=2;
break;
}
}
else
{
printf("ERROR: Serialnumber has to have exactly 6 characters !!\n");
exit_code=1;
break;
}
break;
case '?':
printf("Unknow option %c\n", optopt);
printf("Usage: sensorcal [OPTION]\n%s",Usage);
printf("Exiting ...\n");
}
}
printf("End ...\n");
return(exit_code);
}