should work

master
Steffen Pohle 3 years ago
parent 8908f0f82d
commit 155d093109

@ -37,10 +37,8 @@ enum {
POSCTL_CALIB_MODE_OFF = 0,
POSCTL_CALIB_MODE_START,
POSCTL_CALIB_MODE_DELTA,
POSCTL_CALIB_MODE_AXIS1_MI,
POSCTL_CALIB_MODE_AXIS1_MA,
POSCTL_CALIB_MODE_AXIS2_MI,
POSCTL_CALIB_MODE_AXIS2_MA,
POSCTL_CALIB_MODE_AXIS1,
POSCTL_CALIB_MODE_AXIS2,
POSCTL_CALIB_MODE_FINISH
};
@ -75,18 +73,12 @@ class PosCtl {
PosCtl();
~PosCtl() {};
double calib_rot_angle;
double calib_rot_len;
double calib_axis1min_angle;
double calib_axis1min_len;
double calib_axis1max_angle;
double calib_axis1max_len;
double calib_axis2min_angle;
double calib_axis2min_len;
double calib_axis2max_angle;
double calib_axis2max_len;
position_f_2d calib_rot_v;
vector_2d calib_rot;
position_f_2d calib_axis1_v;
vector_2d calib_axis1;
position_f_2d calib_axis2_v;
vector_2d calib_axis2;
double out[2];

36
gui.h

@ -11,6 +11,7 @@
#include <gdk/gdk.h>
#include <glib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <list>
@ -28,10 +29,37 @@ struct {
} typedef position_2d;
struct {
float x;
float y;
} typedef position_f_2d;
class position_f_2d {
public:
position_f_2d() { x = NAN; y = NAN; };
~position_f_2d() {};
position_f_2d operator + (position_f_2d const &obj) {
position_f_2d res;
res.x = x + obj.x;
res.y = y + obj.y;
return res;
}
position_f_2d operator - (position_f_2d const &obj) {
position_f_2d res;
res.x = x - obj.x;
res.y = y - obj.y;
return res;
}
double x;
double y;
};
class vector_2d {
public:
double a;
double l;
vector_2d () { a = NAN; l = NAN; }
~vector_2d () {};
};
#define DETECT_MOVEMENT_SAMPLES 50

@ -6,6 +6,7 @@
#include <sys/time.h>
#include <execinfo.h>
#include <math.h>
#include "simpleskycam.h"
#include "config.h"
@ -127,3 +128,14 @@ void errorexit (char *fmt,...) {
exit (-1);
}
void calc_vec2anglelen(position_f_2d *p, vector_2d *v) {
if (v == NULL || p == NULL) return;
v->a = atan2(p->x, p->y) * 180.0 / M_PI;
while (v->a < 0.0) v->a += 360.0;
v->l = hypot(p->x, p->y);
}

@ -31,6 +31,8 @@ GtkWidget *posctl_rot_da = NULL;
double linedash1[] = { 1.0, 4.0 };
#define CALIB_MAXSPEED 2.0
void posctl_gui_update();
void cb_posctl_show_window (GtkWidget *widget, gpointer data) {
@ -138,25 +140,17 @@ void cb_posctl_angles_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer
// rotation da
GtkWidget *entry_rot_angle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_rotangle"));
GtkWidget *entry_rot_len = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_rotlen"));
GtkWidget *entry_axis1_minangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1minangle"));
GtkWidget *entry_axis1_minlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1minlen"));
GtkWidget *entry_axis1_maxangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1maxangle"));
GtkWidget *entry_axis1_maxlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1maxlen"));
GtkWidget *entry_axis2_minangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2minangle"));
GtkWidget *entry_axis2_minlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2minlen"));
GtkWidget *entry_axis2_maxangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2maxangle"));
GtkWidget *entry_axis2_maxlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2maxlen"));
GtkWidget *entry_axis1_angle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1angle"));
GtkWidget *entry_axis1_len = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1len"));
GtkWidget *entry_axis2_angle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2angle"));
GtkWidget *entry_axis2_len = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2len"));
float rotangle = atof(gtk_entry_get_text(GTK_ENTRY(entry_rot_angle))) * (M_PI / 180);
float rotlen = atof(gtk_entry_get_text(GTK_ENTRY(entry_rot_len)));
float a1min_angle = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis1_minangle))) * (M_PI / 180);
float a1min_len = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis1_minlen)));
float a1max_angle = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis1_maxangle))) * (M_PI / 180);
float a1max_len = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis1_maxlen)));
float a2min_angle = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis2_minangle))) * (M_PI / 180);
float a2min_len = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis2_minlen)));
float a2max_angle = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis2_maxangle))) * (M_PI / 180);
float a2max_len = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis2_maxlen)));
float a1_angle = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis1_angle))) * (M_PI / 180);
float a1_len = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis1_len)));
float a2_angle = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis2_angle))) * (M_PI / 180);
float a2_len = atof(gtk_entry_get_text(GTK_ENTRY(entry_axis2_len)));
if (posctl_rot_da == NULL) // should only be called once
posctl_rot_da = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_da_rotation"));
@ -190,15 +184,15 @@ void cb_posctl_angles_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer
lpos[0].x = sin(rotangle) * rotlen;
lpos[0].y = cos(rotangle) * rotlen;
lpos[1].x = lpos[0].x + sin(a1min_angle) * a1min_len;
lpos[1].y = lpos[0].y + cos(a1min_angle) * a1min_len;
lpos[2].x = lpos[0].x + sin(a1max_angle) * a1max_len;
lpos[2].y = lpos[0].y + cos(a1max_angle) * a1max_len;
lpos[1].x = lpos[0].x + sin(a1_angle) * a1_len;
lpos[1].y = lpos[0].y + cos(a1_angle) * a1_len;
lpos[2].x = lpos[0].x;
lpos[2].y = lpos[0].y;
lpos[3].x = lpos[0].x + sin(a2min_angle) * a2min_len;
lpos[3].y = lpos[0].y + cos(a2min_angle) * a2min_len;
lpos[4].x = lpos[0].x + sin(a2max_angle) * a2max_len;
lpos[4].y = lpos[0].y + cos(a2max_angle) * a2max_len;
lpos[3].x = lpos[0].x + sin(a2_angle) * a2_len;
lpos[3].y = lpos[0].y + cos(a2_angle) * a2_len;
lpos[4].x = lpos[0].x;
lpos[4].y = lpos[0].y;
// find maximum
for (int i = 0; i < 5; i++) {
@ -234,8 +228,8 @@ void cb_posctl_angles_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer
cairo_line_to(cr, center.x + float (lpos[2].x * center.x * 0.8 / lmax),
center.y - float (lpos[2].y * center.y * 0.8 / lmax));
cairo_stroke(cr);
cairo_move_to(cr, center.x + float (lpos[2].x * center.x * 0.9 / lmax),
center.y - float (lpos[2].y * center.y * 0.9 / lmax));
cairo_move_to(cr, center.x + float (lpos[1].x * center.x * 0.9 / lmax),
center.y - float (lpos[1].y * center.y * 0.9 / lmax));
cairo_show_text(cr, (char *)"1");
// axis2
@ -245,8 +239,8 @@ void cb_posctl_angles_draw(GtkWidget *area, cairo_t *cr, int w, int h, gpointer
cairo_line_to(cr, center.x + float (lpos[4].x * center.x * 0.8 / lmax),
center.y - float (lpos[4].y * center.y * 0.8 / lmax));
cairo_stroke(cr);
cairo_move_to(cr, center.x + float (lpos[4].x * center.x * 0.9 / lmax),
center.y - float (lpos[4].y * center.y * 0.9 / lmax));
cairo_move_to(cr, center.x + float (lpos[3].x * center.x * 0.9 / lmax),
center.y - float (lpos[3].y * center.y * 0.9 / lmax));
cairo_show_text(cr, (char *)"2");
};
@ -310,15 +304,11 @@ void posctl_gui_update() {
GtkWidget *e_cal_rotangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_rotangle"));
GtkWidget *e_cal_rotlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_rotlen"));
GtkWidget *e_cal_a1minangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1minangle"));
GtkWidget *e_cal_a1minlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1minlen"));
GtkWidget *e_cal_a1maxangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1maxangle"));
GtkWidget *e_cal_a1maxlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1maxlen"));
GtkWidget *e_cal_a1angle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1angle"));
GtkWidget *e_cal_a1len = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a1len"));
GtkWidget *e_cal_a2minangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2minangle"));
GtkWidget *e_cal_a2minlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2minlen"));
GtkWidget *e_cal_a2maxangle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2maxangle"));
GtkWidget *e_cal_a2maxlen = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2maxlen"));
GtkWidget *e_cal_a2angle = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2angle"));
GtkWidget *e_cal_a2len = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_a2len"));
GtkWidget *e_posdevice = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_entry_device"));
GtkWidget *btn_a1min = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "posctl_btn_a1_min"));
@ -371,28 +361,20 @@ void posctl_gui_update() {
gtk_widget_set_sensitive(e_posdevice, false);
}
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot_angle);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot.a);
gtk_entry_set_text (GTK_ENTRY(e_cal_rotangle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot_len);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot.l);
gtk_entry_set_text (GTK_ENTRY(e_cal_rotlen), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1min_angle);
gtk_entry_set_text (GTK_ENTRY(e_cal_a1minangle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1min_len);
gtk_entry_set_text (GTK_ENTRY(e_cal_a1minlen), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1max_angle);
gtk_entry_set_text (GTK_ENTRY(e_cal_a1maxangle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1max_len);
gtk_entry_set_text (GTK_ENTRY(e_cal_a1maxlen), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2min_angle);
gtk_entry_set_text (GTK_ENTRY(e_cal_a2minangle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2min_len);
gtk_entry_set_text (GTK_ENTRY(e_cal_a2minlen), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2max_angle);
gtk_entry_set_text (GTK_ENTRY(e_cal_a2maxangle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2max_len);
gtk_entry_set_text (GTK_ENTRY(e_cal_a2maxlen), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1.a);
gtk_entry_set_text (GTK_ENTRY(e_cal_a1angle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1.l);
gtk_entry_set_text (GTK_ENTRY(e_cal_a1len), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2.a);
gtk_entry_set_text (GTK_ENTRY(e_cal_a2angle), txt);
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2.l);
gtk_entry_set_text (GTK_ENTRY(e_cal_a2len), txt);
gtk_entry_set_text (GTK_ENTRY(e_posdevice), posctl.GetDevice().c_str());
strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.out[0]);
@ -442,16 +424,6 @@ PosCtl::PosCtl() {
mode = POSCTL_MODE_OFF;
calib_mode = POSCTL_CALIB_MODE_OFF;
calib_rot_len = 0.0;
calib_rot_angle = 0.0;
calib_axis1min_angle = 0.0;
calib_axis1min_len = 0.0;
calib_axis1max_angle = 0.0;
calib_axis1max_len = 0.0;
calib_axis2min_angle = 0.0;
calib_axis2min_len = 0.0;
calib_axis2max_angle = 0.0;
calib_axis2max_len = 0.0;
device_fd = -1;
device = "";
device_type = POSCTL_DEVTYPE_TTY;
@ -542,22 +514,20 @@ void PosCtl::CalibModeDelta(int x, int y) {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
pid_axis[0].GetParam(&a1min, &a1max, NULL, NULL, NULL);
OutputWriteStart();
fp.x = (x - calib_pos.x) / (float)timediff;
fp.y = -(y - calib_pos.y) / (float)timediff;
LockMutex();
calib_rot_angle = atan2(fp.x, fp.y) * 180.0 / M_PI;
while (calib_rot_angle < 0.0) calib_rot_angle += 360.0;
calib_rot_len = sqrt(fp.x * fp.x + fp.y * fp.y);
calib_rot_v = fp;
calc_vec2anglelen(&fp, &calib_rot);
UnLockMutex();
calib_mode = POSCTL_CALIB_MODE_AXIS1_MI;
calib_mode = POSCTL_CALIB_MODE_AXIS1;
calib_pos.x = x;
calib_pos.y = y;
OutputWriteValue(0, a1min);
OutputWriteValue(0, CALIB_MAXSPEED);
gettimeofday (&calib_timestamp, NULL);
gdk_threads_add_idle(cb_thread_posctl, NULL);
}
@ -567,7 +537,6 @@ void PosCtl::CalibModeDelta(int x, int y) {
void PosCtl::CalibModeAxis(int x, int y) {
struct timeval tv;
float timediff;
double a1min, a1max, a2min, a2max;
position_f_2d fp;
gettimeofday (&tv, NULL);
@ -576,35 +545,19 @@ void PosCtl::CalibModeAxis(int x, int y) {
if (timediff > 5.0) {
printf ("%s:%d %s calib_mode: %d\n", __FILE__, __LINE__, __FUNCTION__, calib_mode);
pid_axis[0].GetParam(&a1min, &a1max, NULL, NULL, NULL);
pid_axis[1].GetParam(&a2min, &a2max, NULL, NULL, NULL);
fp.x = (x - calib_pos.x) / (float)timediff;
fp.y = -(y - calib_pos.y) / (float)timediff;
if (calib_mode == POSCTL_CALIB_MODE_AXIS1_MI) {
calib_axis1min_angle = atan2(fp.x, fp.y) * 180.0 / M_PI;
while (calib_axis1min_angle < 0.0) calib_axis1min_angle += 360.0;
calib_axis1min_len = sqrt(fp.x * fp.x + fp.y * fp.y);
OutputWriteValue(0, a1max);
}
else if (calib_mode == POSCTL_CALIB_MODE_AXIS1_MA) {
calib_axis1max_angle = atan2(fp.x, fp.y) * 180.0 / M_PI;
while (calib_axis1max_angle < 0.0) calib_axis1max_angle += 360.0;
calib_axis1max_len = sqrt(fp.x * fp.x + fp.y * fp.y);
OutputWriteValue(1, a2min);
}
else if (calib_mode == POSCTL_CALIB_MODE_AXIS2_MI) {
calib_axis2min_angle = atan2(fp.x, fp.y) * 180.0 / M_PI;
while (calib_axis2min_angle < 0.0) calib_axis2min_angle += 360.0;
calib_axis2min_len = sqrt(fp.x * fp.x + fp.y * fp.y);
OutputWriteValue(1, a2max);
if (calib_mode == POSCTL_CALIB_MODE_AXIS1) {
calib_axis1_v = fp;
calc_vec2anglelen(&calib_axis1_v, &calib_axis1);
OutputWriteStop();
OutputWriteValue(1, CALIB_MAXSPEED);
}
else if (calib_mode == POSCTL_CALIB_MODE_AXIS2_MA) {
calib_axis2max_angle = atan2(fp.x, fp.y) * 180.0 / M_PI;
while (calib_axis2max_angle < 0.0) calib_axis2max_angle += 360.0;
calib_axis2max_len = sqrt(fp.x * fp.x + fp.y * fp.y);
OutputWriteStart(); // reset speed output to 50%
else if (calib_mode == POSCTL_CALIB_MODE_AXIS2) {
calib_axis2_v = fp;
calc_vec2anglelen(&calib_axis2_v, &calib_axis2);
OutputWriteStart();
}
calib_mode++;
@ -640,10 +593,8 @@ void PosCtl::Loop (int posx, int posy) {
case (POSCTL_CALIB_MODE_DELTA):
CalibModeDelta(posx, posy);
break;
case (POSCTL_CALIB_MODE_AXIS1_MI):
case (POSCTL_CALIB_MODE_AXIS1_MA):
case (POSCTL_CALIB_MODE_AXIS2_MI):
case (POSCTL_CALIB_MODE_AXIS2_MA):
case (POSCTL_CALIB_MODE_AXIS1):
case (POSCTL_CALIB_MODE_AXIS2):
CalibModeAxis(posx, posy);
break;
case (POSCTL_CALIB_MODE_FINISH):

@ -2,7 +2,13 @@
#ifndef _SIMPLESKYCAM_H_
#define _SIMPLESKYCAM_H_
#include "gui.h"
extern void errorexit(char *fmt, ...);
extern void calc_vec2anglelen(position_f_2d *p, vector_2d *v);
#define sindeg(_angle_) sin((M_PI * _angle_ / 180.0))
#define cosdeg(_angle_) cos((M_PI * _angle_ / 180.0))
#endif

@ -762,7 +762,7 @@
</packing>
</child>
<child>
<!-- n-columns=5 n-rows=4 -->
<!-- n-columns=3 n-rows=4 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
@ -771,7 +771,7 @@
<property name="row-spacing">4</property>
<property name="column-spacing">4</property>
<child>
<object class="GtkEntry" id="posctl_entry_a1minangle">
<object class="GtkEntry" id="posctl_entry_a1angle">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
@ -801,7 +801,7 @@
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a2minangle">
<object class="GtkEntry" id="posctl_entry_a2angle">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
@ -888,7 +888,7 @@
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a1minlen">
<object class="GtkEntry" id="posctl_entry_a1len">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
@ -903,7 +903,7 @@
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a2minlen">
<object class="GtkEntry" id="posctl_entry_a2len">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
@ -917,92 +917,6 @@
<property name="top-attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Max: Angle</property>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a2maxangle">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="editable">False</property>
<property name="width-chars">6</property>
<property name="text" translatable="yes">30</property>
<signal name="changed" handler="cb_posctl_entryanglelen" swapped="no"/>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a1maxlen">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="editable">False</property>
<property name="width-chars">6</property>
<property name="text" translatable="yes">15</property>
<signal name="changed" handler="cb_posctl_entryanglelen" swapped="no"/>
</object>
<packing>
<property name="left-attach">4</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a1maxangle">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="editable">False</property>
<property name="width-chars">6</property>
<property name="text" translatable="yes">15</property>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="posctl_entry_a2maxlen">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="editable">False</property>
<property name="width-chars">6</property>
<property name="text" translatable="yes">15</property>
</object>
<packing>
<property name="left-attach">4</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Len</property>
</object>
<packing>
<property name="left-attach">4</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>

@ -11,7 +11,6 @@
#include <sys/stat.h>
#include <math.h>
#include "convert.h"
#include "configuration.h"
#include "videodev-simulation.h"
@ -21,8 +20,8 @@ gpointer simulation_threadprocess_wrapper (gpointer data);
VideoDev_Simulation::VideoDev_Simulation() {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
w = 1920;
h = 1080;
conf_width = 1920;
conf_height = 1080;
inframe = NULL;
simulation_thread = NULL;
};
@ -56,13 +55,14 @@ int VideoDev_Simulation::Open() {
if (inframe != NULL) Close();
conf_width = w;
conf_height = h;
// conf_width = w;
// conf_height = h;
conf_format = convert_from_pixelformat (V4L2_PIX_FMT_RGB24);
simulation.SetResolution(conf_width, conf_height);
simulation_thread = g_thread_new("Simulation", simulation_threadprocess_wrapper, NULL);
inframe = (unsigned char *) malloc (w * h * 3);
inframe = (unsigned char *) malloc (conf_width * conf_height * 3);
return VDEV_STATUS_OK;
};
@ -128,21 +128,22 @@ int VideoDev_Simulation::Grab(VideoFrame *vf) {
dsec = get_cycletime(&lastframetv);
if (dsec < 0.040) usleep (50000 - dsec*1000.0);
memset (inframe, 0x0, w*h*3);
memset (inframe, 0x0, conf_width*conf_height*3);
simulation.GetPos(&posx, &posy);
for (r = 1; r < SIMULATION_SIZE; r++) for (a = 0; a < M_PI * 2.0; a += 0.1) {
x = posx + (sin(a) * r);
y = posy + (cos(a) * r);
if (x >= 0 && (unsigned int)x < w && y >= 0 && (unsigned int)y < h) {
inframe[3*(x+y*w)+0] = 200;
inframe[3*(x+y*w)+1] = 200;
inframe[3*(x+y*w)+2] = 200;
if (x >= 0 && x < conf_width && y >= 0 && y < conf_height) {
inframe[3*(x+y*conf_width)+0] = 200;
inframe[3*(x+y*conf_width)+1] = 200;
inframe[3*(x+y*conf_width)+2] = 200;
}
}
LockMutex();
Convert(&cdata, vf, inframe, (w*h*3), V4L2_PIX_FMT_RGB24, w, h);
Convert(&cdata, vf, inframe, (conf_width*conf_height*3), V4L2_PIX_FMT_RGB24,
conf_width, conf_height);
UnLockMutex();
return VDEV_STATUS_OK;
@ -181,8 +182,10 @@ gpointer simulation_threadprocess_wrapper (gpointer data) {
Simulation::Simulation() {
int i;
posX = 900.0;
posY = 500.0;
w = 1920;
h = 1080;
posX = w/2;
posY = h/2;
running = 0;
time_t t = time(NULL);
@ -191,7 +194,7 @@ Simulation::Simulation() {
//
// object movement
dAngle = 2.0 * M_PI * (double)random() / (double) RAND_MAX;
dLen = 2.5 + 5.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);
@ -208,6 +211,10 @@ Simulation::Simulation() {
axis[i % 2].defLen = dLen + (1.0 * (double)random() / ((double) RAND_MAX) - 0.5);
axis[i % 2].v = 1.0;
axis[i % 2].active = 1;
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);
};
Simulation::~Simulation() {
@ -236,7 +243,7 @@ void Simulation::ThreadProcess() {
running = 1;
do {
usleep (10000);
usleep (20000);
ms = get_cycletime(&tv);
LockMutex();
@ -244,8 +251,8 @@ void Simulation::ThreadProcess() {
// simulate rotation movement
// calculate movement
dx = sin(dAngle) * dLen * ms;
dy = -cos(dAngle) * dLen * ms;
dx = sindeg(dAngle) * dLen * ms;
dy = -cosdeg(dAngle) * dLen * ms;
posX += dx;
posY += dy;
@ -253,16 +260,16 @@ void Simulation::ThreadProcess() {
// simulate motor axis movement
for (int i = 0; i < 2; i++)
if (axis[i].active) {
dx = sin(axis[i].defAngle) * axis[i].defLen * axis[i].v * ms;
dy = -cos(axis[i].defAngle) * axis[i].defLen * axis[i].v * ms;
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;
}
if (posX < 0) posX = 1920.0 - 1.0;
if (posY < 0) posY = 1080.0 - 1.0;
if (posX > 1920) posX = 0.0;
if (posY > 1080) posY = 0.0;
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;
r = running;
UnLockMutex();
} while (r);
@ -278,6 +285,16 @@ void Simulation::Stop() {
}
void Simulation::SetResolution (int w, int h) {
LockMutex();
this->w = w;
this->h = h;
posX = w / 2;
posY = h / 2;
UnLockMutex();
}
void Simulation::AxisSetValue (int a, double v) {
printf ("%s:%d %s Axis:%d Value:%f\n", __FILE__, __LINE__, __FUNCTION__, a, v);
if (a < 0 || a > 1) return;
@ -288,7 +305,7 @@ void Simulation::AxisSetValue (int a, double v) {
void Simulation::AxisStop() {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
axis[0].active = 1;
axis[1].active = 1;
axis[0].active = 0;
axis[1].active = 0;
}

@ -37,6 +37,8 @@ struct SimAxisCtl {
class Simulation {
private:
int w;
int h;
float posX;
float posY;
float dAngle;
@ -57,6 +59,7 @@ public:
void AxisSetValue(int a, double v);
void AxisStop();
void SetResolution (int w, int h);
void LockMutex() { g_mutex_lock(&mutex); };
void UnLockMutex() { g_mutex_unlock(&mutex); };
};
@ -64,8 +67,6 @@ public:
class VideoDev_Simulation: public VideoDev {
private:
uint32_t w;
uint32_t h;
struct timeval lastframetv;
unsigned char *inframe;
ConvertData cdata;

Loading…
Cancel
Save