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", "gpio1_16", "EHRPWM0A_duty@60Hz"
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);
32 // Initialise pins used
33 GPIO_Export(GPIO1_16);
39 * Sets the actuator to the desired mode. No checks are
40 * done to see if setting to the desired mode will conflict with
41 * the current mode - the caller must guarantee this itself.
42 * @param a The actuator whose mode is to be changed
43 * @param mode The mode to be changed to
44 * @param arg An argument specific to the mode to be set.
45 * e.g for CONTROL_START it represents the experiment name.
47 void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
53 char filename[BUFSIZ];
54 const char *experiment_name = (const char*) arg;
57 if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
59 Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
62 Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
64 Data_Open(&(a->data_file), filename);
66 a->activated = true; // Don't forget this
67 a->allow_actuation = true;
69 a->control_changed = false;
72 ret = pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
75 Fatal("Failed to create Actuator_Loop for Actuator %d", a->id);
80 case CONTROL_EMERGENCY: //TODO add proper case for emergency
82 a->allow_actuation = false;
85 a->allow_actuation = true;
88 a->allow_actuation = false;
90 Actuator_SetControl(a, NULL);
91 pthread_join(a->thread, NULL); // Wait for thread to exit
92 Data_Close(&(a->data_file)); // Close DataFile
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));
132 else if (!a->allow_actuation)
135 Actuator_SetValue(a, a->control.value);
140 // Keep pthreads happy
145 * Set an Actuators control variable
146 * @param a - Actuator to control
147 * @param c - Control to set to
149 void Actuator_SetControl(Actuator * a, ActuatorControl * c)
151 pthread_mutex_lock(&(a->mutex));
154 a->control_changed = true;
155 pthread_cond_broadcast(&(a->cond));
156 pthread_mutex_unlock(&(a->mutex));
161 * Set an Actuator value
162 * @param a - The Actuator
163 * @param value - The value to set
165 void Actuator_SetValue(Actuator * a, double value)
169 gettimeofday(&t, NULL);
171 DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
177 // Onboard LEDs test actuator
178 FILE *led_handle = NULL; //code reference: http://learnbuildshare.wordpress.com/2013/05/19/beaglebone-black-controlling-user-leds-using-c/
179 const char *led_format = "/sys/class/leds/beaglebone:green:usr%d/brightness";
181 bool turn_on = value;
183 for (int i = 0; i < 4; i++)
185 snprintf(buf, 50, led_format, i);
186 if ((led_handle = fopen(buf, "w")) != NULL)
189 fwrite("1", sizeof(char), 1, led_handle);
191 fwrite("0", sizeof(char), 1, led_handle);
195 Log(LOGDEBUG, "LED fopen failed: %s", strerror(errno));
200 GPIO_Set(GPIO1_16, (bool)(value));
204 // PWM analogue actuator (currently generates one PWM signal with first PWM module)
205 static long freq = 16666666; // This is 60Hz
206 PWM_Set(EHRPWM0A, true, freq, value * freq); // Set the duty cycle
211 Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
214 Data_Save(&(a->data_file), &d, 1);
218 * Helper: Begin Actuator response in a given format
219 * @param context - the FCGIContext
220 * @param format - Format
221 * @param id - ID of Actuator
223 void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat format)
229 FCGI_BeginJSON(context, STATUS_OK);
230 FCGI_JSONLong("id", id);
231 FCGI_JSONPair("name", g_actuator_names[id]);
234 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
240 * Helper: End Actuator response in a given format
241 * @param context - the FCGIContext
242 * @param id - ID of the Actuator
243 * @param format - Format
245 void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat format)
262 * Handle a request for an Actuator
263 * @param context - FCGI context
264 * @param params - Parameters passed
266 void Actuator_Handler(FCGIContext * context, char * params)
269 gettimeofday(&now, NULL);
270 double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
273 double start_time = 0;
274 double end_time = current_time;
278 FCGIValue values[] = {
279 {"id", &id, FCGI_REQUIRED(FCGI_INT_T)},
280 {"set", &set, FCGI_DOUBLE_T},
281 {"start_time", &start_time, FCGI_DOUBLE_T},
282 {"end_time", &end_time, FCGI_DOUBLE_T},
283 {"format", &fmt_str, FCGI_STRING_T}
286 // enum to avoid the use of magic numbers
295 // Fill values appropriately
296 if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
298 // Error occured; FCGI_RejectJSON already called
302 // Get the Actuator identified
304 if (id < 0 || id >= NUMACTUATORS)
306 FCGI_RejectJSON(context, "Invalid Actuator id");
312 DataFormat format = Data_GetFormat(&(values[FORMAT]));
315 Actuator_BeginResponse(context, id, format);
318 if (FCGI_RECEIVED(values[SET].flags))
321 FCGI_JSONDouble("set", set);
326 Actuator_SetControl(a, &c);
330 Data_Handler(&(a->data_file), &(values[START_TIME]), &(values[END_TIME]), format, current_time);
333 Actuator_EndResponse(context, id, format);