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/CPose3D.h>
13 #include <iostream>
14 #include <thread>
15 
16 using namespace mrpt;
17 using namespace mrpt::poses;
18 using namespace mrpt::io;
19 using namespace std;
20 
21 void thread_reader(CPipeReadEndPoint& read_pipe)
22 {
23  try
24  {
25  std::cout << "[thread_reader ID:" << std::this_thread::get_id()
26  << "] Started." << std::endl;
27 
28  // Simple read commands:
29  size_t len = 0;
30  char buf[100];
31  read_pipe.Read(&len, sizeof(len));
32  read_pipe.Read(buf, len);
33  buf[len] = 0;
34 
35  cout << "RX: " << buf << endl;
36 
37  // Read MRPT object from a pipe:
38  // *Note*: If the object class is known in advance, one can avoid smart
39  // pointers with ReadObject(&existingObj)
40  auto arch = mrpt::serialization::archiveFrom(read_pipe);
41  auto obj = arch.ReadObject();
42  if (IS_CLASS(obj, CPose3D))
43  {
44  CPose3D::Ptr ptrPose = std::dynamic_pointer_cast<CPose3D>(obj);
45  cout << "RX pose: " << *ptrPose << endl;
46  }
47 
48  printf("[thread_reader] Finished.\n");
49  }
50  catch (std::exception& e)
51  {
52  cerr << e.what() << endl;
53  }
54 }
55 
56 void thread_writer(CPipeWriteEndPoint& write_pipe)
57 {
58  try
59  {
60  std::cout << "[thread_writer ID:" << std::this_thread::get_id()
61  << "] Started." << std::endl;
62 
63  // Simple write commands:
64  const char* str = "Hello world!";
65  size_t len = strlen(str);
66  write_pipe.Write(&len, sizeof(len));
67  write_pipe.Write(str, len);
68 
69  // Send MRPT objects:
70  // *NOTE*: For efficiency, one should first write to an intermediary
71  // mrpt::utils::CMemoryChunk to write only once to the pipe.
72  mrpt::poses::CPose3D pose(1, 2, 3, 0.1, 0.2, 0.3);
73  auto arch = mrpt::serialization::archiveFrom(write_pipe);
74  arch.WriteObject(&pose);
75 
76  printf("[thread_writer] Finished.\n");
77  }
78  catch (std::exception& e)
79  {
80  cerr << e.what() << endl;
81  }
82 }
83 
84 // ------------------------------------------------------
85 // ThreadsTest
86 // ------------------------------------------------------
87 void ThreadsTest()
88 {
89  // Create a pipe:
90  std::unique_ptr<CPipeReadEndPoint> read_pipe;
91  std::unique_ptr<CPipeWriteEndPoint> write_pipe;
92 
93  CPipe::createPipe(read_pipe, write_pipe);
94 
95  // And send the two end-points to two threads:
96  std::thread hT1(thread_reader, std::ref(*read_pipe));
97  std::thread hT2(thread_writer, std::ref(*write_pipe));
98 
99  // Wait for the threads to end.
100  hT1.join();
101  hT2.join();
102 }
103 
104 // ------------------------------------------------------
105 // MAIN
106 // ------------------------------------------------------
107 int main()
108 {
109  try
110  {
111  ThreadsTest();
112 
113  return 0;
114  }
115  catch (std::exception& e)
116  {
117  std::cout << "MRPT exception caught: " << e.what() << std::endl;
118  return -1;
119  }
120  catch (...)
121  {
122  printf("Untyped exception!!");
123  return -1;
124  }
125 }
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
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::poses::CPose3D::Ptr
std::shared_ptr< CPose3D > Ptr
Definition: CPose3D.h:90
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
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
IS_CLASS
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
Definition: CObject.h:103
len
GLenum GLsizei len
Definition: glext.h:4712
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



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