-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdavis_packet.c
283 lines (239 loc) · 12.2 KB
/
davis_packet.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "ztypes.h"
#include "davis_packet.h"
#define WTROWS 55
#define WTCOLS 35
static int16_t windtab[WTROWS][WTCOLS]={
{0, 1, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, 112, 116, 120, 124, 127, 128},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, -6},
{135, 1, 2, 2, 1, 1, 0, 0, 0, -1, 0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 2, 2, 2, 1, 1, 3, 10, 17, 17, 4, -4, -6},
{140, 1, 2, 2, 1, 1, 0, 0, 0, -1, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 3, 3, 2, 2, 2, 1, 1, 3, 11, 18, 17, 4, -4, -6},
{145, 2, 2, 2, 1, 1, 0, 0, 0, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 3, 11, 19, 18, 4, -4, -6},
{150, 2, 2, 2, 1, 1, 0, 0, -1, -1, 0, 0, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 3, 12, 19, 19, 4, -4, -6}
};
float interpolate2 (float rx0, float rx1, float ry0, float ry1, float x0, float x1, float y0, float y1, float x, float y){
float dy0, dy1;
if (rx0 == rx1)
return y + x0 + (y - ry0) / (ry1 - ry0) * (y1 - y0);
if (ry0 == ry1)
return y + y0 + (x - rx0) / (rx1 - rx0) * (x1 - x0);
dy0 = x0 + (y - ry0) / (ry1 - ry0) * (y0 - x0);
dy1 = x1 + (y - ry0) / (ry1 - ry0) * (y1 - x1);
return y + dy0 + (x - rx0) / (rx1 - rx0) * (dy1 - dy0);
}
float calc_wind_speed(int16_t raw_mph, int16_t raw_angle){
uint16_t s0,s1,a0,a1;
if (raw_angle>128){
raw_angle = 256 - raw_angle;
}
s0=1;a0=1;
while (windtab[s0][0] < raw_mph) s0++;
while (windtab[0][a0] < raw_angle) a0++;
if (windtab[s0][0] == raw_mph) {
s1 = s0;
} else {
if (s0>1){
s0--;
}
if (s0 == WTROWS-1 ){
s1 = WTROWS-1;
} else {
s1 = s0 + 1;
}
}
if (windtab[0][a0] == raw_angle) {
a1 = a0;
} else {
if (a0>1){
a0--;
}
if (a0 == WTCOLS-1 ){
a1 = WTCOLS-2;
} else {
a1 = a0 + 1;
}
}
if ((s0==s1) && (a0==a1)){
return (float) (raw_mph + windtab[s0][a0]);
}
return interpolate2((float)windtab[0][a0], (float)windtab[0][a1],
(float)windtab[s0][0], (float)windtab[s1][0],
(float)windtab[s0][a0], (float)windtab[s0][a1],
(float)windtab[s1][a0], (float)windtab[s1][a1],
(float)raw_angle, (float)raw_mph);
}
uint16_t crc16_ccitt( const void *buf, int len ){
unsigned short crc = 0;
while( len-- ) {
int i;
crc ^= *(char *)buf++ << 8;
for( i = 0; i < 8; ++i ) {
if( crc & 0x8000 )
crc = (crc << 1) ^ 0x1021;
else
crc = crc << 1;
}
}
return crc;
}
t_DAVIS_PACKET *new_davis_packet(uint8_t *data, uint16_t station_type){
t_DAVIS_PACKET *packet;
int i;
/*
for (i=0;i<DAVIS_PACKET_SIZE;i++){
printf ("%.02x ",data[i]);
}
printf ("\n");*/
packet = malloc ( sizeof ( t_DAVIS_PACKET));
memcpy(packet->data, data, DAVIS_PACKET_SIZE);
packet->station_type = station_type;
packet->sensor_value = FLT_MIN;
packet->received_crc = (data[6]<<8) | data[7];
packet->computed_crc = crc16_ccitt(data, 6);
packet->valid = packet->received_crc == packet->computed_crc;
// printf ("received_crc %.4x\n",packet->received_crc);
// printf ("computed_crc %.4x\n",packet->computed_crc);
packet->transmitter_id = data[0] & 0b111;
packet->battery_state = (data[0] & 0b0001000) >> 3;
packet->packet_type = data[0]>>4;
// printf ("packet_type: %d\n", packet->packet_type);
switch (packet->packet_type){
case DAVIS_HUMIDITY: strcpy(packet->description,"humidity");break;
case DAVIS_RAIN: strcpy(packet->description,"rain-counter");break;
case DAVIS_RAIN_RATE: strcpy(packet->description,"rain-rate");break;
case DAVIS_SOIL: strcpy(packet->description, "soil");break;
case DAVIS_SOLAR_CELL_OUTPUT: strcpy(packet->description,"solar-cell-output");break;
case DAVIS_SOLAR_RADIATION: strcpy(packet->description,"solar-radiation");break;
case DAVIS_TEMPERATURE: strcpy(packet->description,"temperature");break;
case DAVIS_WIND_GUST: strcpy(packet->description,"wind-gust");break;
case DAVIS_UV_INDEX: strcpy(packet->description,"uv-index");break;
case DAVIS_SUPERCAP_VOLTAGE: strcpy(packet->description,"supercap-voltage");break;
default:sprintf (packet->description, "type_%.2u", packet->packet_type);break;
}
if (packet->packet_type!=DAVIS_SOIL){
packet->wind_speed = -1;
if (data[1]!=0xff){
float ecspd;
ecspd = calc_wind_speed((int16_t)data[1], (int16_t)data[2]);
//packet->wind_speed = (float)data[1] * 1.609344;
packet->wind_speed = ecspd * 1.609344;
}
// printf ("wind_speed: %f\n", packet->wind_speed);
packet->wind_direction=-1;
if (station_type==STATION_TYPE_VUE){
if (data[2]!=0xff){
packet->wind_direction=(float)data[2] * 1.40625 + 0.3;
} else {
packet->wind_direction = 0;
}
} else
if (station_type==STATION_TYPE_VP2){
if (data[2]!=0xff){
packet->wind_direction=9 + ((float)data[2] * 342.)/255.;
} else {
packet->wind_direction = 0;
}
}
// printf ("wind_direction: %f\n", packet->wind_direction);
if (packet->packet_type==DAVIS_SUPERCAP_VOLTAGE){
packet->sensor_value = (float)((data[3]<<2)|((data[4] & 0b11000000)>>6)) / 100.;
} else
if (packet->packet_type==DAVIS_UV_INDEX){
packet->sensor_value = (float) (((data[3] << 8) + data[4]) >> 6) / 50.;
} else
if (packet->packet_type==DAVIS_RAIN_RATE){
if (data[3]!=0xff){
if (data[4] & 0x40){ // strong rain
packet->sensor_value = 720. / (float)(((data[4] & 0x30) / 16 * 250) + data[3]); // mm/h
} else {
packet->sensor_value = 11520. / (float)(((data[4] & 0x30) / 16 * 250) + data[3]);
}
}
} else
if (packet->packet_type==DAVIS_TEMPERATURE){
int16_t far16;
float far;
float deg;
far16 = (((data[3] << 8) | data[4]));
far = ((float) far16) / 160.;
deg = (far-32) * 5. / 9.;
packet->sensor_value = deg;
} else
if (packet->packet_type==DAVIS_HUMIDITY){
packet->sensor_value = (float)(((data[4] >> 4) << 8) + data[3]) / 10.0;
} else
if (packet->packet_type==DAVIS_SOLAR_RADIATION){
packet->sensor_value = (float)(((data[3] << 8) + data[4]) >> 6) * 1.757936;
} else
if (packet->packet_type==DAVIS_RAIN){
//if (data[3]!=0x80){
packet->sensor_value = (float) data[3];
//}
} else
if (packet->packet_type==DAVIS_SOLAR_CELL_OUTPUT){
packet->sensor_value = (float)((data[3]<<2)|((data[4] & 0b11000000)>>6)) / 100.;
} else
if (packet->packet_type==DAVIS_WIND_GUST){
packet->wind_gust = (float) data[3] * 1.609344;
packet->wind_gust_index = data[4]>>4;
}
}
// printf ("packet_type %d, sensor_value %f\n",packet->packet_type, packet->sensor_value);
return packet;
}
void free_davis_packet(t_DAVIS_PACKET *packet){
free (packet);
}