MRPT  1.9.9
NodeletsTest_impl.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /** \example comms_nodelets_example/NodeletsTest_impl.cpp */
11 
13 
14 //! [example-nodelets]
15 #include <mrpt/comms/nodelets.h>
16 #include <mrpt/poses/CPose3D.h>
17 #include <cstdio> // printf()
18 #include <thread>
19 #include <chrono>
20 #include <iostream>
21 
22 // Test payload:
23 const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6);
24 
25 // Create the topic directory. Only once per process, and must be shared
26 // by all nodelets/threads.
28 
30 {
31  using namespace mrpt::comms;
32  using namespace std;
33 
34  try
35  {
36 #ifdef NODELETS_TEST_VERBOSE
37  printf("[publisher] Started\n");
38 #endif
39 
40  for (int i = 0; i < 5; i++)
41  {
42  std::this_thread::sleep_for(100ms);
43  dir->getTopic("/robot/odom")->publish(p_tx);
44  }
45 
46 #ifdef NODELETS_TEST_VERBOSE
47  printf("[publisher] Finish\n");
48 #endif
49  }
50  catch (std::exception& e)
51  {
52  cerr << e.what() << endl;
53  }
54  catch (...)
55  {
56  printf("[thread_publisher] Runtime error!\n");
57  }
58 }
59 
61 {
62 #ifdef NODELETS_TEST_VERBOSE
63  std::cout << "sub2: rx CPose3D" << p.asString() << std::endl;
64 #endif
65 }
66 
67 void onNewMsg2(int idx, const mrpt::poses::CPose3D& p)
68 {
69 #ifdef NODELETS_TEST_VERBOSE
70  std::cout << "onNewMsg2: idx=" << idx << " rx CPose3D" << p.asString()
71  << std::endl;
72 #endif
73 }
74 
76 {
77  using namespace mrpt::comms;
78  using namespace std;
79 
80  try
81  {
82 #ifdef NODELETS_TEST_VERBOSE
83  printf("[subscriber] Connecting\n");
84 #endif
85 
86 #ifdef NODELETS_TEST_VERBOSE
87  printf("[subscriber] Connected. Waiting for a message...\n");
88 #endif
89 
90  // Create a subscriber with a lambda:
91  Subscriber::Ptr sub1 =
92  dir->getTopic("/robot/odom")
93  ->createSubscriber<mrpt::poses::CPose3D>(
94  [](const mrpt::poses::CPose3D& p_rx) -> void {
95 #ifdef NODELETS_TEST_VERBOSE
96  std::cout << "sub1: rx CPose3D" << p_rx.asString()
97  << std::endl;
98 #endif
99  nodelets_test_passed_ok = (p_rx == p_tx);
100  });
101 
102  // Create a subscriber with a regular function via std::function:
103  auto sub2 = dir->getTopic("/robot/odom")
104  ->createSubscriber<mrpt::poses::CPose3D>(
105  std::function<void(const mrpt::poses::CPose3D&)>(
106  &onNewMsg));
107 
108  // Create a subscriber with a regular function:
109  auto sub3 = dir->getTopic("/robot/odom")
110  ->createSubscriber<mrpt::poses::CPose3D>(&onNewMsg);
111 
112  // Create a subscriber with std::bind:
113  using namespace std::placeholders;
114  auto sub4 = dir->getTopic("/robot/odom")
115  ->createSubscriber<mrpt::poses::CPose3D>(std::bind(
116  onNewMsg2, 123 /*fixed 1st argument*/,
117  _1 /* 2nd arg is the subscribed data */));
118 
119  // wait for messages to arrive.
120  // The nodelet is up and live until "sub" gets out of scope.
121  std::this_thread::sleep_for(1000ms);
122 
123 #ifdef NODELETS_TEST_VERBOSE
124  printf("[subscriber] Finish\n");
125 #endif
126  }
127  catch (std::exception& e)
128  {
129  cerr << e.what() << endl;
130  }
131  catch (...)
132  {
133  cerr << "[thread_subscriber] Runtime error!" << endl;
134  }
135 }
136 
138 {
139  using namespace std::chrono_literals;
140 
141  std::thread(thread_publisher).detach();
142  std::thread(thread_subscriber).detach();
143  std::this_thread::sleep_for(1000ms);
144 }
145 //! [example-nodelets]
STL namespace.
void thread_subscriber()
void onNewMsg2(int idx, const mrpt::poses::CPose3D &p)
bool nodelets_test_passed_ok
auto dir
const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6)
[example-nodelets]
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
void thread_publisher()
void onNewMsg(const mrpt::poses::CPose3D &p)
Serial and networking devices and utilities.
GLfloat GLfloat p
Definition: glext.h:6305
void NodeletsTest()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020