3 * @brief Implementation of Actuator related functionality
8 // Files containing GPIO and PWM definitions
11 /** Number of actuators **/
12 int g_num_actuators = 0;
14 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
15 static Actuator g_actuators[ACTUATORS_MAX];
17 * Add and initialise an Actuator
18 * @param name - Human readable name of the actuator
19 * @param user_id - Caller specified ID to be associated with this actuator
20 * @param set - Function to call whenever the actuator should be set
21 * @param init - Function to call to initialise the actuator (may be NULL)
22 * @param cleanup - Function to call to deinitialise the actuator (may be NULL)
23 * @param sanity - Function to call to check that a user specified value is sane (may be NULL)
24 * @param initial_value - The initial value to set the actuator to
25 * @returns Number of actuators added so far
27 int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn cleanup, SanityFn sanity, double initial_value)
29 if (++g_num_actuators > ACTUATORS_MAX)
31 Fatal("Too many sensors; Increase ACTUATORS_MAX from %d in actuator.h and recompile", ACTUATORS_MAX);
33 Actuator * a = &(g_actuators[g_num_actuators-1]);
34 a->id = g_num_actuators-1;
36 Data_Init(&(a->data_file));
38 a->set = set; // Set read function
39 a->init = init; // Set init function
43 pthread_mutex_init(&(a->mutex), NULL);
47 if (!init(name, user_id))
48 Fatal("Couldn't initialise actuator %s", name);
51 Actuator_SetValue(a, initial_value, false);
53 return g_num_actuators;
58 * Initialisation of *all* Actuators
60 #include "actuators/pregulator.h"
61 #include "actuators/relays.h"
64 //Actuator_Add("ledtest",0, Ledtest_Set, NULL,NULL,NULL);
65 //Actuator_Add("filetest", 0, Filetest_Set, Filetest_Init, Filetest_Cleanup, Filetest_Sanity, 0);
66 Actuator_Add("pregulator", 0, Pregulator_Set, Pregulator_Init, Pregulator_Cleanup, Pregulator_Sanity, 0);
67 Actuator_Add("can_select", RELAY_CANSELECT, Relay_Set, Relay_Init, Relay_Cleanup, Relay_Sanity, 0);
68 Actuator_Add("can_enable", RELAY_CANENABLE, Relay_Set, Relay_Init, Relay_Cleanup, Relay_Sanity, 0);
69 Actuator_Add("main_pressure", RELAY_MAIN, Relay_Set, Relay_Init, Relay_Cleanup, Relay_Sanity, 1);
73 * Deinitialise actuators
75 void Actuator_Cleanup()
77 for (int i = 0; i < g_num_actuators; ++i)
79 Actuator * a = g_actuators+i;
80 if (a->cleanup != NULL)
81 a->cleanup(a->user_id);
88 * Sets the actuator to the desired mode. No checks are
89 * done to see if setting to the desired mode will conflict with
90 * the current mode - the caller must guarantee this itself.
91 * @param a The actuator whose mode is to be changed
92 * @param mode The mode to be changed to
93 * @param arg An argument specific to the mode to be set.
94 * e.g for CONTROL_START it represents the experiment name.
96 void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
103 char filename[BUFSIZ];
104 const char *experiment_path = (const char*) arg;
107 ret = snprintf(filename, BUFSIZ, "%s/actuator_%d", experiment_path, a->id);
111 Fatal("Experiment path \"%s\" too long (%d, limit %d)",
112 experiment_path, ret, BUFSIZ);
115 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
117 Data_Open(&(a->data_file), filename);
119 case CONTROL_RESUME: //Case fallthrough; no break before
122 a->activated = true; // Don't forget this
123 a->control_changed = false;
125 ret = pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
128 Fatal("Failed to create Actuator_Loop for Actuator %d", a->id);
131 Log(LOGDEBUG, "Resuming actuator %d", a->id);
135 case CONTROL_EMERGENCY: //TODO add proper case for emergency
137 a->activated = false;
138 Actuator_SetControl(a, NULL);
139 pthread_join(a->thread, NULL); // Wait for thread to exit
141 Log(LOGDEBUG, "Paused actuator %d", a->id);
146 if (a->activated) //May have been paused before
148 a->activated = false;
149 Actuator_SetControl(a, NULL);
150 pthread_join(a->thread, NULL); // Wait for thread to exit
152 Data_Close(&(a->data_file)); // Close DataFile
154 Log(LOGDEBUG, "Stopped actuator %d", a->id);
157 Fatal("Unknown control mode: %d", mode);
162 * Sets all actuators to the desired mode.
163 * @see Actuator_SetMode for more information.
164 * @param mode The mode to be changed to
165 * @param arg An argument specific to the mode to be set.
167 void Actuator_SetModeAll(ControlModes mode, void * arg)
169 if (mode == CONTROL_START)
171 for (int i = 0; i < g_num_actuators; i++)
172 Actuator_SetMode(&g_actuators[i], mode, arg);
173 if (mode == CONTROL_STOP)
178 * Actuator control thread
179 * @param arg - Cast to an Actuator*
180 * @returns NULL to keep pthreads happy
182 void * Actuator_Loop(void * arg)
184 Actuator * a = (Actuator*)(arg);
186 // Loop until stopped
189 pthread_mutex_lock(&(a->mutex));
190 while (!a->control_changed)
192 pthread_cond_wait(&(a->cond), &(a->mutex));
194 a->control_changed = false;
195 pthread_mutex_unlock(&(a->mutex));
199 Actuator_SetValue(a, a->control.start, true);
200 // Currently does discrete steps after specified time intervals
202 struct timespec wait;
203 DOUBLE_TO_TIMEVAL(a->control.stepwait, &wait);
204 while (!a->control_changed && a->control.steps > 0 && a->activated)
206 clock_nanosleep(CLOCK_MONOTONIC, 0, &wait, NULL);
207 a->control.start += a->control.stepsize;
208 Actuator_SetValue(a, a->control.start, true);
212 if (a->control_changed)
214 clock_nanosleep(CLOCK_MONOTONIC, 0, &wait, NULL);
217 // Note that although this loop has a sleep in it which would seem to make it hard to enforce urgent shutdowns,
218 // You can call the Actuator's cleanup function immediately (and this loop should later just exit)
219 // tl;dr This function isn't/shouldn't be responsible for the emergency Actuator stuff
220 // (That should be handled by the Fatal function... at some point)
225 // Keep pthreads happy
230 * Set an Actuators control variable
231 * @param a - Actuator to control
232 * @param c - Control to set to
234 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
236 pthread_mutex_lock(&(a->mutex));
239 a->control_changed = true;
240 pthread_cond_broadcast(&(a->cond));
241 pthread_mutex_unlock(&(a->mutex));
246 * Set an Actuator value
247 * @param a - The Actuator
248 * @param value - The value to set
249 * @param record - Whether or not to record the value to the Actuator's DataFile.
251 void Actuator_SetValue(Actuator * a, double value, bool record)
253 if (a->sanity != NULL && !a->sanity(a->user_id, value))
256 Log(LOGERR,"Insane value %lf for actuator %s", value, a->name);
259 if (!(a->set(a->user_id, value)))
261 Fatal("Failed to set actuator %s to %lf", a->name, value);
266 clock_gettime(CLOCK_MONOTONIC, &t);
267 DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), a->last_setting.value};
268 // Record value change
271 d.time_stamp -= 1e-6;
272 Data_Save(&(a->data_file), &d, 1);
274 d.time_stamp += 1e-6;
275 Data_Save(&(a->data_file), &d, 1);
281 * Helper: Begin Actuator response in a given format
282 * @param context - the FCGIContext
283 * @param a - the actuator to begin a response for
284 * @param format - Format
286 void Actuator_BeginResponse(FCGIContext * context, Actuator * a, DataFormat format)
292 FCGI_BeginJSON(context, STATUS_OK);
293 FCGI_JSONLong("id", a->id);
294 FCGI_JSONLong("user_id", a->user_id); //TODO: Don't need to show this?
295 FCGI_JSONPair("name", a->name);
298 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
304 * Helper: End Actuator response in a given format
305 * @param context - the FCGIContext
306 * @param a - the actuator to end a response for
307 * @param format - Format
309 void Actuator_EndResponse(FCGIContext * context, Actuator * a, DataFormat format)
324 * Handle a request for an Actuator
325 * @param context - FCGI context
326 * @param params - Parameters passed
328 void Actuator_Handler(FCGIContext * context, char * params)
331 clock_gettime(CLOCK_MONOTONIC, &now);
332 double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
336 double start_time = 0;
337 double end_time = current_time;
341 FCGIValue values[] = {
342 {"id", &id, FCGI_INT_T},
343 {"name", &name, FCGI_STRING_T},
344 {"set", &set, FCGI_STRING_T},
345 {"start_time", &start_time, FCGI_DOUBLE_T},
346 {"end_time", &end_time, FCGI_DOUBLE_T},
347 {"format", &fmt_str, FCGI_STRING_T}
350 // enum to avoid the use of magic numbers
360 // Fill values appropriately
361 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
363 // Error occured; FCGI_RejectJSON already called
367 // Get the Actuator identified
370 if (FCGI_RECEIVED(values[NAME].flags))
372 if (FCGI_RECEIVED(values[ID].flags))
374 FCGI_RejectJSON(context, "Can't supply both id and name");
377 a = Actuator_Identify(name);
380 FCGI_RejectJSON(context, "Unknown actuator name");
385 else if (!FCGI_RECEIVED(values[ID].flags))
387 FCGI_RejectJSON(context, "No id or name supplied");
390 else if (id < 0 || id >= g_num_actuators)
392 FCGI_RejectJSON(context, "Invalid Actuator id");
397 a = &(g_actuators[id]);
401 DataFormat format = Data_GetFormat(&(values[FORMAT]));
406 if (FCGI_RECEIVED(values[SET].flags))
410 ActuatorControl c = {0.0, 0.0, 0.0, 0}; // Need to set default values (since we don't require them all)
411 // sscanf returns the number of fields successfully read...
412 int n = sscanf(set, "%lf_%lf_%lf_%d", &(c.start), &(c.stepwait), &(c.stepsize), &(c.steps)); // Set provided values in order
415 // If the user doesn't provide all 4 values, the Actuator will get set *once* using the first of the provided values
416 // (see Actuator_Loop)
417 // Not really a problem if n = 1, but maybe generate a warning for 2 <= n < 4 ?
418 Log(LOGDEBUG, "Only provided %d values (expect %d) for Actuator setting", n, 4);
421 if (c.stepwait < 0 || c.steps < 0 || (a->sanity != NULL && !a->sanity(a->user_id, c.start)))
423 FCGI_RejectJSON(context, "Bad Actuator setting");
426 Actuator_SetControl(a, &c);
430 Actuator_BeginResponse(context, a, format);
432 FCGI_JSONPair("set", set);
435 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
438 Actuator_EndResponse(context, a, format);
442 * Get the name of an Actuator given its id
443 * @param id - ID of the actuator
444 * @returns The Actuator's name
446 const char * Actuator_GetName(int id)
448 return g_actuators[id].name;
452 * Identify an Actuator from its name string
453 * @param name - The name of the Actuator
456 Actuator * Actuator_Identify(const char * name)
458 for (int i = 0; i < g_num_actuators; ++i)
460 if (strcmp(g_actuators[i].name, name) == 0)
461 return &(g_actuators[i]);
467 * Returns the last DataPoint that is currently available.
468 * @param id - The actuator ID for which to retrieve data from
469 * @return The last DataPoint
471 DataPoint Actuator_LastData(int id)
473 Actuator * a = &(g_actuators[id]);
474 return a->last_setting;