Main MRPT website > C++ reference for MRPT 1.9.9
test.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 #include <mrpt/io/CPipe.h>
11 #include <mrpt/poses/CPose2D.h>
12 #include <mrpt/poses/CPose3D.h>
14 #include <thread>
15 #include <iostream>
16 
17 using namespace mrpt;
18 using namespace mrpt::poses;
19 using namespace mrpt::system;
20 using namespace mrpt::io;
21 using namespace mrpt::serialization;
22 using namespace std;
23 
24 void thread_reader(CPipeReadEndPoint& read_pipe)
25 {
26  try
27  {
28  std::cout << "[thread_reader ID:" << std::this_thread::get_id()
29  << "] Started." << std::endl;
30 
31  // Simple read commands:
32  size_t len = 0;
33  char buf[100];
34  read_pipe.Read(&len, sizeof(len));
35  read_pipe.Read(buf, len);
36  buf[len] = 0;
37  cout << "RX: " << buf << endl;
38 
39  // Read MRPT object from a pipe:
40  // *Note*: If the object class is known in advance, one can avoid smart
41  // pointers with ReadObject(&existingObj)
42  auto arch = archiveFrom(read_pipe);
43  auto var =
44  arch.ReadVariant<mrpt::poses::CPose2D, mrpt::poses::CPose3D>();
45  var.match([](auto& pose) { cout << "RX pose: " << pose << endl; });
46  var = arch.ReadVariant<mrpt::poses::CPose2D, mrpt::poses::CPose3D>();
47  var.match([](auto& pose) { cout << "RX pose: " << pose << endl; });
48 
49  printf("[thread_reader] Finished.\n");
50  }
51  catch (std::exception& e)
52  {
53  cerr << e.what() << endl;
54  }
55 }
56 
57 void thread_writer(CPipeWriteEndPoint& write_pipe)
58 {
59  try
60  {
61  std::cout << "[thread_writer ID:" << std::this_thread::get_id()
62  << "] Started." << std::endl;
63 
64  // Simple write commands:
65  const char* str = "Hello world!";
66  size_t len = strlen(str);
67  write_pipe.Write(&len, sizeof(len));
68  write_pipe.Write(str, len);
69 
70  // Send MRPT objects:
71  mrpt::poses::CPose3D pose1(1, 2, 3, 1.57, 3.14, 0);
72  mrpt::poses::CPose2D pose2(1.0, 2.0, 3.1415);
73  std::variant<mrpt::poses::CPose3D, mrpt::poses::CPose2D> var1(
74  std::move(pose1));
75  std::variant<mrpt::poses::CPose3D, mrpt::poses::CPose2D> var2(
76  std::move(pose2));
77  auto arch = archiveFrom(write_pipe);
78  arch.WriteVariant(var1);
79  arch.WriteVariant(var2);
80 
81  printf("[thread_writer] Finished.\n");
82  }
83  catch (std::exception& e)
84  {
85  cerr << e.what() << endl;
86  }
87 }
88 
89 // ------------------------------------------------------
90 // ThreadsTest
91 // ------------------------------------------------------
92 void ThreadsTest()
93 {
94  // Create a pipe:
95  std::unique_ptr<CPipeReadEndPoint> read_pipe;
96  std::unique_ptr<CPipeWriteEndPoint> write_pipe;
97 
98  CPipe::createPipe(read_pipe, write_pipe);
99 
100  // And send the two end-points to two threads:
101  std::thread hT1(thread_reader, std::ref(*read_pipe));
102  std::thread hT2(thread_writer, std::ref(*write_pipe));
103 
104  std::this_thread::sleep_for(std::chrono::milliseconds(10));
105  // Wait for the threads to end.
106  hT2.join();
107  // We need to close this to ensure the pipe gets flushed
108  // Remember Unix uses buffered io
109  // write_pipe.reset();
110  hT1.join();
111 }
112 
113 // ------------------------------------------------------
114 // MAIN
115 // ------------------------------------------------------
116 int main()
117 {
118  try
119  {
120  ThreadsTest();
121 
122  return 0;
123  }
124  catch (std::exception& e)
125  {
126  std::cout << "MRPT exception caught: " << e.what() << std::endl;
127  return -1;
128  }
129  catch (...)
130  {
131  printf("Untyped exception!!");
132  return -1;
133  }
134 }
thread_reader
void thread_reader(CPipeReadEndPoint &read_pipe)
Definition: vision_stereo_rectify/test.cpp:21
mrpt::io
Definition: img/CImage.h:22
mrpt::io::CPipeReadEndPoint
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:128
CPipe.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CPose2D.h
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::poses::CPose2D
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:40
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
len
GLenum GLsizei len
Definition: glext.h:4712
mrpt::serialization
Definition: aligned_serialization.h:14
CPose3D.h
mrpt::io::CPipeWriteEndPoint
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:151
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:561
ref
GLenum GLint ref
Definition: glext.h:4050
ThreadsTest
void ThreadsTest()
Definition: vision_stereo_rectify/test.cpp:87
CArchive.h
mrpt::io::CPipeBaseEndPoint::Read
virtual size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CPipe.cpp:120
thread_writer
void thread_writer(CPipeWriteEndPoint &write_pipe)
Definition: vision_stereo_rectify/test.cpp:56
mrpt::io::CPipeBaseEndPoint::Write
virtual size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CPipe.cpp:219
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST