Tweak Actuators/Sensors code
[matches/MCTX3420.git] / server / actuator.c
index 3e26e37..04938ba 100644 (file)
@@ -23,7 +23,7 @@ static Actuator g_actuators[ACTUATORS_MAX];
  * @param init - Function to call to initialise the actuator (may be NULL)
  * @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)
        {
@@ -42,6 +42,8 @@ int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn
 
        pthread_mutex_init(&(a->mutex), NULL);
 
+       Actuator_SetValue(a, initial_value, false);
+
        return g_num_actuators;
 }
 
@@ -54,7 +56,7 @@ int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn
 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);
 }
 
 /**
@@ -160,16 +162,18 @@ 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)
+               while (!a->control_changed && a->control.steps > 0 && a->activated)
                {
                        usleep(1e6*(a->control.stepwait));
                        a->control.start += a->control.stepsize;
-                       Actuator_SetValue(a, a->control.start);
+                       Actuator_SetValue(a, a->control.start, true);
                        
                        a->control.steps--;
                }
+               if (a->control_changed)
+                       continue;
                usleep(1e6*(a->control.stepwait));
 
                //TODO:
@@ -206,12 +210,13 @@ void Actuator_SetControl(Actuator * a, ActuatorControl * c)
  * @param a - The Actuator
  * @param value - The value to set
  */
-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)))
        {
@@ -221,9 +226,17 @@ 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);
+       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;
 }
 
 /**
@@ -364,7 +377,7 @@ void Actuator_Handler(FCGIContext * context, char * params)
                        //      If the user doesn't provide all 4 values, the Actuator will get set *once* using the first of the provided values
                        //      (see Actuator_Loop)
                        //  Not really a problem if n = 1, but maybe generate a warning for 2 <= n < 4 ?
-                       Log(LOGDEBUG, "Only provided %d values (expect %d) for Actuator setting", n);
+                       Log(LOGDEBUG, "Only provided %d values (expect %d) for Actuator setting", n, 4);
                }
                // SANITY CHECKS
                if (c.stepwait < 0 || c.steps < 0 || (a->sanity != NULL && !a->sanity(a->user_id, c.start)))

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