3 * @purpose Implementation of Actuator related functionality
9 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
10 static Actuator g_actuators[NUMACTUATORS];
12 /** Human readable names for the Actuators **/
13 const char * g_actuator_names[NUMACTUATORS] = {
14 "actuator_test0", "actuator_test1"
18 * One off initialisation of *all* Actuators
22 for (int i = 0; i < NUMACTUATORS; ++i)
24 g_actuators[i].id = i;
25 Data_Init(&(g_actuators[i].data_file));
26 pthread_mutex_init(&(g_actuators[i].mutex), NULL);
32 * @param a - The Actuator to start
33 * @param experiment_name - Prepended to DataFile filename
35 void Actuator_Start(Actuator * a, const char * experiment_name)
38 char filename[BUFSIZ];
39 if (sprintf(filename, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
41 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
44 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
46 Data_Open(&(a->data_file), filename);
48 a->activated = true; // Don't forget this
50 a->control_changed = false;
53 pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
58 * @param s - The Actuator to stop
60 void Actuator_Stop(Actuator * a)
64 Actuator_SetControl(a, NULL);
65 pthread_join(a->thread, NULL); // Wait for thread to exit
66 Data_Close(&(a->data_file)); // Close DataFile
73 void Actuator_StopAll()
75 for (int i = 0; i < NUMACTUATORS; ++i)
76 Actuator_Stop(g_actuators+i);
82 void Actuator_StartAll(const char * experiment_name)
84 for (int i = 0; i < NUMACTUATORS; ++i)
85 Actuator_Start(g_actuators+i, experiment_name);
89 * Actuator control thread
90 * @param arg - Cast to an Actuator*
91 * @returns NULL to keep pthreads happy
93 void * Actuator_Loop(void * arg)
95 Actuator * a = (Actuator*)(arg);
100 pthread_mutex_lock(&(a->mutex));
101 while (!a->control_changed)
103 pthread_cond_wait(&(a->cond), &(a->mutex));
105 a->control_changed = false;
106 pthread_mutex_unlock(&(a->mutex));
110 Actuator_SetValue(a, a->control.value);
115 // Keep pthreads happy
120 * Set an Actuators control variable
121 * @param a - Actuator to control
122 * @param c - Control to set to
124 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
126 pthread_mutex_lock(&(a->mutex));
129 a->control_changed = true;
130 pthread_cond_broadcast(&(a->cond));
131 pthread_mutex_unlock(&(a->mutex));
136 * Set an Actuator value
137 * @param a - The Actuator
138 * @param value - The value to set
140 void Actuator_SetValue(Actuator * a, double value)
144 gettimeofday(&t, NULL);
146 DataPoint d = {TIMEVAL_DIFF(t, g_options.start_time), value};
156 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
159 Data_Save(&(a->data_file), &d, 1);
163 * Helper: Begin Actuator response in a given format
164 * @param context - the FCGIContext
165 * @param format - Format
166 * @param id - ID of Actuator
168 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
174 FCGI_BeginJSON(context, STATUS_OK);
175 FCGI_JSONLong("id", id);
178 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
184 * Helper: End Actuator response in a given format
185 * @param context - the FCGIContext
186 * @param id - ID of the Actuator
187 * @param format - Format
189 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
206 * Handle a request for an Actuator
207 * @param context - FCGI context
208 * @param params - Parameters passed
210 void Actuator_Handler(FCGIContext * context, char * params)
213 gettimeofday(&now, NULL);
214 double current_time = TIMEVAL_DIFF(now, g_options.start_time);
217 double start_time = 0;
218 double end_time = current_time;
222 FCGIValue values[] = {
223 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
224 {"set", &set, FCGI_DOUBLE_T},
225 {"start_time", &start_time, FCGI_DOUBLE_T},
226 {"end_time", &end_time, FCGI_DOUBLE_T},
227 {"format", &fmt_str, FCGI_STRING_T}
230 // enum to avoid the use of magic numbers
239 // Fill values appropriately
240 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
242 // Error occured; FCGI_RejectJSON already called
246 // Get the Actuator identified
248 if (id < 0 || id >= NUMACTUATORS)
250 FCGI_RejectJSON(context, "Invalid Actuator id");
256 DataFormat format = Data_GetFormat(&(values[FORMAT]));
259 Actuator_BeginResponse(context, id, format);
262 if (FCGI_RECEIVED(values[SET].flags))
265 FCGI_JSONDouble("set", set);
270 Actuator_SetControl(a, &c);
274 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
277 Actuator_EndResponse(context, id, format);