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;
49 if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
51 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
54 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
56 Data_Open(&(a->data_file), filename);
58 a->activated = true; // Don't forget this
59 a->allow_actuation = true;
61 a->control_changed = false;
64 ret = pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
67 Fatal("Failed to create Actuator_Loop for Actuator %d", a->id);
72 case CONTROL_EMERGENCY: //TODO add proper case for emergency
74 a->allow_actuation = false;
77 a->allow_actuation = true;
80 a->allow_actuation = false;
82 Actuator_SetControl(a, NULL);
83 pthread_join(a->thread, NULL); // Wait for thread to exit
84 Data_Close(&(a->data_file)); // Close DataFile
87 Fatal("Unknown control mode: %d", mode);
92 * Sets all actuators to the desired mode.
93 * @see Actuator_SetMode for more information.
94 * @param mode The mode to be changed to
95 * @param arg An argument specific to the mode to be set.
97 void Actuator_SetModeAll(ControlModes mode, void * arg)
99 for (int i = 0; i < NUMACTUATORS; i++)
100 Actuator_SetMode(&g_actuators[i], mode, arg);
104 * Actuator control thread
105 * @param arg - Cast to an Actuator*
106 * @returns NULL to keep pthreads happy
108 void * Actuator_Loop(void * arg)
110 Actuator * a = (Actuator*)(arg);
112 // Loop until stopped
115 pthread_mutex_lock(&(a->mutex));
116 while (!a->control_changed)
118 pthread_cond_wait(&(a->cond), &(a->mutex));
120 a->control_changed = false;
121 pthread_mutex_unlock(&(a->mutex));
124 else if (!a->allow_actuation)
127 Actuator_SetValue(a, a->control.value);
132 // Keep pthreads happy
137 * Set an Actuators control variable
138 * @param a - Actuator to control
139 * @param c - Control to set to
141 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
143 pthread_mutex_lock(&(a->mutex));
146 a->control_changed = true;
147 pthread_cond_broadcast(&(a->cond));
148 pthread_mutex_unlock(&(a->mutex));
153 * Set an Actuator value
154 * @param a - The Actuator
155 * @param value - The value to set
157 void Actuator_SetValue(Actuator * a, double value)
161 gettimeofday(&t, NULL);
163 DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
169 FILE *led_handle = NULL; //code reference: http://learnbuildshare.wordpress.com/2013/05/19/beaglebone-black-controlling-user-leds-using-c/
170 const char *led_format = "/sys/class/leds/beaglebone:green:usr%d/brightness";
172 bool turn_on = value;
174 for (int i = 0; i < 4; i++)
176 snprintf(buf, 50, led_format, i);
177 if ((led_handle = fopen(buf, "w")) != NULL)
180 fwrite("1", sizeof(char), 1, led_handle);
182 fwrite("0", sizeof(char), 1, led_handle);
186 Log(LOGDEBUG, "LED fopen failed: %s", strerror(errno));
194 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
197 Data_Save(&(a->data_file), &d, 1);
201 * Helper: Begin Actuator response in a given format
202 * @param context - the FCGIContext
203 * @param format - Format
204 * @param id - ID of Actuator
206 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
212 FCGI_BeginJSON(context, STATUS_OK);
213 FCGI_JSONLong("id", id);
216 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
222 * Helper: End Actuator response in a given format
223 * @param context - the FCGIContext
224 * @param id - ID of the Actuator
225 * @param format - Format
227 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
244 * Handle a request for an Actuator
245 * @param context - FCGI context
246 * @param params - Parameters passed
248 void Actuator_Handler(FCGIContext * context, char * params)
251 gettimeofday(&now, NULL);
252 double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
255 double start_time = 0;
256 double end_time = current_time;
260 FCGIValue values[] = {
261 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
262 {"set", &set, FCGI_DOUBLE_T},
263 {"start_time", &start_time, FCGI_DOUBLE_T},
264 {"end_time", &end_time, FCGI_DOUBLE_T},
265 {"format", &fmt_str, FCGI_STRING_T}
268 // enum to avoid the use of magic numbers
277 // Fill values appropriately
278 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
280 // Error occured; FCGI_RejectJSON already called
284 // Get the Actuator identified
286 if (id < 0 || id >= NUMACTUATORS)
288 FCGI_RejectJSON(context, "Invalid Actuator id");
294 DataFormat format = Data_GetFormat(&(values[FORMAT]));
297 Actuator_BeginResponse(context, id, format);
300 if (FCGI_RECEIVED(values[SET].flags))
303 FCGI_JSONDouble("set", set);
308 Actuator_SetControl(a, &c);
312 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
315 Actuator_EndResponse(context, id, format);