simple_receiver.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_dynamics_api package.
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Christian Emmerich
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
37 
38 #include <signal.h>
39 
40 #ifdef WIN32
41 #include <winsock2.h>
42 #undef min
43 #undef max
44 #endif
45 
46 using namespace std;
47 namespace rcdyn = rc::dynamics;
48 
52 static bool caught_signal = false;
53 void signal_callback_handler(int signum)
54 {
55  printf("Caught signal %d, stopping program!\n", signum);
56  caught_signal = true;
57 }
58 
62 void printUsage(char* arg)
63 {
64  cout << "\nRequests a data stream from the specified rc_visard IP "
65  "\nand simply prints received data to std out."
66  << "\n\nUsage: \n"
67  << arg << " -v <rcVisardIP> -s <stream> [-i <networkInterface>][-n <numMessages>]" << endl;
68 }
69 
70 int main(int argc, char* argv[])
71 {
72 #ifdef WIN32
73  WSADATA wsaData;
74  WSAStartup(MAKEWORD(2, 2), &wsaData);
75 #endif
76 
77  // Register signals and signal handler for proper program escape
78  signal(SIGINT, signal_callback_handler);
79  signal(SIGTERM, signal_callback_handler);
80 
84  string visardIP, networkIface = "", streamName = "";
85  unsigned int maxNumMsgs = 50, cntMsgs = 0;
86  bool userSetIp = false;
87  bool userSetStreamType = false;
88 
89  int i = 1;
90  while (i < argc)
91  {
92  std::string p = argv[i++];
93 
94  if (p == "-s" && i < argc)
95  {
96  streamName = string(argv[i++]);
97  userSetStreamType = true;
98  }
99  else if (p == "-i" && i < argc)
100  {
101  networkIface = string(argv[i++]);
102  }
103  else if (p == "-v" && i < argc)
104  {
105  visardIP = string(argv[i++]);
106  userSetIp = true;
107  }
108  else if (p == "-n" && i < argc)
109  {
110  maxNumMsgs = (unsigned int)std::max(0, atoi(argv[i++]));
111  }
112  else if (p == "-h")
113  {
114  printUsage(argv[0]);
115  return EXIT_SUCCESS;
116  }
117  else
118  {
119  printUsage(argv[0]);
120  return EXIT_FAILURE;
121  }
122  }
123  if (!userSetIp)
124  {
125  cerr << "Please specify rc_visard IP." << endl;
126  printUsage(argv[0]);
127  return EXIT_FAILURE;
128  }
129  if (!userSetStreamType)
130  {
131  cerr << "Please specify stream type." << endl;
132  printUsage(argv[0]);
133  return EXIT_FAILURE;
134  }
135 
139  cout << "connecting rc_visard " << visardIP << "..." << endl;
140  auto rcvisardDynamics = rcdyn::RemoteInterface::create(visardIP);
141 
142  try
143  {
144  // start the rc::dynamics module on the rc_visard
145  cout << "starting rc_dynamics module on rc_visard..." << endl;
146  rcvisardDynamics->start();
147  }
148  catch (exception& e)
149  {
150  cout << "ERROR! Could not start rc_dynamics module on rc_visard: " << e.what() << endl;
151  return EXIT_FAILURE;
152  }
153 
154  try
155  {
156  // easy-to-use creation of a data receiver, parameterized via stream type
157  cout << "creating receiver and waiting for first messages to arrive..." << endl;
158  auto receiver = rcvisardDynamics->createReceiverForStream(streamName, networkIface);
159  receiver->setTimeout(250);
160 
161  // receive rc_dynamics protobuf msgs and print them
162  for (; cntMsgs < maxNumMsgs && !caught_signal; ++cntMsgs)
163  {
164  auto msg = receiver->receive(rcvisardDynamics->getPbMsgTypeOfStream(streamName));
165  if (msg)
166  {
167  cout << "received msg " << endl << msg->DebugString() << endl;
168  }
169  }
170  }
171  catch (exception& e)
172  {
173  cout << "ERROR during streaming: " << e.what() << endl;
174  }
175 
179  try
180  {
181  cout << "stopping rc_dynamics module on rc_visard..." << endl;
182  rcvisardDynamics->stop();
183  }
184  catch (exception& e)
185  {
186  cout << "ERROR! Could not start rc_dynamics module on rc_visard: " << e.what() << endl;
187  }
188 
189  cout << "Received " << cntMsgs << " " << streamName << " messages." << endl;
190 
191 #ifdef WIN32
192  ::WSACleanup();
193 #endif
194 
195  return EXIT_SUCCESS;
196 }
static Ptr create(const std::string &rc_visard_ip, unsigned int requests_timeout=5000)
Creates a local instance of rc_visard&#39;s remote pose interface.
void signal_callback_handler(int signum)
static bool caught_signal
catching signals for proper program escape
void printUsage(char *arg)
Print usage of example including command line args.
int main(int argc, char *argv[])


rc_dynamics_api
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Endres
autogenerated on Sat Mar 6 2021 03:53:19