robot_interface
Native robot interface for making the visual manipulation
control_ur.hpp
1 // Copyright (c) 2019 Intel Corporation. All Rights Reserved
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #pragma once
20 
21 #include <rclcpp/rclcpp.hpp>
23 
24 #include "ur_modern_driver/log.h"
25 #include "ur_modern_driver/pipeline.h"
26 #include "ur_modern_driver/ur/commander.h"
27 #include "ur_modern_driver/ur/factory.h"
28 #include "ur_modern_driver/ur/messages.h"
29 #include "ur_modern_driver/ur/parser.h"
30 #include "ur_modern_driver/ur/producer.h"
31 #include "ur_modern_driver/ur/rt_state.h"
32 #include "ur_modern_driver/ur/state.h"
33 
34 static const std::vector<std::string> JOINTS = { "shoulder_pan_joint", "shoulder_lift_joint", "elbow_joint",
35  "wrist_1_joint", "wrist_2_joint", "wrist_3_joint" };
36 static const std::string HOST = "192.168.0.5";
37 static const bool SHUTDOWN_ON_DISCONNECT = true;
38 static const int UR_SECONDARY_PORT = 30002;
39 static const int UR_RT_PORT = 30003;
40 
41 struct ProgArgs
42 {
43 public:
44  std::string host;
45  std::vector<std::string> joint_names;
46  bool shutdown_on_disconnect;
47 };
48 
49 class IgnorePipelineStoppedNotifier : public INotifier
50 {
51 public:
52  void started(std::string name)
53  {
54  LOG_INFO("Starting pipeline %s", name.c_str());
55  }
56  void stopped(std::string name)
57  {
58  LOG_INFO("Stopping pipeline %s", name.c_str());
59  }
60 };
61 
62 class ShutdownOnPipelineStoppedNotifier : public INotifier
63 {
64 public:
65  void started(std::string name)
66  {
67  LOG_INFO("Starting pipeline %s", name.c_str());
68  }
69  void stopped(std::string name)
70  {
71  LOG_INFO("Shutting down on stopped pipeline %s", name.c_str());
72  rclcpp::shutdown();
73  exit(1);
74  }
75 };
76 
77 class URControl: public ArmControlBase, public URRTPacketConsumer
78 {
79 public:
80  URControl(const std::string node_name, const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
81  : ArmControlBase(node_name, options), gripper_powered_up_(false)
82  {
83  for (auto const& joint : JOINTS)
84  {
85  joint_names_.push_back(joint);
86  }
87  }
88 
89  ~URControl()
90  {
91  rt_pl_->stop();
92  state_pl_->stop();
93  factory_.reset(nullptr);
94  notifier_ = nullptr;
95  LOG_INFO("UR control interface shut down.");
96  }
97 
98  // Overload ArmControlBase functions
99  virtual bool moveToTcpPose(double x, double y, double z,
100  double alpha, double beta, double gamma,
101  double vel, double acc);
102 
103  virtual bool open(const double distance = 0);
104 
105  virtual bool close(const double distance = 0);
106 
107  // Send URScript to ur robot controller
108  bool urscriptInterface(const std::string command_script);
109 
110  // Start socket communication loop
111  bool startLoop();
112 
113  // Overload URRTPacketConsumer functions
114  virtual bool consume(RTState_V1_6__7& state);
115  virtual bool consume(RTState_V1_8& state);
116  virtual bool consume(RTState_V3_0__1& state);
117  virtual bool consume(RTState_V3_2__3& state);
118 
119  virtual void setupConsumer()
120  {
121  }
122  virtual void teardownConsumer()
123  {
124  }
125  virtual void stopConsumer()
126  {
127  }
128 
129  // Functions to publish joint states
130  bool publishJoints(RTShared& packet, rclcpp::Time t);
131  bool publish(RTShared& packet);
132 
133  // Function to get tool pose
134  bool getTcpPose(RTShared& packet);
135 
136  // Parse parameters
137  void parseArgs();
138 
139 private:
140 
141  ProgArgs args_;
142  std::string local_ip_;
143  std::unique_ptr<URFactory> factory_;
144 
145  // Robot rt message
146  std::unique_ptr<URParser<RTPacket>> rt_parser_;
147  std::unique_ptr<URStream> rt_stream_;
148  std::unique_ptr<URProducer<RTPacket>> rt_prod_;
149  std::unique_ptr<URCommander> rt_commander_;
150  vector<IConsumer<RTPacket> *> rt_vec_;
151  std::unique_ptr<MultiConsumer<RTPacket>> rt_cons_;
152  std::unique_ptr<Pipeline<RTPacket>> rt_pl_;
153 
154  INotifier *notifier_;
155 
156  // Robot state message
157  std::unique_ptr<URParser<StatePacket>> state_parser_;
158  std::unique_ptr<URStream> state_stream_;
159  std::unique_ptr<URProducer<StatePacket>> state_prod_;
160  vector<IConsumer<StatePacket> *> state_vec_;
161  std::unique_ptr<MultiConsumer<StatePacket>> state_cons_;
162  std::unique_ptr<Pipeline<StatePacket>> state_pl_;
163 
164  bool gripper_powered_up_;
165 };
Definition: control_ur.hpp:62
Definition: control_ur.hpp:41
Robot arm control interface.
Definition: control_base.hpp:55
Definition: control_ur.hpp:49
Native robot control interface for visual manipulation.
Definition: control_ur.hpp:77