enhanced dumpmoving_scmd.c

This commit is contained in:
Edward V. Emelianov 2025-02-17 21:39:48 +03:00
parent 5250aa185d
commit 828523c7e1
4 changed files with 58 additions and 50 deletions

View File

@ -43,6 +43,7 @@ void logmnt(FILE *fcoords, mountdata_t *m){
t, RAD2DEG(m->motposition.X), RAD2DEG(m->motposition.Y), t, RAD2DEG(m->motposition.X), RAD2DEG(m->motposition.Y),
RAD2DEG(m->encposition.X), RAD2DEG(m->encposition.Y), RAD2DEG(m->encposition.X), RAD2DEG(m->encposition.Y),
m->millis, m->temperature, m->voltage); m->millis, m->temperature, m->voltage);
fflush(fcoords);
} }
/** /**
@ -67,7 +68,7 @@ void dumpmoving(FILE *fcoords, double t, int N){
int ctr = -1; int ctr = -1;
double xlast = mdata.motposition.X, ylast = mdata.motposition.Y; double xlast = mdata.motposition.X, ylast = mdata.motposition.Y;
double t0 = sl_dtime(); double t0 = sl_dtime();
DBG("millis = %u", millis); //DBG("millis = %u", millis);
while(sl_dtime() - t0 < t && ctr < N){ while(sl_dtime() - t0 < t && ctr < N){
usleep(10000); usleep(10000);
if(MCC_E_OK != Mount.getMountData(&mdata)){ WARNX("Can't get data"); continue;} if(MCC_E_OK != Mount.getMountData(&mdata)){ WARNX("Can't get data"); continue;}

View File

@ -22,6 +22,7 @@
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <usefull_macros.h> #include <usefull_macros.h>
@ -32,11 +33,15 @@
typedef struct{ typedef struct{
int help; int help;
int Ncycles; int Ncycles;
double reqint;
char *coordsoutput; char *coordsoutput;
char *axis;
} parameters; } parameters;
static parameters G = { static parameters G = {
.Ncycles = 40, .Ncycles = 40,
.reqint = 0.1,
.axis = "X",
}; };
static FILE *fcoords = NULL; static FILE *fcoords = NULL;
@ -44,6 +49,8 @@ static sl_option_t cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"}, {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"},
{"ncycles", NEED_ARG, NULL, 'n', arg_int, APTR(&G.Ncycles), "N cycles in stopped state (default: 40)"}, {"ncycles", NEED_ARG, NULL, 'n', arg_int, APTR(&G.Ncycles), "N cycles in stopped state (default: 40)"},
{"coordsfile", NEED_ARG, NULL, 'o', arg_string, APTR(&G.coordsoutput),"output file with coordinates log"}, {"coordsfile", NEED_ARG, NULL, 'o', arg_string, APTR(&G.coordsoutput),"output file with coordinates log"},
{"reqinterval", NEED_ARG, NULL, 'i', arg_double, APTR(&G.reqint), "mount requests interval (default: 0.1)"},
{"axis", NEED_ARG, NULL, 'a', arg_string, APTR(&G.axis), "axis to move (X or Y)"},
end_option end_option
}; };
@ -67,45 +74,39 @@ static conf_t Config = {
// dump thread // dump thread
static void *dumping(void _U_ *u){ static void *dumping(void _U_ *u){
dumpmoving(fcoords, 90., G.Ncycles); dumpmoving(fcoords, 3600., G.Ncycles);
return NULL; return NULL;
} }
// return TRUE if motor position is reached +- 0.1 degrees // return TRUE if motor position is reached +- 0.1 degrees
#define X2count (DEG2RAD(0.1)) #define XYcount (DEG2RAD(0.1))
#define Y2count (DEG2RAD(0.1)) static int Wait(double tag){
static int WaitX(double xtag){
mountdata_t mdata; mountdata_t mdata;
red("Wait for %g degrees\n", RAD2DEG(xtag)); red("Wait for %g degrees\n", RAD2DEG(tag));
int errcnt = 0; int errcnt = 0;
double sign = 0.; double sign = 0.;
uint32_t millis = 0; uint32_t millis = 0;
double curpos = 0.;
do{ do{
if(MCC_E_OK != Mount.getMountData(&mdata)) ++errcnt; if(MCC_E_OK != Mount.getMountData(&mdata)) ++errcnt;
else{ else{
errcnt = 0; errcnt = 0;
if(mdata.millis == millis) continue; if(mdata.millis == millis) continue;
millis = mdata.millis; millis = mdata.millis;
if(sign == 0.) sign = (mdata.motposition.X > xtag) ? 1. : -1.; if(*G.axis == 'X') curpos = mdata.motposition.X;
printf("X=%g deg, need %g deg; delta=%g arcmin\n", RAD2DEG(mdata.motposition.X), else curpos = mdata.motposition.Y;
RAD2DEG(xtag), RAD2DEG(sign*(mdata.motposition.X - xtag))*60.); if(sign == 0.) sign = (curpos > tag) ? 1. : -1.;
//printf("%s=%g deg, need %g deg; delta=%g arcmin\n", G.axis, RAD2DEG(curpos),
// RAD2DEG(tag), RAD2DEG(sign*(curpos - tag))*60.);
} }
}while(sign*(mdata.motposition.X - xtag) > X2count && errcnt < 10); }while(sign*(curpos - tag) > XYcount && errcnt < 10);
if(errcnt >= 10){ if(errcnt >= 10){
WARNX("Too much errors"); WARNX("Too much errors");
return FALSE; return FALSE;
} }
green("X reached position %g degrees\n", RAD2DEG(xtag)); green("%s reached position %g degrees\n", G.axis, RAD2DEG(tag));
return TRUE; return TRUE;
} }
/*
static int Yreached(double ytag){
mountdata_t mdata;
if(MCC_E_OK != Mount.getMountData(&mdata)) return FALSE;
if(fabs(mdata.motposition.Y - ytag) <= Y2count) return TRUE;
return FALSE;
}
*/
// check current position and go to 0 if non-zero // check current position and go to 0 if non-zero
static void chk0(){ static void chk0(){
@ -119,14 +120,37 @@ static void chk0(){
} }
} }
// move X to 40 degr with given speed until given coord
static void move(double target, double limit, double speed){
#define SCMD() do{if(MCC_E_OK != Mount.shortCmd(&cmd)) ERRX("Can't run command"); }while(0)
green("Move %s to %g until %g with %gdeg/s\n", G.axis, target, limit, speed);
short_command_t cmd = {0};
if(*G.axis == 'X'){
cmd.Xmot = DEG2RAD(target);
cmd.Xspeed = DEG2RAD(speed);
}else{
cmd.Ymot = DEG2RAD(target);
cmd.Yspeed = DEG2RAD(speed);
}
SCMD();
if(!Wait(DEG2RAD(limit))) signals(9);
#undef SCMD
}
int main(int argc, char **argv){ int main(int argc, char **argv){
sl_init(); sl_init();
sl_parseargs(&argc, &argv, cmdlnopts); sl_parseargs(&argc, &argv, cmdlnopts);
if(G.help) sl_showhelp(-1, cmdlnopts); if(G.help) sl_showhelp(-1, cmdlnopts);
if(strcmp(G.axis, "X") && strcmp(G.axis, "Y")){
WARNX("\"Axis\" should be X or Y");
return 1;
}
if(G.coordsoutput){ if(G.coordsoutput){
if(!(fcoords = fopen(G.coordsoutput, "w"))) if(!(fcoords = fopen(G.coordsoutput, "w")))
ERRX("Can't open %s", G.coordsoutput); ERRX("Can't open %s", G.coordsoutput);
}else fcoords = stdout; }else fcoords = stdout;
Config.MountReqInterval = G.reqint;
mcc_errcodes_t e = Mount.init(&Config); mcc_errcodes_t e = Mount.init(&Config);
if(e != MCC_E_OK){ if(e != MCC_E_OK){
WARNX("Can't init devices"); WARNX("Can't init devices");
@ -137,41 +161,23 @@ int main(int argc, char **argv){
signal(SIGINT, signals); // ctrl+C - quit signal(SIGINT, signals); // ctrl+C - quit
signal(SIGQUIT, signals); // ctrl+\ - quit signal(SIGQUIT, signals); // ctrl+\ - quit
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
// move to X=30 degr with different speeds // move to X=40 degr with different speeds
// start with 1degr/s, increase to 15, decrease to 1
short_command_t cmd = {0};
pthread_t dthr; pthread_t dthr;
#define SCMD() do{if(MCC_E_OK != Mount.shortCmd(&cmd)) ERRX("Can't run command"); }while(0)
chk0(); chk0();
logmnt(fcoords, NULL); logmnt(fcoords, NULL);
if(pthread_create(&dthr, NULL, dumping, NULL)) ERRX("Can't run dump thread"); if(pthread_create(&dthr, NULL, dumping, NULL)) ERRX("Can't run dump thread");
// goto 10 degr with 1deg/s // goto 1 degr with 1'/s
cmd.Xmot = DEG2RAD(30.); move(10., 1., 1./60.);
cmd.Xspeed = DEG2RAD(1.); // goto 2 degr with 2'/s
SCMD(); move(10., 2., 2./60.);
if(!WaitX(DEG2RAD(10.))) signals(9); // goto 3 degr with 5'/s
// goto 20 degr with 5deg/s move(10., 3., 5./60.);
cmd.Xmot = DEG2RAD(30.); // goto 4 degr with 10'/s
cmd.Xspeed = DEG2RAD(2.); move(10., 4., 10./60.);
SCMD(); // and go back with 5deg/s
if(!WaitX(DEG2RAD(20.))) signals(9); move(0., 0., 5.);
// goto 30 degr with 15deg/s // be sure to move @ 0,0
cmd.Xmot = DEG2RAD(40.);
cmd.Xspeed = DEG2RAD(5.);
SCMD();
if(!WaitX(DEG2RAD(30.))) signals(9);
// goto 40 degr with 1deg/s
cmd.Xmot = DEG2RAD(40.);
cmd.Xspeed = DEG2RAD(1.);
SCMD();
if(!WaitX(DEG2RAD(40.))) signals(9);
// and go back with 15deg/s
cmd.Xmot = DEG2RAD(0.);
cmd.Xspeed = DEG2RAD(15.);
SCMD();
if(!WaitX(DEG2RAD(0.))) signals(9);
Mount.moveTo(0., 0.); Mount.moveTo(0., 0.);
waitmoving(10);
// wait moving ends // wait moving ends
pthread_join(dthr, NULL); pthread_join(dthr, NULL);
#undef SCMD #undef SCMD

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 15.0.1, 2025-02-16T21:56:40. --> <!-- Written by QtCreator 15.0.1, 2025-02-17T21:39:16. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -253,6 +253,7 @@ static void *mountthread(void _U_ *u){
pthread_mutex_unlock(&datamutex); pthread_mutex_unlock(&datamutex);
// allow writing & getters // allow writing & getters
//DBG("t0=%g, tnow=%g", t0-t00, dtime()-t00); //DBG("t0=%g, tnow=%g", t0-t00, dtime()-t00);
if(dtime() - t0 >= Conf.MountReqInterval) usleep(50);
while(dtime() - t0 < Conf.MountReqInterval); while(dtime() - t0 < Conf.MountReqInterval);
t0 = dtime(); t0 = dtime();
} }