Automatic commit of irc logs
[matches/MCTX3420.git] / server / actuator.c
index ab48d19..7716318 100644 (file)
@@ -8,9 +8,6 @@
 // Files containing GPIO and PWM definitions
 #include "bbb_pin.h"
 
-
-
-
 /** Number of actuators **/
 int g_num_actuators = 0;
 
@@ -19,11 +16,15 @@ static Actuator g_actuators[ACTUATORS_MAX];
 /** 
  * Add and initialise an Actuator
  * @param name - Human readable name of the actuator
- * @param read - Function to call whenever the actuator should be read
+ * @param user_id - Caller specified ID to be associated with this actuator
+ * @param set - Function to call whenever the actuator should be set
  * @param init - Function to call to initialise the actuator (may be NULL)
+ * @param cleanup - Function to call to deinitialise the actuator (may be NULL)
+ * @param sanity - Function to call to check that a user specified value is sane (may be NULL)
+ * @param initial_value - The initial value to set the actuator to
  * @returns Number of actuators added so far
  */
-int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn cleanup, SanityFn sanity)
+int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn cleanup, SanityFn sanity, double initial_value)
 {
        if (++g_num_actuators > ACTUATORS_MAX)
        {
@@ -36,27 +37,53 @@ int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn
        a->name = name;
        a->set = set; // Set read function
        a->init = init; // Set init function
-       if (init != NULL)
-               init(name, user_id); // Call it
-       a->sanity = sanity;
 
+       a->sanity = sanity;
+       a->cleanup = cleanup;
        pthread_mutex_init(&(a->mutex), NULL);
 
+       if (init != NULL)
+       {
+               if (!init(name, user_id))
+                       Fatal("Couldn't initialise actuator %s", name);
+       }
+
+       Actuator_SetValue(a, initial_value, false);
+
        return g_num_actuators;
 }
 
 
 /**
- * One off initialisation of *all* Actuators
+ * Initialisation of *all* Actuators
  */
-#include "actuators/ledtest.h"
-#include "actuators/filetest.h"
+#include "actuators/pregulator.h"
+#include "actuators/relays.h"
 void Actuator_Init()
 {
        //Actuator_Add("ledtest",0,  Ledtest_Set, NULL,NULL,NULL);
-       Actuator_Add("filetest", 0, Filetest_Set, Filetest_Init, Filetest_Cleanup, Filetest_Sanity);
+       //Actuator_Add("filetest", 0, Filetest_Set, Filetest_Init, Filetest_Cleanup, Filetest_Sanity, 0);
+       Actuator_Add("pregulator", 0, Pregulator_Set, Pregulator_Init, Pregulator_Cleanup, Pregulator_Sanity, 0);
+       Actuator_Add("can_select", RELAY_CANSELECT, Relay_Set, Relay_Init, Relay_Cleanup, Relay_Sanity, 0);
+       Actuator_Add("can_enable", RELAY_CANENABLE, Relay_Set, Relay_Init, Relay_Cleanup, Relay_Sanity, 0);
+       Actuator_Add("main_pressure", RELAY_MAIN, Relay_Set, Relay_Init, Relay_Cleanup, Relay_Sanity, 1);
+}
+
+/**
+ * Deinitialise actuators
+ */
+void Actuator_Cleanup()
+{
+       for (int i = 0; i < g_num_actuators; ++i)
+       {
+               Actuator * a = g_actuators+i;
+               if (a->cleanup != NULL)
+                       a->cleanup(a->user_id);
+       }
+       g_num_actuators = 0;
 }
 
+
 /**
  * Sets the actuator to the desired mode. No checks are
  * done to see if setting to the desired mode will conflict with
@@ -72,12 +99,17 @@ void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
        {
                case CONTROL_START:
                        {
+                               // Set filename
                                char filename[BUFSIZ];
-                               const char *experiment_name = (const char*) arg;
+                               const char *experiment_path = (const char*) arg;
+                               int ret;
 
-                               if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ)
+                               ret = snprintf(filename, BUFSIZ, "%s/actuator_%d", experiment_path, a->id);
+
+                               if (ret >= BUFSIZ) 
                                {
-                                       Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
+                                       Fatal("Experiment path \"%s\" too long (%d, limit %d)",
+                                                       experiment_path, ret, BUFSIZ);
                                }
 
                                Log(LOGDEBUG, "Actuator %d with DataFile \"%s\"", a->id, filename);
@@ -134,8 +166,12 @@ void Actuator_SetMode(Actuator * a, ControlModes mode, void *arg)
  */
 void Actuator_SetModeAll(ControlModes mode, void * arg)
 {
-       for (int i = 0; i < ACTUATORS_MAX; i++)
+       if (mode == CONTROL_START)
+               Actuator_Init();
+       for (int i = 0; i < g_num_actuators; i++)
                Actuator_SetMode(&g_actuators[i], mode, arg);
+       if (mode == CONTROL_STOP)
+               Actuator_Cleanup();
 }
 
 /**
@@ -160,17 +196,22 @@ void * Actuator_Loop(void * arg)
                if (!a->activated)
                        break;
 
-               Actuator_SetValue(a, a->control.start);
+               Actuator_SetValue(a, a->control.start, true);
                // Currently does discrete steps after specified time intervals
-               while (a->control.steps > 0 && a->activated)
+
+               struct timespec wait;
+               DOUBLE_TO_TIMEVAL(a->control.stepwait, &wait);
+               while (!a->control_changed && a->control.steps > 0 && a->activated)
                {
-                       usleep(1e6*(a->control.stepwait));
+                       clock_nanosleep(CLOCK_MONOTONIC, 0, &wait, NULL);
                        a->control.start += a->control.stepsize;
-                       Actuator_SetValue(a, a->control.start);
+                       Actuator_SetValue(a, a->control.start, true);
                        
                        a->control.steps--;
                }
-               usleep(1e6*(a->control.stepwait));
+               if (a->control_changed)
+                       continue;
+               clock_nanosleep(CLOCK_MONOTONIC, 0, &wait, NULL);
 
                //TODO:
                // Note that although this loop has a sleep in it which would seem to make it hard to enforce urgent shutdowns,
@@ -205,13 +246,15 @@ void Actuator_SetControl(Actuator * a, ActuatorControl * c)
  * Set an Actuator value
  * @param a - The Actuator
  * @param value - The value to set
+ * @param record - Whether or not to record the value to the Actuator's DataFile.
  */
-void Actuator_SetValue(Actuator * a, double value)
+void Actuator_SetValue(Actuator * a, double value, bool record)
 {
        if (a->sanity != NULL && !a->sanity(a->user_id, value))
        {
                //ARE YOU INSANE?
-               Fatal("Insane value %lf for actuator %s", value, a->name);
+               Log(LOGERR,"Insane value %lf for actuator %s", value, a->name);
+               return;
        }
        if (!(a->set(a->user_id, value)))
        {
@@ -219,18 +262,26 @@ void Actuator_SetValue(Actuator * a, double value)
        }
 
        // Set time stamp
-       struct timeval t;
-       gettimeofday(&t, NULL);
-       // Record and save DataPoint
-       DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), value};
-       Data_Save(&(a->data_file), &d, 1);
+       struct timespec t;
+       clock_gettime(CLOCK_MONOTONIC, &t);
+       DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), a->last_setting.value};
+       // Record value change
+       if (record)
+       {       
+               d.time_stamp -= 1e-6;
+               Data_Save(&(a->data_file), &d, 1);
+               d.value = value;
+               d.time_stamp += 1e-6;
+               Data_Save(&(a->data_file), &d, 1);
+       }
+       a->last_setting = d;
 }
 
 /**
  * Helper: Begin Actuator response in a given format
  * @param context - the FCGIContext
+ * @param a - the actuator to begin a response for
  * @param format - Format
- * @param id - ID of Actuator
  */
 void Actuator_BeginResponse(FCGIContext * context, Actuator * a, DataFormat format)
 {
@@ -252,7 +303,7 @@ void Actuator_BeginResponse(FCGIContext * context, Actuator * a, DataFormat form
 /**
  * Helper: End Actuator response in a given format
  * @param context - the FCGIContext
- * @param id - ID of the Actuator
+ * @param a - the actuator to end a response for
  * @param format - Format
  */
 void Actuator_EndResponse(FCGIContext * context, Actuator * a, DataFormat format)
@@ -276,8 +327,8 @@ void Actuator_EndResponse(FCGIContext * context, Actuator * a, DataFormat format
  */
 void Actuator_Handler(FCGIContext * context, char * params)
 {
-       struct timeval now;
-       gettimeofday(&now, NULL);
+       struct timespec now;
+       clock_gettime(CLOCK_MONOTONIC, &now);
        double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
        int id = 0;
        char * name = "";
@@ -358,7 +409,7 @@ void Actuator_Handler(FCGIContext * context, char * params)
        
                ActuatorControl c = {0.0, 0.0, 0.0, 0}; // Need to set default values (since we don't require them all)
                // sscanf returns the number of fields successfully read...
-               int n = sscanf(set, "%lf,%lf,%lf,%d", &(c.start), &(c.stepwait), &(c.stepsize), &(c.steps)); // Set provided values in order
+               int n = sscanf(set, "%lf_%lf_%lf_%d", &(c.start), &(c.stepwait), &(c.stepsize), &(c.steps)); // Set provided values in order
                if (n != 4)
                {
                        //      If the user doesn't provide all 4 values, the Actuator will get set *once* using the first of the provided values
@@ -411,3 +462,14 @@ Actuator * Actuator_Identify(const char * name)
        }
        return NULL;
 }
+
+/**
+ * Returns the last DataPoint that is currently available.
+ * @param id - The actuator ID for which to retrieve data from
+ * @return The last DataPoint
+ */
+DataPoint Actuator_LastData(int id)
+{
+       Actuator * a = &(g_actuators[id]);
+       return a->last_setting;
+}

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