add mutex to pusirobo sending commands

This commit is contained in:
Edward Emelianov 2021-07-21 17:14:38 +03:00
parent b32ca38018
commit cbf42d86e5
3 changed files with 23 additions and 7 deletions

View File

@ -29,6 +29,8 @@ static char *conffile = NULL; // configuration file name
configuration theconf = { configuration theconf = {
.maxUsteps=DEFAULT_MAXUSTEPS, .maxUsteps=DEFAULT_MAXUSTEPS,
.maxVsteps=DEFAULT_MAXVSTEPS, .maxVsteps=DEFAULT_MAXVSTEPS,
.maxFpos=Fmaxsteps,
.minFpos=0,
.minarea=DEFAULT_MINAREA, .minarea=DEFAULT_MINAREA,
.maxarea=DEFAULT_MAXAREA, .maxarea=DEFAULT_MAXAREA,
.Nerosions=DEFAULT_NEROSIONS, .Nerosions=DEFAULT_NEROSIONS,
@ -110,6 +112,11 @@ static confparam parvals[] = {
"gain value in manual mode"}, "gain value in manual mode"},
{"starssort", PAR_INT, (void*)&theconf.starssort, 0, -DBL_EPSILON, 1.+DBL_EPSILON, {"starssort", PAR_INT, (void*)&theconf.starssort, 0, -DBL_EPSILON, 1.+DBL_EPSILON,
"stars sorting algorithm: by distance from target (0) or by intensity (1)"}, "stars sorting algorithm: by distance from target (0) or by intensity (1)"},
// immutable parameters (max<min -> 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} {NULL, 0, NULL, 0, 0., 0., NULL}
}; };

View File

@ -23,6 +23,7 @@
// min/max total steps range // min/max total steps range
#define MINSTEPS (100) #define MINSTEPS (100)
#define MAXSTEPS (50000) #define MAXSTEPS (50000)
#define Fmaxsteps (64000)
// steps per pixel // steps per pixel
#define COEFMIN (0.1) #define COEFMIN (0.1)
#define COEFMAX (10000) #define COEFMAX (10000)
@ -56,6 +57,8 @@
typedef struct{ typedef struct{
int maxUsteps; // max amount of steps by both axes int maxUsteps; // max amount of steps by both axes
int maxVsteps; 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 minarea; // min/max area of star image
int maxarea; int maxarea;
int Nerosions; // amount of erosions/dilations int Nerosions; // amount of erosions/dilations

View File

@ -63,7 +63,6 @@
#define CURPOSstatus "curpos" #define CURPOSstatus "curpos"
// max range of U and V motors (all in microsteps!) // max range of U and V motors (all in microsteps!)
#define UVmaxsteps (35200) #define UVmaxsteps (35200)
#define Fmaxsteps (64000)
// steps to move from the edge // steps to move from the edge
#define UVedgesteps (960) #define UVedgesteps (960)
@ -91,6 +90,9 @@ static pusistate state = PUSI_DISCONN; // server state
static int sockfd = -1; // server file descriptor 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) // current steps counters (zero at the middle)
static int Uposition = 0, Vposition = 0, Fposition = 0; static int Uposition = 0, Vposition = 0, Fposition = 0;
static uint8_t fixerr = 0; // ==1 if can't fixed static uint8_t fixerr = 0; // ==1 if can't fixed
@ -212,6 +214,7 @@ static int waitOK(char **retval){
return ret; return ret;
} }
/** /**
* @brief send_message - send character string `msg` to pusiserver * @brief send_message - send character string `msg` to pusiserver
* @param msg - message * @param msg - message
@ -221,22 +224,27 @@ static int waitOK(char **retval){
static int send_message(const char *msg, char **ans){ static int send_message(const char *msg, char **ans){
if(!msg || sockfd < 0) return FALSE; if(!msg || sockfd < 0) return FALSE;
size_t L = strlen(msg); size_t L = strlen(msg);
if(pthread_mutex_lock(&sendmesg_mutex)) return FALSE;
clearbuf(); clearbuf();
if(send(sockfd, msg, L, 0) != (ssize_t)L){ if(send(sockfd, msg, L, 0) != (ssize_t)L){
LOGWARN("send_message(): send() failed"); LOGWARN("send_message(): send() failed");
return FALSE; return FALSE;
} }
DBG("Message '%s' sent", msg); 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){ static void send_message_nocheck(const char *msg){
if(!msg || sockfd < 0) return; if(!msg || sockfd < 0) return;
size_t L = strlen(msg); size_t L = strlen(msg);
if(pthread_mutex_lock(&sendmesg_mutex)) return;
clearbuf(); clearbuf();
if(send(sockfd, msg, L, 0) != (ssize_t)L){ if(send(sockfd, msg, L, 0) != (ssize_t)L){
WARN("send"); WARN("send");
} }
pthread_mutex_unlock(&sendmesg_mutex);
DBG("Unchecked message '%s' sent", msg); 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 *motors[] = {"Umotor", "Vmotor", "Fmotor"};
const char *statuses[] = {Ustatus, Vstatus, Fstatus}; const char *statuses[] = {Ustatus, Vstatus, Fstatus};
int *pos[] = {&Uposition, &Vposition, &Fposition}; int *pos[] = {&Uposition, &Vposition, &Fposition};
const int maxpos[] = {UVmaxsteps, UVmaxsteps, Fmaxsteps};
const int minpos[] = {-UVmaxsteps, -UVmaxsteps, 0};
for(int i = 0; i < 3; ++i){ for(int i = 0; i < 3; ++i){
const char *stat = "moving"; const char *stat = "moving";
if(moving_finished(statuses[i], pos[i])) stat = "stopping"; if(moving_finished(statuses[i], pos[i])) stat = "stopping";
l = snprintf(bptr, buflen, "\"%s\": { \"status\": \"%s\", \"position\": %d, \"minpos\": %d, \"maxpos\": %d }%s", l = snprintf(bptr, buflen, "\"%s\": { \"status\": \"%s\", \"position\": %d }%s",
motors[i], stat, *pos[i], minpos[i], maxpos[i], (i==2)?"":", "); motors[i], stat, *pos[i], (i==2)?"":", ");
buflen -= l; bptr += l; buflen -= l; bptr += l;
} }
} }
@ -751,7 +757,7 @@ char *set_pfocus(const char *newstatus, char *buf, int buflen){
return buf; return buf;
} }
int newval = atoi(newstatus); int newval = atoi(newstatus);
if(newval < 0 || newval > Fmaxsteps){ if(newval < theconf.minFpos || newval > theconf.maxFpos){
snprintf(buf, buflen, FAIL); snprintf(buf, buflen, FAIL);
}else{ }else{
if(!setF(newval)) snprintf(buf, buflen, FAIL); if(!setF(newval)) snprintf(buf, buflen, FAIL);