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));
57 void Actuator_Pause(Actuator *a)
62 Actuator_SetControl(a, NULL);
63 pthread_join(a->thread, NULL); // Wait for thread to exit
67 void Actuator_Resume(Actuator *a)
72 pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
78 * @param s - The Actuator to stop
80 void Actuator_Stop(Actuator * a)
84 Data_Close(&(a->data_file)); // Close DataFile
88 void Actuator_PauseAll()
90 for (int i = 0; i < NUMACTUATORS; ++i)
91 Actuator_Pause(g_actuators+i);
94 void Actuator_ResumeAll()
96 for (int i = 0; i < NUMACTUATORS; ++i)
97 Actuator_Resume(g_actuators+i);
103 void Actuator_StopAll()
105 for (int i = 0; i < NUMACTUATORS; ++i)
106 Actuator_Stop(g_actuators+i);
110 * Start all Actuators
112 void Actuator_StartAll(const char * experiment_name)
114 for (int i = 0; i < NUMACTUATORS; ++i)
115 Actuator_Start(g_actuators+i, experiment_name);
119 * Actuator control thread
120 * @param arg - Cast to an Actuator*
121 * @returns NULL to keep pthreads happy
123 void * Actuator_Loop(void * arg)
125 Actuator * a = (Actuator*)(arg);
127 // Loop until stopped
130 pthread_mutex_lock(&(a->mutex));
131 while (!a->control_changed)
133 pthread_cond_wait(&(a->cond), &(a->mutex));
135 a->control_changed = false;
136 pthread_mutex_unlock(&(a->mutex));
140 Actuator_SetValue(a, a->control.value);
145 // Keep pthreads happy
150 * Set an Actuators control variable
151 * @param a - Actuator to control
152 * @param c - Control to set to
154 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
156 pthread_mutex_lock(&(a->mutex));
159 a->control_changed = true;
160 pthread_cond_broadcast(&(a->cond));
161 pthread_mutex_unlock(&(a->mutex));
166 * Set an Actuator value
167 * @param a - The Actuator
168 * @param value - The value to set
170 void Actuator_SetValue(Actuator * a, double value)
174 gettimeofday(&t, NULL);
176 DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
186 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
189 Data_Save(&(a->data_file), &d, 1);
193 * Helper: Begin Actuator response in a given format
194 * @param context - the FCGIContext
195 * @param format - Format
196 * @param id - ID of Actuator
198 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
204 FCGI_BeginJSON(context, STATUS_OK);
205 FCGI_JSONLong("id", id);
208 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
214 * Helper: End Actuator response in a given format
215 * @param context - the FCGIContext
216 * @param id - ID of the Actuator
217 * @param format - Format
219 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
236 * Handle a request for an Actuator
237 * @param context - FCGI context
238 * @param params - Parameters passed
240 void Actuator_Handler(FCGIContext * context, char * params)
243 gettimeofday(&now, NULL);
244 double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
247 double start_time = 0;
248 double end_time = current_time;
252 FCGIValue values[] = {
253 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
254 {"set", &set, FCGI_DOUBLE_T},
255 {"start_time", &start_time, FCGI_DOUBLE_T},
256 {"end_time", &end_time, FCGI_DOUBLE_T},
257 {"format", &fmt_str, FCGI_STRING_T}
260 // enum to avoid the use of magic numbers
269 // Fill values appropriately
270 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
272 // Error occured; FCGI_RejectJSON already called
276 // Get the Actuator identified
278 if (id < 0 || id >= NUMACTUATORS)
280 FCGI_RejectJSON(context, "Invalid Actuator id");
286 DataFormat format = Data_GetFormat(&(values[FORMAT]));
289 Actuator_BeginResponse(context, id, format);
292 if (FCGI_RECEIVED(values[SET].flags))
295 FCGI_JSONDouble("set", set);
300 Actuator_SetControl(a, &c);
304 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
307 Actuator_EndResponse(context, id, format);