59 printf(
"Caught signal %d, stopping program!\n", signum);
68 cout <<
"\nLists available rcdynamics data streams of the specified rc_visard IP, " 69 "\nor requests a data stream and either prints received messages or records " 70 "\nthem as csv-file, see -o option." 72 << arg <<
" -v <rcVisardIP> -l | -s <stream> [-a] [-i <networkInterface>]" 73 " [-n <maxNumData>][-t <maxRecTimeSecs>][-o <output_file>]" 77 int main(
int argc,
char* argv[])
81 WSAStartup(MAKEWORD(2, 2), &wsaData);
91 string out_file_name, visard_ip, network_iface =
"", stream_name;
92 unsigned int max_num_recording = 50, max_secs_recording = 5;
93 bool user_autostart =
false;
94 bool user_set_out_file =
false;
95 bool user_set_max_num_msgs =
false;
96 bool user_set_max_recording_time =
false;
97 bool user_set_ip =
false;
98 bool user_set_stream_type =
false;
99 bool only_list_streams =
false;
104 std::string p = argv[i++];
108 only_list_streams =
true;
110 else if (p ==
"-s" && i < argc)
112 stream_name = string(argv[i++]);
113 user_set_stream_type =
true;
117 user_autostart =
true;
119 else if (p ==
"-i" && i < argc)
121 network_iface = string(argv[i++]);
123 else if (p ==
"-v" && i < argc)
125 visard_ip = string(argv[i++]);
128 else if (p ==
"-n" && i < argc)
130 max_num_recording = (
unsigned int)std::max(0, atoi(argv[i++]));
131 user_set_max_num_msgs =
true;
133 else if (p ==
"-t" && i < argc)
135 max_secs_recording = (
unsigned int)std::max(0, atoi(argv[i++]));
136 user_set_max_recording_time =
true;
138 else if (p ==
"-o" && i < argc)
140 out_file_name = string(argv[i++]);
141 user_set_out_file =
true;
157 cerr <<
"Please specify rc_visard IP." << endl;
162 if (!user_set_stream_type && !only_list_streams)
164 cerr <<
"Please specify stream type." << endl;
169 if (!user_set_max_num_msgs && !user_set_max_recording_time)
171 user_set_max_num_msgs =
true;
177 ofstream output_file;
178 if (user_set_out_file)
180 output_file.open(out_file_name);
181 if (!output_file.is_open())
183 cerr <<
"Could not open file '" << out_file_name <<
"' for writing!" << endl;
191 cout <<
"connecting to rc_visard " << visard_ip <<
"..." << endl;
192 auto rc_dynamics = RemoteInterface::create(visard_ip);
196 cout <<
"... system not yet ready. Trying again." << endl;
199 cout <<
"... connected!" << endl;
200 }
catch (exception &e) {
201 cout <<
"ERROR! Could not connect to rc_dynamics module on rc_visard: " << e.what() << endl;
206 if (only_list_streams)
208 auto streams = rc_dynamics->getAvailableStreams();
209 string first_column =
"Available streams:";
210 size_t first_column_width = first_column.length();
211 for (
auto&& s : streams)
212 if (s.length() > first_column_width)
213 first_column_width = s.length();
214 first_column_width += 5;
215 cout << left << setw(first_column_width) << first_column <<
"Protobuf message types:" << endl;
216 for (
auto&& s : streams)
217 cout << left << setw(first_column_width) << s << rc_dynamics->getPbMsgTypeOfStream(s) << endl;
218 cout << endl <<
"rc_dynamics is in state: " << rc_dynamics->getDynamicsState();
219 cout << endl <<
"rc_slam is in state: " << rc_dynamics->getSlamState();
220 cout << endl <<
"rc_stereo_ins is in state: " << rc_dynamics->getStereoInsState();
226 if (user_autostart && stream_name !=
"imu")
230 cout <<
"starting SLAM on rc_visard..." << endl;
231 rc_dynamics->startSlam();
238 cout <<
"SLAM not available!" << endl;
239 cout <<
"starting stereo INS on rc_visard..." << endl;
240 rc_dynamics->start();
244 cout <<
"ERROR! Could not start rc_dynamics module on rc_visard: " << e.what() << endl;
253 unsigned int cnt_msgs = 0;
256 cout <<
"Initializing " << stream_name <<
" data stream..." << endl;
257 auto receiver = rc_dynamics->createReceiverForStream(stream_name, network_iface);
259 unsigned int timeout_millis = 100;
260 receiver->setTimeout(timeout_millis);
261 cout <<
"Listening for " << stream_name <<
" messages..." << endl;
263 chrono::time_point<chrono::system_clock> start = chrono::system_clock::now();
264 chrono::duration<double> elapsed_secs(0);
265 while (!
caught_signal && (!user_set_max_num_msgs || cnt_msgs < max_num_recording) &&
266 (!user_set_max_recording_time || elapsed_secs.count() < max_secs_recording))
268 auto msg = receiver->receive(rc_dynamics->getPbMsgTypeOfStream(stream_name));
271 if (output_file.is_open())
276 output_file << (h << *msg) << endl;
279 output_file << (l << *msg) << endl;
283 cout <<
"received " << stream_name <<
" msg:" << endl << msg->DebugString() << endl;
289 cerr <<
"did not receive any data during last " << timeout_millis <<
" ms." << endl;
291 elapsed_secs = chrono::system_clock::now() - start;
296 cout <<
"Caught exception during streaming, stopping: " << e.what() << endl;
303 if (user_autostart && stream_name !=
"imu")
307 cout <<
"stopping rc_dynamics module on rc_visard..." << endl;
312 cout <<
"Caught exception: " << e.what() << endl;
316 if (output_file.is_open())
319 cout <<
"Recorded " << cnt_msgs <<
" " << stream_name <<
" messages to '" << out_file_name <<
"'." << endl;
323 cout <<
"Received " << cnt_msgs <<
" " << stream_name <<
" messages." << endl;
void printUsage(char *arg)
Print usage of example including command line args.
void signal_callback_handler(int signum)
static bool caught_signal
catching signals for proper program escape
int main(int argc, char *argv[])
struct and methods to "organice" printing of csv-Lines