X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Factuator.c;h=7473485d54d771b88a44c053d8f269261e0c12d7;hb=828cdbf49f52572e93c5c5a48e05277525a4055f;hp=9b9a1a8f73bdc975667b9b5829a5a309efcf3215;hpb=e3e7914fe2f59765e5f92371329652a02518928c;p=matches%2FMCTX3420.git diff --git a/server/actuator.c b/server/actuator.c index 9b9a1a8..7473485 100644 --- a/server/actuator.c +++ b/server/actuator.c @@ -8,9 +8,6 @@ // Files containing GPIO and PWM definitions #include "bbb_pin.h" - - - /** Number of actuators **/ int g_num_actuators = 0; @@ -38,7 +35,7 @@ int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn a->init = init; // Set init function a->sanity = sanity; - + a->cleanup = cleanup; pthread_mutex_init(&(a->mutex), NULL); if (init != NULL) @@ -54,16 +51,35 @@ int Actuator_Add(const char * name, int user_id, SetFn set, InitFn init, CleanFn /** - * 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, 0); + //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 @@ -79,12 +95,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; + + ret = snprintf(filename, BUFSIZ, "%s/actuator_%d", experiment_path, a->id); - if (snprintf(filename, BUFSIZ, "%s_a%d", experiment_name, a->id) >= BUFSIZ) + 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); @@ -141,8 +162,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(); } /** @@ -169,9 +194,12 @@ void * Actuator_Loop(void * arg) Actuator_SetValue(a, a->control.start, true); // Currently does discrete steps after specified time intervals + + 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, true); @@ -179,7 +207,7 @@ void * Actuator_Loop(void * arg) } if (a->control_changed) continue; - usleep(1e6*(a->control.stepwait)); + 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, @@ -229,8 +257,8 @@ void Actuator_SetValue(Actuator * a, double value, bool record) } // Set time stamp - struct timeval t; - gettimeofday(&t, NULL); + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); DataPoint d = {TIMEVAL_DIFF(t, *Control_GetStartTime()), a->last_setting.value}; // Record value change if (record) @@ -294,8 +322,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 = ""; @@ -376,7 +404,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 @@ -429,3 +457,9 @@ Actuator * Actuator_Identify(const char * name) } return NULL; } + +DataPoint Actuator_LastData(int id) +{ + Actuator * a = &(g_actuators[id]); + return a->last_setting; +}