3 * @brief 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);
31 * Sets the actuator to the desired mode. No checks are
32 * done to see if setting to the desired mode will conflict with
33 * the current mode - the caller must guarantee this itself.
34 * @param a The actuator whose mode is to be changed
35 * @param mode The mode to be changed to
36 * @param arg An argument specific to the mode to be set.
37 * e.g for CONTROL_START it represents the experiment name.
39 void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
45 char filename[BUFSIZ];
46 const char *experiment_name = (const char*) arg;
48 if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
50 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
53 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
55 Data_Open(&(a->data_file), filename);
57 case CONTROL_RESUME: //Case fallthrough; no break before
60 a->activated = true; // Don't forget this
61 a->control_changed = false;
63 ret = pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
66 Fatal("Failed to create Actuator_Loop for Actuator %d", a->id);
69 Log(LOGDEBUG, "Resuming actuator %d", a->id);
73 case CONTROL_EMERGENCY: //TODO add proper case for emergency
76 Actuator_SetControl(a, NULL);
77 pthread_join(a->thread, NULL); // Wait for thread to exit
79 Log(LOGDEBUG, "Paused actuator %d", a->id);
84 if (a->activated) //May have been paused before
87 Actuator_SetControl(a, NULL);
88 pthread_join(a->thread, NULL); // Wait for thread to exit
90 Data_Close(&(a->data_file)); // Close DataFile
92 Log(LOGDEBUG, "Stopped actuator %d", a->id);
95 Fatal("Unknown control mode: %d", mode);
100 * Sets all actuators to the desired mode.
101 * @see Actuator_SetMode for more information.
102 * @param mode The mode to be changed to
103 * @param arg An argument specific to the mode to be set.
105 void Actuator_SetModeAll(ControlModes mode, void * arg)
107 for (int i = 0; i < NUMACTUATORS; i++)
108 Actuator_SetMode(&g_actuators[i], mode, arg);
112 * Actuator control thread
113 * @param arg - Cast to an Actuator*
114 * @returns NULL to keep pthreads happy
116 void * Actuator_Loop(void * arg)
118 Actuator * a = (Actuator*)(arg);
120 // Loop until stopped
123 pthread_mutex_lock(&(a->mutex));
124 while (!a->control_changed)
126 pthread_cond_wait(&(a->cond), &(a->mutex));
128 a->control_changed = false;
129 pthread_mutex_unlock(&(a->mutex));
133 Actuator_SetValue(a, a->control.value);
138 // Keep pthreads happy
143 * Set an Actuators control variable
144 * @param a - Actuator to control
145 * @param c - Control to set to
147 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
149 pthread_mutex_lock(&(a->mutex));
152 a->control_changed = true;
153 pthread_cond_broadcast(&(a->cond));
154 pthread_mutex_unlock(&(a->mutex));
159 * Set an Actuator value
160 * @param a - The Actuator
161 * @param value - The value to set
163 void Actuator_SetValue(Actuator * a, double value)
167 gettimeofday(&t, NULL);
169 DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
175 FILE *led_handle = NULL; //code reference: http://learnbuildshare.wordpress.com/2013/05/19/beaglebone-black-controlling-user-leds-using-c/
176 const char *led_format = "/sys/class/leds/beaglebone:green:usr%d/brightness";
178 bool turn_on = value;
180 for (int i = 0; i < 4; i++)
182 snprintf(buf, 50, led_format, i);
183 if ((led_handle = fopen(buf, "w")) != NULL)
186 fwrite("1", sizeof(char), 1, led_handle);
188 fwrite("0", sizeof(char), 1, led_handle);
192 Log(LOGDEBUG, "LED fopen failed: %s", strerror(errno));
200 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
203 Data_Save(&(a->data_file), &d, 1);
207 * Helper: Begin Actuator response in a given format
208 * @param context - the FCGIContext
209 * @param format - Format
210 * @param id - ID of Actuator
212 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
218 FCGI_BeginJSON(context, STATUS_OK);
219 FCGI_JSONLong("id", id);
222 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
228 * Helper: End Actuator response in a given format
229 * @param context - the FCGIContext
230 * @param id - ID of the Actuator
231 * @param format - Format
233 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
248 * Handle a request for an Actuator
249 * @param context - FCGI context
250 * @param params - Parameters passed
252 void Actuator_Handler(FCGIContext * context, char * params)
255 gettimeofday(&now, NULL);
256 double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
259 double start_time = 0;
260 double end_time = current_time;
264 FCGIValue values[] = {
265 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
266 {"set", &set, FCGI_DOUBLE_T},
267 {"start_time", &start_time, FCGI_DOUBLE_T},
268 {"end_time", &end_time, FCGI_DOUBLE_T},
269 {"format", &fmt_str, FCGI_STRING_T}
272 // enum to avoid the use of magic numbers
281 // Fill values appropriately
282 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
284 // Error occured; FCGI_RejectJSON already called
288 // Get the Actuator identified
290 if (id < 0 || id >= NUMACTUATORS)
292 FCGI_RejectJSON(context, "Invalid Actuator id");
298 DataFormat format = Data_GetFormat(&(values[FORMAT]));
301 Actuator_BeginResponse(context, id, format);
304 if (FCGI_RECEIVED(values[SET].flags))
307 FCGI_JSONDouble("set", set);
312 Actuator_SetControl(a, &c);
316 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
319 Actuator_EndResponse(context, id, format);