Merge branch 'master' of github:/szmoore/MCTX3420
[matches/MCTX3420.git] / server / actuator.c
index af69c7f..40f9b46 100644 (file)
@@ -5,13 +5,16 @@
 
 #include "actuator.h"
 #include "options.h"
+// Files containing GPIO and PWM definitions
+#include "bbb_pin.h"
+
 
 /** Array of Actuators (global to this file) initialised by Actuator_Init **/
 static Actuator g_actuators[NUMACTUATORS];
 
 /** Human readable names for the Actuators **/
 const char * g_actuator_names[NUMACTUATORS] = {        
-       "actuator_test0", "actuator_test1"
+       "actuator_test0", "gpio1_16", "EHRPWM0A_duty@60Hz"
 };
 
 /**
@@ -25,6 +28,12 @@ void Actuator_Init()
                Data_Init(&(g_actuators[i].data_file));
                pthread_mutex_init(&(g_actuators[i].mutex), NULL);
        }
+
+       // Initialise pins used
+       GPIO_Export(GPIO1_16);
+       PWM_Export(EHRPWM0A);
+       PWM_Export(EHRPWM0B);
+       
 }
 
 /**
@@ -44,7 +53,6 @@ void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
                        {
                                char filename[BUFSIZ];
                                const char *experiment_name = (const char*) arg;
-                               int ret;
 
                                if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
                                {
@@ -54,34 +62,43 @@ void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
                                Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
                                // Open DataFile
                                Data_Open(&(a->data_file), filename);
-
+                       } 
+               case CONTROL_RESUME:  //Case fallthrough; no break before
+                       {
+                               int ret;
                                a->activated = true; // Don't forget this
-                               a->allow_actuation = true;
-
                                a->control_changed = false;
 
-                               // Create the thread
                                ret = pthread_create(&(a->thread), NULL, Actuator_Loop, (void*)(a));
                                if (ret != 0)
                                {
                                        Fatal("Failed to create Actuator_Loop for Actuator %d", a->id);
                                }
+
+                               Log(LOGDEBUG, "Resuming actuator %d", a->id);
                        }
                break;
 
                case CONTROL_EMERGENCY: //TODO add proper case for emergency
                case CONTROL_PAUSE:
-                       a->allow_actuation = false;
+                       a->activated = false;
+                       Actuator_SetControl(a, NULL);
+                       pthread_join(a->thread, NULL); // Wait for thread to exit
+
+                       Log(LOGDEBUG, "Paused actuator %d", a->id);
                break;
-               case CONTROL_RESUME:
-                       a->allow_actuation = true;
+
                break;
                case CONTROL_STOP:
-                       a->allow_actuation = false;
-                       a->activated = false;
-                       Actuator_SetControl(a, NULL);
-                       pthread_join(a->thread, NULL); // Wait for thread to exit       
+                       if (a->activated) //May have been paused before
+                       {
+                               a->activated = false;
+                               Actuator_SetControl(a, NULL);
+                               pthread_join(a->thread, NULL); // Wait for thread to exit       
+                       }
                        Data_Close(&(a->data_file)); // Close DataFile
+                       
+                       Log(LOGDEBUG, "Stopped actuator %d", a->id);
                break;
                default:
                        Fatal("Unknown control mode: %d", mode);
@@ -121,8 +138,6 @@ void * Actuator_Loop(void * arg)
                pthread_mutex_unlock(&(a->mutex));
                if (!a->activated)
                        break;
-               else if (!a->allow_actuation)
-                       continue;
 
                Actuator_SetValue(a, a->control.value);
        }
@@ -164,10 +179,40 @@ void Actuator_SetValue(Actuator * a, double value)
        //TODO: Set actuator
        switch (a->id)
        {
-               case ACTUATOR_TEST0:
+               case ACTUATOR_TEST0: 
+                       {
+                       // Onboard LEDs test actuator
+                               FILE *led_handle = NULL;        //code reference: http://learnbuildshare.wordpress.com/2013/05/19/beaglebone-black-controlling-user-leds-using-c/
+                               const char *led_format = "/sys/class/leds/beaglebone:green:usr%d/brightness";
+                               char buf[50];
+                               bool turn_on = value;
+
+                               for (int i = 0; i < 4; i++) 
+                               {
+                                       snprintf(buf, 50, led_format, i);
+                                       if ((led_handle = fopen(buf, "w")) != NULL)
+                                       {
+                                               if (turn_on)
+                                                       fwrite("1", sizeof(char), 1, led_handle);
+                                               else
+                                                       fwrite("0", sizeof(char), 1, led_handle);
+                                               fclose(led_handle);
+                                       }
+                                       else
+                                               Log(LOGDEBUG, "LED fopen failed: %s", strerror(errno)); 
+                               }
+                       }
                        break;
                case ACTUATOR_TEST1:
+                       GPIO_Set(GPIO1_16, (bool)(value));
+                       break;
+               case ACTUATOR_TEST2:
+               {
+                       // PWM analogue actuator (currently generates one PWM signal with first PWM module)
+                       static long freq = 16666666; // This is 60Hz
+                       PWM_Set(EHRPWM0A, true, freq, value * freq); // Set the duty cycle
                        break;
+               }
        }
 
        Log(LOGDEBUG, "Actuator %s set to %f", g_actuator_names[a->id], value);
@@ -190,6 +235,7 @@ void Actuator_BeginResponse(FCGIContext * context, ActuatorId id, DataFormat for
                case JSON:
                        FCGI_BeginJSON(context, STATUS_OK);
                        FCGI_JSONLong("id", id);
+                       FCGI_JSONPair("name", g_actuator_names[id]);
                        break;
                default:
                        FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
@@ -217,8 +263,6 @@ void Actuator_EndResponse(FCGIContext * context, ActuatorId id, DataFormat forma
 }
 
 
-
-
 /**
  * Handle a request for an Actuator
  * @param context - FCGI context

UCC git Repository :: git.ucc.asn.au