-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
executable file
·183 lines (146 loc) · 4.19 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Add sensor errors to .xml
add sensor disables, cut off distances to .xml
*/
#include"/usr/include/time.h"
#include "nsrCore.h"
#include "View/nsrOsgView.h"
#include "View/nsrOsgCommonView.h"
#include "Visualize/nsrLinuxKeyBoard2.h"
#include "Visualize/nsrPlot.h"
#include "Sim/Sim.h"
#include "Sim/nsrRosInterface.h"
#include "Sim/nsrSimParamReader.h"
#include "Sim/nsrPoseSim.h"
#include <X11/Xlib.h> //for getting screen resolution
#include <signal.h>
//for cpu controll//////
#define _GNU_SOURCE
#include <sched.h>
////////////////////////
#undef TAG
#define TAG "cpp:Main: "
#define SCR_DEFAULT_WIDTH 1024
#define SCR_DEFAULT_HEIGHT 768
int scr_width = SCR_DEFAULT_WIDTH,
scr_height = SCR_DEFAULT_HEIGHT;
void scrAutoDetect()
{
Display* d = XOpenDisplay(NULL);
Screen* s = DefaultScreenOfDisplay(d);
scr_width = s->width;
scr_height = s->height;
if(scr_width == 0 || scr_height == 0)
printf("\nWarning Screen resolution autodetect failed \nusing default resolutions (1024X768)\ncheck HDMI connections \n");
XCloseDisplay(d);
}
void intHandler(int sig);
void linuxInit()
{
signal(SIGINT, intHandler); //ctrl+c
mySetTime(0);
scrAutoDetect();
char root_path[100] = "../../"; //It's relative to currrent build dir
strcat(root_path, PROJECT_NAME);
NativeOpen(root_path, myTime());
setSharedBuffers();
nsrReadSimParams("Parameters.xml");
Init_Sim(Sim); //overrides parameters
nsrPoseMakerInit();
simInit();
nsrOsgInit();
nsrOsgInitOsgWindow(0, 0, scr_width, scr_height);
LOGI(TAG, " Initialization complete!-\n");
}
static volatile int programInterrupted = 0;
void finish()
{
LOGDUMP();
LOGI(TAG, " Destroyed0!-\n");
simClose();
LOGI(TAG, " Destroyed1!-\n");
nsrPoseMakerClose();
LOGI(TAG, " Destroyed2!-\n");
nsrOsgPause();
LOGI(TAG, " Destroyed3!-\n");
nsrOsgClose();
LOGI(TAG, " Destroyed4!-\n");
NativeClose();
LOGI(TAG, " Destroyed5!-\n");
}
void ask_for_finish()
{
programInterrupted++;
if(programInterrupted >=3)
exit(110);
LOGW(TAG, " Catched interrupt request for %s time!\n", programInterrupted==1?"1st":"2nd");
}
void intHandler(int sig) //CTRL+C
{
ask_for_finish();
//finish();
}
static nsrKeyBoard selfkb;
static int last_read_index = 0;
void exitIOhandler()
{
uint16_t ch = selfkb.getch(last_read_index);
if(ch == KEY_ESC || ch == KEY_Q) {
ask_for_finish();
}
}
int pauseTrap()
{
uint16_t ch = selfkb.getch(last_read_index);
if(ch == KEY_P) {
ch = 0;
while(ch != KEY_P && ch != KEY_ESC && ch != KEY_Q) {
refreshPlotsOnEnd();
usleep(100000);
ch = selfkb.getch(last_read_index);
}
}
return 0;
}
int main(int argc, char *argv[])
{
if(argc >= 2)
execution_turn = atoi(argv[1]); //can be overriden by the .xml if present
/*cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(1, &mask);
CPU_SET(2, &mask);
int result = sched_setaffinity(0, sizeof(mask), &mask);
*/
linuxInit();
//execution_turn = 0;
double last_time_s = -1, frame_timestamp_s = -1;
frame_timestamp_s = nsrPoseMakerGetStartTime() + param_camera_phase_percent * (1. / param_camera_fps); ////first frame start from an offset from 1st row time
while(!_viewer->done() && !programInterrupted) {
if(time_reached(0.25, 0., frame_timestamp_s, last_time_s) == 1) //dump logs every 0.25 seconds
LOGDUMP();
frame_timestamp_s += (1. / param_camera_fps);
double sensor_time_needs = frame_timestamp_s; //make sensors needs until next frame
double camera_time_needs = frame_timestamp_s - (param_td + param_td_err) + 0.5 * (param_tr + param_tr_err) - 0. + 0.5 * param_te; //max time in next camera frame
if(camera_time_needs > frame_timestamp_s) {
printf("At least some part of the frame is captured in the future, increase camera delay!\n");
exit(100);
}
//printf("t1:%f, t2:%f\n", sensor_time_needs, frame_timestamp_s);
if(nsrPoseMakerLoop(sensor_time_needs) < 0)
break; //end of path .csv
if(simLoop(frame_timestamp_s) < 0) //Loop until next frame
break;
if(nsrOsgDraw(frame_timestamp_s) < 0) //Render next frame
break;
pauseTrap();
exitIOhandler();
last_time_s = frame_timestamp_s;
}
while(programInterrupted == 1 && execution_turn<0) { //Wait for a second interrupt (not in an automatic run)
exitIOhandler();
refreshPlotsOnEnd();
}
finish();
return 0;
}