From 1788fea1c3145f6b9d8bb77f15244680789d66ec Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Tue, 14 May 2024 17:20:03 +0300 Subject: [PATCH] some changes in artifical-star plugin, add simplest centroids example --- Astar_cameras/artifical_star.c | 103 ++++++++------- CMakeLists.txt | 3 +- Readme.md | 16 +++ ccdcapture.c | 2 +- ccdcapture.h | 1 + examples/CMakeLists.txt | 3 + examples/ccd_client.c | 44 +++++-- examples/centroids.c | 232 +++++++++++++++++++++++++++++++++ examples/main.c | 107 --------------- locale/ru/messages.po | 2 +- 10 files changed, 346 insertions(+), 167 deletions(-) create mode 100644 examples/centroids.c delete mode 100644 examples/main.c diff --git a/Astar_cameras/artifical_star.c b/Astar_cameras/artifical_star.c index 1cad0b1..ab11657 100644 --- a/Astar_cameras/artifical_star.c +++ b/Astar_cameras/artifical_star.c @@ -56,11 +56,9 @@ static uint8_t bitpix = 16; // bit depth: 8 or 16 static il_Image *imagemask = NULL, *imagebg; // mask & background static char *maskfilename = NULL, *bgfilename = NULL; // filenames -// local counters of `mag`, `x` and `y` setters -static int magctr = 0, xctr = 0, yctr = 0; - typedef struct{ - int Nstars; // amount of stars + int Nstars; // amount of stars (1..MAX_STARS) + int curstarno; // current star number int x0, y0; // center of field in array coordinates double rotan0; // starting rotation angle double xs[MAX_STARS], ys[MAX_STARS]; // center of star field in array coordinates @@ -73,16 +71,18 @@ typedef struct{ double vY; // Y -//- double vR; // rotation speed (arcsec/s) double fluct; // stars position fluctuations (arcsec/sec) - double noiselambda; // poisson noice lambda value + double noiselambda; // poisson noice lambda value per second + double darklambda; // poisson noice lambda value for dark noise } settings_t; static settings_t settings = { .Nstars = 1, .x0 = 512, .y0 = 512, .fwhm = 1.5, .beta = 1., .scale = 0.03, - .fluct = 0.3, .noiselambda = 1., + .fluct = 0.3, .noiselambda = 1.1, .darklambda = 1. }; // min/max for parameters +static int nummin = 1, nummax = MAX_STARS, nomin = 0, nomax = MAX_STARS - 1; static const double fwhmmin = 0.1, fwhmmax = 10., scalemin = 0.001, scalemax = 3600., magmin = -30., magmax = 30.; static const double vmin = -20., vmax = 20., fluctmin = 0., fluctmax = 3., betamin = 0.5; static const double vrotmin = -36000., vrotmax = 36000.; // limit rotator speed to 10 degrees per second @@ -95,7 +95,7 @@ static double Xfluct = 0., Yfluct = 0.; // fluctuation additions in arcsec static il_Image *star = NULL; // template of star 0m static double FWHM0 = 0., scale0 = 0., BETA0 = 0.; // template fwhm/scale static int templ_wh = 0; // template width/height in pixels -static double noicelambdamin = 1.; +static double lambdamin = 1.; /** * @brief test_template - test star template and recalculate new if need @@ -140,28 +140,29 @@ DBG("MAKE STAR, wh=%d, beta=%g", templ_wh, settings.beta); for(int N = 0; N < settings.Nstars; ++N){\ int Xstar = Xc + (settings.xs[N]*cosr - settings.ys[N]*sinr)/settings.scale; \ int Ystar = Yc + (settings.ys[N]*cosr + settings.xs[N]*sinr)/settings.scale;\ + fprintf(stderr, "Xstar=%d, Ystar=%d\t", Xstar, Ystar);\ if(Xstar - tw2 < 0){\ - X0 = tw2 - Xstar + 1;\ + X0 = tw2 - Xstar;\ x0 = 0;\ }else{\ - X0 = 1; x0 = Xstar - tw2;\ + X0 = 0; x0 = Xstar - tw2;\ }\ if(Ystar - tw2 < 0){\ - Y0 = tw2 - Ystar + 1;\ + Y0 = tw2 - Ystar;\ y0 = 0;\ }else{\ - Y0 = 1; y0 = Ystar - tw2;\ + Y0 = 0; y0 = Ystar - tw2;\ }\ if(Xstar + tw2 > w-1){\ /* templ_wh - (Xc + tw2 - (w - 1))*/ \ X1 = templ_wh - Xstar - tw2 + w - 1; \ - }else X1 = templ_wh;\ + }else X1 = templ_wh - 1;\ if(Ystar + tw2 > h-1){\ Y1 = templ_wh - Ystar - tw2 + h - 1;\ - }else Y1 = templ_wh;\ - double mul = exptime * maxval * pow(10, -0.4*settings.mag[N]); /* multiplier due to "star" magnitude */ \ + }else Y1 = templ_wh - 1;\ + double mul = 100. * exptime * maxval * pow(10, -0.4*settings.mag[N]); /* multiplier due to "star" magnitude */ \ /* check if the 'star' out of frame */ \ - DBG("X0=%d, X1=%d, Y0=%d, Y1=%d, x0=%d, y0=%d, mul=%g", X0,X1,Y0,Y1,x0,y0, mul);\ + fprintf(stderr, "X0=%d, X1=%d, Y0=%d, Y1=%d, x0=%d, y0=%d, mul=%g\n", X0,X1,Y0,Y1,x0,y0, mul);\ if(X0 < 0 || X0 > templ_wh - 1 || Y0 < 0 || Y0 > templ_wh - 1) continue;\ if(x0 < 0 || x0 > w-1 || y0 < 0 || y0 > h-1) continue;\ if(X1 < 0 || X1 > templ_wh || Y1 < 0 || Y1 > templ_wh) continue;\ @@ -204,12 +205,13 @@ DBG("MAKE STAR, wh=%d, beta=%g", templ_wh, settings.beta); }\ } \ }\ - if(settings.noiselambda > 1.){ /* apply noise */ \ + if(settings.noiselambda > 1. || settings.darklambda > 1.){ /* apply noise */ \ w *= h; \ type *out = (type*)ima->data;\ OMP_FOR()\ for(int i = 0; i < w; ++i){\ - type p = il_Poisson(settings.noiselambda); \ + type p = il_Poisson((settings.noiselambda - 1.)*exptime + settings.darklambda); \ + /* type p = rand() % 5; */ \ out[i] = (out[i] + p > maxval) ? maxval : out[i] + p; \ }\ }\ @@ -243,7 +245,7 @@ static int campoll(cc_capture_status *st, float *remain){ static int startexp(){ if(capstat == CAPTURE_PROCESS) return FALSE; capstat = CAPTURE_PROCESS; - double Tnow = dtime(), dT = Tnow - texpstart; + double Tnow = dtime(), dT = Tnow - texpstart, Xcd, Ycd; if(dT < 0.) dT = 0.; else if(dT > 1.) dT = 1.; // dT for fluctuations amplitude if(Tstart < 0.) Tstart = Tnow; @@ -257,9 +259,9 @@ static int startexp(){ else if(rotangle > rotanmax) rotangle -= 360.*3600.; sincos(rotangle * M_PI/3600./180., &sinr, &cosr); double xx = dX/settings.scale, yy = dY/settings.scale; - Xc = xx*cosr - yy*sinr + settings.x0 - camera.array.xoff - camera.geometry.xoff; - Yc = yy*cosr + xx*sinr + settings.y0 - camera.array.yoff - camera.geometry.yoff; - DBG("dX=%g, dY=%g; Xc=%d, Yc=%d", dX, dY, Xc, Yc); + Xcd = xx*cosr - yy*sinr + settings.x0 - camera.array.xoff - camera.geometry.xoff; + Ycd = yy*cosr + xx*sinr + settings.y0 - camera.array.yoff - camera.geometry.yoff; + DBG("dX=%g, dY=%g; Xc=%g, Yc=%g", dX, dY, Xcd, Ycd); // add fluctuations double fx = settings.fluct * dT * (2.*drand48() - 1.); // [-fluct*dT, +fluct*dT] double fy = settings.fluct * dT * (2.*drand48() - 1.); @@ -269,7 +271,8 @@ static int startexp(){ if(Yfluct + fy < -settings.fluct || Yfluct + fy > settings.fluct) Yfluct -= fy; else Yfluct += fy; DBG("Xfluct=%g, Yfluct=%g, pix: %g/%g\n\n", Xfluct, Yfluct, Xfluct/settings.scale, Yfluct/settings.scale); - Xc += Xfluct/settings.scale; Yc += Yfluct/settings.scale; + Xcd += Xfluct/settings.scale; Ycd += Yfluct/settings.scale; + Xc = (int) Xcd; Yc = (int) Ycd; test_template(); return TRUE; } @@ -450,12 +453,28 @@ static int whlgetnam(char *n, int l){ strncpy(n, "Dummy filter wheel", l); return TRUE; } +/* +static cc_hresult setstarsamount(const char *str, cc_charbuff *ans){ + char buf[32], *bptr = buf; + strncpy(buf, str, 31); + char *val = cc_get_keyval(&bptr); + if(val){ // setter + ; + } + snprintf(buf, 31, "nstars=%d", settings.Nstars); + cc_charbufaddline(ans, buf); + return RESULT_SILENCE; +} + +static cc_hresult setstarno(const char *str, cc_charbuff *ans){ + return RESULT_SILENCE; +}*/ static cc_hresult setXYs(const char *str, cc_charbuff *ans){ char buf[256], *bptr = buf; strncpy(buf, str, 255); char *val = cc_get_keyval(&bptr); - if(!val){ // getter + if(!val){ // getter - show all for(int i = 0; i < settings.Nstars; ++i){ snprintf(buf, 255, "x[%d]=%g, y[%d]=%g\n", i, settings.xs[i], i, settings.ys[i]); cc_charbufaddline(ans, buf); @@ -464,23 +483,18 @@ static cc_hresult setXYs(const char *str, cc_charbuff *ans){ } double dval = atof(val); if(strcmp(bptr, "x") == 0){ - if(xctr >= MAX_STARS){ - cc_charbufaddline(ans, "MAX: " STR(MAX_STARS) "stars"); - return RESULT_FAIL; - } - DBG("x[%d]=%g", xctr, dval); - settings.xs[xctr++] = dval; + DBG("x[%d]=%g", settings.curstarno, dval); + settings.xs[settings.curstarno] = dval; + snprintf(buf, 255, "x[%d]=%g\n", settings.curstarno, dval); + cc_charbufaddline(ans, buf); } else if(strcmp(bptr, "y") == 0){ - if(yctr >= MAX_STARS){ - cc_charbufaddline(ans, "MAX: " STR(MAX_STARS) "stars"); - return RESULT_FAIL; - } - DBG("y[%d]=%g", yctr, dval); - settings.ys[yctr++] = dval; + DBG("y[%d]=%g", settings.curstarno, dval); + settings.ys[settings.curstarno] = dval; + snprintf(buf, 255, "y[%d]=%g\n", settings.curstarno, dval); + cc_charbufaddline(ans, buf); } else{ return RESULT_BADKEY;} // unreachable - settings.Nstars = (xctr < yctr) ? xctr : yctr; - DBG("Nstars=%d", settings.Nstars); + return RESULT_SILENCE; } @@ -502,12 +516,10 @@ static cc_hresult setmag(const char *str, cc_charbuff *ans){ cc_charbufaddline(ans, buf); return RESULT_BADVAL; } - if(magctr >= MAX_STARS){ - cc_charbufaddline(ans, "MAX: " STR(MAX_STARS) "stars"); - return RESULT_FAIL; - } - DBG("mag[%d]=%g", magctr, dval); - settings.mag[magctr++] = dval; + DBG("mag[%d]=%g", settings.curstarno, dval); + settings.mag[settings.curstarno] = dval; + snprintf(buf, 255, "mag[%d]=%g\n", settings.curstarno, dval); + cc_charbufaddline(ans, buf); return RESULT_SILENCE; } @@ -559,11 +571,14 @@ static cc_hresult loadbg(const char *str, cc_charbuff *ans){ static cc_parhandler_t handlers[] = { {"beta", "Moffat `beta` parameter", NULL, (void*)&settings.beta, (void*)&betamin, NULL, CC_PAR_DOUBLE}, {"bkg", "load background image", loadbg, (void*)&bgfilename, NULL, NULL, CC_PAR_STRING}, + {"curstar", "set number of current star to change parameters", NULL, (void*)&settings.curstarno, (void*)&nomin, (void*)&nomax, CC_PAR_INT}, {"fluct", "stars position fluctuations (arcsec per sec)", NULL, (void*)&settings.fluct, (void*)&fluctmin, (void*)&fluctmax, CC_PAR_DOUBLE}, {"fwhm", "stars min FWHM, arcsec", NULL, (void*)&settings.fwhm, (void*)&fwhmmin, (void*)&fwhmmax, CC_PAR_DOUBLE}, - {"lambda", "Poisson noice lambda value (>1)", NULL, (void*)&settings.noiselambda, (void*)&noicelambdamin, NULL, CC_PAR_DOUBLE}, + {"lambda", "Poisson noice lambda value (>1) per second", NULL, (void*)&settings.noiselambda, (void*)&lambdamin, NULL, CC_PAR_DOUBLE}, + {"lambda0", "Poisson noice lambda value (>1) for dark noise", NULL, (void*)&settings.darklambda, (void*)&lambdamin, NULL, CC_PAR_DOUBLE}, {"mag", "Next star magnitude: 0m is 0xffff/0xff (16/8 bit) ADUs per second", setmag, NULL, (void*)&magmin, (void*)&magmax, CC_PAR_DOUBLE}, {"mask", "load mask image (binary, ANDed)", loadmask, (void*)&maskfilename, NULL, NULL, CC_PAR_STRING}, + {"nstars", "set amount of stars (not more than " Stringify(MAX_STARS) ")", NULL, (void*)&settings.Nstars, (void*)&nummin, (void*)&nummax, CC_PAR_INT}, {"rotangle", "Starting rotation angle (arcsec)", NULL, (void*)&settings.rotan0, (void*)&rotanmin, (void*)&rotanmax, CC_PAR_DOUBLE}, {"scale", "CCD scale: arcsec/pix", NULL, (void*)&settings.scale, (void*)&scalemin, (void*)&scalemax, CC_PAR_DOUBLE}, {"vr", "rotation speed (arcsec/s)", NULL, (void*)&settings.vR, (void*)&vrotmin, (void*)&vrotmax, CC_PAR_DOUBLE}, diff --git a/CMakeLists.txt b/CMakeLists.txt index fc5eee2..e66faac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,12 +133,13 @@ configure_file("${PROJLIB}.pc.in" ${PCFILE} @ONLY) set_target_properties(${PROJLIB} PROPERTIES VERSION ${VERSION}) set_target_properties(${PROJLIB} PROPERTIES PUBLIC_HEADER ${LIBHEADER}) +include(GNUInstallDirs) # Installation of the program install(FILES ${MO_FILE} DESTINATION "share/locale/ru/LC_MESSAGES") install(TARGETS ${PROJ} DESTINATION "bin") install(TARGETS ${PROJLIB} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) -install(FILES ${PCFILE} DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) +install(FILES ${PCFILE} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) find_package(Gettext REQUIRED) diff --git a/Readme.md b/Readme.md index 75ce169..53e67be 100644 --- a/Readme.md +++ b/Readme.md @@ -264,3 +264,19 @@ Commands: - yc = (int) - y center of field in array coordinates +## Examples + +### ccd_client + +Connect to server, send `infty` and simply get up to N images (or quit if no images in 5 seconds). + +Usage: +``` + -U, --isun use UNIX socket + -h, --help show this help + -i, --infty run in infinity capturing loop (else - request each frame) + -k, --shmkey=arg shared memory (with image data) key (default: 7777777) + -n, --nframes=arg make series of N frames (2 default) + -s, --sock=arg command socket name or port + -x, --exptime=arg set exposure time to given value (seconds!) +``` diff --git a/ccdcapture.c b/ccdcapture.c index 378789d..65c13af 100644 --- a/ccdcapture.c +++ b/ccdcapture.c @@ -308,7 +308,7 @@ cc_IMG *cc_getshm(key_t key, size_t imsize){ // find plugin -static void *open_plugin(const char *name){ +void *open_plugin(const char *name){ DBG("try to open lib %s", name); void* dlh = dlopen(name, RTLD_NOLOAD); // library may be already opened if(!dlh){ diff --git a/ccdcapture.h b/ccdcapture.h index 85d3723..ba1bcc0 100644 --- a/ccdcapture.h +++ b/ccdcapture.h @@ -297,6 +297,7 @@ typedef struct{ // custom plugin parameters cc_hresult cc_plugin_customcmd(const char *str, cc_parhandler_t *handlers, cc_charbuff *ans); +void *open_plugin(const char *name); cc_Focuser *open_focuser(const char *pluginname); cc_Camera *open_camera(const char *pluginname); cc_Wheel *open_wheel(const char *pluginname); diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 53c2310..aed2095 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,3 +3,6 @@ cmake_minimum_required(VERSION 3.20) include_directories(..) add_executable(ccd_client ccd_client.c) target_link_libraries(ccd_client ccdcapture usefull_macros) + +add_executable(centroids centroids.c) +target_link_libraries(centroids ccdcapture usefull_macros improc m) diff --git a/examples/ccd_client.c b/examples/ccd_client.c index bceeef2..f1f263b 100644 --- a/examples/ccd_client.c +++ b/examples/ccd_client.c @@ -40,7 +40,8 @@ typedef struct{ static glob_pars G = { .shmkey = 7777777, - .nframes = 10 + .nframes = 2, + .exptime = -1. }; /* @@ -63,6 +64,8 @@ static int refresh_img(){ if(!shimg) return FALSE; static size_t imnumber = 0; if(shimg->imnumber == imnumber) return FALSE; + double ts = dtime(); + if(ts - shimg->timestamp > G.exptime + 1.) return FALSE; // too old image imnumber = shimg->imnumber; void *optr = img.data; memcpy(&img, shimg, sizeof(img)); @@ -82,6 +85,7 @@ int main(int argc, char **argv){ for(int i = 0; i < argc; ++i) printf("%4d: %s\n", i, argv[i]); } + if(G.nframes < 1) ERRX("nframes should be > 0"); if(!G.sockname) ERRX("Point socket name or port"); cc_strbuff *cbuf = cc_strbufnew(BUFSIZ, STRBUFSZ); int sock = cc_open_socket(FALSE, G.sockname, !G.isun); @@ -93,27 +97,39 @@ int main(int argc, char **argv){ red("Can't read shmkey, try yours\n"); shmemkey = G.shmkey; } - if(RESULT_OK == cc_setint(sock, cbuf, CC_CMD_INFTY, 1)) green("ask for INFTY\n"); - else red("Can't ask for INFTY\n"); + if(G.infty){ + if(RESULT_OK == cc_setint(sock, cbuf, CC_CMD_INFTY, 1)) green("ask for INFTY\n"); + else red("Can't ask for INFTY\n"); + } float xt = 0.f; if(RESULT_OK == cc_getfloat(sock, cbuf, CC_CMD_EXPOSITION, &xt)){ green("Old exp time: %gs\n", xt); } fflush(stdout); - if(RESULT_OK == cc_setfloat(sock, cbuf, CC_CMD_EXPOSITION, 0.5)) green("ask for exptime 0.5s\n"); - else red("Can't change exptime to 0.5s\n"); + if(G.exptime > 0.){ + if(RESULT_OK == cc_setfloat(sock, cbuf, CC_CMD_EXPOSITION, G.exptime)) green("ask for exptime %gs\n", G.exptime); + else red("Can't change exptime to %gs\n", G.exptime); + } shimg = cc_getshm(shmemkey, 0); if(!shimg) ERRX("Can't get shared memory segment"); int i = 0; time_t oldtime = time(NULL); - double oldtimestamp = shimg->timestamp; + double waittime = ((int)G.exptime) + 5.; do{ - if(cc_refreshbuf(sock, cbuf) && cc_getline(cbuf)){ - printf("\t\tServer sent: `%s`\n", cbuf->string); + if(!G.infty){ // ask new image in non-infty mode + if(RESULT_OK != cc_setint(sock, cbuf, CC_CMD_EXPSTATE, CAMERA_CAPTURE)){ + WARNX("Can't ask new image\n"); + usleep(1000); + continue; + } + usleep(1000); + } + if(cc_refreshbuf(sock, cbuf)){ + while(cc_getline(cbuf)) printf("\t\tServer sent: `%s`\n", cbuf->string); } time_t now = time(NULL); - if(now - oldtime > 5){ - WARNX("No new images for 5 seconds"); + if(now - oldtime > waittime){ + WARNX("No new images for %g seconds", waittime); break; } if(!refresh_img()){ @@ -122,9 +138,11 @@ int main(int argc, char **argv){ } ++i; oldtime = now; - printf("Got image #%zd, size %dx%d, bitpix %d, time %g\n", img.imnumber, img.w, img.h, img.bitpix, img.timestamp-oldtimestamp); - }while(i < 2); - if(RESULT_OK != cc_setint(sock, cbuf, CC_CMD_INFTY, 0)) red("Can't clear INFTY\n"); + printf("Got image #%zd, size %dx%d, bitpix %d, time %.2f\n", img.imnumber, img.w, img.h, img.bitpix, img.timestamp); + }while(i < G.nframes); + if(G.infty){ + if(RESULT_OK != cc_setint(sock, cbuf, CC_CMD_INFTY, 0)) red("Can't clear INFTY\n"); + } if(xt > 0.){ if(RESULT_OK != cc_setfloat(sock, cbuf, CC_CMD_EXPOSITION, xt)) red("Can't return exptime to %gs\n", xt); } diff --git a/examples/centroids.c b/examples/centroids.c new file mode 100644 index 0000000..478e6b4 --- /dev/null +++ b/examples/centroids.c @@ -0,0 +1,232 @@ +/* + * This file is part of the CCD_Capture project. + * Copyright 2024 Edward V. Emelianov . + * + * 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 . + */ + +/* + * A bit more usefull than ccd_client: get images and calculate their gravity center + */ + +#include +#include +#include +#include +#include + +#include "socket.h" + +#define STRBUFSZ (256) + +typedef struct{ + int isun; // command socket is UNIX socket instead of INET + int shmkey; // shared memory (with image data) key + int infty; // run in infinity loop (if not - run by requests) + int nframes; // amount of frames to take + int help; // show this help + double exptime; // time of exposition in seconds + double background; // fixed bg + char *sockname; // UNIX socket name of command socket or port of local socket + char *outfile; // output file name +} glob_pars; + + +static glob_pars G = { + .shmkey = 7777777, + .nframes = 10, + .background = -1., + .exptime = -1. +}; + +/* + * Define command line options by filling structure: + * name has_arg flag val type argptr help +*/ +myoption cmdlnopts[] = { + {"background",NEED_ARG, NULL, 'b', arg_double, APTR(&G.background),"fixed background level"}, + {"sock", NEED_ARG, NULL, 's', arg_string, APTR(&G.sockname), "command socket name or port"}, + {"isun", NO_ARGS, NULL, 'U', arg_int, APTR(&G.isun), "use UNIX socket"}, + {"shmkey", NEED_ARG, NULL, 'k', arg_int, APTR(&G.shmkey), "shared memory (with image data) key (default: 7777777)"}, + {"infty", NO_ARGS, NULL, 'i', arg_int, APTR(&G.infty), "run in infinity capturing loop (else - request each frame)"}, + {"nframes", NEED_ARG, NULL, 'n', arg_int, APTR(&G.nframes), "make series of N frames (default: 10)"}, + {"exptime", NEED_ARG, NULL, 'x', arg_double, APTR(&G.exptime), "set exposure time to given value (seconds!)"}, + {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"}, + {"output", NEED_ARG, NULL, 'o', arg_string, APTR(&G.outfile), "output file with T/x/y/w"}, +}; + +static cc_IMG *shimg = NULL, img = {0}; +static FILE *out = NULL; +//static double *sq = NULL; +//static int sqsz = 0; + +static void calcimg(){ + int H = img.h, W = img.w, npix = 0; + /*int m = (H>W) ? H : W; + if(m > sqsz){ + sq = realloc(sq, sizeof(double)*m); + for(int i = sqsz; i < m; ++i) sq[i] = (double)i*i; + sqsz = m; + }*/ + double Xs = 0., X2s = 0., Ys = 0., Y2s = 0., Is = 0; + double t0 = dtime(); + uint8_t *d = (uint8_t*)img.data; + double Timestamp = img.timestamp; + static double bg = -1.; + if(bg < 0.){ + if(G.background >= 0.) bg = G.background; + else{ + il_Image *ii = il_u82Image(d, W, H); + if(ii){ + il_Image_background(ii, &bg); + il_Image_free(&ii); + }else bg = 5.; + } + } + printf("bg=%g\n", bg); +/* SYNC lasts too long! +#pragma omp parallel +{ + double Xsp = 0., X2sp = 0., Ysp = 0., Y2sp = 0., Isp = 0; + #pragma omp for nowait + for(int x = 0; x < W; ++x){ + for(int y = 0; y < H; ++y){ + double val = *d++ - bg; + if(val < DBL_EPSILON) continue; + Xsp += val * x; Ysp += val * y; X2sp += val * x * x; Y2sp += val * y * y; + Isp += val; + } + } + #pragma omp critical + { + Xs += Xsp; Ys += Ysp; X2s += X2sp; Y2s += Y2sp; Is += Isp; + } +}*/ + for(int y = 0; y < H; ++y){ + for(int x = 0; x < W; ++x){ + // *d = (*d > bg) ? *d - bg : 0; + double val = *d++ - bg; + if(val < DBL_EPSILON) continue; + //Xs += val * x; Ys += val * y; X2s += val * sq[x]; Y2s += val * sq[y]; - no time advantage + Xs += val * x; Ys += val * y; X2s += val * x * x; Y2s += val * y * y; + Is += val; + ++npix; + } + } + /*char buf[256]; + snprintf(buf, 255, "im%06zd.png", img.imnumber); + il_write_png(buf, W, H, 1, img.data);*/ + printf("Xs=%g, X2s=%g, Ys=%g, Y2s=%g, Is=%g\n", Xs, X2s, Ys, Y2s, Is); + double xc = Xs/Is, yc = Ys/Is, sX = sqrt(X2s/Is-xc*xc), sY = sqrt(Y2s/Is-yc*yc); + green("Xc = %.2f, Yc=%.2f, Xcs=%.2f, Ycs=%.2f, I=%.1f, T=%gms; npix=%d\n", xc, yc, sX, sY, Is, (dtime() - t0)*1e3, npix); + if(out) fprintf(out, "%.2f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n", Timestamp, xc, yc, Is, sX, sY); +} + +static int refresh_img(){ + if(!shimg) return FALSE; + static size_t imnumber = 0; + if(shimg->imnumber == imnumber) return FALSE; + double ts = dtime(); + if(ts - shimg->timestamp > G.exptime + 1.) return FALSE; // too old image + imnumber = shimg->imnumber; + void *optr = img.data; + memcpy(&img, shimg, sizeof(img)); + img.data = realloc(optr, img.bytelen); + memcpy(img.data, (uint8_t*)shimg + sizeof(cc_IMG), img.bytelen); + calcimg(); + return TRUE; +} + +int main(int argc, char **argv){ + initial_setup(); + parseargs(&argc, &argv, cmdlnopts); + if(G.help) showhelp(-1, cmdlnopts); + if(argc > 0){ + WARNX("%d unused parameters:", argc); + for(int i = 0; i < argc; ++i) + printf("%4d: %s\n", i, argv[i]); + } + if(G.nframes < 1) ERRX("nframes should be > 0"); + if(!G.sockname) ERRX("Point socket name or port"); + if(G.outfile){ + out = fopen(G.outfile, "w"); + if(!out) ERR("Can't open %s for writing"); + fprintf(out, "# Time\t\tXc\tYc\tI\tsX\tsY\t\n"); + } + cc_strbuff *cbuf = cc_strbufnew(BUFSIZ, STRBUFSZ); + int sock = cc_open_socket(FALSE, G.sockname, !G.isun); + if(sock < 0) ERR("Can't open socket %s", G.sockname); + int shmemkey = 0; + if(RESULT_OK == cc_getint(sock, cbuf, CC_CMD_SHMEMKEY, &shmemkey)){ + green("Got shm key: %d\n", shmemkey); + }else{ + red("Can't read shmkey, try yours\n"); + shmemkey = G.shmkey; + } + if(RESULT_OK != cc_setint(sock, cbuf, CC_CMD_8BIT, 1)){ + ERRX("Can't set 8 bit mode"); + } + if(G.infty){ + if(RESULT_OK == cc_setint(sock, cbuf, CC_CMD_INFTY, 1)) green("ask for INFTY\n"); + else red("Can't ask for INFTY\n"); + } + float xt = 0.f; + if(RESULT_OK == cc_getfloat(sock, cbuf, CC_CMD_EXPOSITION, &xt)){ + green("Old exp time: %gs\n", xt); + } + fflush(stdout); + if(G.exptime > 0.){ + if(RESULT_OK == cc_setfloat(sock, cbuf, CC_CMD_EXPOSITION, G.exptime)) green("ask for exptime %gs\n", G.exptime); + else red("Can't change exptime to %gs\n", G.exptime); + }else G.exptime = xt; + shimg = cc_getshm(shmemkey, 0); + if(!shimg) ERRX("Can't get shared memory segment"); + int i = 0; + time_t oldtime = time(NULL); + double waittime = ((int)G.exptime) + 5.; + do{ + if(!G.infty){ // ask new image in non-infty mode + if(RESULT_OK != cc_setint(sock, cbuf, CC_CMD_EXPSTATE, CAMERA_CAPTURE)){ + WARNX("Can't ask new image\n"); + usleep(1000); + continue; + } + usleep(1000); + } + if(cc_refreshbuf(sock, cbuf)){ + while(cc_getline(cbuf)) printf("\t\tServer sent: `%s`\n", cbuf->string); + } + time_t now = time(NULL); + if(now - oldtime > waittime){ + WARNX("No new images for %g seconds", waittime); + break; + } + if(!refresh_img()){ + usleep(1000); + continue; + } + ++i; + oldtime = now; + printf("Got image #%zd, size %dx%d, bitpix %d, time %.2f\n", img.imnumber, img.w, img.h, img.bitpix, img.timestamp); + }while(i < G.nframes); + if(G.infty){ + if(RESULT_OK != cc_setint(sock, cbuf, CC_CMD_INFTY, 0)) red("Can't clear INFTY\n"); + } + if(xt > 0.){ + if(RESULT_OK != cc_setfloat(sock, cbuf, CC_CMD_EXPOSITION, xt)) red("Can't return exptime to %gs\n", xt); + } + cc_strbufdel(&cbuf); + close(sock); + return 0; +} diff --git a/examples/main.c b/examples/main.c deleted file mode 100644 index 1746d1b..0000000 --- a/examples/main.c +++ /dev/null @@ -1,107 +0,0 @@ -/* - * This file is part of the CCD_Capture project. - * Copyright 2023 Edward V. Emelianov . - * - * 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 . - */ - -/* - * A simple example to take 8-bit images with default size and given exposition time. - * Works in `infinity` mode or requesting each file after receiving previous. - */ - -#include -#include -#include - -#include "socket.h" - -typedef struct{ - char *sockname; // UNIX socket name of command socket or port of local socket - int isun; // command socket is UNIX socket instead of INET - int shmkey; // shared memory (with image data) key - int infty; // run in infinity loop (if not - run by requests) - int nframes; // amount of frames to take - int help; // show this help - double exptime; // time of exposition in seconds -} glob_pars; - - -static glob_pars G = { - .shmkey = 7777777, - .nframes = 10 -}; - -/* - * Define command line options by filling structure: - * name has_arg flag val type argptr help -*/ -myoption cmdlnopts[] = { - {"sock", NEED_ARG, NULL, 's', arg_string, APTR(&G.sockname), "command socket name or port"}, - {"isun", NO_ARGS, NULL, 'U', arg_int, APTR(&G.isun), "use UNIX socket"}, - {"shmkey", NEED_ARG, NULL, 'k', arg_int, APTR(&G.shmkey), "shared memory (with image data) key (default: 7777777)"}, - {"infty", NO_ARGS, NULL, 'i', arg_int, APTR(&G.infty), "run in infinity capturing loop (else - request each frame)"}, - {"nframes", NEED_ARG, NULL, 'n', arg_int, APTR(&G.nframes), "make series of N frames"}, - {"exptime", NEED_ARG, NULL, 'x', arg_double, APTR(&G.exptime), "set exposure time to given value (seconds!)"}, - {"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"}, -}; - -static cc_IMG *shimg = NULL, img = {0}; - -static int refresh_img(){ - if(!shimg) return FALSE; - static size_t imnumber = 0; - if(shimg->imnumber == imnumber) return FALSE; - imnumber = shimg->imnumber; - void *optr = img.data; - memcpy(&img, shimg, sizeof(img)); - img.data = realloc(optr, img.bytelen); - memcpy(img.data, (uint8_t*)shimg + sizeof(cc_IMG), img.bytelen); - return TRUE; -} - -int main(int argc, char **argv){ - initial_setup(); - parseargs(&argc, &argv, cmdlnopts); - if(G.help) showhelp(-1, cmdlnopts); - if(argc > 0){ - WARNX("%d unused parameters:", argc); - for(int i = 0; i < argc; ++i) - printf("%4d: %s\n", i, argv[i]); - } - if(!G.sockname) ERRX("Point socket name or port"); - int sock = cc_open_socket(FALSE, G.sockname, !G.isun); - if(sock < 0) ERR("Can't open socket %s", G.sockname); - shimg = cc_getshm(G.shmkey, 0); - if(!shimg) ERRX("Can't get shared memory segment"); - int i = 0; - time_t oldtime = time(NULL); - double oldtimestamp = shimg->timestamp; - do{ - time_t now = time(NULL); - if(now - oldtime > 5){ - WARNX("No new images for 5 seconds"); - break; - } - if(!refresh_img()){ - usleep(1000); - continue; - } - ++i; - oldtime = now; - printf("Got image #%zd, size %dx%d, bitpix %d, time %g\n", img.imnumber, img.w, img.h, img.bitpix, img.timestamp-oldtimestamp); - }while(i < 10); - close(sock); - return 0; -} diff --git a/locale/ru/messages.po b/locale/ru/messages.po index b89cf5a..31703cb 100644 --- a/locale/ru/messages.po +++ b/locale/ru/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-02-21 16:53+0300\n" +"POT-Creation-Date: 2024-03-20 17:02+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n"