remove deprecated code

This commit is contained in:
2022-03-10 12:19:52 +03:00
parent 12bd669ff6
commit a57a78adee
52 changed files with 428 additions and 2323 deletions

View File

@@ -0,0 +1 @@
SUBSYSTEMS=="usb", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="82cd", MODE:="0666"

4
MMPP:lib/CMake.readme Normal file
View File

@@ -0,0 +1,4 @@
cmake defines:
-DDEBUG=1 - debug mode
-DNOGETTEXT=1 - don't run xgettext
-DEXAMPLES=1 - to compile examples

166
MMPP:lib/CMakeLists.txt Normal file
View File

@@ -0,0 +1,166 @@
cmake_minimum_required(VERSION 3.9)
set(PROJ mmpp)
set(MINOR_VERSION "1")
set(MID_VERSION "0")
set(MAJOR_VERSION "0")
set(PROJ_VERSION "${MAJOR_VERSION}.${MID_VERSION}.${MINOR_VERSION}")
project(${PROJ} VERSION ${PROJ_VERSION} LANGUAGES C)
# default flags
set(CMAKE_C_FLAGS "${CFLAGS} -O2")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_COLOR_MAKEFILE ON)
# threads number definition
if(NOT DEFINED PROCESSOR_COUNT)
set(PROCESSOR_COUNT 2) # by default 2 cores
set(cpuinfo_file "/proc/cpuinfo")
if(EXISTS "${cpuinfo_file}")
file(STRINGS "${cpuinfo_file}" procs REGEX "^processor.: [0-9]+$")
list(LENGTH procs PROCESSOR_COUNT)
endif()
endif()
add_definitions(-DTHREAD_NUMBER=${PROCESSOR_COUNT})
message("In multithreaded operations will use ${PROCESSOR_COUNT} threads")
# cmake -DDEBUG=1 -> debugging
if(DEFINED DEBUG AND DEBUG EQUAL 1)
set(CMAKE_BUILD_TYPE "Debug")
else()
set(CMAKE_BUILD_TYPE "Release")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DEBUG)
set(CMAKE_VERBOSE_MAKEFILE true)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
message("install to ${CMAKE_CURRENT_SOURCE_DIR}/install ")
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/install)
endif()
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_DEBUG})
else()
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_RELEASE})
endif()
message("Build type: ${CMAKE_BUILD_TYPE}")
# here is one of two variants: all .c in directory or .c files in list
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES)
# directory should contain dir locale/ru for gettext translations
if(0)
set(LCPATH ${CMAKE_SOURCE_DIR}/locale/ru)
if(NOT DEFINED LOCALEDIR)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LOCALEDIR ${CMAKE_CURRENT_SOURCE_DIR}/locale)
else()
set(LOCALEDIR ${CMAKE_INSTALL_PREFIX}/share/locale)
endif()
endif()
endif(0)
###### pkgconfig ######
set(MODULES libudev)
# find packages:
find_package(PkgConfig REQUIRED)
pkg_check_modules(${PROJ} REQUIRED ${MODULES})
pkg_check_modules(USB libusb)
if(NOT USB_FOUND)
pkg_check_modules(USB REQUIRED libusb-1.0)
endif()
# external modules like OpenMP:
include(FindOpenMP)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
add_definitions(-DOMP_FOUND)
endif()
###### additional flags ######
#list(APPEND ${PROJ}_LIBRARIES "-lfftw3_threads")
# gettext files
set(PO_FILE ${LCPATH}/messages.po)
set(MO_FILE ${LCPATH}/LC_MESSAGES/${PROJ}.mo)
set(RU_FILE ${LCPATH}/ru.po)
# exe file
#add_executable(${PROJ} ${SOURCES})
# library
add_library(${PROJ} SHARED ${SOURCES})
# library header files
set(LIBHEADER "libmmpp.h")
# -I
include_directories(${${PROJ}_INCLUDE_DIRS})
# -L
link_directories(${${PROJ}_LIBRARY_DIRS})
# -D
add_definitions(-DLOCALEDIR=\"${LOCALEDIR}\"
-DPACKAGE_VERSION=\"${PROJ_VERSION}\" -DGETTEXT_PACKAGE=\"${PROJ}\"
-DMINOR_VERSION=\"${MINOR_VERSION}\" -DMID_VERSION=\"${MID_VERSION}\"
-DMAJOR_VERSION=\"${MAJOR_VESION}\")
# -l
target_link_libraries(${PROJ} ${${PROJ}_LIBRARIES})
set(PCFILE "${CMAKE_BINARY_DIR}/${PROJ}.pc")
configure_file("${PROJ}.pc.in" ${PCFILE} @ONLY)
set_target_properties(${PROJ} PROPERTIES VERSION ${PROJ_VERSION})
set_target_properties(${PROJ} PROPERTIES PUBLIC_HEADER ${LIBHEADER})
# Installation of the program
include(GNUInstallDirs)
#install(TARGETS ${PROJ} DESTINATION "bin")
install(TARGETS ${PROJ} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${PCFILE} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
install(FILES "99-edmund_hsfw.rules" DESTINATION "/etc/udev/rules.d")
# EXAMPLES
if(DEFINED EXAMPLES AND EXAMPLES EQUAL 1)
add_subdirectory(examples)
endif()
###### gettext ######
if(0)
if(NOT DEFINED NOGETTEXT)
add_definitions(-DGETTEXT)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message("Generate locale files @ make")
find_package(Gettext REQUIRED)
find_program(GETTEXT_XGETTEXT_EXECUTABLE xgettext)
if(NOT GETTEXT_XGETTEXT_EXECUTABLE OR NOT GETTEXT_MSGFMT_EXECUTABLE)
message(FATAL_ERROR "xgettext not found")
endif()
file(MAKE_DIRECTORY ${LCPATH})
file(MAKE_DIRECTORY ${LCPATH}/LC_MESSAGES)
add_custom_command(
OUTPUT ${PO_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${GETTEXT_XGETTEXT_EXECUTABLE} --from-code=utf-8 ${SOURCES} -c -k_ -kN_ -o ${PO_FILE}
COMMAND sed -i 's/charset=.*\\\\n/charset=koi8-r\\\\n/' ${PO_FILE}
COMMAND enconv ${PO_FILE}
DEPENDS ${SOURCES}
)
# we need this to prevent ru.po & .mo from deleting by make clean
add_custom_target(
RU_FILE
COMMAND [ -f ${RU_FILE} ] && ${GETTEXT_MSGMERGE_EXECUTABLE} -Uis ${RU_FILE} ${PO_FILE} || cp ${PO_FILE} ${RU_FILE}
DEPENDS ${PO_FILE} ${SOURCES}
)
add_custom_target(
MO_FILE
COMMAND make RU_FILE && ${GETTEXT_MSGFMT_EXECUTABLE} ${RU_FILE} -o ${MO_FILE}
DEPENDS ${RU_FILE}
)
add_dependencies(${PROJ} MO_FILE)
else() # install .mo file
install(FILES ${MO_FILE} DESTINATION "${LOCALEDIR}/ru/LC_MESSAGES")
endif()
endif(NOT DEFINED NOGETTEXT)
endif(0)

176
MMPP:lib/Readme.md Normal file
View File

@@ -0,0 +1,176 @@
MMPPlib
==================
This is a library for MMPP (multimode photometer-polarimeter) easy management. Be careful using it in multi-threading applications because almost all its functions are **not thread-safe**!
The library functions can be divided onto two parts: turrets management and motors management. The original tools for this were [HSFW_management](https://github.com/eddyem/eddys_snippets/tree/master/HSFW_management) and [MMPP_control](https://github.com/eddyem/mmpp/tree/master/MMPP_control). Now you can easily write your own C/C++ application using this library. The two examples will facilitate this job.
## HSFW turrets management
The example files `wheels.c`, `wheelscmdlnopts.c` and `wheelscmdlnopts.h` demonstrate how you can use base functions of turrets' management.
Each wheel describes by appropriate structure:
typedef struct{
int fd; // file descriptor of device
char *serial; // serial number
char ID; // identificator
char name[9]; // wheel name
int maxpos; // max position
} wheel_descr;
The first thing you need to do when start working with turrets is to discovery them. This is done by function
int find_wheels(wheel_descr **wheels, wheel_error *err);
This function returns amount of wheels found (or 0 if none). The `wheels` parameter is array of `wheel_descr`, one element for each wheel. The second, `err`, is error state (if wheels not found):
typedef enum{
WHERR_ALLOK // no errors
,WHERR_UDEV // udev error
,WHERR_CANTOPEN // can't open file device
} wheel_error;
You can got `WHERR_CANTOPEN` error if your rights are not sufficient to open the device file.
To properly delete memory and close devices call function
void del_wheels(wheel_descr *w, int N);
which arguments are: `w` -- array of descriptors, and `N` -- their amount in array.
After device is opened you can work with it using functions:
wheel_status wheel_getpos(wheel_descr *w);
bool wheel_clear_err(wheel_descr *w);
bool move_wheel(wheel_descr *w, int filter_pos);
bool wheel_home(wheel_descr *w);
The `wheel_getpos` returns not only a position but also an error code if something went wrong:
typedef enum{
WHEEL_MOVING = -2 // still moving
,WHEEL_ERROR = -1 // communication error
,WHEEL_POSERR = 0 // wrong wheel position
,WHEEL_POS1, WHEEL_POS2, WHEEL_POS3, WHEEL_POS4, WHEEL_POS5, WHEEL_POS6
,WHEEL_POS7, WHEEL_POS8, WHEEL_POS9, WHEEL_POS10
} wheel_status;
So its positive values are wheel position and negative are error codes.
Other three functions returns boolean value: `false` in case of error.
You can run `wheel_clear_err` to reset error state of device.
The `move_wheel` will rotate given wheel into position `filter_pos` (if this position isn't available or in case of other error it will return `false`).
Sometimes you may want to home wheel manually, then call `wheel_home`.
## MMPP motors management
These are the base functions you need to manage MMPP motors:
int mmpp_tryopen(char *dev, int spd);
void mmpp_close();
int mot_handshake();
bool mot_getstatus(int Nmcu, motor_state *s);
bool get_rst(int N, bool clear);
bool get_alive(int N);
int stop_all();
int get_temp(double *t1, double *t2);
int init_motors();
int mot_wait();
ttysend_status tty_sendcmd(char *cmd);
bool get_ADC(int N, ADC_state *s);
ttysend_status movemotor(int mcu, int motnum, int steps, int absmove);
void reset_MCU(int N);
There is also three special functions, don't use them without a real need:
char *tty_get();
int tty_send(char *cmd);
char* tty_sendraw(char *string);
Here are all base types used in those functions. The motor state:
typedef enum{
STP_UNKNOWN, // wrong state
STP_SLEEP, // don't moving
STP_ACCEL, // start moving with acceleration
STP_MOVE, // moving with constant speed
STP_MVSLOW, // moving with slowest constant speed
STP_DECEL, // moving with deceleration
STP_STOP, // stop motor right now (by demand)
STP_STOPZERO, // stop motor and zero its position (on end-switch)
STP_MOVE0, // move towards 0 endswitch (negative direction)
STP_MOVE1 // move towards 1 endswitch (positive direction)
} stp_state;
Base answers on `tty_sendcmd`:
typedef enum{
SEND_ERR, // communication error
SEND_ALLOK, // no errors
SEND_ACTIVE, // motor is still moving
SEND_TOOBIG, // amount of steps too big
SEND_ZEROMOVE, // give 0 steps to move
SEND_ESWITCH, // staying on end-switch & try to move further
SEND_NEEDINIT, // motor needs initialisation
SEND_NEGATMOVE, // try to move to negative position
SEND_OTHER, // unknown state
} ttysend_status;
The end- (proximity or limit) switches' state:
typedef enum{
ESW_ERROR, // wrong value
ESW_RELEASED, // opened
ESW_HALL, // hall sensor
ESW_BUTTON // user button
} ESW_status;
If reset state detected, you can know the reason of reset:
typedef enum{
RESET_NONE, // no sw/wd reset occured
RESET_SW, // software reset have been before last status call
RESET_WDG // watchdog reset -//-
} reset_status;
The state of each MCU:
typedef struct{
reset_status rst; // reset status (was MCU reseted by watchdog or software?)
stp_state state[2]; // status of stepper motor
int stepsleft[2]; // steps left to reach target position
int curpos[2]; // current position (negative for non-initialized state or error)
ESW_status ESW_status[2][2];// End-switches status, [i][j], i - motor, j - esw 0 or 1
} motor_state;
And ADC values:
typedef struct{
double Vdd; // value of Vdd (+3.3V)
double Imot; // motors' current (Amperes)
double Vmot; // motors' voltage (+12V)
} ADC_state;
So, before start working with the device you need to open it with `mmpp_tryopen`, its parameter `dev` is device name, and `spd` is communication speed (9600 by default). This function returns `0` if all OK.
Opened device at the end of work should be closed by `mmpp_close`.
Run `mot_handshake` if you doubt that all OK with MCUs, this function will return `0` if at least one MCU found and `1` if none. After running this function you can run `get_alive` to determine which MCU have connection problems (`get_alive` returns `false` if given MCU didn't answer on a handshake).
The `mot_getstatus` returns `false` if cannot communicate with given MCU. Otherwise it will fill the `s` parameter. After running of this function you can determinate reset status non only by `rst` parameter, but also by function `get_rst` (its parameter `clear` should be set to `true` if you want to clear global reset status).
To stop all movement, run `stop_all`.
Just after powering the device all motors are in uninitialized condition. To initialize them you need to move all onto zero's end-switch using `init_motors`. This function is **blocking**!!! So if you want non-blocking initialization write it yourself like at example `tm.c`. This function returns `0` if all OK, or it will return the last problem MCU and motor value as `Nmcu*10+motnum`.
Another blocking function is `mot_wait` which will blocks until all motors stopped.
To move motor run `movemotor`. It have four parameters: `mcu` (MCU number, 1 or 2), `motnum` (motor number, 0 or 1), `steps` (steps amount) and `absmove` (set it to `true` if `steps` are absolute position). Its return value can tell about possible problems.
If you want to get MCU's temperature, run `get_temp`. Its both parameters are temperature values in degrees of Celsius.
The ADC state available with`get_ADC`.
To reset MCU just run `reset_MCU`. To make certain that reset occurred, run `mot_getstatus` and check both `curpos`, they should be `-1`.
Function`tty_sendcmd` allows to send some special commands with return status analyse. If you want to make particular communications, use `tty_send` to send command without subsequent reading or `tty_sendraw` with answer reading. To read MCU's answer separately you can call `tty_get`.
For examples of these functions usage look into files `tm.c`, `tmcmdlnopts.c` and `tmcmdlnopts.h`.

72
MMPP:lib/common.h Normal file
View File

@@ -0,0 +1,72 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// common macros
#pragma once
#ifndef COMMON_H__
#define COMMON_H__
#include <stdio.h>
#include <stdlib.h>
// unused arguments of functions
#define _U_ __attribute__((__unused__))
// break absent in `case`
#define FALLTHRU __attribute__ ((fallthrough))
// and synonym for FALLTHRU
#define NOBREAKHERE __attribute__ ((fallthrough))
// weak functions
#define WEAK __attribute__ ((weak))
#define COLOR_RED "\033[1;31;40m"
#define COLOR_GREEN "\033[1;32;40m"
#define COLOR_OLD "\033[0;0;0m"
/*
* print function name, debug messages
* debug mode, -DEBUG
*/
#ifdef EBUG
#define FNAME() do{ fprintf(stderr, COLOR_OLD); \
fprintf(stderr, "\n%s (%s, line %d)\n", __func__, __FILE__, __LINE__);} while(0)
#define DBG(...) do{ fprintf(stderr, COLOR_OLD); \
fprintf(stderr, "%s (%s, line %d): ", __func__, __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n");} while(0)
#define red(...) do{printf(COLOR_RED); printf(__VA_ARGS__); printf(COLOR_OLD);}while(0)
#define green(...) do{printf(COLOR_GREEN); printf(__VA_ARGS__); printf(COLOR_OLD);}while(0)
#else
#define FNAME()
#define DBG(...)
#define red(...)
#define green(...)
#endif //EBUG
/*
* Memory allocation
*/
#define ALLOC(type, var, size) type * var = ((type *)my_alloc(size, sizeof(type)))
#define MALLOC(type, size) ((type *)my_alloc(size, sizeof(type)))
#define FREE(ptr) do{if(ptr){free(ptr); ptr = NULL;}}while(0)
#ifndef DBL_EPSILON
#define DBL_EPSILON (2.2204460492503131e-16)
#endif
void *my_alloc(size_t N, size_t S);
#endif // COMMON_H__

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.9)
project(examples)
link_libraries(mmpp)
include_directories(../)
add_executable(testmove tmcmdlnopts.c tm.c)
target_link_libraries(testmove -lusefull_macros)
install(TARGETS testmove DESTINATION "bin")
add_executable(wheels wheelscmdlnopts.c wheels.c)
target_link_libraries(wheels -lusefull_macros)
install(TARGETS wheels DESTINATION "bin")

View File

@@ -0,0 +1,63 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tmcmdlnopts.h"
#include <signal.h>
#include <stdio.h>
#include <libmmpp.h>
#include <usefull_macros.h>
// All return states of main():
enum{
RET_ALLOK = 0,
RET_NOTFOUND, // none of turrets found or didn't found seeking MCU
RET_ONLYONE, // only one turret found
RET_COMMERR, // communication error
RET_CANTINIT, // can't init turrets
RET_ERROR = 9, // uncoverable error - from libsnippets
RET_HELPCALL = 255 // user call help (or give wrong parameter[s]) - from libsnippets
};
static glob_pars *G;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void __attribute__((noreturn)) signals(int sig){
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
if(G->pidfile) // remove unnesessary PID file
unlink(G->pidfile);
restore_console();
mmpp_close();
exit(sig);
}
int main(int argc, char **argv){
initial_setup();
signal(SIGTERM, signals); // kill (-15)
signal(SIGINT, signals); // ctrl+C
signal(SIGQUIT, SIG_IGN); // ctrl+\ .
signal(SIGTSTP, SIG_IGN); // ctrl+Z
setbuf(stdout, NULL);
G = parse_args(argc, argv);
check4running(NULL, G->pidfile);
;
signals(0);
}

View File

@@ -0,0 +1,86 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "templatecmdlnopts.h"
#include <assert.h>
#include <libmmpp.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
int quiet = 0; // less messages @ stdout
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.pidfile = "/tmp/MMPP_wheels.pid"
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), N_("show this help")},
{"quiet", NO_ARGS, NULL, 'q', arg_none, APTR(&quiet), N_("don't show anything @screen from stdout")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), N_("PID-file name")},
{"status", NO_ARGS, NULL, 'S', arg_none, APTR(&G.getstatus), N_("get device status")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
// format of help: "Usage: progname [args]\n"
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("%d unused parameters:\n", argc);
for(int i = 0; i < argc; ++i)
printf("\t%4d: %s\n", i+1, argv[i]);
}
return &G;
}
/**
* @brief MSG show coloured message if `quiet` not set
* !! This function adds trailing '\n' to message
* @param s1 - green part of message (may be null)
* @param s2 - normal colored part of messate (may be null)
*/
void MSG(const char *s1, const char *s2){
if(quiet) return;
if(s1){
green("%s%s", s1, s2 ? ": " : "\n");
}
if(s2) printf("%s\n", s2);
}

View File

@@ -0,0 +1,40 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TMCMDLNOPTS_H__
#define TMCMDLNOPTS_H__
#include <usefull_macros.h>
/*
* here are some typedef's for global data
*/
typedef struct{
char *pidfile; // pid file name
int gettemp; // get MCU temperature
int getstatus; // get status of all devices
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
extern int quiet;
glob_pars *parse_args(int argc, char **argv);
void MSG(const char *s1, const char *s2);
#endif // TMCMDLNOPTS_H__

264
MMPP:lib/examples/tm.c Normal file
View File

@@ -0,0 +1,264 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Motors testing tool *
******************************************************************************/
#include "tmcmdlnopts.h"
#include <signal.h>
#include <stdio.h>
#include <libmmpp.h>
#include <usefull_macros.h>
// All return states of main():
enum{
RET_ALLOK = 0,
RET_NOTFOUND, // none of MCU found or didn't found seeking MCU
RET_ONLYONE, // only one MCU found
RET_COMMERR, // communication error
RET_CANTINIT, // can't init motors: error during initiation or some of motors are moving
RET_WAITERR, // error occured during waiting procedure
RET_ERROR = 9, // uncoverable error - from libsnippets
RET_HELPCALL = 255 // user call help (or give wrong parameter[s]) - from libsnippets
};
static glob_pars *G;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void __attribute__((noreturn)) signals(int sig){
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
if(G->pidfile) // remove unnesessary PID file
unlink(G->pidfile);
restore_console();
mmpp_close();
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)
signal(SIGINT, signals); // ctrl+C
signal(SIGQUIT, SIG_IGN); // ctrl+\ .
signal(SIGTSTP, SIG_IGN); // ctrl+Z
setbuf(stdout, NULL);
G = parse_args(argc, argv);
check4running(NULL, G->pidfile);
DBG("Try to open serial %s", G->comdev);
if(mmpp_tryopen(G->comdev, G->speed)){
ERRX(_("Can't open %s with speed %d. Exit."), G->comdev, G->speed);
}
if(mot_handshake()){
WARNX(_("Controllers not found"));
signals(RET_NOTFOUND);
}
for(int i = 1; i < 3; ++i){
if(get_alive(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) 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)){
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)){
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("\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_STATE[%d][%d]=%d\n", i, j, s.ESW_status[i][j]);
}
}
}
}
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

@@ -0,0 +1,104 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tmcmdlnopts.h"
#include <assert.h>
#include <libmmpp.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
int quiet = 0; // less messages @ stdout
// DEFAULTS
// default global parameters
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
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), N_("show this help")},
{"quiet", NO_ARGS, NULL, 'q', arg_none, APTR(&quiet), N_("don't show anything @screen from stdout")},
{"comdev", NEED_ARG, NULL, 'd', arg_string, APTR(&G.comdev), N_("terminal device filename")},
{"baudrate",NEED_ARG, NULL, 'b', arg_int, APTR(&G.speed), N_("TTY baudrate")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), N_("PID-file name")},
{"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
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
// format of help: "Usage: progname [args]\n"
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("%d unused parameters:\n", argc);
for(int i = 0; i < argc; ++i)
printf("\t%4d: %s\n", i+1, argv[i]);
}
return &G;
}
/**
* @brief MSG show coloured message if `quiet` not set
* !! This function adds trailing '\n' to message
* @param s1 - green part of message (may be null)
* @param s2 - normal colored part of messate (may be null)
*/
void MSG(const char *s1, const char *s2){
if(quiet) return;
if(s1){
green("%s%s", s1, s2 ? ": " : "\n");
}
if(s2) printf("%s\n", s2);
}

View File

@@ -0,0 +1,51 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TMCMDLNOPTS_H__
#define TMCMDLNOPTS_H__
#include <usefull_macros.h>
/*
* here are some typedef's for global data
*/
typedef struct{
char *comdev; // TTY device
char *pidfile; // pid file name
int gettemp; // get MCU temperature
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
extern glob_pars const Gdefault;
extern int quiet;
glob_pars *parse_args(int argc, char **argv);
void MSG(const char *s1, const char *s2);
#endif // TMCMDLNOPTS_H__

149
MMPP:lib/examples/wheels.c Normal file
View File

@@ -0,0 +1,149 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Wheels testing tool *
******************************************************************************/
#include "wheelscmdlnopts.h"
#include <signal.h>
#include <stdio.h>
#include <libmmpp.h>
#include <usefull_macros.h>
// All return states of main():
enum{
RET_ALLOK = 0,
RET_NOTFOUND, // none of turrets found or didn't found seeking MCU
RET_ONLYONE, // only one turret found
RET_COMMERR, // communication error
RET_CANTINIT, // can't init turrets
RET_ERROR = 9, // uncoverable error - from libsnippets
RET_HELPCALL = 255 // user call help (or give wrong parameter[s]) - from libsnippets
};
static glob_pars *G;
static wheel_descr *wheels;
static int found;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void __attribute__((noreturn)) signals(int sig){
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
if(G->pidfile) // remove unnesessary PID file
unlink(G->pidfile);
restore_console();
del_wheels(wheels, found);
exit(sig);
}
/**
* @brief find_wheel_by_ID - find wheel by ID
* @param w - array of wheel descriptors
* @param N - length of w
* @param ID - sought wheel ID
* @return NULL if no such wheel or pointer to it
*/
wheel_descr *find_wheel_by_ID(wheel_descr *w, int N, char ID){
for(int i = 0; i < N; ++i){
if(w[i].ID == ID) return &w[i];
}
return NULL;
}
int main(int argc, char **argv){
initial_setup();
signal(SIGTERM, signals); // kill (-15)
signal(SIGINT, signals); // ctrl+C
signal(SIGQUIT, SIG_IGN); // ctrl+\ .
signal(SIGTSTP, SIG_IGN); // ctrl+Z
setbuf(stdout, NULL);
G = parse_args(argc, argv);
check4running(NULL, G->pidfile);
wheel_error werr;
found = find_wheels(&wheels, &werr);
if(found == 0) ERRX(_("No wheels found"));
else green("Found %d wheels\n", found);
if(werr != WHERR_ALLOK){
WARNX(_("Got wheel error: %d"), werr);
signals(1);
}
if(G->getstatus){
for(int i = 0; i < found; ++i){
green("Wheel #%d:\n", i);
printf("\tserial: %s\n", wheels[i].serial);
printf("\tID: %c\n", wheels[i].ID);
printf("\tname: %s\n", wheels[i].name);
printf("\tmaxpos: %d\n\n", wheels[i].maxpos);
}
}
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){ // count arguments of --wheel-id
while(G->wh_ids[Nw]) ++Nw;
}
if(G->gotopos){ // count arguments of --goto
while(G->gotopos[Ng]) ++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{ // 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]);
wheel_descr *w = find_wheel_by_ID(wheels, found, ID);
if(!w) WARNX(_("No wheel with ID %c found!"), ID);
else{
int pos = *G->gotopos[i];
if(!move_wheel(w, pos)){
WARNX(_("Can't rotate wheel %c to position %d"), ID, pos);
wheel_clear_err(w);
}else{ // wait until wheel is moving
while(WHEEL_MOVING == wheel_getpos(w)){
DBG("still moving");
usleep(100000);
}
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);
}
}
};
}
;
signals(0);
}

View File

@@ -0,0 +1,87 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wheelscmdlnopts.h"
#include <assert.h>
#include <libmmpp.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
int quiet = 0; // less messages @ stdout
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.pidfile = "/tmp/MMPP_wheels.pid"
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), N_("show this help")},
{"quiet", NO_ARGS, NULL, 'q', arg_none, APTR(&quiet), N_("don't show anything @screen from stdout")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), N_("PID-file name")},
{"status", NO_ARGS, NULL, 's', arg_none, APTR(&G.getstatus), N_("get device status")},
{"goto", MULT_PAR, NULL, 'g', arg_int, APTR(&G.gotopos), N_("go to given position")},
{"wheel-id",MULT_PAR, NULL, 'I', arg_string, APTR(&G.wh_ids), N_("name wheel by ID")},
{"home", NO_ARGS, NULL, 'H', arg_none, APTR(&G.gohome), N_("rotate all wheels to home position")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("%d unused parameters:\n", argc);
for(int i = 0; i < argc; ++i)
printf("\t%4d: %s\n", i+1, argv[i]);
}
return &G;
}
/**
* @brief MSG show coloured message if `quiet` not set
* !! This function adds trailing '\n' to message
* @param s1 - green part of message (may be null)
* @param s2 - normal colored part of messate (may be null)
*/
void MSG(const char *s1, const char *s2){
if(quiet) return;
if(s1){
green("%s%s", s1, s2 ? ": " : "\n");
}
if(s2) printf("%s\n", s2);
}

View File

@@ -0,0 +1,43 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TMCMDLNOPTS_H__
#define TMCMDLNOPTS_H__
#include <usefull_macros.h>
/*
* here are some typedef's for global data
*/
typedef struct{
char *pidfile; // pid file name
int gettemp; // get MCU temperature
int getstatus; // get status of all devices
char **wh_ids; // array of wheel ids
int **gotopos; // rotate wheels to given position
int gohome; // turn all wheels to home state
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
extern int quiet;
glob_pars *parse_args(int argc, char **argv);
void MSG(const char *s1, const char *s2);
#endif // TMCMDLNOPTS_H__

357
MMPP:lib/hidmanage.c Normal file
View File

@@ -0,0 +1,357 @@
/*
* hidmanage.c - manage HID devices
*
* Copyright 2016 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "common.h"
#include "hsfw.h"
#include "libmmpp.h"
#include <libudev.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>
#include <unistd.h>
static bool check_and_clear_err(int fd);
/**
* @brief writereg - write buffer to register & check answer
* @param fd - wheel fd
* @param buf - data to write
* @param l - lenght of `buf`
* @return 0 if all OK
*/
static int writereg(int fd, uint8_t *buf, int l){
uint8_t reg = buf[0];
if(ioctl(fd, HIDIOCSFEATURE(l), buf) < 0 || buf[0] != reg){
DBG("can't write register %d", reg);
return 1;
}
return 0;
}
/**
* @brief readreg - read register to buf & check answer
* @param fd - wheel fd
* @param buf - buffer for reading
* @param reg - register to read
* @param l - data length
* @return 0 if all OK
*/
static int readreg(int fd, uint8_t *buf, int reg, int l){
memset(buf, 0, l);
buf[0] = reg;
if(ioctl(fd, HIDIOCGFEATURE(l), buf) < 0 || buf[0] != reg){
DBG("can't read register %d", reg);
return 1;
}
return 0;
}
/**
* poll status register until moving stops
* @param fd - turret file descriptor
* @param msg - ==1 to show message
* @return current position or error code
*/
static wheel_status poll_wheelstatus(int fd){
uint8_t buf[REG_STATUS_LEN];
int i, stat = 1;
for(i = 0; i < HANDSHAKE_TRIES && stat; ++i){
stat = readreg(fd, buf, REG_STATUS, REG_STATUS_LEN);
if(!stat){
if(buf[2] == 0xff || buf[3] == 0xff) stat = 1;
}
if(buf[5]){
return WHEEL_ERROR;
}
}
if(i == HANDSHAKE_TRIES) return WHEEL_MOVING;
return buf[4];
}
// library function for position polling
wheel_status wheel_getpos(wheel_descr *w){
return poll_wheelstatus(w->fd);
}
/**
* @brief go_home - blocking go to home position
* @param fd - wheel file descriptor
* @return true if all OK
*/
static bool go_home(int fd){
poll_wheelstatus(fd); // wait for last moving
uint8_t buf[REG_HOME_LEN];
int i, stat = 1;
for(i = 0; i < HANDSHAKE_TRIES && stat; ++i){
memset(buf, 0, REG_HOME_LEN);
buf[0] = REG_HOME;
stat = writereg(fd, buf, REG_HOME_LEN);
if(stat){usleep(100000); continue;}
if((stat = readreg(fd, buf, REG_HOME, REG_HOME_LEN))) continue;
if(buf[1] != 0xff){
stat = 1; continue;
}else{
readreg(fd, buf, REG_HOME, REG_HOME_LEN);
break;
}
}
if(i == HANDSHAKE_TRIES) return false;
// now poll REG_STATUS
while(WHEEL_MOVING == poll_wheelstatus(fd)){DBG("still moving");};
check_and_clear_err(fd);
return true;
}
// the same as above, but non-blocking and library function
bool wheel_home(wheel_descr *w){
int fd = w->fd;
poll_wheelstatus(fd); // wait for last moving
uint8_t buf[REG_HOME_LEN];
int i, stat = 1;
for(i = 0; i < HANDSHAKE_TRIES && stat; ++i){
memset(buf, 0, REG_HOME_LEN);
buf[0] = REG_HOME;
stat = writereg(fd, buf, REG_HOME_LEN);
if(stat){usleep(100000); continue;}
if((stat = readreg(fd, buf, REG_HOME, REG_HOME_LEN))) continue;
if(buf[1] != 0xff){
stat = 1; continue;
}else{
readreg(fd, buf, REG_HOME, REG_HOME_LEN);
break;
}
}
if(i == HANDSHAKE_TRIES) return false;
return true;
}
/**
* @brief check_and_clear_err - check error state and clear it if need
* @param fd - opened device file descriptor
* @return true if all OK
*/
static bool check_and_clear_err(int fd){
int i, stat = 1;
uint8_t buf[REG_STATUS_LEN];
for(i = 0; i < HANDSHAKE_TRIES && stat; ++i){
stat = readreg(fd, buf, REG_STATUS, REG_STATUS_LEN);
if(stat) usleep(100000);
}
if(i == HANDSHAKE_TRIES) return false;
if(buf[1] != 0xff){
if(buf[5]){
stat = 1;
for(i = 0; i < HANDSHAKE_TRIES && stat; ++i){
memset(buf, 0, sizeof(buf));
buf[0] = REG_CLERR;
stat = writereg(fd, buf, REG_CLERR_LEN);
usleep(100000);
if(!stat) stat = readreg(fd, buf, REG_STATUS, REG_STATUS_LEN);
if(!stat && buf[5]) stat = 1;
}
if(i == HANDSHAKE_TRIES) return false;
}
readreg(fd, buf, REG_STATUS, REG_STATUS_LEN);
if(buf[1] != 0xff){
go_home(fd);
readreg(fd, buf, REG_STATUS, REG_STATUS_LEN);
}
}
return true;
}
// the same - but library function
bool wheel_clear_err(wheel_descr *w){
return check_and_clear_err(w->fd);
}
static void get_props(wheel_descr *wheel){
uint8_t buf[REG_NAME_LEN+1];
int fd = wheel->fd;
if(fd < 0){
return;
}
check_and_clear_err(fd);
// get status of wheel
if(readreg(fd, buf, REG_INFO, REG_INFO_LEN)) return;
wheel->ID = buf[5];
wheel->maxpos = buf[4];
DBG("Wheel with id '%c' and maxpos %d", wheel->ID, wheel->maxpos);
char *getwname(int id){
memset(buf, 0, sizeof(buf));
buf[0] = REG_NAME;
buf[1] = WHEEL_NAME;
buf[2] = id;
if(writereg(fd, buf, REG_NAME_LEN)) return NULL;
if(readreg(fd, buf, REG_NAME, REG_NAME_LEN)) return NULL;
if(buf[6]){
char *x = strchr((char*)&buf[6], ' ');
if(x) *x = 0;
return (char*)&buf[6];
}
else return NULL;
}
char *nm = getwname(wheel->ID);
if(nm){
strncpy(wheel->name, nm, 9);
DBG("Wheel name: %s", wheel->name);
}
}
/**
* Find turrets present
* @param wheels (o) - if not NULL - list of wheels found (like "ABC") (allocated here!)
* @param err (o) - status code
* @return amount of devices found
*
* WARNING! If there's more than one turret with wheels having same name
* access by wheel ID could lead undefined behaviour!
*/
int find_wheels(wheel_descr **wheels, wheel_error *err){
#define RETERR(x) do{if(err){*err = x;} return 0;}while(0)
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
wheel_descr *Found = NULL;
if(err) *err = WHERR_ALLOK;
// Create the udev object
udev = udev_new();
int N = 0;
if(!udev){
RETERR(WHERR_UDEV);
}
// Create a list of the devices in the 'hidraw' subsystem.
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "hidraw");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
// Check out each device found
udev_list_entry_foreach(dev_list_entry, devices){
const char *path;
struct udev_device *dev;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
const char *devpath = udev_device_get_devnode(dev);
DBG("Device Node Path: %s", devpath);
dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
if(!dev){
fprintf(stderr, "Unable to find parent usb device for %s\n", devpath);
udev_device_unref(dev);
continue;
}
const char *vid, *pid;
vid = udev_device_get_sysattr_value(dev,"idVendor");
pid = udev_device_get_sysattr_value(dev, "idProduct");
DBG(" VID/PID: %s/%s", vid, pid);
if(strcmp(vid, WHEEL_VID) == 0 && strcmp(pid, WHEEL_PID) == 0){
++N;
if(!Found){
Found = MALLOC(wheel_descr, 1);
}else{
Found = realloc(Found, sizeof(wheel_descr)*N);
if(!Found){
perror("realloc()");
exit(-1);
}
}
wheel_descr *curdev = &Found[N-1];
int fd = open(devpath, O_RDWR|O_NONBLOCK);
if(fd < 0){
if(err) *err = WHERR_CANTOPEN;
curdev->fd = -1;
}else
curdev->fd = fd;
DBG("%s %s",
udev_device_get_sysattr_value(dev,"manufacturer"),
udev_device_get_sysattr_value(dev,"product"));
curdev->serial = strdup(udev_device_get_sysattr_value(dev, "serial"));
DBG("serial: %s\n", curdev->serial);
get_props(curdev);
}
udev_device_unref(dev);
}
// Free the enumerator object
udev_enumerate_unref(enumerate);
if(wheels){
*wheels = Found;
}else
free(Found);
return N;
#undef RETERR
}
/**
* @brief del_wheels - free memory of wheels descriptors
* @param w - array of descriptors
* @param N - length of w
*/
void del_wheels(wheel_descr *w, int N){
if(N == 0) return;
for(int i = 0; i < N; ++i){
FREE(w[i].serial);
close(w[i].fd);
}
FREE(w);
}
/**
* @brief move_wheel - unblocking move given wheel
* @param w - wheel
* @param filter_pos - target position
* @return true if all OK
*/
bool move_wheel(wheel_descr *w, int filter_pos){
int wheel_fd = w->fd;
if(wheel_fd < 0) return false;
if(filter_pos == poll_wheelstatus(wheel_fd)){
DBG("Wheel %c is ON position (%d)", w->ID, filter_pos);
return true; // @ target position
}
uint8_t buf[REG_GOTO_LEN];
int i, stat = 1;
for(i = 0; i < HANDSHAKE_TRIES && stat; ++i){
DBG("i=%d",i);
memset(buf, 0, REG_GOTO_LEN);
buf[0] = REG_GOTO;
buf[1] = filter_pos;
stat = writereg(wheel_fd, buf, REG_GOTO_LEN);
usleep(100000);
if(stat) continue;
if((stat = readreg(wheel_fd, buf, REG_GOTO, REG_GOTO_LEN))) continue;
if(buf[1] != 0xff){
stat = 1; continue;
}else{
readreg(wheel_fd, buf, REG_GOTO, REG_HOME_LEN);
break;
}
}
if(i == HANDSHAKE_TRIES) return false;
//poll_wheelstatus(wheel_fd);
//check_and_clear_err(wheel_fd);
return true;
}

55
MMPP:lib/hsfw.h Normal file
View File

@@ -0,0 +1,55 @@
/*
* hsfw.h - functions for work with wheels
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __HSFW_H__
#define __HSFW_H__
#define REG_CLERR (0x02)
#define REG_CLERR_LEN (2)
#define REG_STATUS (0x0a)
#define REG_STATUS_LEN (6)
#define REG_INFO (0x0b)
#define REG_INFO_LEN (7)
#define REG_GOTO (0x14)
#define REG_GOTO_LEN (3)
#define REG_HOME (0x15)
#define REG_HOME_LEN (3)
#define REG_NAME (0x16)
#define REG_NAME_LEN (14)
// absolute max position (5 for wheels 'A'..'E' & 8 for wheels 'F'..'G')
#define ABS_MAX_POS_A (5)
#define ABS_MAX_POS_B (8)
// end of 5-position wheel descriptor range
#define POS_A_END ('E')
// end of 8-pos range
#define POS_B_END ('H')
enum name_cmd{
RESTORE_DEFVALS = 1,
RENAME_FILTER,
FILTER_NAME,
RENAME_WHEEL,
WHEEL_NAME
};
int get_max_pos(char filter_id);
#endif // __HSFW_H__

462
MMPP:lib/libmmpp.c Normal file
View File

@@ -0,0 +1,462 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libmmpp.h"
#include "tty_procs.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#if defined GETTEXT
#include <libintl.h>
#define _(String) gettext(String)
#define gettext_noop(String) String
#define N_(String) gettext_noop(String)
#else
#define _(String) (String)
#define N_(String) (String)
#endif
// tty device
static TTYdescr *dev = NULL;
static bool alive[3] = {false,true,true}; // ==true if controller answers, false if no
static bool reset[3] = {false,false,false}; // reset occured
/**
* @brief tty_tryopen - try to open serial device
* @param devnm - path to device
* @param spd - speed (number)
* @return 0 if all OK
*/
int mmpp_tryopen(char *devnm, int spd){
if(dev) close_tty(&dev);
dev = new_tty(devnm, spd, 256);
if(!dev) return 1;
if(!tty_open(dev, true)) return 1;
return 0;
}
/**
* @brief tty_close - close TTY device
*/
void mmpp_close(){
if(!dev) return;
close_tty(&dev);
}
/**
* test connection (1,2 -> ALIVE)
* @return 1 if none of MCU found, 0 if at least 1 found
*/
int mot_handshake(){
char buff[32], *ret;
int mcu;
FNAME();
for(mcu = 1; mcu < 3; ++mcu){
DBG("MCU #%d", mcu);
// check if MCU alive
sprintf(buff, "%d", mcu);
int notresp = 1;
alive[mcu] = false;
// make HANDSHAKE_TRIES tries
for(int tr = 0; tr < HANDSHAKE_TRIES; ++tr){
ret = tty_sendraw(buff);
if(ret && 0 == strcmp(ret, "ALIVE\n")){
notresp = 0;
break;
}
}
if(notresp){
continue;
}
alive[mcu] = true;
}
if(alive[1] == false && alive[2] == false) return 1;
return 0;
}
/**
* Get temperature of both MCUs
* @param t1, t2 (o) - temperatures of first and second MCUs (==-300 if error)
* @return amount of successful calls
*/
int get_temp(double *t1, double *t2){
char *val, buff[] = "xGT\n", *ans;
int ret = 0;
if(t1) *t1 = -300.;
if(t2) *t2 = -300.;
for(int i = 1; i < 3; ++i){
if(!alive[i]){
DBG("MCU %d didn't respond!", i);
continue;
}
buff[0] = '0' + (char)i;
if((ans = tty_sendraw(buff))){
val = keyval("TEMP", ans);
DBG("val: %s", val);
if(val){
++ret;
double t;
if(str2double(&t, val)){
if(i == 1){if(t1) *t1 = t/10.;}
else{if(t2) *t2 = t/10.;}
}
}
}
}
return ret;
}
/**
* @brief stop_all - send commands to stop all motors
* @return 0 if all OK, else return amount of motors failed to stop
*/
int stop_all(){
int ret = 4;
if(alive[1]){
if(SEND_ALLOK == tty_sendcmd("1M0S")) --ret;
if(SEND_ALLOK == tty_sendcmd("1M1S")) --ret;
}
if(alive[2]){
if(SEND_ALLOK == tty_sendcmd("2M0S")) --ret;
if(SEND_ALLOK == tty_sendcmd("2M1S")) --ret;
}
return ret;
}
/**
* @brief init_motors - BLOCKING (!!!) init all motors simultaneously (if they need to)
* @return 0 if all OK, or Nmcu*10+motnum for problem motor (motnum == 2 for problems with MCU)
* !!! in case of non-zero returning you should repeat initialisation
*/
int init_motors(){
FNAME();
char buf[32];
motor_state S[3];
#define RETVAL() (Nmcu*10+motnum)
int Nmcu, motnum, needinit = 0;
for(Nmcu = 1; Nmcu < 3; ++Nmcu){
if(!mot_getstatus(Nmcu, &S[Nmcu])) return 10*Nmcu+2;
for(motnum = 0; motnum < 2; ++motnum){
// check position
if(S[Nmcu].curpos[motnum] < 0) needinit = 1;
}
}
if(!needinit) return 0;
red("Need to init, start!\n");
for(Nmcu = 1; Nmcu < 3; ++Nmcu){
for(motnum = 0; motnum < 2; ++motnum){
int pos = S[Nmcu].curpos[motnum];
if(pos >= 0) continue;
// check if we are on zero endswitch
if(S[Nmcu].ESW_status[motnum][0] == ESW_HALL){ // move a little from zero esw
red("from esw\n");
sprintf(buf, "%dM%dM100", Nmcu, motnum);
if(SEND_ERR == tty_sendcmd(buf)){
return RETVAL();
}
mot_wait();
}
sprintf(buf, "%dM%dM-40000", Nmcu, motnum);
if(SEND_ALLOK != tty_sendcmd(buf)){
return RETVAL();
}
}}
mot_wait();
green("check\n");
// check current positions
for(Nmcu = 1; Nmcu < 3; ++Nmcu){
if(!mot_getstatus(Nmcu, &S[Nmcu])) return 10*Nmcu+2;
for(motnum = 0; motnum < 2; ++motnum){
if(S[Nmcu].curpos[motnum]){
return RETVAL();
}
}
}
return 0;
#undef RETVAL
}
/**
* Wait while all motors are stopped
* @return 0 if all OK
*/
int mot_wait(){
int failcount = 0;
bool mov[3] = {false,true, true};
if(!alive[1] && !alive[2]) return 0; // all are dead here
while(failcount < FAIL_TRIES && (mov[1] || mov[2])){
for(int Nmcu = 1; Nmcu < 3; ++Nmcu){
usleep(10000);
DBG("alive=%d/%d, mov=%d/%d", alive[1],alive[2],mov[1],mov[2]);
if(!alive[Nmcu] || !mov[Nmcu]) continue;
motor_state S;
if(!mot_getstatus(Nmcu, &S)){
++failcount;
}else failcount = 0;
if(S.state[0] == STP_SLEEP && S.state[1] == STP_SLEEP)
mov[Nmcu] = false;
}
}
if(failcount >= FAIL_TRIES){
return 1;
}
return 0;
}
/**
* read data from TTY
* WARNING! Not thread-safe!!!
* @return static buffer with data read or NULL
*/
char *tty_get(){
if(!dev) return NULL;
static char buf[TBUFLEN];
char *ptr = buf;
size_t L = 0, l = TBUFLEN;
double t0 = dtime();
*ptr = 0;
while(dtime() - t0 < TTYTIMEOUT && l){
size_t r = read_tty(dev);
if(!r) continue;
t0 = dtime();
if(r > l) r = l;
DBG("got %zd bytes: %s", r, dev->buf);
strncpy(ptr, dev->buf, r);
L += r; l -= r; ptr += r;
}
buf[L] = 0;
if(L){
return buf;
}
DBG("no answer");
return NULL;
}
/**
* Send given string command to port
* @return 0 if failed
*/
int tty_send(char *cmd){
if(!dev) return 0;
size_t l = 0;
char *s = cpy2buf(cmd, &l);
if(!s) return 0;
if(write_tty(dev->comfd, s, l)) return 0;
return 1;
}
/**
* send RAW string to port device
* @param string - string to send
* @return string received or NULL in case of error
*/
char* tty_sendraw(char *string){
if(!dev) return NULL;
DBG("sendraw %s", string);
if(!tty_send(string)) return NULL;
return tty_get();
}
/**
* Send given string command to port with answer analysis
* @return status
*/
ttysend_status tty_sendcmd(char *cmd){
DBG("SEND: %s", cmd);
if(!tty_send(cmd)) return SEND_ERR;
char *got = tty_get();
if(!got) return SEND_ERR;
if(strcmp(got, "ALLOK\n") == 0) return SEND_ALLOK;
else if(strcmp(got, "IsMoving") == 0) return SEND_ACTIVE;
else if(strcmp(got, "OnEndSwitch\n") == 0) return SEND_ESWITCH;
else if(strcmp(got, "ZeroMove") == 0) return SEND_ZEROMOVE;
else if(strcmp(got, "TooBigNumber") == 0) return SEND_TOOBIG;
return SEND_OTHER;
}
/**
* @brief mot_getstatus - get status of motors for given controller
* @param Nmcu - (1 or 2) MCU number
* @param s (o) - status
* @return true if all OK
*/
bool mot_getstatus(int Nmcu, motor_state *s){
char buff[32], *ans, cmd[4] = "xGS", *val;
if(Nmcu < 1 || Nmcu > 2) return false;
cmd[0] = '0' + Nmcu;
ans = tty_sendraw(cmd);
if(!ans){
alive[Nmcu] = false;
return false;
}
alive[Nmcu] = true;
motor_state S;
memset(&S, 0, sizeof(S));
val = keyval("WDGRESET", ans);
if(val) s->rst = RESET_WDG;
else{
val = keyval("SOFTRESET", ans);
if(val) s->rst = RESET_SW;
}
if(s->rst != RESET_NONE) reset[Nmcu] = true;
for(int i = 0; i < 2; ++i){
sprintf(buff, "POS%d", i);
val = keyval(buff, ans);
if(val){
S.curpos[i] = atoi(val);
}
sprintf(buff, "MOTOR%d", i);
val = keyval(buff, ans);
stp_state ms = STP_UNKNOWN;
if(val){
if(strcmp(val, "ACCEL") == 0) ms = STP_ACCEL;
else if(strcmp(val, "DECEL") == 0) ms = STP_DECEL;
else if(strcmp(val, "MOVE") == 0) ms = STP_MOVE;
else if(strcmp(val, "MOVETO0") == 0) ms = STP_MOVE0;
else if(strcmp(val, "MOVETO1") == 0) ms = STP_MOVE1;
else if(strcmp(val, "MVSLOW") == 0) ms = STP_MVSLOW;
else if(strcmp(val, "STOP") == 0) ms = STP_STOP;
else if(strcmp(val, "STOPZERO") == 0) ms = STP_STOPZERO;
else if(strcmp(val, "SLEEP") == 0) ms = STP_SLEEP;
S.state[i] = ms;
}
if(ms != STP_UNKNOWN && ms != STP_SLEEP){ // moving
sprintf(buff, "STEPSLEFT%d", i);
val = keyval(buff, ans);
if(val){
S.stepsleft[i] = atoi(val);
}
}
// end-switches
for(int j = 0; j < 2; ++j){
sprintf(buff, "ESW%d%d", i, j);
val = keyval(buff, ans);
if(val){
ESW_status s = ESW_ERROR;
if(strcmp(val, "RLSD") == 0) s = ESW_RELEASED;
else if(strcmp(val, "BTN") == 0) s = ESW_BUTTON;
else if(strcmp(val, "HALL") == 0) s = ESW_HALL;
S.ESW_status[i][j] = s;
}
}
}
if(s) *s = S;
return true;
}
/**
* @brief get_rst - get reset state for mcu #N
* @param N - number of MCU
* @param clear - true to clear reset state
* @return
*/
bool get_rst(int N, bool clear){
if(N < 1 || N > 2) return false;
bool state = reset[N];
if(clear) reset[N] = 0;
return state;
}
/**
* @brief get_alive - return true if MCU #N is alive
* @param N - number of MCU (1 or 2)
* @return alive[N]
*/
bool get_alive(int N){
if(N < 1 || N > 2) return false;
return alive[N];
}
/**
* @brief get_ADC - ADC values
* @param N - number of MCU (1 or 2)
* @param s (o) - state of all ADC channels
* @return true if all OK
*/
bool get_ADC(int N, ADC_state *s){
if(N < 1 || N > 2 || !s || !alive[N]) return false;
char buff[] = "xGAy", cmds[3] = "DIM", *ansv[] = {"VDD", "IMOT", "VMOT"};
double *vals[3] = {&s->Vdd, &s->Imot, &s->Vmot};
int got = 0;
buff[0] = '0' + N;
for(int i = 0; i < 3; ++i){
buff[3] = cmds[i];
char *ans = tty_sendraw(buff);
if(!ans) continue;
char *v = keyval(ansv[i], ans);
if(v){
double t;
if(str2double(&t, v)){
*vals[i] = t / 100.;
DBG("Got %s=%g", ansv[i], *vals[i]);
++got;
}
}
}
if(got != 3) return false;
return true;
}
/**
* @brief reset_MCU - reset given controller
* @param N - MCU # (1 or 2)
*/
void reset_MCU(int N){
if(N < 1 || N > 2) return;
char cmd[] = "xR";
cmd[0] = '0' + N;
tty_sendraw(cmd);
}
/**
* @brief movemotor - move motor
* @param mcu - MCU# (controller No, 1 or 2)
* @param motnum - motor# (0 or 1)
* @param steps - steps amount
* @param absmove - !=0 if steps are absolute position
* @return
*/
ttysend_status movemotor(int mcu, int motnum, int steps, int absmove){
if(mcu < 1 || mcu > 3 || motnum < 0 || motnum > 1) return SEND_OTHER;
char buf[32];
motor_state mstate;
if(!mot_getstatus(mcu, &mstate)) return SEND_ERR;
if(mstate.state[motnum] != STP_SLEEP) return SEND_ACTIVE;
int curpos = mstate.curpos[motnum];
if(curpos < 0){ // need to init
return SEND_NEEDINIT;
}
if(absmove){
if(motnum == 1){ // convert rotator angle to positive
int perrev = (mcu == 1) ? STEPSREV1 : STEPSREV2;
steps %= perrev;
if(steps < 0) steps += perrev;
}
if(steps < 0){
return SEND_NEGATMOVE;
}
steps -= curpos;
}
if(steps == 0){
return SEND_ZEROMOVE;
}
DBG("try to move motor%d of mcu %d for %d steps", motnum, mcu, steps);
snprintf(buf, 32, "%dM%dM%d", mcu, motnum, steps);
return tty_sendcmd(buf);
}

155
MMPP:lib/libmmpp.h Normal file
View File

@@ -0,0 +1,155 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef LIBMMPP_H__
#define LIBMMPP_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
// default baudrate for communication
#define BAUD_RATE (9600)
// amount of tries to establish handshake
#define HANDSHAKE_TRIES (10)
// how many fails can we omit when waiting for moving end
#define FAIL_TRIES (5)
// steps per full revolution for both rotation stages (1 - polaroid, 2 - waveplate)
#define STEPSREV1 (36000)
#define STEPSREV2 (28800)
/*
* MMPP controllers state
*/
// motors:
typedef enum{
STP_UNKNOWN, // wrong state
STP_SLEEP, // don't moving
STP_ACCEL, // start moving with acceleration
STP_MOVE, // moving with constant speed
STP_MVSLOW, // moving with slowest constant speed
STP_DECEL, // moving with deceleration
STP_STOP, // stop motor right now (by demand)
STP_STOPZERO, // stop motor and zero its position (on end-switch)
STP_MOVE0, // move towards 0 endswitch (negative direction)
STP_MOVE1 // move towards 1 endswitch (positive direction)
} stp_state;
// answers on motor commands
typedef enum{
SEND_ERR, // communication error
SEND_ALLOK, // no errors
SEND_ACTIVE, // motor is still moving
SEND_TOOBIG, // amount of steps too big
SEND_ZEROMOVE, // give 0 steps to move
SEND_ESWITCH, // staying on end-switch & try to move further
SEND_NEEDINIT, // motor needs initialisation
SEND_NEGATMOVE, // try to move to negative position
SEND_OTHER, // unknown state
} ttysend_status;
// end-switch state
typedef enum{
ESW_ERROR, // wrong value
ESW_RELEASED, // opened
ESW_HALL, // hall sensor
ESW_BUTTON // user button
} ESW_status;
// reset state
typedef enum{
RESET_NONE, // no sw/wd reset occured
RESET_SW, // software reset have been before last status call
RESET_WDG // watchdog reset -//-
} reset_status;
// motor state
typedef struct{
reset_status rst; // reset status (was MCU reseted by watchdog or software?)
stp_state state[2]; // status of stepper motor
int stepsleft[2]; // steps left to reach target position
int curpos[2]; // current position (negative for non-initialized state or error)
ESW_status ESW_status[2][2];// End-switches status, [i][j], i - motor, j - esw 0 or 1
} motor_state;
// ADC state
typedef struct{
double Vdd; // value of Vdd (+3.3V)
double Imot; // motors' current (Amperes)
double Vmot; // motors' voltage (+12V)
} ADC_state;
int mmpp_tryopen(char *dev, int spd);
void mmpp_close();
int mot_handshake();
bool mot_getstatus(int Nmcu, motor_state *s);
bool get_rst(int N, bool clear);
bool get_alive(int N);
int stop_all();
int get_temp(double *t1, double *t2);
int init_motors();
int mot_wait();
ttysend_status tty_sendcmd(char *cmd);
bool get_ADC(int N, ADC_state *s);
ttysend_status movemotor(int mcu, int motnum, int steps, int absmove);
void reset_MCU(int N);
char *tty_get();
int tty_send(char *cmd);
char* tty_sendraw(char *string);
/************************************************************************************
* Wheels management *
************************************************************************************/
#define WHEEL_VID "10c4"
#define WHEEL_PID "82cd"
// wheel descriptor
typedef struct{
int fd; // file descriptor of device
char *serial; // serial number
char ID; // identificator
char name[9]; // wheel name
int maxpos; // max position
} wheel_descr;
typedef enum{
WHERR_ALLOK // no errors
,WHERR_UDEV // udev error
,WHERR_CANTOPEN // can't open file device
} wheel_error;
int find_wheels(wheel_descr **wheels, wheel_error *err);
void del_wheels(wheel_descr *w, int N);
// return value of polling wheel status (if > -1 - current position)
typedef enum{
WHEEL_MOVING = -2 // still moving
,WHEEL_ERROR = -1 // communication error
,WHEEL_POSERR = 0 // wrong wheel position
,WHEEL_POS1, WHEEL_POS2, WHEEL_POS3, WHEEL_POS4, WHEEL_POS5, WHEEL_POS6
,WHEEL_POS7, WHEEL_POS8, WHEEL_POS9, WHEEL_POS10
} wheel_status;
wheel_status wheel_getpos(wheel_descr *w);
bool wheel_clear_err(wheel_descr *w);
bool move_wheel(wheel_descr *w, int filter_pos);
bool wheel_home(wheel_descr *w);
#ifdef __cplusplus
}
#endif
#endif // LIBMMPP_H__

10
MMPP:lib/mmpp.pc.in Normal file
View File

@@ -0,0 +1,10 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: @PROJ@
Description: MMPP (multi-mode photometer-polarimeter) control library
Version: @VERSION@
Libs: -L${libdir} -l@PROJ@
Cflags: -I${includedir}

310
MMPP:lib/tty_procs.c Normal file
View File

@@ -0,0 +1,310 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tty_procs.h"
#include <errno.h>
#include <fcntl.h> // read
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/ioctl.h> // ioctl
#include <sys/stat.h> // read
#include <sys/time.h> // gettimeofday
#include <unistd.h> // close
// buffer for data
static char bufo[TBUFLEN+1];
/**
* @brief my_alloc - safe memory allocation for macro ALLOC
* @param N - number of elements to allocate
* @param S - size of single element (typically sizeof)
* @return pointer to allocated memory area
*/
void *my_alloc(size_t N, size_t S){
void *p = calloc(N, S);
if(!p){
perror("malloc()");
exit(-1);
}
return p;
}
/**
* @brief dtime - function for different purposes that need to know time intervals
* @return double value: UNIX time in seconds
*/
double dtime(){
double t;
struct timeval tv;
gettimeofday(&tv, NULL);
t = tv.tv_sec + ((double)tv.tv_usec)/1e6;
return t;
}
typedef struct {
int speed; // communication speed in bauds/s
tcflag_t bspeed; // baudrate from termios.h
} spdtbl;
static const spdtbl speeds[] = {
{50, B50},
{75, B75},
{110, B110},
{134, B134},
{150, B150},
{200, B200},
{300, B300},
{600, B600},
{1200, B1200},
{1800, B1800},
{2400, B2400},
{4800, B4800},
{9600, B9600},
{19200, B19200},
{38400, B38400},
{57600, B57600},
{115200, B115200},
{230400, B230400},
{460800, B460800},
{500000, B500000},
{576000, B576000},
{921600, B921600},
{1000000, B1000000},
{1152000, B1152000},
{1500000, B1500000},
{2000000, B2000000},
{2500000, B2500000},
{3000000, B3000000},
{3500000, B3500000},
{4000000, B4000000},
{0,0}
};
/**
* @brief conv_spd - test if `speed` is in .speed of `speeds` array
* @param speed - integer speed (bps)
* @return 0 if error, Bxxx if all OK
*/
static tcflag_t conv_spd(int speed){
const spdtbl *spd = speeds;
int curspeed = 0;
do{
curspeed = spd->speed;
if(curspeed == speed)
return spd->bspeed;
++spd;
}while(curspeed);
return 0;
}
/**
* @brief new_tty - create new TTY structure with partially filled fields
* @param comdev - TTY device filename
* @param speed - speed (number)
* @param bufsz - size of buffer for input data (or 0 if opened only to write)
* @return pointer to TTY structure if all OK
*/
TTYdescr *new_tty(char *comdev, int speed, size_t bufsz){
tcflag_t spd = conv_spd(speed);
if(!spd) return NULL;
DBG("create %s with speed %d and buffer size %zd", comdev, speed, bufsz);
TTYdescr *descr = MALLOC(TTYdescr, 1);
descr->portname = strdup(comdev);
descr->baudrate = spd;
descr->speed = speed;
if(descr->portname && bufsz){
descr->buf = MALLOC(char, bufsz+1);
descr->bufsz = bufsz;
DBG("allocate buffer with size %zd", bufsz);
return descr;
}
FREE(descr->portname);
FREE(descr);
return NULL;
}
/**
* @brief tty_init - open & setup terminal
* @param descr (io) - port descriptor
* @return 0 if all OK or error code
*/
int tty_init(TTYdescr *descr){
DBG("\nOpen port..."); // |O_NONBLOCK
if ((descr->comfd = open(descr->portname, O_RDWR|O_NOCTTY)) < 0){
return errno ? errno : 1;
}
DBG("OK\nGet current settings...");
if(tcgetattr(descr->comfd, &descr->oldtty) < 0){ // Get settings
return errno ? errno : 1;
}
descr->tty = descr->oldtty;
descr->tty.c_lflag = 0; // ~(ICANON | ECHO | ECHOE | ISIG)
descr->tty.c_oflag = 0;
descr->tty.c_cflag = descr->baudrate|CS8|CREAD|CLOCAL; // 9.6k, 8N1, RW, ignore line ctrl
descr->tty.c_cc[VMIN] = 0; // non-canonical mode
descr->tty.c_cc[VTIME] = 5;
if(tcsetattr(descr->comfd, TCSANOW, &descr->tty) < 0){
return errno ? errno : 1;
}
// make exclusive open
if(descr->exclusive){
if(ioctl(descr->comfd, TIOCEXCL)){
perror("ioctl(TIOCEXCL)");
}}
DBG("OK");
return 0;
}
/**
* @brief tty_open - init & open tty device
* @param d - already filled structure (with new_tty or by hands)
* @param exclusive - == 1 to make exclusive open
* @return pointer to TTY structure if all OK
*/
TTYdescr *tty_open(TTYdescr *d, bool exclusive){
DBG("open %s with speed %d%s", d->portname, d->speed, exclusive ? "" : " (exclusive)");
if(!d || !d->portname || !d->baudrate) return NULL;
if(exclusive) d->exclusive = true;
else d->exclusive = false;
if(tty_init(d)) return NULL;
return d;
}
/**
* @brief restore_tty - restore opened TTY to previous state and close it
*/
void close_tty(TTYdescr **descr){
if(descr == NULL || *descr == NULL) return;
TTYdescr *d = *descr;
if(d->comfd){
DBG("close file..");
ioctl(d->comfd, TCSANOW, &d->oldtty); // return TTY to previous state
close(d->comfd);
}
FREE(d->portname);
FREE(d->buf);
FREE(*descr);
DBG("done!\n");
}
/**
* @brief read_tty - read data from TTY with 10ms timeout
* @param buff (o) - buffer for data read
* @param length - buffer len
* @return amount of bytes read
*/
size_t read_tty(TTYdescr *d){
if(d->comfd < 0) return 0;
size_t L = 0;
ssize_t l;
size_t length = d->bufsz;
char *ptr = d->buf;
fd_set rfds;
struct timeval tv;
int retval;
do{
l = 0;
FD_ZERO(&rfds);
FD_SET(d->comfd, &rfds);
tv.tv_sec = 0; tv.tv_usec = 50000;
retval = select(d->comfd + 1, &rfds, NULL, NULL, &tv);
if (!retval) break;
if(FD_ISSET(d->comfd, &rfds)){
if((l = read(d->comfd, ptr, length)) < 1){
break;
}
ptr += l; L += l;
length -= l;
}
}while(l && length);
d->buflen = L;
d->buf[L] = 0;
return (size_t)L;
}
/**
* copy given string to `buf` & add '\n' if need
* @return 0 if failed
*/
char *cpy2buf(char *string, size_t *l){
size_t L = strlen(string);
if(L > TBUFLEN-1){
return NULL;
}
strcpy(bufo, string);
if(bufo[L-1] != '\n'){
bufo[L++] = '\n';
bufo[L] = 0;
}
if(l) *l = L;
return bufo;
}
/**
* @brief write_tty - write data to serial port
* @param buff (i) - data to write
* @param length - its length
* @return 0 if all OK
*/
int write_tty(int comfd, const char *buff, size_t length){
ssize_t L = write(comfd, buff, length);
if((size_t)L != length){
return 1;
}
return 0;
}
/**
* return static buffer - value of `key`
* NOT THREAD SAFE!
*/
char *keyval(char *key, char *haystack){
static char buff[32];
char *got = strstr(haystack, key);
if(!got) return NULL;
got = strchr(got, '=');
if(!got) return NULL;
++got;
char *el = strchr(got, '\n');
if(!el) return NULL;
size_t L = (size_t)(el - got);
if(L > 31 || L == 0 || !*got) return NULL;
strncpy(buff, got, L);
buff[L] = 0;
return buff;
}
/**
* @brief str2double - safely convert data from string to double
* @param num (o) - double number read from string
* @param str (i) - input string
* @return 1 if success, 0 if fails
*/
int str2double(double *num, const char *str){
double res;
char *endptr;
if(!str) return 0;
res = strtod(str, &endptr);
if(endptr == str || *str == '\0' || *endptr != '\0'){
return false;
}
if(num) *num = res; // you may run it like myatod(NULL, str) to test wether str is double number
return true;
}

59
MMPP:lib/tty_procs.h Normal file
View File

@@ -0,0 +1,59 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TTY_PROCS_H__
#define TTY_PROCS_H__
#include "common.h"
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <termios.h> // tcsetattr, baudrates
// tty Rx static buffer
#define TBUFLEN (1024)
// read timeout (in seconds)
#define TTYTIMEOUT (0.05)
typedef struct {
char *portname; // device filename (should be freed before structure freeing)
int speed; // baudrate in human-readable format
tcflag_t baudrate; // baudrate (B...)
struct termios oldtty; // TTY flags for previous port settings
struct termios tty; // TTY flags for current settings
int comfd; // TTY file descriptor
char *buf; // buffer for data read
size_t bufsz; // size of buf
size_t buflen; // length of data read into buf
bool exclusive; // should device be exclusive opened
} TTYdescr;
int str2double(double *num, const char *str);
double dtime();
TTYdescr *new_tty(char *comdev, int speed, size_t bufsz);
int tty_init(TTYdescr *descr);
TTYdescr *tty_open(TTYdescr *d, bool exclusive);
size_t read_tty(TTYdescr *d);
int write_tty(int comfd, const char *buff, size_t length);
void close_tty(TTYdescr **descr);
int handshake();
char *keyval(char *key, char *haystack);
char *cpy2buf(char *string, size_t *l);
#endif // TTY_PROCS_H__