3 * @purpose Implementation of Actuator related functionality
10 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
11 static Actuator g_actuators[NUMACTUATORS];
13 /** Human readable names for the Actuators **/
14 const char * g_actuator_names[NUMACTUATORS] = {
15 "actuator_test0", "actuator_test1"
19 * One off initialisation of *all* Actuators
23 for (int i = 0; i < NUMACTUATORS; ++i)
25 g_actuators[i].id = i;
26 Data_Init(&(g_actuators[i].data_file));
27 pthread_mutex_init(&(g_actuators[i].mutex), NULL);
33 * @param a - The Actuator to start
34 * @param experiment_name - Prepended to DataFile filename
36 void Actuator_Start(Actuator * a, const char * experiment_name)
39 char filename[BUFSIZ];
40 if (sprintf(filename, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
42 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
45 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
47 Data_Open(&(a->data_file), filename);
49 a->activated = true; // Don't forget this
51 a->control_changed = false;
54 pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
59 * @param s - The Actuator to stop
61 void Actuator_Stop(Actuator * a)
65 Actuator_SetControl(a, NULL);
66 pthread_join(a->thread, NULL); // Wait for thread to exit
67 Data_Close(&(a->data_file)); // Close DataFile
74 void Actuator_StopAll()
76 for (int i = 0; i < NUMACTUATORS; ++i)
77 Actuator_Stop(g_actuators+i);
83 void Actuator_StartAll(const char * experiment_name)
85 for (int i = 0; i < NUMACTUATORS; ++i)
86 Actuator_Start(g_actuators+i, experiment_name);
90 * Actuator control thread
91 * @param arg - Cast to an Actuator*
92 * @returns NULL to keep pthreads happy
94 void * Actuator_Loop(void * arg)
96 Actuator * a = (Actuator*)(arg);
101 pthread_mutex_lock(&(a->mutex));
102 while (!a->control_changed)
104 pthread_cond_wait(&(a->cond), &(a->mutex));
106 a->control_changed = false;
107 pthread_mutex_unlock(&(a->mutex));
111 Actuator_SetValue(a, a->control.value);
116 // Keep pthreads happy
121 * Set an Actuators control variable
122 * @param a - Actuator to control
123 * @param c - Control to set to
125 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
127 pthread_mutex_lock(&(a->mutex));
130 a->control_changed = true;
131 pthread_cond_broadcast(&(a->cond));
132 pthread_mutex_unlock(&(a->mutex));
137 * Set an Actuator value
138 * @param a - The Actuator
139 * @param value - The value to set
141 void Actuator_SetValue(Actuator * a, double value)
145 gettimeofday(&t, NULL);
147 DataPoint d = {TIMEVAL_DIFF(t, g_options.start_time), value};
157 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
160 Data_Save(&(a->data_file), &d, 1);
164 * Helper: Begin Actuator response in a given format
165 * @param context - the FCGIContext
166 * @param format - Format
167 * @param id - ID of Actuator
169 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
175 FCGI_BeginJSON(context, STATUS_OK);
176 FCGI_JSONLong("id", id);
179 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
185 * Helper: End Actuator response in a given format
186 * @param context - the FCGIContext
187 * @param id - ID of the Actuator
188 * @param format - Format
190 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
207 * Handle a request for an Actuator
208 * @param context - FCGI context
209 * @param params - Parameters passed
211 void Actuator_Handler(FCGIContext * context, char * params)
214 gettimeofday(&now, NULL);
215 double current_time = TIMEVAL_DIFF(now, g_options.start_time);
218 double start_time = 0;
219 double end_time = current_time;
223 FCGIValue values[] = {
224 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
225 {"set", &set, FCGI_DOUBLE_T},
226 {"start_time", &start_time, FCGI_DOUBLE_T},
227 {"end_time", &end_time, FCGI_DOUBLE_T},
228 {"format", &fmt_str, FCGI_STRING_T}
231 // enum to avoid the use of magic numbers
240 // Fill values appropriately
241 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
243 // Error occured; FCGI_RejectJSON already called
247 // Get the Actuator identified
249 if (id < 0 || id >= NUMACTUATORS)
251 FCGI_RejectJSON(context, "Invalid Actuator id");
257 DataFormat format = Data_GetFormat(&(values[FORMAT]));
262 Actuator_BeginResponse(context, id, format);
265 if (FCGI_RECEIVED(values[SET].flags))
268 FCGI_JSONDouble("set", set);
273 Actuator_SetControl(a, &c);
277 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
280 Actuator_EndResponse(context, id, format);
286 FCGI_RejectJSON(context, "Experiment is not running.");