3 * @brief Implementation of Actuator related functionality
8 // Files containing GPIO and PWM definitions
12 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
13 static Actuator g_actuators[NUMACTUATORS];
15 /** Human readable names for the Actuators **/
16 const char * g_actuator_names[NUMACTUATORS] = {
17 "actuator_test0", "actuator_test1", "actuator_test2"
21 * One off initialisation of *all* Actuators
25 for (int i = 0; i < NUMACTUATORS; ++i)
27 g_actuators[i].id = i;
28 Data_Init(&(g_actuators[i].data_file));
29 pthread_mutex_init(&(g_actuators[i].mutex), NULL);
34 * Sets the actuator to the desired mode. No checks are
35 * done to see if setting to the desired mode will conflict with
36 * the current mode - the caller must guarantee this itself.
37 * @param a The actuator whose mode is to be changed
38 * @param mode The mode to be changed to
39 * @param arg An argument specific to the mode to be set.
40 * e.g for CONTROL_START it represents the experiment name.
42 void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
48 char filename[BUFSIZ];
49 const char *experiment_name = (const char*) arg;
52 if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
54 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
57 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
59 Data_Open(&(a->data_file), filename);
61 a->activated = true; // Don't forget this
62 a->allow_actuation = true;
64 a->control_changed = false;
67 ret = pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
70 Fatal("Failed to create Actuator_Loop for Actuator %d", a->id);
75 case CONTROL_EMERGENCY: //TODO add proper case for emergency
77 a->allow_actuation = false;
80 a->allow_actuation = true;
83 a->allow_actuation = false;
85 Actuator_SetControl(a, NULL);
86 pthread_join(a->thread, NULL); // Wait for thread to exit
87 Data_Close(&(a->data_file)); // Close DataFile
90 Fatal("Unknown control mode: %d", mode);
95 * Sets all actuators to the desired mode.
96 * @see Actuator_SetMode for more information.
97 * @param mode The mode to be changed to
98 * @param arg An argument specific to the mode to be set.
100 void Actuator_SetModeAll(ControlModes mode, void * arg)
102 for (int i = 0; i < NUMACTUATORS; i++)
103 Actuator_SetMode(&g_actuators[i], mode, arg);
107 * Actuator control thread
108 * @param arg - Cast to an Actuator*
109 * @returns NULL to keep pthreads happy
111 void * Actuator_Loop(void * arg)
113 Actuator * a = (Actuator*)(arg);
115 // Loop until stopped
118 pthread_mutex_lock(&(a->mutex));
119 while (!a->control_changed)
121 pthread_cond_wait(&(a->cond), &(a->mutex));
123 a->control_changed = false;
124 pthread_mutex_unlock(&(a->mutex));
127 else if (!a->allow_actuation)
130 Actuator_SetValue(a, a->control.value);
135 // Keep pthreads happy
140 * Set an Actuators control variable
141 * @param a - Actuator to control
142 * @param c - Control to set to
144 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
146 pthread_mutex_lock(&(a->mutex));
149 a->control_changed = true;
150 pthread_cond_broadcast(&(a->cond));
151 pthread_mutex_unlock(&(a->mutex));
156 * Set an Actuator value
157 * @param a - The Actuator
158 * @param value - The value to set
160 void Actuator_SetValue(Actuator * a, double value)
164 gettimeofday(&t, NULL);
166 DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
172 // Onboard LEDs test actuator
173 FILE *led_handle = NULL; //code reference: http://learnbuildshare.wordpress.com/2013/05/19/beaglebone-black-controlling-user-leds-using-c/
174 const char *led_format = "/sys/class/leds/beaglebone:green:usr%d/brightness";
176 bool turn_on = value;
178 for (int i = 0; i < 4; i++)
180 snprintf(buf, 50, led_format, i);
181 if ((led_handle = fopen(buf, "w")) != NULL)
184 fwrite("1", sizeof(char), 1, led_handle);
186 fwrite("0", sizeof(char), 1, led_handle);
190 Log(LOGDEBUG, "LED fopen failed: %s", strerror(errno));
195 // GPIO pin digital actuator
197 // Quick actuator function for testing pins
198 // GPIOPin can be passed as argument, but is just defined here for testing purposes
199 // Modify this to only export on first run, only unexport on shutdown
207 // PWM analogue actuator (currently generates one PWM signal with first PWM module)
210 if (pwminit == 0) { // If inactive, start the pwm module
215 pwm_set_period(FREQ); // Frequency is 50Hz defined in pwm header file
217 if(value >= 0 && value <= 1000) {
218 double duty = value/1000 * 100; // Convert pressure to duty percentage
219 pwm_set_duty((int)duty); // Set duty percentage for actuator (0-100%)
226 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
229 Data_Save(&(a->data_file), &d, 1);
233 * Helper: Begin Actuator response in a given format
234 * @param context - the FCGIContext
235 * @param format - Format
236 * @param id - ID of Actuator
238 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
244 FCGI_BeginJSON(context, STATUS_OK);
245 FCGI_JSONLong("id", id);
248 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
254 * Helper: End Actuator response in a given format
255 * @param context - the FCGIContext
256 * @param id - ID of the Actuator
257 * @param format - Format
259 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
276 * Handle a request for an Actuator
277 * @param context - FCGI context
278 * @param params - Parameters passed
280 void Actuator_Handler(FCGIContext * context, char * params)
283 gettimeofday(&now, NULL);
284 double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
287 double start_time = 0;
288 double end_time = current_time;
292 FCGIValue values[] = {
293 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
294 {"set", &set, FCGI_DOUBLE_T},
295 {"start_time", &start_time, FCGI_DOUBLE_T},
296 {"end_time", &end_time, FCGI_DOUBLE_T},
297 {"format", &fmt_str, FCGI_STRING_T}
300 // enum to avoid the use of magic numbers
309 // Fill values appropriately
310 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
312 // Error occured; FCGI_RejectJSON already called
316 // Get the Actuator identified
318 if (id < 0 || id >= NUMACTUATORS)
320 FCGI_RejectJSON(context, "Invalid Actuator id");
326 DataFormat format = Data_GetFormat(&(values[FORMAT]));
329 Actuator_BeginResponse(context, id, format);
332 if (FCGI_RECEIVED(values[SET].flags))
335 FCGI_JSONDouble("set", set);
340 Actuator_SetControl(a, &c);
344 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
347 Actuator_EndResponse(context, id, format);