From 94216b07413a2d803ce04ce561cc860b0354e9cd Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Thu, 9 Feb 2023 00:10:42 +0100 Subject: [PATCH] adding some more logging. enableing cross compiling. --- Makefile | 73 ++++++++++++++++++++++--- Makefile.config | 7 ++- Makefile.rules.crosswindows | 13 +++++ Makefile.rules.linux | 11 ++-- checkdumpfile.cc | 3 + convert.cc | 11 +++- copydlls.sh | 38 +++++++++++++ copyshare.sh | 10 ++++ debug.cc | 58 ++++++++++++++++++++ debug.h | 3 + dng.h | 1 + error.cc | 43 +++++++++++++++ main.cc | 38 ------------- pid.cc | 4 +- posctl.cc | 39 +++++++++++-- video.cc | 18 ++++-- video.h | 1 - videodev-dummy.h | 2 + videodev-dumpfile.cc | 12 +++- videodev-dumpfile.h | 6 +- videodev-simulation.cc | 106 +++++++++++++++++++++++++----------- videodev-simulation.h | 6 +- videodev-v4l2.cc | 4 ++ videodev.cc | 2 - videodev.h | 5 +- windows.cc | 14 +++++ windows.h | 69 +++++++++++++++++++++++ 27 files changed, 488 insertions(+), 109 deletions(-) create mode 100644 Makefile.rules.crosswindows create mode 100755 copydlls.sh create mode 100755 copyshare.sh create mode 100644 debug.cc create mode 100644 windows.cc create mode 100644 windows.h diff --git a/Makefile b/Makefile index cf1c53f..cbef025 100644 --- a/Makefile +++ b/Makefile @@ -10,24 +10,57 @@ APP = simpleskycam include Makefile.config -include Makefile.rules -OBJECTS := $(OBJECTS) gui.oo main.oo error.oo \ +OBJECTS := $(OBJECTS) gui.oo main.oo error.oo debug.oo \ video.oo videoframe.oo \ - videodev.oo videodev-v4l2.oo videodev-dumpfile.oo videodev-simulation.oo \ + videodev.oo videodev-dumpfile.oo videodev-simulation.oo \ convert.oo filter.oo detect.oo histogram.oo pid.oo \ - posctl.oo json.oo configuration.oo ser.oo dng.oo debayer.oo + posctl.oo json.oo configuration.oo debayer.oo DISTNAME=simpleskycam-$(VERSION) -DEPENDFILE=.depend + +# +# add configuration for debug_angles +# +ifeq ($(DEBUG_ANGLES),1) +CPPFLAGS := $(CPPFLAGS) +OBJECTS := $(OBJECTS) debug-angles.oo +endif +# + +# +# compile and use DNG files +# +ifeq ($(USE_DNG),1) +LDFLAGS := $(LDFLAGS) -ltiff +OBJECTS := $(OBJECTS) dng.oo +endif +# + +# +# compile and use DNG files +# +ifeq ($(USE_SER),1) +OBJECTS := $(OBJECTS) ser.oo +endif +# + + + + + + ifeq ($(TARGET),) noconfig: configlinux help endif -all: dep Makefile.rules $(TARGET) +all: Makefile.rules $(TARGET) help: echo "set up configuration" echo " make configlinux to generate the linix build" + echo " make configcross to generate the windows build using cross tools." + echo " " echo " make checkdumpfile to create a test tool for videodumps." checkdumpfile: checkdumpfile.cc @@ -53,13 +86,40 @@ config: Makefile.rules ifeq ($(USE_SVBONY),1) echo "#define USE_SVBONY" >> config.h endif +ifeq ($(USE_SER),1) + echo "#define USE_SER" >> config.h +endif +ifeq ($(USE_DNG),1) + echo "#define USE_DNG" >> config.h +endif +ifeq ($(USE_V4L2),1) + echo "#define USE_V4L2" >> config.h +endif + echo "" >> config.h ifeq ($(DEBUG_ANGLES),1) echo "#define DEBUG_ANGLES" >> config.h +endif +ifeq ($(DEBUG_POSCTL),1) + echo "#define DEBUG_POSCTL" >> config.h endif echo "" >> config.h echo "#endif" >> config.h +buildwindows: clean + make configcross + make $(TARGET) -j 9 + mkdir SimpleSkyCam-$(VERSION) + cp *.exe SimpleSkyCam-$(VERSION)/ + cp simpleskycam.ui SimpleSkyCam-$(VERSION)/ + cp README.md SimpleSkyCam-$(VERSION)/ + cp ChangeLog SimpleSkyCam-$(VERSION)/ + cp LICENSE SimpleSkyCam-$(VERSION)/ + ./copydlls.sh + ./copyshare.sh + mv *.dll SimpleSkyCam-$(VERSION)/ + mv share SimpleSkyCam-$(VERSION)/ + $(TARGET): $(OBJECTS) $(CPP) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(LIBS) @@ -94,9 +154,6 @@ dist: clean tar cvzf $(DISTNAME).tgz $(DISTNAME) rm -rf $(DISTNAME) -dep: - $(CXX) -M `ls *.cc` $(CPPFLAGS) > $(DEPENDFILE) - .PHONY: all .PHONY: count .PHONY: clean diff --git a/Makefile.config b/Makefile.config index aceb9d4..8b642f1 100644 --- a/Makefile.config +++ b/Makefile.config @@ -3,7 +3,10 @@ # configurations should be made in here. # set one of these variables to 0 to disable the function. # -USE_SVBONY = 1 -DEBUG_ANGLES = 0 +USE_SVBONY = 0 +USE_DNG = 0 +USE_SER = 0 +DEBUG_ANGLES = 1 +DEBUG_POSCTL = 1 diff --git a/Makefile.rules.crosswindows b/Makefile.rules.crosswindows new file mode 100644 index 0000000..9e52897 --- /dev/null +++ b/Makefile.rules.crosswindows @@ -0,0 +1,13 @@ + +include Makefile.config + +TARGET = $(APP).exe +CROSSENV = /opt/W64-cross-compile/ +CPP = /usr/bin/x86_64-w64-mingw32-g++ +CPPFLAGS = -ggdb -Wall -O0 `PKG_CONFIG_PATH=$(CROSSENV)/lib/pkgconfig pkg-config --cflags gtk+-3.0 gmodule-export-2.0` -Wl,--export-dynamic -DBUILD_WINDOWS=1 -Wdeprecated +INCLUDES = +LDFLAGS = -lws2_32 -ljpeg +LIBS = `PKG_CONFIG_PATH=$(CROSSENV)/lib/pkgconfig pkg-config --libs gtk+-3.0 gmodule-export-2.0` -L/usr/lib -mwindows + +OBJECTS := $(OBJECTS) windows.oo + diff --git a/Makefile.rules.linux b/Makefile.rules.linux index fb7e3d2..6fd0f25 100644 --- a/Makefile.rules.linux +++ b/Makefile.rules.linux @@ -1,13 +1,14 @@ include Makefile.config +USE_V4L2 = 1 TARGET = $(APP) CPP = c++ CPPFLAGS = -std=c++11 -pg -ggdb -Wall -Werror -O0 `pkg-config --cflags gtk+-3.0 gmodule-export-2.0` -Wl,--export-dynamic -I/usr/include -DBUILD_LINUX=1 INCLUDES = LDFLAGS = -LIBS = `pkg-config --libs gtk+-3.0 gmodule-export-2.0` -L/usr/lib -ljpeg -ltiff -pg +LIBS = `pkg-config --libs gtk+-3.0 gmodule-export-2.0` -L/usr/lib -ljpeg -pg OBJECTS = @@ -23,13 +24,11 @@ endif # -# add configuration for debug_angles +# add makefile configuration for svbony cams # -ifeq ($(DEBUG_ANGLES),1) -CPPFLAGS := $(CPPFLAGS) -OBJECTS := $(OBJECTS) debug-angles.oo +ifeq ($(USE_V4L2),1) +OBJECTS := $(OBJECTS) videodev-v4l2.oo endif # - diff --git a/checkdumpfile.cc b/checkdumpfile.cc index 6dbf62e..cf3e693 100644 --- a/checkdumpfile.cc +++ b/checkdumpfile.cc @@ -11,7 +11,10 @@ #include #include #include +#ifdef BUILD_WINDOWS +#else #include +#endif #include diff --git a/convert.cc b/convert.cc index fdd3e7c..affed15 100644 --- a/convert.cc +++ b/convert.cc @@ -4,7 +4,12 @@ #include #include #include + +#ifdef BUILD_WINDOWS +#include "windows.h" +#else #include +#endif #include "convert.h" #include "gui.h" @@ -72,11 +77,15 @@ int convert_debug_open(uint32_t pixelformat, int srcw, int srch) { printf ("%s:%d try to create folder\n", __FILE__, __LINE__); // create folder + // FIXME: how to do thin on windows +#ifdef BUILD_WINDOWS + if ((mkdir (config.debugpath)) == -1) { +#else if ((mkdir (config.debugpath, 0777)) == -1) { +#endif printf ("%s:%d could not create debug folder.\n", __FILE__, __LINE__); return -1; } - if ((convert_debug_fd = creat (fullfname, 0666)) == -1) { printf ("%s:%d could again not create file '%s'. Error:%s\n", __FILE__, __LINE__, fullfname, strerror(errno)); return -1; diff --git a/copydlls.sh b/copydlls.sh new file mode 100755 index 0000000..ed361b6 --- /dev/null +++ b/copydlls.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +CROSS_DEST_DIR=/opt/W64-cross-compile/lib +CROSS_COMPILER_DIR=/usr/x86_64-w64-mingw32/lib +CROSS_GCC_DIR=/usr/lib/gcc/x86_64-w64-mingw32/10-win32 + +cp -v $CROSS_COMPILER_DIR/zlib1.dll ./ + +# copy dll dependencys +copy_dependency() { + local I + for I in `strings $1 | grep -i '\.dll$' | grep -e "^lib"` + do + if [ -e ./$I ] + then + echo "File Exist" + + elif [ -e $CROSS_COMPILER_DIR/$I ] + then + cp -v $CROSS_COMPILER_DIR/$I ./ + copy_dependency $CROSS_COMPILER_DIR/$I + + elif [ -e $CROSS_GCC_DIR/$I ] + then + cp -v $CROSS_GCC_DIR/$I ./ + copy_dependency $CROSS_GCC_DIR/$I + + elif [ -e $CROSS_DEST_DIR/$I ] + then + cp -v $CROSS_DEST_DIR/$I ./ + copy_dependency $CROSS_DEST_DIR/$I + fi + + done +} + +copy_dependency simpleskycam.exe + diff --git a/copyshare.sh b/copyshare.sh new file mode 100755 index 0000000..2eb2180 --- /dev/null +++ b/copyshare.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +CROSS_PREFIX=/opt/W64-cross-compile + +mkdir share +cp -rf $CROSS_PREFIX/share/glib-2.0 share/glib-2.0 +cp -rf $CROSS_PREFIX/share/gtk-2.0 share/gtk-4.0 +cp -rf $CROSS_PREFIX/share/gtk-3.0 share/gtk-3.0 +cp -rf $CROSS_PREFIX/share/icons share/icons + diff --git a/debug.cc b/debug.cc new file mode 100644 index 0000000..e4ace42 --- /dev/null +++ b/debug.cc @@ -0,0 +1,58 @@ + +#include +#include +#include +#include + +#include + +#include "debug.h" + +GMutex debug_mutex; + +#define LEN 2048 + +void debug_init() { + g_mutex_init (&debug_mutex); +} + +void debug_tofile(char *fname, char *fmt, ...) { + static struct timeval tv; + static int firstrun = 1; + struct timeval tv1; + va_list args; + char buffer[LEN]; + int fd, len; + long long int ms; + + std::string s = setlocale(LC_ALL, NULL); + setlocale (LC_ALL, "C"); + + g_mutex_lock(&debug_mutex); + + if (firstrun) { + gettimeofday(&tv,NULL); + firstrun = 0; + } + gettimeofday(&tv1,NULL); + ms = ((tv1.tv_sec - tv.tv_sec) * 1000) + + ((tv1.tv_usec - tv.tv_usec) / 1000); +#ifdef BUILD_WINDOWS + fd = open(fname, O_WRONLY | O_APPEND | O_CREAT); +#else + fd = open(fname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); +#endif + if (fd < 0) errorexit((char*)"%s:%d could not open debug '%s' file. Error:%s\n", __FILE__, __LINE__, fname, strerror(errno)); + + snprintf (buffer, 32, "%lld,", ms); + len = strlen (buffer); + va_start (args, fmt); + vsnprintf (buffer+len, LEN-1-len, fmt, args); + va_end (args); + buffer[LEN-1] = 0; + write (fd, buffer, strlen(buffer)); + close (fd); + g_mutex_unlock(&debug_mutex); + + setlocale (LC_ALL, s.c_str()); +} diff --git a/debug.h b/debug.h index 5d16ece..07b82c3 100644 --- a/debug.h +++ b/debug.h @@ -17,4 +17,7 @@ extern void debug_angles_motionevent(GdkEvent *event); extern void debug_angles_btnpress(GdkEvent *event); #endif +extern void debug_init(); +extern void debug_tofile(char *fname, char *fmt, ...); + #endif diff --git a/dng.h b/dng.h index 8a2123a..7521e0b 100644 --- a/dng.h +++ b/dng.h @@ -1,6 +1,7 @@ #ifndef _DNG_H_ #define _DNG_H_ + /* Example usage of the class: --------------------------- diff --git a/error.cc b/error.cc index 1c5ef10..90a7e97 100644 --- a/error.cc +++ b/error.cc @@ -4,6 +4,10 @@ * *****************************************************************************************/ +#ifndef BUILD_WINDOWS +#include +#endif + #include #include #include @@ -57,3 +61,42 @@ void errormessage_display (char *window, char *title, char *fmt,...) { }; +/* + * print error message and the backtrace + */ +#define SIZE 100 +void errorexit (char *fmt,...) { + // + // error message + va_list args; + char text[4096]; + + va_start (args, fmt); + vsnprintf (text, 4096, fmt, args); + va_end (args); + + printf ("***************************\n"); + printf (" ERROR\n"); + printf ("***************************\n"); + printf ("%s", text); + + // + // backtrace + int j, nptrs; + void *buffer[SIZE]; + char **s; + +#ifndef BUILD_WINDOWS + nptrs = backtrace(buffer, SIZE); + if ((s = (char**) backtrace_symbols(buffer, nptrs)) == NULL) { + for (j = 0; j < nptrs; j++) printf ("%-5d %p\n", j, buffer[j]); + } + else { + for (j = 0; j < nptrs; j++) printf ("%-5d %s\n", j, s[j]); + } +#endif + exit (-1); +} + + + diff --git a/main.cc b/main.cc index 64a3e55..27cf320 100644 --- a/main.cc +++ b/main.cc @@ -5,7 +5,6 @@ *****************************************************************************************/ #include -#include #include #include "simpleskycam.h" @@ -15,8 +14,6 @@ #include "filter.h" #include "detect.h" #include "convert.h" -#include "ser.h" -#include "dng.h" /************************************************************************** * global variables @@ -93,41 +90,6 @@ float get_cycletime(struct timeval *t) { } -/* - * print error message and the backtrace - */ -#define SIZE 100 -void errorexit (char *fmt,...) { - // - // error message - va_list args; - char text[4096]; - - va_start (args, fmt); - vsnprintf (text, 4096, fmt, args); - va_end (args); - - printf ("***************************\n"); - printf (" ERROR\n"); - printf ("***************************\n"); - printf ("%s", text); - - // - // backtrace - int j, nptrs; - void *buffer[SIZE]; - char **s; - - nptrs = backtrace(buffer, SIZE); - if ((s = (char**) backtrace_symbols(buffer, nptrs)) == NULL) { - for (j = 0; j < nptrs; j++) printf ("%-5d %p\n", j, buffer[j]); - } - else { - for (j = 0; j < nptrs; j++) printf ("%-5d %s\n", j, s[j]); - } - exit (-1); -} - void calc_vec2anglelen(position_f_2d *p, vector_2d *v) { if (v == NULL || p == NULL) return; diff --git a/pid.cc b/pid.cc index 6b351f8..d4db5c2 100644 --- a/pid.cc +++ b/pid.cc @@ -127,7 +127,7 @@ double PID::Update(double target, double value) { void PID::SetParam (double mi, double ma, double p, double i, double d) { Kp = p; Ki = i/1000000.0; - Kd = d; + Kd = d/1000000.0; Min = mi; Max = ma; Start(); @@ -137,7 +137,7 @@ void PID::SetParam (double mi, double ma, double p, double i, double d) { void PID::GetParam (double *mi, double *ma, double *p, double *i, double *d) { if (p != NULL) *p = Kp; if (i != NULL) *i = Ki*1000000.0; - if (d != NULL) *d = Kd; + if (d != NULL) *d = Kd*1000000.0; if (mi != NULL) *mi = Min; if (ma != NULL) *ma = Max; }; diff --git a/posctl.cc b/posctl.cc index 8b6b5ba..2398a5c 100644 --- a/posctl.cc +++ b/posctl.cc @@ -20,6 +20,10 @@ #include "histogram.h" #include "error.h" +#ifdef BUILD_WINDOWS +#include "windows.h" +#endif + extern PosCtl posctl; extern Simulation simulation; @@ -565,6 +569,11 @@ gboolean cb_thread_posctl (gpointer data) { PosCtl::PosCtl() { mode = POSCTL_MODE_OFF; +#ifdef DEBUG_POSCTL + debug_tofile((char*)"posctl.log", (char*)"\n"); + debug_tofile((char*)"posctl.log", (char*)"mode,posX,posY,targetX,targetY,dx,dy,axis1.pv,axis1.op,axis1.kp,axis1.ki,axis1.kd,axis2.pv,axis2.op,axis2.kp,axis2.ki,axis2.kd\n"); +#endif + calib_mode = POSCTL_CALIB_MODE_OFF; device_fd = -1; device = ""; @@ -728,6 +737,13 @@ void PosCtl::CalibModeFinish() { * Loop, if new data is aviable */ void PosCtl::Loop (int posx, int posy) { +#ifdef DEBUG_POSCTL + static int lastmode = -1; + double kp0,ki0,kd0, kp1,ki1,kd1; + if (lastmode != mode && mode != POSCTL_MODE_CONTROL) { + debug_tofile((char*)"posctl.log", (char*)"%d,,,,,,,,,,,,,,,,", mode); + } +#endif // // calibration mode? if (mode == POSCTL_MODE_CALIB) { @@ -774,6 +790,15 @@ void PosCtl::Loop (int posx, int posy) { axis_op[0] = pid_axis[0].Update(0.0, axis_pv[0]); axis_op[1] = pid_axis[1].Update(0.0, axis_pv[1]); +#ifdef DEBUG_POSCTL + pid_axis[0].GetParam(NULL, NULL, &kp0, &ki0, &kd0); + pid_axis[1].GetParam(NULL, NULL, &kp1, &ki1, &kd1); + debug_tofile((char*)"posctl.log", (char*)"%d,%d,%d,%d,%d,%d,%d,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n", + mode, posx, posy, target_pos.x, target_pos.y, posx - target_pos.x, posy - target_pos.y, + axis_pv[0], axis_op[0],kp0,ki0,kd0, + axis_pv[1], axis_op[1],kp1,ki1,kd1); +#endif + if (OutputWriteValue(0, axis_op[0]) || OutputWriteValue(1, axis_op[1])) mode = POSCTL_MODE_OFF; @@ -790,6 +815,10 @@ void PosCtl::Loop (int posx, int posy) { target_pos.y = posy; UnLockMutex(); } + +#ifdef DEBUG_POSCTL + lastmode = mode; +#endif }; @@ -847,7 +876,8 @@ int PosCtl::WriteTTY (char * outbuf) { int PosCtl::ReadTTY (char * inbuf, int length) { ssize_t len; - + // FIXME: how to use the com/usb-com port on windows. +#ifndef BUILD_WINDOWS // make device non-blocking fcntl(device_fd, F_SETFL, fcntl(device_fd, F_GETFL) | O_NONBLOCK); @@ -874,7 +904,7 @@ int PosCtl::ReadTTY (char * inbuf, int length) { // make device blocking fcntl(device_fd, F_SETFL, fcntl(device_fd, F_GETFL) & ~O_NONBLOCK); - +#endif return 0; } @@ -886,7 +916,8 @@ int PosCtl::OutputWriteValue (int axis, double value) { simulation.AxisSetValue(axis, value); return 0; } - + // FIXME: how to use the com/usb-com port on windows. +#ifndef BUILD_WINDOWS if (device_fd <= 0) if (OutputOpen() != 0) return -1; // @@ -902,7 +933,7 @@ int PosCtl::OutputWriteValue (int axis, double value) { WriteTTY(outbuf); ReadTTY(outbuf, sizeof(outbuf) - 1); - +#endif return 0; }; diff --git a/video.cc b/video.cc index 03ec911..523f6cc 100644 --- a/video.cc +++ b/video.cc @@ -307,7 +307,9 @@ void cb_video_btnrefreshlist (GtkWidget *widget, gpointer data) { devlist.clear(); VideoDev_Dumpfile vdef1; vdef1.GetDeviceList(&devlist); +#ifdef USE_V4L2 VideoDev_V4L2 vdef2; vdef2.GetDeviceList(&devlist); +#endif #ifdef USE_SVBONY VideoDev_SVBCam vdef3; vdef3.GetDeviceList(&devlist); #endif @@ -353,11 +355,13 @@ void cb_video_btnrec (GtkWidget *widget, gpointer data) { // // load the selected driver - if (driver.compare("V4L2") == 0) videodev = new VideoDev_V4L2; + if (driver.compare("VIDEODUMP") == 0) videodev = new VideoDev_Dumpfile; +#ifdef USE_V4L2 + else if (driver.compare("V4L2") == 0) videodev = new VideoDev_V4L2; +#endif #ifdef USE_SVBONY else if (driver.compare("SVBCAM") == 0) videodev = new VideoDev_SVBCam; #endif - else if (driver.compare("VIDEODUMP") == 0) videodev = new VideoDev_Dumpfile; else if (driver.compare("SIMULATION") == 0) videodev = new VideoDev_Simulation; else videodev = new VideoDev; @@ -578,7 +582,7 @@ void cb_vidctrl_scale_change (GtkRange *range, gpointer data) { GtkWidget *grid = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "vidctrl-grid")); GtkWidget *scale = NULL; GtkWidget *label = NULL; - int idx = (long int)data; + int64_t idx = (int64_t)data; double value; label = gtk_grid_get_child_at(GTK_GRID(grid), 0, idx); @@ -597,7 +601,7 @@ void cb_vidctrl_entry_change (GtkWidget *widget, gpointer data) { GtkWidget *grid = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "vidctrl-grid")); GtkWidget *label = NULL; GtkWidget *entry = NULL; - int idx = (long int)data; + int64_t idx = (int64_t)data; int value; label = gtk_grid_get_child_at(GTK_GRID(grid), 0, idx); @@ -789,11 +793,13 @@ void cb_video_cbox_videodev (GtkWidget *widget, gpointer data) { // // load the selected driver - if (driver.compare("V4L2") == 0) videodev = new VideoDev_V4L2; + if (driver.compare("DUMMY") == 0) videodev = new VideoDev; +#ifdef USE_V4L2 + else if (driver.compare("V4L2") == 0) videodev = new VideoDev_V4L2; +#endif #ifdef USE_SVBONY else if (driver.compare("SVBCAM") == 0) videodev = new VideoDev_SVBCam; #endif - else if (driver.compare("DUMMY") == 0) videodev = new VideoDev; else videodev = new VideoDev; videodev->GetDeviceFormats(device, &lst_format); diff --git a/video.h b/video.h index 6c153c2..b99025b 100644 --- a/video.h +++ b/video.h @@ -11,7 +11,6 @@ #include #include #include -#include #include #include "json.h" diff --git a/videodev-dummy.h b/videodev-dummy.h index b3c51ae..f222ffe 100644 --- a/videodev-dummy.h +++ b/videodev-dummy.h @@ -16,7 +16,9 @@ #include #include #include +#ifndef BUILD_WINDOWS #include +#endif #include #include diff --git a/videodev-dumpfile.cc b/videodev-dumpfile.cc index bb3b7ae..fc8aa17 100644 --- a/videodev-dumpfile.cc +++ b/videodev-dumpfile.cc @@ -15,7 +15,14 @@ #include #include #include + +#ifdef BUILD_WINDOWS +#include +#include +#include +#else #include +#endif #include @@ -66,16 +73,19 @@ int VideoDev_Dumpfile::GetDeviceList(std::list *list) { } while ((de = readdir (dir))) { +#ifndef BUILD_WINDOWS if (de->d_type & DT_REG) { +#endif for (i = 0; i < 255 && de->d_name[i] != 0; i++) fname[i] = toupper(de->d_name[i]); fname[i] = 0; if (strstr (fname, ".VIDEODUMP") != NULL) { device = (std::string) "VIDEODUMP " + (std::string) de->d_name; list->push_back(device); } +#ifndef BUILD_WINDOWS } +#endif } - closedir(dir); return 1; diff --git a/videodev-dumpfile.h b/videodev-dumpfile.h index f8a731e..1dbca4e 100644 --- a/videodev-dumpfile.h +++ b/videodev-dumpfile.h @@ -10,13 +10,13 @@ #include #include #include -#include +// #include #include #include #include #include -#include -#include +// #include +// #include #include #include diff --git a/videodev-simulation.cc b/videodev-simulation.cc index 05a7b46..83d7fad 100644 --- a/videodev-simulation.cc +++ b/videodev-simulation.cc @@ -7,10 +7,15 @@ ************************************************************************************/ #include +#ifdef BUILD_WINDOWS +#include "windows.h" +#else #include +#endif #include #include +#include "debug.h" #include "convert.h" #include "configuration.h" #include "videodev-simulation.h" @@ -191,48 +196,69 @@ Simulation::Simulation() { posY = h/2; running = 0; +#ifdef DEBUG_POSCTL + debug_tofile((char*)"simulation.log", (char*)"\n"); + debug_tofile((char*)"simulation.log", (char*)"dAngle,dLen,x,y,dx,dy,timedelay,a1.defAngle,a1.defLen,a1.v,a1.dx,a1.dy,a2.defAngle,a2.defLen,a2.v,a2.dx,a2.dy,finalX,finalY\n"); +#endif + Reset(); }; void Simulation::Reset() { int i; + static int _first_run = 1; LockMutex(); time_t t = time(NULL); - srandom (t); - - // - // object movement - dAngle = 360.0 * (double)random() / (double) RAND_MAX; - dLen = 1.0 + 5.0 * (double)random() / (double) RAND_MAX; - printf ("%s:%d %s dAngle:%f dLen:%f\n", __FILE__, __LINE__, __FUNCTION__, dAngle, dLen); - - // - // axis movement - i = 0; - - axis[i % 2].defAngle = dAngle + (10.0 * (double)random() / ((double) RAND_MAX)) - 5.0; - axis[i % 2].defLen = dLen + (3.0 * (double)random() / ((double) RAND_MAX) - 1.5); - axis[i % 2].v = 1.0; - axis[i % 2].active = 1; - i++; - axis[i % 2].defAngle = dAngle + (10.0 * (double)random() / ((double) RAND_MAX)) + 85.0; - axis[i % 2].defLen = dLen + (3.0 * (double)random() / ((double) RAND_MAX) - 1.5); - axis[i % 2].v = 1.0; - axis[i % 2].active = 1; + + srand (t); + + if (_first_run) { + // + // object movement + dAngle = 360.0 * (double)rand() / (double) RAND_MAX; + dLen = 1.0 + 5.0 * (double)rand() / (double) RAND_MAX; + printf ("%s:%d %s dAngle:%f dLen:%f\n", __FILE__, __LINE__, __FUNCTION__, dAngle, dLen); + + // + // axis movement + i = 0; + + axis[i % 2].defAngle = 180.0 + dAngle + (10.0 * (double)rand() / ((double) RAND_MAX)) - 5.0; + axis[i % 2].defLen = dLen + (3.0 * (double)rand() / ((double) RAND_MAX) - 1.5); + axis[i % 2].v = 1.0; + axis[i % 2].active = 1; + + i++; + axis[i % 2].defAngle = 180.0 + dAngle + (10.0 * (double)rand() / ((double) RAND_MAX)) + 85.0; + axis[i % 2].defLen = dLen + (3.0 * (double)rand() / ((double) RAND_MAX) - 1.5); + axis[i % 2].v = 1.0; + axis[i % 2].active = 1; + + // normalize angle between 0..360° + for (i = 0; i < 2; i++) { + while (axis[i].defAngle < 0.0) axis[i].defAngle += 360.0; + while (axis[i].defAngle > 360.0) axis[i].defAngle -= 360.0; + } + } + else { + dAngle +=45.0; + axis[0].defAngle += 45.0; + axis[1].defAngle += 45.0; + } printf ("%s:%d %s Axis1: %f° Len:%f Axis2: %f° Len:%f\n", __FILE__, __LINE__, __FUNCTION__, axis[0].defAngle,axis[0].defLen, axis[1].defAngle,axis[1].defLen); UnLockMutex(); + _first_run = 0; } Simulation::~Simulation() { LockMutex (); running = 0; UnLockMutex (); - }; @@ -248,7 +274,10 @@ void Simulation::ThreadProcess() { int r = 1; struct timeval tv; double ms; - double dx, dy; + double dx[3], dy[3]; +#ifdef DEBUG_POSCTL + double x, y; +#endif get_cycletime (&tv); if (running) return; @@ -263,25 +292,40 @@ void Simulation::ThreadProcess() { // simulate rotation movement // calculate movement - dx = sindeg(dAngle) * dLen * ms; - dy = -cosdeg(dAngle) * dLen * ms; - posX += dx; - posY += dy; + dx[0] = sindeg(dAngle) * dLen * ms; + dy[0] = -cosdeg(dAngle) * dLen * ms; // // simulate motor axis movement for (int i = 0; i < 2; i++) if (axis[i].active) { - dx = sindeg(axis[i].defAngle) * axis[i].defLen * (1.0 + axis[i].v) * ms; - dy = -cosdeg(axis[i].defAngle) * axis[i].defLen * (1.0 + axis[i].v) * ms; - posX -= dx; - posY -= dy; + dx[i+1] = sindeg(axis[i].defAngle) * axis[i].defLen * (1.0 + axis[i].v) * ms; + dy[i+1] = -cosdeg(axis[i].defAngle) * axis[i].defLen * (1.0 + axis[i].v) * ms; + } + else { + dx[i+1] = 0.0; + dy[i+1] = 0.0; } + // this is needed for debugging +#ifdef DEBUG_POSCTL + x = posX; y = posY; +#endif + posX = posX + dx[0] + dx[1] + dx[2]; + posY = posY + dy[0] + dy[1] + dy[2]; if (posX < 0) posX = w - 1.0; if (posY < 0) posY = h - 1.0; if (posX > w) posX = 0.0; if (posY > h) posY = 0.0; + +#ifdef DEBUG_POSCTL + debug_tofile((char*)"simulation.log", (char*)"%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n", + dAngle, dLen, x, y, dx[0], dy[0], ms, + axis[0].defAngle,axis[0].defLen,axis[0].v,dx[1],dy[1], + axis[1].defAngle,axis[1].defLen,axis[1].v,dx[2],dy[2], + posX, posY + ); +#endif r = running; UnLockMutex(); } while (r); diff --git a/videodev-simulation.h b/videodev-simulation.h index 57adf6e..a34bf38 100644 --- a/videodev-simulation.h +++ b/videodev-simulation.h @@ -10,13 +10,13 @@ #include #include #include -#include +//#include #include #include #include #include -#include -#include +//#include +//#include #include #include diff --git a/videodev-v4l2.cc b/videodev-v4l2.cc index 7a324fe..16c366f 100644 --- a/videodev-v4l2.cc +++ b/videodev-v4l2.cc @@ -1,5 +1,8 @@ #include "convert.h" + +#ifdef USE_V4L2 + #include "videodev-v4l2.h" VideoDev_V4L2::VideoDev_V4L2() { @@ -576,3 +579,4 @@ int VideoDev_V4L2::GetDevCtrl(unsigned int id, int *value) { }; +#endif diff --git a/videodev.cc b/videodev.cc index 105ff21..e9317ec 100644 --- a/videodev.cc +++ b/videodev.cc @@ -9,14 +9,12 @@ #include #include #include -#include #include #include #include #include #include #include -#include #include #include diff --git a/videodev.h b/videodev.h index 2987cbc..c4ec9dc 100644 --- a/videodev.h +++ b/videodev.h @@ -156,9 +156,12 @@ public: }; -#include "videodev-v4l2.h" #include "videodev-dumpfile.h" #include "videodev-simulation.h" + +#ifdef USE_V4L2 +#include "videodev-v4l2.h" +#endif #ifdef USE_SVBONY #include "videodev-svbcam.h" #endif diff --git a/windows.cc b/windows.cc new file mode 100644 index 0000000..0696d79 --- /dev/null +++ b/windows.cc @@ -0,0 +1,14 @@ + + +#include +#include +#include "windows.h" + +void strfromd (char* dest, int len, char *fmt,...) { + va_list args; + + va_start (args, fmt); + vsnprintf (dest, len-1, fmt, args); + va_end (args); + dest[len-1] = 0; +} diff --git a/windows.h b/windows.h new file mode 100644 index 0000000..25a92a6 --- /dev/null +++ b/windows.h @@ -0,0 +1,69 @@ + +#ifndef _WINDOWS_H_ +#define _WINDOWS_H_ + +#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + +#define WIN32_LEAN_AND_MEAN + +#define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) ver##0000 +#define _NTDDI_VERSION_FROM_WIN32_WINNT(ver) _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) + +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +#endif +#ifndef NTDDI_VERSION +# define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT) +#endif + +// #include +#include +#include +#include +#include + +#define socklen_t size_t + +#ifndef bzero +#define bzero(__z__, __x__) memset (__z__, 0x0, __x__) +#endif + +#ifndef MSG_NOSIGNAL +# define MSG_NOSIGNAL 0 +#endif + +#endif + +extern void strfromd (char* dest, int len, char *fmt,...); + + +#define __u32 uint32_t + +/* + * since this application is ported from linux and uses some linux based definitions + * we needed to copy some of the definitions and information. + * + * the following part comes from the Video 4 Linux 2 source more details can be found + * at https://linuxtv.org. + */ + +#define V4L2_PIX_FMT_YUYV v4l2_fourcc('Y', 'U', 'Y', 'V') /* 16 YUV 4:2:2 */ +#define V4L2_PIX_FMT_YYUV v4l2_fourcc('Y', 'Y', 'U', 'V') /* 16 YUV 4:2:2 */ +#define V4L2_PIX_FMT_YVYU v4l2_fourcc('Y', 'V', 'Y', 'U') /* 16 YVU 4:2:2 */ +#define V4L2_PIX_FMT_UYVY v4l2_fourcc('U', 'Y', 'V', 'Y') /* 16 YUV 4:2:2 */ +#define V4L2_PIX_FMT_VYUY v4l2_fourcc('V', 'Y', 'U', 'Y') /* 16 YUV 4:2:2 */ +#define V4L2_PIX_FMT_RGB32 v4l2_fourcc('R', 'G', 'B', '4') /* 32 RGB-8-8-8-8 */ +#define V4L2_PIX_FMT_BGR24 v4l2_fourcc('B', 'G', 'R', '3') /* 24 BGR-8-8-8 */ +#define V4L2_PIX_FMT_RGB24 v4l2_fourcc('R', 'G', 'B', '3') /* 24 RGB-8-8-8 */ +#define V4L2_PIX_FMT_BGR32 v4l2_fourcc('B', 'G', 'R', '4') /* 32 BGR-8-8-8-8 */ +#define V4L2_PIX_FMT_MJPEG v4l2_fourcc('M', 'J', 'P', 'G') /* Motion-JPEG */ +#define V4L2_PIX_FMT_SGRBG8 v4l2_fourcc('G', 'R', 'B', 'G') /* 8 GRGR.. BGBG.. */ +#define V4L2_PIX_FMT_SGRBG16 v4l2_fourcc('G', 'R', '1', '6') /* 16 GRGR.. BGBG.. */ + + +#define v4l2_fourcc(a, b, c, d)\ + ((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | ((__u32)(d) << 24)) +#define v4l2_fourcc_be(a, b, c, d) (v4l2_fourcc(a, b, c, d) | (1U << 31)) + + +#endif