From cbf42d86e582d18aa3bbb9059a7e4855d80fa87a Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Wed, 21 Jul 2021 17:14:38 +0300 Subject: [PATCH] add mutex to pusirobo sending commands --- LocCorr/config.c | 7 +++++++ LocCorr/config.h | 3 +++ LocCorr/pusirobo.c | 20 +++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/LocCorr/config.c b/LocCorr/config.c index aa93fd3..8a0d510 100644 --- a/LocCorr/config.c +++ b/LocCorr/config.c @@ -29,6 +29,8 @@ static char *conffile = NULL; // configuration file name configuration theconf = { .maxUsteps=DEFAULT_MAXUSTEPS, .maxVsteps=DEFAULT_MAXVSTEPS, + .maxFpos=Fmaxsteps, + .minFpos=0, .minarea=DEFAULT_MINAREA, .maxarea=DEFAULT_MAXAREA, .Nerosions=DEFAULT_NEROSIONS, @@ -110,6 +112,11 @@ static confparam parvals[] = { "gain value in manual mode"}, {"starssort", PAR_INT, (void*)&theconf.starssort, 0, -DBL_EPSILON, 1.+DBL_EPSILON, "stars sorting algorithm: by distance from target (0) or by intensity (1)"}, + // immutable parameters (max user can't change) + {"focmax", PAR_INT, (void*)&theconf.maxFpos, 0, 1., 0., + "maximal focus position in microsteps"}, + {"focmin", PAR_INT, (void*)&theconf.minFpos, 0, 1., 0., + "minimal focus position in microsteps"}, {NULL, 0, NULL, 0, 0., 0., NULL} }; diff --git a/LocCorr/config.h b/LocCorr/config.h index 8038566..99e20bc 100644 --- a/LocCorr/config.h +++ b/LocCorr/config.h @@ -23,6 +23,7 @@ // min/max total steps range #define MINSTEPS (100) #define MAXSTEPS (50000) +#define Fmaxsteps (64000) // steps per pixel #define COEFMIN (0.1) #define COEFMAX (10000) @@ -56,6 +57,8 @@ typedef struct{ int maxUsteps; // max amount of steps by both axes int maxVsteps; + int maxFpos; // min/max F position (in microsteps) - user can't change this + int minFpos; int minarea; // min/max area of star image int maxarea; int Nerosions; // amount of erosions/dilations diff --git a/LocCorr/pusirobo.c b/LocCorr/pusirobo.c index 257dd4d..7d08a6f 100644 --- a/LocCorr/pusirobo.c +++ b/LocCorr/pusirobo.c @@ -63,7 +63,6 @@ #define CURPOSstatus "curpos" // max range of U and V motors (all in microsteps!) #define UVmaxsteps (35200) -#define Fmaxsteps (64000) // steps to move from the edge #define UVedgesteps (960) @@ -91,6 +90,9 @@ static pusistate state = PUSI_DISCONN; // server state static int sockfd = -1; // server file descriptor +// mutex for message sending +static pthread_mutex_t sendmesg_mutex = PTHREAD_MUTEX_INITIALIZER; + // current steps counters (zero at the middle) static int Uposition = 0, Vposition = 0, Fposition = 0; static uint8_t fixerr = 0; // ==1 if can't fixed @@ -212,6 +214,7 @@ static int waitOK(char **retval){ return ret; } + /** * @brief send_message - send character string `msg` to pusiserver * @param msg - message @@ -221,22 +224,27 @@ static int waitOK(char **retval){ static int send_message(const char *msg, char **ans){ if(!msg || sockfd < 0) return FALSE; size_t L = strlen(msg); + if(pthread_mutex_lock(&sendmesg_mutex)) return FALSE; clearbuf(); if(send(sockfd, msg, L, 0) != (ssize_t)L){ LOGWARN("send_message(): send() failed"); return FALSE; } DBG("Message '%s' sent", msg); - return waitOK(ans); + int r = waitOK(ans); + pthread_mutex_unlock(&sendmesg_mutex); + return r; } static void send_message_nocheck(const char *msg){ if(!msg || sockfd < 0) return; size_t L = strlen(msg); + if(pthread_mutex_lock(&sendmesg_mutex)) return; clearbuf(); if(send(sockfd, msg, L, 0) != (ssize_t)L){ WARN("send"); } + pthread_mutex_unlock(&sendmesg_mutex); DBG("Unchecked message '%s' sent", msg); } @@ -684,13 +692,11 @@ char *pusi_status(const char *messageid, char *buf, int buflen){ const char *motors[] = {"Umotor", "Vmotor", "Fmotor"}; const char *statuses[] = {Ustatus, Vstatus, Fstatus}; int *pos[] = {&Uposition, &Vposition, &Fposition}; - const int maxpos[] = {UVmaxsteps, UVmaxsteps, Fmaxsteps}; - const int minpos[] = {-UVmaxsteps, -UVmaxsteps, 0}; for(int i = 0; i < 3; ++i){ const char *stat = "moving"; if(moving_finished(statuses[i], pos[i])) stat = "stopping"; - l = snprintf(bptr, buflen, "\"%s\": { \"status\": \"%s\", \"position\": %d, \"minpos\": %d, \"maxpos\": %d }%s", - motors[i], stat, *pos[i], minpos[i], maxpos[i], (i==2)?"":", "); + l = snprintf(bptr, buflen, "\"%s\": { \"status\": \"%s\", \"position\": %d }%s", + motors[i], stat, *pos[i], (i==2)?"":", "); buflen -= l; bptr += l; } } @@ -751,7 +757,7 @@ char *set_pfocus(const char *newstatus, char *buf, int buflen){ return buf; } int newval = atoi(newstatus); - if(newval < 0 || newval > Fmaxsteps){ + if(newval < theconf.minFpos || newval > theconf.maxFpos){ snprintf(buf, buflen, FAIL); }else{ if(!setF(newval)) snprintf(buf, buflen, FAIL);