Library ready

This commit is contained in:
eddyem
2019-05-04 18:36:02 +03:00
parent 46fb5d529d
commit a2ac6e2c74
11 changed files with 504 additions and 41 deletions

View File

@@ -3,8 +3,7 @@ project(examples)
link_libraries(mmpp)
include_directories(../)
#target_link_libraries(hello -lm)
#add_executable(movestages movecmdlnopts.c move.c)
#target_link_libraries(movestages -lusefull_macros)
add_executable(testmove tmcmdlnopts.c tm.c)
target_link_libraries(testmove -lusefull_macros)
add_executable(wheels wheelscmdlnopts.c wheels.c)
target_link_libraries(wheels -lusefull_macros)

View File

@@ -79,10 +79,6 @@ static int parsemotans(ttysend_status ans, const char *prefix){
return 0;
}
/**
* move motor
* @return 0 if motor can't move, else return 1
*/
/**
* @brief movemotor - move motor
* @param mcu - MCU# (controller No, 1 or 2)

View File

@@ -16,6 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Motors testing tool *
******************************************************************************/
#include "tmcmdlnopts.h"
#include <signal.h>
#include <stdio.h>
@@ -50,6 +54,93 @@ void __attribute__((noreturn)) signals(int sig){
exit(sig);
}
static double convangle(double val){
int X = (int)(val / 360.);
val -= 360. * (double)X;
return val;
}
static void parsestatus(ttysend_status st){
if(quiet) return;
switch(st){
case SEND_ERR:
red("communication error");
break;
case SEND_ALLOK:
green("all OK");
break;
case SEND_ACTIVE:
red("motor is still moving");
break;
case SEND_TOOBIG:
red("the steps amount is too large");
break;
case SEND_ZEROMOVE:
green("already at position");
break;
case SEND_ESWITCH:
red("on end-switch and can't move further");
break;
case SEND_NEEDINIT:
red("motors aren't initialised");
break;
case SEND_NEGATMOVE:
red("try to move into negative position");
break;
case SEND_OTHER:
default:
red("other error (wrong motor/MCU number?)");
}
printf("\n");
}
static void pollstatus(){
motor_state S;
bool mvng[3] = {false, true, true}, starting = true;
int curpos[4];
double adc[6];
while(mvng[1] || mvng[2]){
for(int Nmcu = 1; Nmcu < 3; ++Nmcu){
if(!mot_getstatus(Nmcu, &S)){
mvng[Nmcu] = false;
continue;
}
if(S.state[0] == STP_SLEEP && S.state[1] == STP_SLEEP)
mvng[Nmcu] = false;
curpos[2*(Nmcu-1)+0] = S.curpos[0];
curpos[2*(Nmcu-1)+1] = S.curpos[1];
if(G->getADC){
ADC_state s;
for(int Nmcu = 1; Nmcu < 3; ++Nmcu){
if(get_ADC(Nmcu, &s)){
adc[3*(Nmcu-1)+0] = s.Vdd;
adc[3*(Nmcu-1)+1] = s.Imot;
adc[3*(Nmcu-1)+2] = s.Vmot;
}
}
}
}
if(!mvng[1] && !mvng[2]){
if(starting){
starting = false;
continue;
}
}else starting = false;
if(!quiet){
printf("1: %6d, %6d", curpos[0],curpos[1]);
if(G->getADC){
printf(", VDD=%-5.1f Imot=%-5.1f Vmot=%-5.1f", adc[0], adc[1], adc[2]);
}
printf(" 2: %6d, %6d", curpos[2],curpos[3]);
if(G->getADC){
printf(", VDD=%-5.1f Imot=%-5.1f Vmot=%-5.1f", adc[3], adc[4], adc[5]);
}
printf(" \r");
}
}
printf("\n\n");
}
int main(int argc, char **argv){
initial_setup();
signal(SIGTERM, signals); // kill (-15)
@@ -69,41 +160,105 @@ int main(int argc, char **argv){
}
for(int i = 1; i < 3; ++i){
if(get_alive(i)){
green("MCU #%d found\n", i);
if(get_rst(i, true))
red("Controller #%d was in reset state\n", i);
}else red("MCU #%d not found\n", i);
if(!quiet) green("MCU #%d found\n", i);
}else WARNX(_("MCU #%d not found"), i);
}
if(G->stopall){
int r = stop_all();
if(r) red("Error for %d motors of 4\n", r);
else green("Successfully send command to stop all\n");
if(r) WARNX(_("Error for %d motors of 4"), r);
else MSG("Successfully send command to stop all", NULL);
}
if(G->gettemp){
double t1, t2;
if(get_temp(&t1, &t2)){
green("Got MCU temp:\n");
if(t1 > -300.) printf("\tMCU#1 - %gdegrC\n", t1);
if(t2 > -300.) printf("\tMCU#2 - %gdegrC\n", t2);
}else red("Can't get MCU temp\n");
MSG("Got MCU temp:", NULL);
if(t1 > -300.) printf("\tTMCU1=%gdegrC\n", t1);
if(t2 > -300.) printf("\tTMCU2=%gdegrC\n", t2);
}else WARNX(_("Can't get MCU temp"));
}
if(G->getstatus){
for(int N = 1; N < 3; ++N){
motor_state s;
if(mot_getstatus(N, &s)){
green("MCU#%d state:\n", N);
if(get_rst(N, true))
WARNX(_("Controller #%d was in reset state"), N);
if(!quiet) green("MCU#%d state:\n", N);
for(int i = 0; i < 2; ++i){
printf("\tstate[%d]=%d\n",i, s.state[i]);
printf("\tstepslefs[%d]=%d\n", i, s.stepsleft[i]);
printf("\tcurpos[%d]=%d\n", i, s.curpos[i]);
printf("\tSTATE[%d]=%d\n",i, s.state[i]);
printf("\tSTEPSLEFT[%d]=%d\n", i, s.stepsleft[i]);
printf("\tCURPOS[%d]=%d\n", i, s.curpos[i]);
for(int j = 0; j < 2; ++j)
printf("\tESW_status[%d][%d]=%d\n", i, j, s.ESW_status[i][j]);
printf("\tESW_STATE[%d][%d]=%d\n", i, j, s.ESW_status[i][j]);
}
}
}
}
int ini = init_motors();
if(ini) red("Can't init motors: %d\n", ini);
else green("Motors are ready!\n");
if(G->sendraw){
char **raw = G->sendraw;
do{
MSG(_("Send raw string"), *raw);
char *got = tty_sendraw(*raw);
if(got){
MSG(_("Receive"), got);
if(quiet) printf("%s", got);
}else WARNX(_("Nothing received"));
}while(*(++raw));
}
if(G->getADC){
MSG(_("Get ADC values"), NULL);
ADC_state s;
for(int i = 1; i < 3; ++i){
if(get_ADC(i, &s)){
printf("VDD%d=%g\n", i, s.Vdd);
printf("IMOT%d=%g\n", i, s.Imot);
printf("VMOT%d=%g\n", i, s.Vmot);
}else WARNX(_("Can't get ADC values for MCU#%d"), i);
}
}
if(G->rot1angle > -999. || G->rot2angle > -999. || G->l1steps != INT_MAX || G->l2steps != INT_MAX){
// all other commands are tied with moving, so check if motors are inited
MSG("Init motors", NULL);
// move all uninitialized motors to their zero position
int ini = init_motors(); // BLOCKING call!!!
// you can use non-blocking initialisation by proper rewriting of `init_motors`
if(ini){
WARNX(_("Can't init motors: %d\n"), ini);
signals(RET_CANTINIT);
}else green("Motors are ready!\n");
ttysend_status st;
if(G->rot1angle > -999.){
double angle = convangle(G->rot1angle);
int steps = (int)((STEPSREV1/360.) * angle);
MSG("Try to rotate polaroid ...", NULL);
st = movemotor(1, 1, steps, G->absmove);
parsestatus(st);
}
if(G->rot2angle > -999.){
double angle = convangle(G->rot2angle);
int steps = (int)((STEPSREV2/360.) * angle);
MSG("Try to rotate waveplate ...", NULL);
st = movemotor(2, 1, steps, G->absmove);
parsestatus(st);
}
if(G->l1steps != INT_MAX){
MSG("Try to move polaroid stage ...", NULL);
st = movemotor(1, 0, G->l1steps, G->absmove);
parsestatus(st);
}
if(G->l2steps != INT_MAX){
MSG("Try to move waveplate stage ...", NULL);
st = movemotor(2, 0, G->l2steps, G->absmove);
parsestatus(st);
}
}
pollstatus();
if(G->reset){
int **N = G->reset;
while(*N){
if(!quiet) green("Reset controller #%d\n", **N);
reset_MCU(**N);
++N;
}
}
signals(0);
}

View File

@@ -36,6 +36,10 @@ glob_pars const Gdefault = {
.comdev = "/dev/ttyUSB0"
,.pidfile = "/tmp/MMPP_con.pid"
,.speed = BAUD_RATE
,.rot1angle = -1000.
,.rot2angle = -1000.
,.l1steps = INT_MAX
,.l2steps = INT_MAX
};
/*
@@ -51,6 +55,14 @@ static myoption cmdlnopts[] = {
{"stopall", NO_ARGS, NULL, 's', arg_none, APTR(&G.stopall), N_("stop all motors")},
{"gettemp", NO_ARGS, NULL, 't', arg_none, APTR(&G.gettemp), N_("get MCU temperature")},
{"status", NO_ARGS, NULL, 'S', arg_none, APTR(&G.getstatus), N_("get device status")},
{"sendraw", MULT_PAR, NULL, 'W', arg_string, APTR(&G.sendraw), N_("send raw command (you can use this flag several times)")},
{"getADC", NO_ARGS, NULL, 'A', arg_none, APTR(&G.getADC), N_("get ADC values for both MCUs")},
{"absmove", NO_ARGS, NULL, 'a', arg_none, APTR(&G.absmove), N_("absolute move (without this flag moving is relative)")},
{"rot1", NEED_ARG, NULL, 'R', arg_double, APTR(&G.rot1angle), N_("rotate polaroid to given angle")},
{"rot2", NEED_ARG, NULL, 'r', arg_double, APTR(&G.rot2angle), N_("rotate lambda/4 to given angle")},
{"lin1", NEED_ARG, NULL, 'L', arg_int, APTR(&G.l1steps), N_("move polaroid linear stage to N steps")},
{"lin2", NEED_ARG, NULL, 'l', arg_int, APTR(&G.l2steps), N_("move wave-plate linear stage to N steps")},
{"reset", MULT_PAR, NULL, 'E', arg_int, APTR(&G.reset), N_("reset given mcu (may be included several times)")},
end_option
};

View File

@@ -31,6 +31,14 @@ typedef struct{
int stopall; // stop all motors
int speed; // TTY speed
int getstatus; // get status of all devices
char **sendraw; // send raw command[s]
int getADC; // get ADC values
double rot1angle; // rotator 1 angle
double rot2angle; // rotator 2 angle
int l1steps; // move linear stage 1 (polaroid) for N steps
int l2steps; // move linear stage 2 (L/4) for N steps
int absmove; // absolute move (to given position from zero-esw)
int **reset; // reset given MCU's
} glob_pars;
// default & global parameters

View File

@@ -16,6 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Wheels testing tool *
******************************************************************************/
#include "wheelscmdlnopts.h"
#include <signal.h>
#include <stdio.h>
@@ -91,31 +95,33 @@ int main(int argc, char **argv){
printf("\tmaxpos: %d\n\n", wheels[i].maxpos);
}
}
if(G->gohome){
if(G->gohome){ // non-blocking moving to home position
for(int i = 0; i < found; ++i){
if(!wheel_home(&wheels[i])) WARNX(_("Can't move wheel %c to home position"), wheels[i].ID);
else{
green("Wheel %c is moving to home position\n");
}
}
// now we can wait until all wheels reach home position
for(int i = 0; i < found; ++i){
while(WHEEL_MOVING == wheel_getpos(&wheels[i])){
usleep(100000);
}
// we should check current position because wheel can be blocked and don't move
if(wheel_getpos(&wheels[i]) != 1)
WARNX(_("Wheel %c didn't reach home position"), wheels[i].ID);
}
}
int Nw = 0, Ng = 0;
if(G->wh_ids){
if(G->wh_ids){ // count arguments of --wheel-id
while(G->wh_ids[Nw]) ++Nw;
}
if(G->gotopos){
if(G->gotopos){ // count arguments of --goto
while(G->gotopos[Ng]) ++Ng;
}
if(Nw != Ng){
if(Nw != Ng){ // it's better to write --goto after each --wheel-id
WARNX(_("Amoung of `--wheel-id` should be equal to amount of `--goto`!"));
}else{
}else{ // here is an example of searching wheel by its ID and blocking moving
for(int i = 0; i < Nw; ++i){
char ID = *G->wh_ids[i];
DBG("id: %c, goto: %d", ID, *G->gotopos[i]);
@@ -126,12 +132,12 @@ int main(int argc, char **argv){
if(!move_wheel(w, pos)){
WARNX(_("Can't rotate wheel %c to position %d"), ID, pos);
wheel_clear_err(w);
}else{
}else{ // wait until wheel is moving
while(WHEEL_MOVING == wheel_getpos(w)){
DBG("still moving");
usleep(100000);
}
int curpos = wheel_getpos(w);
int curpos = wheel_getpos(w); // poll again to check current position
if(curpos != pos) WARNX(_("Wheel %c can't reach position %d, current position: %d"), ID, pos, curpos);
else green("Wheel %c is on position %d\n", ID, pos);
}