serial.h
Go to the documentation of this file.
1 
32 // Based on example from:
33 // https://github.com/labust/labust-ros-pkg/wiki/Create-a-Serial-Port-application
34 
35 #ifndef CREATE_SERIAL_H
36 #define CREATE_SERIAL_H
37 
38 #include <condition_variable>
39 #include <functional>
40 #include <memory>
41 #include <mutex>
42 #include <thread>
43 
44 #include <boost/asio.hpp>
45 
46 #include "create/data.h"
47 #include "create/types.h"
48 #include "create/util.h"
49 
50 namespace create {
51  class Serial : public std::enable_shared_from_this<Serial> {
52 
53  protected:
54  boost::asio::io_service io;
55  boost::asio::signal_set signals;
56  boost::asio::serial_port port;
57 
58  private:
59  std::thread ioThread;
60  std::condition_variable dataReadyCond;
61  std::mutex dataReadyMut;
62  bool dataReady;
63  bool isReading;
64  bool firstRead;
65  uint8_t byteRead;
66 
67  // Callback executed when data arrives from Create
68  void onData(const boost::system::error_code& e, const std::size_t& size);
69  // Callback to execute once data arrives
70  std::function<void()> callback;
71  // Start and stop reading data from Create
72  bool startReading();
73  void stopReading();
74 
75  protected:
76  std::shared_ptr<Data> data;
77  // These are for possible diagnostics
78  uint64_t corruptPackets;
79  uint64_t totalPackets;
80 
81  virtual bool startSensorStream() = 0;
82  virtual void processByte(uint8_t byteRead) = 0;
83 
84  void signalHandler(const boost::system::error_code& error, int signal_number);
85  // Notifies main thread that data is fresh and makes the user callback
86  void notifyDataReady();
87 
88  public:
89  Serial(std::shared_ptr<Data> data);
90  ~Serial();
91  bool connect(const std::string& port, const int& baud = 115200, std::function<void()> cb = 0);
92  void disconnect();
93  inline bool connected() const { return port.is_open(); };
94  bool send(const uint8_t* bytes, const uint32_t numBytes);
95  bool sendOpcode(const Opcode& code);
96  uint64_t getNumCorruptPackets() const;
97  uint64_t getTotalPackets() const;
98  };
99 } // namespace create
100 
101 #endif // CREATE_SERIAL_H
std::function< void()> callback
Definition: serial.h:70
uint8_t byteRead
Definition: serial.h:65
uint64_t getTotalPackets() const
Definition: serial.cpp:198
boost::asio::serial_port port
Definition: serial.h:56
bool connected() const
Definition: serial.h:93
virtual void processByte(uint8_t byteRead)=0
bool sendOpcode(const Opcode &code)
Definition: serial.cpp:189
bool firstRead
Definition: serial.h:64
void notifyDataReady()
Definition: serial.cpp:142
std::condition_variable dataReadyCond
Definition: serial.h:60
Opcode
Definition: types.h:154
Definition: create.h:47
uint64_t totalPackets
Definition: serial.h:79
bool startReading()
Definition: serial.cpp:74
void stopReading()
Definition: serial.cpp:129
std::shared_ptr< Data > data
Definition: serial.h:76
boost::asio::signal_set signals
Definition: serial.h:55
void disconnect()
Definition: serial.cpp:60
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:179
virtual bool startSensorStream()=0
void signalHandler(const boost::system::error_code &error, int signal_number)
Definition: serial.cpp:24
bool dataReady
Definition: serial.h:62
bool connect(const std::string &port, const int &baud=115200, std::function< void()> cb=0)
Definition: serial.cpp:36
std::thread ioThread
Definition: serial.h:59
void onData(const boost::system::error_code &e, const std::size_t &size)
Definition: serial.cpp:159
std::mutex dataReadyMut
Definition: serial.h:61
boost::asio::io_service io
Definition: serial.h:54
bool isReading
Definition: serial.h:63
Serial(std::shared_ptr< Data > data)
Definition: serial.cpp:10
uint64_t getNumCorruptPackets() const
Definition: serial.cpp:194
uint64_t corruptPackets
Definition: serial.h:78


libcreate
Author(s): Jacob Perron
autogenerated on Mon Nov 2 2020 03:59:34