HLR  0.0.1
Driver.hpp
1 #ifndef KINEMATICS_RV2AJ_DRIVER_HPP
2 #define KINEMATICS_RV2AJ_DRIVER_HPP
3 
4 #include <ros/ros.h>
5 #include "HLR/RV2AJDriverClear.h"
6 #include "HLR/RV2AJDriverContinue.h"
7 #include "HLR/RV2AJDriverPause.h"
8 #include "HLR/RV2AJDriverQueueAdd.h"
9 #include "HLR/RV2AJDriverQueueCount.h"
10 #include "std_msgs/String.h"
11 #include "std_msgs/UInt64.h"
12 
13 #include <array>
14 #include <condition_variable>
15 #include <mutex>
16 #include <optional>
17 #include <queue>
18 #include <thread>
19 
20 #include "Serial.hpp"
21 
22 namespace HLR
23 {
24 namespace Kinematics
25 {
26 namespace RV2AJ
27 {
29 class Driver
30 {
31 public:
39  Driver(bool simulation,
40  const std::optional<std::string>& serial_path = std::nullopt,
41  bool debug = false);
42 
44  virtual ~Driver();
45 
50  Driver(const Driver& d) = delete;
51 
56  Driver(Driver&& d) = delete;
57 
63  Driver& operator=(const Driver& d) = delete;
64 
70  Driver& operator=(Driver&& d) = delete;
71 
78  bool cont(HLR::RV2AJDriverContinue::Request& req,
79  HLR::RV2AJDriverContinue::Response& res);
80 
87  bool pause(HLR::RV2AJDriverPause::Request& req,
88  HLR::RV2AJDriverPause::Response& res);
89 
96  bool clear(HLR::RV2AJDriverClear::Request& req,
97  HLR::RV2AJDriverClear::Response& res);
98 
105  bool queue_count(HLR::RV2AJDriverQueueCount::Request& req,
106  HLR::RV2AJDriverQueueCount::Response& res);
107 
114  bool queue_add(HLR::RV2AJDriverQueueAdd::Request& req,
115  HLR::RV2AJDriverQueueAdd::Response& res);
116 
117 private:
124  struct Command
125  {
130  std::array<std::string, 3> to_mb4() const;
131 
133  uint64_t id;
135  double base;
137  double shoulder;
139  double elbow;
141  double j4;
143  double wrist;
145  double wrist_rotate;
147  double j7;
149  double speed;
150  };
151 
153  enum class State
154  {
156  cont = 0,
158  pause = 1,
160  move = 2,
162  exit = 3
163  };
164 
172  void tx(const Command& cmd);
173 
179  void tx(const std::string& mb4);
180 
185  void rx();
186 
191  void rx_sub_callback(const std_msgs::String::ConstPtr& msg);
192 
194  void work();
195 
197  void work_reply();
198 
200  bool debug;
201 
203  std::size_t device_count = 0;
204 
206  uint64_t cmd_id = 0;
207 
209  ros::NodeHandle nh;
210 
212  std::mutex tx_m;
213 
215  ros::Publisher tx_pub;
216 
218  ros::Publisher tx_id_pub;
219 
221  bool tx_abort = false;
222 
224  std::mutex tx_abort_m;
225 
227  std::mutex rx_m;
228 
230  bool rx_block = true;
231 
233  std::mutex rx_block_m;
234 
236  std::condition_variable rx_block_cv;
237 
239  ros::Subscriber rx_sub;
240 
242  ros::ServiceServer srv_continue;
243 
245  ros::ServiceServer srv_pause;
246 
248  ros::ServiceServer srv_clear;
249 
251  ros::ServiceServer srv_queue_count;
252 
254  ros::ServiceServer srv_queue_add;
255 
257  std::thread worker;
258 
260  State worker_state = State::cont;
261 
263  std::mutex worker_state_m;
264 
266  std::queue<Command> commands;
267 
269  std::mutex commands_m;
270 
272  std::condition_variable commands_cv;
273 
275  std::thread worker_reply;
276 
278  State worker_reply_state = State::cont;
279 
281  std::mutex worker_reply_state_m;
282 
284  std::queue<std::string> replies;
285 
287  std::mutex replies_m;
288 
290  std::condition_variable replies_cv;
291 
293  Serial serial;
294 };
295 } // namespace RV2AJ
296 } // namespace Kinematics
297 } // namespace HLR
298 
299 #endif /* KINEMATICS_RV2AJ_DRIVER_HPP */
Driver for the Mitsubishi RV2-AJ.
Definition: Driver.hpp:29
bool queue_add(HLR::RV2AJDriverQueueAdd::Request &req, HLR::RV2AJDriverQueueAdd::Response &res)
Add requested command to the queue if compliant.
Driver(bool simulation, const std::optional< std::string > &serial_path=std::nullopt, bool debug=false)
Constructor.
virtual ~Driver()
Destructor.
OO wrapper around POSIX serial devices.
Definition: Serial.hpp:14
bool pause(HLR::RV2AJDriverPause::Request &req, HLR::RV2AJDriverPause::Response &res)
Pause processing events in queue.
bool clear(HLR::RV2AJDriverClear::Request &req, HLR::RV2AJDriverClear::Response &res)
Clears all to-be processed events in queue and stops current move.
bool cont(HLR::RV2AJDriverContinue::Request &req, HLR::RV2AJDriverContinue::Response &res)
Continue processing events in queue.
bool queue_count(HLR::RV2AJDriverQueueCount::Request &req, HLR::RV2AJDriverQueueCount::Response &res)
Returns the number of elements currently inside the queue.
Driver & operator=(const Driver &d)=delete
Copy assignment operator, deleted.