-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadcs.c
265 lines (217 loc) · 7.23 KB
/
adcs.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
#include <polysat_pkt/status-structs.h>
#include <polysat_pkt/shared-structs.h>
#include <polysat_pkt/payload_cmd.h>
#include <polysat_drivers/drivers/accelerometer.h>
#include <polysat_drivers/drivers/gyroscope.h>
#include <polysat_drivers/drivers/magnetometer.h>
#include <polysat_drivers/driverdb.h>
#include <polysat/polysat.h>
#include <polysat_pkt/filemgr_cmd.h>
#include <polysat_pkt/datalogger_cmd.h>
#include <limits.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include "adcs-telemetry.h"
struct ADCSState {
ProcessData *proc;
void *create_evt;
};
static struct ADCSState *gState;
#define SENSOR_OPEN_INTV_MS 20
#define SENSOR_RETRY_INTV_MS (1000 * 60 * 15)
#define ACCEL_TYPE_FLAG (1 << 0)
#define GYRO_TYPE_FLAG (1 << 1)
#define MAG_TYPE_FLAG (1 << 2)
// Structure to hold sensor related information
// This enables generic sensor handling code, minimizing special cases
struct SensorInfo {
const char *name;
const char *location;
const char *type;
int flags;
void (*marshal)(struct SensorInfo *sensor, void *dst);
int offset;
struct Sensor *sensor;
struct DeviceInfo *dev_info;
int disabled;
};
// Declarations of functions that read sensor values and pack them
// into status response
static void marshal_accel(struct SensorInfo *si, void *dst);
static void marshal_gyro(struct SensorInfo *si, void *dst);
static void marshal_mag(struct SensorInfo *si, void *dst);
#define ACCEL_SENSOR(n,l,field) { n, l, DRVR_CLS_ACCELEROMETER, \
ACCEL_TYPE_FLAG, &marshal_accel, \
offsetof(struct ADCSReaderStatus, field), \
NULL, NULL, 0 }
#define GYRO_SENSOR(n,l,field) { n, l, DRVR_CLS_GYROSCOPE, \
GYRO_TYPE_FLAG, &marshal_gyro, \
offsetof(struct ADCSReaderStatus, field), \
NULL, NULL, 0 }
#define MAG_SENSOR(n,l,field) { n, l, DRVR_CLS_MAGNETOMETER, \
MAG_TYPE_FLAG, &marshal_mag, \
offsetof(struct ADCSReaderStatus, field), \
NULL, NULL, 0 }
// Sensors managed by this process
struct SensorInfo sensors[] = {
ACCEL_SENSOR("mb_accel", DEVICE_LOCATION_MB, accel),
GYRO_SENSOR("mb_gyro", DEVICE_LOCATION_MB, gyro),
MAG_SENSOR("mb", DEVICE_LOCATION_MB, mag_mb),
MAG_SENSOR("Magnetometer", DEVICE_LOCATION_MINUS_Z, mag_nz),
MAG_SENSOR("Magnetometer", DEVICE_LOCATION_PLUS_Z, mag_pz),
MAG_SENSOR("Magnetometer", DEVICE_LOCATION_MINUS_Y, mag_ny),
MAG_SENSOR("Magnetometer", DEVICE_LOCATION_PLUS_Y, mag_py),
MAG_SENSOR("Magnetometer", DEVICE_LOCATION_MINUS_X, mag_nx),
MAG_SENSOR("Magnetometer", DEVICE_LOCATION_PLUS_X, mag_px),
{ NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, 0 }
};
static void marshal_accel(struct SensorInfo *si, void *dst)
{
struct AccelerometerSensor *accel = (struct AccelerometerSensor*)si->sensor;
struct ADCS3DData *ad = (struct ADCS3DData*)dst;
AccelData accelCache;
if (accel->read) {
accel->read(accel, &accelCache);
ad->x = htonl(accelCache.x_result);
ad->y = htonl(accelCache.y_result);
ad->z = htonl(accelCache.z_result);
}
// ad->x = htonl(accel->accelCache.x_result);
// ad->y = htonl(accel->accelCache.y_result);
// ad->z = htonl(accel->accelCache.z_result);
}
static void marshal_gyro(struct SensorInfo *si, void *dst)
{
struct GyroscopeSensor *gyro = (struct GyroscopeSensor*)si->sensor;
struct ADCS3DData *gd = (struct ADCS3DData*)dst;
GyroData data;
if (gyro->read) {
if (gyro->read(gyro, &data) >= 0) {
gd->x = htonl(data.x);
gd->y = htonl(data.y);
gd->z = htonl(data.z);
}
}
}
static void marshal_mag(struct SensorInfo *si, void *dst)
{
struct MagnetometerSensor *mag = (struct MagnetometerSensor*)si->sensor;
struct ADCS3DData *md = (struct ADCS3DData*)dst;
md->x = htonl(mag->magnetometerCache.x_result);
md->y = htonl(mag->magnetometerCache.y_result);
md->z = htonl(mag->magnetometerCache.z_result);
}
// Read all the sensors and place their values into the status packet
static void marshal_sensors(void *dst)
{
struct SensorInfo *curr;
struct timeval now;
gettimeofday(&now, NULL);
for (curr = sensors; curr->name; curr++) {
if (curr->offset < 0 || !curr->sensor || !curr->marshal)
continue;
if (curr->sensor->update_cached_values)
curr->sensor->update_cached_values(curr->sensor, &now);
curr->marshal(curr, ((char*)dst) + curr->offset);
}
}
void adcs_status(int socket, unsigned char cmd, void * data,
size_t dataLen, struct sockaddr_in * src)
{
struct ADCSReaderStatus status;
struct ADCSState *adcs = gState;
memset(&status, 0, sizeof(status));
marshal_sensors(&status);
PROC_cmd_sockaddr(adcs->proc, CMD_STATUS_RESPONSE, &status,
sizeof(status), src);
}
// Initializes the sensors slowly via event callbacks to minimize impact on
// event loop stalling the process
int initialize_cfged_sensors(void * arg)
{
struct SensorInfo *curr = NULL;
if(arg)
curr = (struct SensorInfo *)arg;
while (curr && curr->name && curr->sensor)
curr++;
if(!curr || !curr->name){
gState->create_evt = EVT_sched_add(PROC_evt(gState->proc),
EVT_ms2tv(SENSOR_RETRY_INTV_MS),
&initialize_cfged_sensors, NULL);
return EVENT_REMOVE;
}
// create the devices
if (!curr->disabled) {
if (!curr->dev_info) {
while ((curr->dev_info = enumerate_devices(curr->dev_info,
curr->type, curr->location)))
if (0 == strcasecmp(curr->name, curr->dev_info->name))
break;
}
if (!curr->dev_info)
curr->disabled = 1;
if (!curr->sensor && curr->dev_info) {
curr->sensor = create_device(curr->dev_info);
}
}
curr++;
gState->create_evt = EVT_sched_add(PROC_evt(gState->proc),
EVT_ms2tv(SENSOR_OPEN_INTV_MS),
initialize_cfged_sensors, (void*)curr);
return EVENT_REMOVE;
}
static void cleanup_sensors(void)
{
struct SensorInfo *curr;
for (curr = sensors; curr->name; curr++) {
if (!curr->sensor || !curr->sensor->close)
continue;
curr->sensor->close(&curr->sensor);
}
}
// Simple SIGINT handler example
int sigint_handler(int signum, void *arg)
{
DBG("SIGINT handler!\n");
EVT_exit_loop(PROC_evt(arg));
return EVENT_KEEP;
}
// Entry point
int main(int argc, char *argv[])
{
struct ADCSState adcs;
// initialize state structure
memset(&adcs, 0, sizeof(adcs));
gState = &adcs;
// Initialize the process
adcs.proc = PROC_init("adcs", WD_ENABLED);
if (!adcs.proc)
return -1;
/* Enable debug interface for development
* Can be removed in 'published' revisions
*/
DBG_INIT();
// Add a signal handler call back for SIGINT signal
PROC_signal(adcs.proc, SIGINT, &sigint_handler, adcs.proc);
initialize_cfged_sensors(sensors);
// Enter the main event loop
EVT_start_loop(PROC_evt(adcs.proc));
// Remove any pending events
if (adcs.create_evt)
EVT_sched_remove(PROC_evt(adcs.proc), adcs.create_evt);
// Close any open sensors
cleanup_sensors();
// Clean up, whenever we exit event loop
PROC_cleanup(adcs.proc);
return 0;
}