Kuka.hpp
Go to the documentation of this file.
1 #ifndef GRL_KUKA_HPP
2 #define GRL_KUKA_HPP
3 
4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string/predicate.hpp>
6 #include <boost/container/static_vector.hpp>
7 #include <boost/range/algorithm/copy.hpp>
8 #include <boost/range/algorithm/transform.hpp>
9 #include <boost/exception/all.hpp>
10 
11 
13 #include "grl/tags.hpp"
14 #include "grl/exception.hpp"
15 
16 namespace KUKA {
17 namespace LBRState {
18 /// @todo replace all instances of this with the getter now provided
19 const int NUM_DOF = 7;
20 const int LBRMONITORMESSAGEID = 0x245142;
21 }
22 namespace LBRCommand {
23 // Following from Kuka friLBRCommand.cpp
24 const int LBRCOMMANDMESSAGEID = 0x34001;
25 }
26 
27 } // namespace KUKA
28 
29 namespace grl {
30 namespace robot {
31 namespace arm {
32 
33 /// @brief Internal implementation class for driver use only, stores all the
34 /// kuka state data in a simple object
35 /// @todo replace with something generic
36 /// @deprecated this is an old implemenation that will be removed in the future,
37 /// do not depend on this struct directly.
38 /// @todo commandedPosition and commandedPosition_goal are used a bit
39 /// ambiguously, figure out the difference and clean it up.
40 struct KukaState {
41  typedef boost::container::static_vector<double, KUKA::LBRState::NUM_DOF>
43  typedef boost::container::static_vector<double, 7> cartesian_state;
44  typedef std::chrono::time_point<std::chrono::high_resolution_clock>
46 
50  cartesian_state externalForce;
53  cartesian_state wrenchJava;
55 
58  std::chrono::milliseconds sendPeriod; ///< time duration between each FRI low
59  /// level UDP packet loop update
60 
61  // Each of the following have an equivalent in kuka's friClientIf.h
62  // which needed to be reimplemented due to licensing restrictions
63  // in the corresponding C++ code
64  flatbuffer::ESessionState sessionState; // KUKA::FRI::ESessionState
66  connectionQuality; // KUKA::FRI::EConnectionQuality
67  flatbuffer::ESafetyState safetyState; // KUKA::FRI::ESafetyState
68  flatbuffer::EOperationMode operationMode; // KUKA::FRI::EOperationMode
69  flatbuffer::EDriveState driveState; // KUKA::FRI::EDriveState
70 
71  // The point in time associated with the current measured
72  // state of the arm (position, torque, etc.). When commanding
73  // the arm use commanded_goal_timestamp.
75 
76  /////////////////////////////////////////////////////////////////////////////////////////////
77  // members below here define the driver state and are not part of the FRI arm
78  // message format
79  /////////////////////////////////////////////////////////////////////////////////////////////
80 
81  /// we need to mind arm constraints, so we set a goal then work towards it
83 
84  /// time duration over which commandedPosition_goal is expected to be reached.
85  /// this will be used when computing the trajectory with which the low level
86  /// algorithm will approach the goal position.
87  /// This should be strictly greater than or equal to timestamp.
88  ///
89  /// @note: this is part of the driver wrapper and is not present in the
90  /// underlying kuka APIs.
92 
93  /// velocity_limits we need to mind arm constraints, so we set a goal then
94  /// work towards it. While velocity limits are not provided explicitly by KUKA
95  /// in their low level C++ API, if you send a command that violates the
96  /// velocity limits the arm stops immediately with an error.
98 
99 
100  void clear() {
101  position.clear();
102  torque.clear();
103  externalTorque.clear();
104  externalForce.clear();
105  commandedPosition.clear();
106  commandedTorque.clear();
107  commandedCartesianWrenchFeedForward.clear();
108  ipoJointPosition.clear();
109  commandedPosition_goal.clear();
110  goal_position_command_time_duration = 0;
111  }
112 
113  void clearCommands() {
114  commandedPosition.clear();
115  commandedTorque.clear();
116  commandedCartesianWrenchFeedForward.clear();
117  commandedPosition_goal.clear();
118  }
119 };
120 
121 constexpr auto KUKA_LBR_IIWA_14_R820 = "KUKA_LBR_IIWA_14_R820";
122 constexpr auto KUKA_LBR_IIWA_7_R800 = "KUKA_LBR_IIWA_7_R800";
123 
124 /// @brief copy vector of joint velocity limits in radians/s
125 ///
126 /// @todo R800 velocity limits aren't correct!
127 /// @todo find a better design where this can be expanded for more models of
128 /// robot in the future, maybe a std::unordered_map?
129 template <typename OutputIterator>
130 OutputIterator
131 copy(std::string model, OutputIterator it,
133  if (boost::iequals(model, KUKA_LBR_IIWA_14_R820)) {
134 
135  // R820 velocity limits
136  // A1 - 85 °/s == 1.483529864195 rad/s
137  // A2 - 85 °/s == 1.483529864195 rad/s
138  // A3 - 100 °/s == 1.745329251994 rad/s
139  // A4 - 75 °/s == 1.308996938996 rad/s
140  // A5 - 130 °/s == 2.268928027593 rad/s
141  // A6 - 135 °/s == 2.356194490192 rad/s
142  // A1 - 135 °/s == 2.356194490192 rad/s
143 
144  KukaState::joint_state maxVel;
145  maxVel.push_back(1.483529864195); // RK: Commented out secondsPerTick
146  maxVel.push_back(1.483529864195);
147  maxVel.push_back(1.745329251994);
148  maxVel.push_back(1.308996938996);
149  maxVel.push_back(2.268928027593);
150  maxVel.push_back(2.356194490192);
151  maxVel.push_back(2.356194490192);
152 
153  return boost::copy(maxVel, it);
154  } else if (boost::iequals(model, KUKA_LBR_IIWA_7_R800)) {
155 
156  /// @RK: updated the right joint velocity information based
157  // on the 800 model from the KUKA manual
158 
159  // R800 velocity limits
160  // A1 - 98 °/s == 1.71042 rad/s
161  // A2 - 98 °/s == 1.71042 rad/s
162  // A3 - 100 °/s == 1.74533 rad/s
163  // A4 - 130 °/s == 2.26893 rad/s
164  // A5 - 140 °/s == 2.44346 rad/s
165  // A6 - 180 °/s == 3.14159 rad/s
166  // A1 - 180 °/s == 3.14159 rad/s
167 
168  KukaState::joint_state maxVel;
169  maxVel.push_back(1.71042);
170  maxVel.push_back(1.71042);
171  maxVel.push_back(1.74533);
172  maxVel.push_back(2.26893);
173  maxVel.push_back(2.44346);
174  maxVel.push_back(3.14159);
175  maxVel.push_back(3.14159);
176 
177  return boost::copy(maxVel, it);
178  }
179 
180  else
181  return it;
182 }
183 
184 
185 /// @brief Internal class, defines some default status variables
186 ///
187 /// This class defines some connection functions and parameter definitions
188 /// that are shared amongst many of the KUKA API components
189 class KukaUDP {
190 
191 public:
192  enum ParamIndex {
193  RobotModel, // RobotModel (options are KUKA_LBR_IIWA_14_R820,
194  // KUKA_LBR_IIWA_7_R800)
195  localhost, // 192.170.10.100
196  localport, // 30200
197  remotehost, // 192.170.10.2
198  remoteport, // 30200
199  is_running_automatically // true by default, this means that an internal
200  // thread will be created to run the driver.
201  };
202 
203  enum ThreadingRunMode { run_manually = 0, run_automatically = 1 };
204 
205  typedef std::tuple<std::string, std::string, std::string, std::string,
206  std::string, ThreadingRunMode>
208 
209  static const Params defaultParams() {
210  return std::make_tuple(KUKA_LBR_IIWA_14_R820, std::string("192.170.10.100"),
211  std::string("30200"), std::string("192.170.10.2"),
212  std::string("30200"), run_automatically);
213  }
214 
215  /// Advanced functionality, do not use without a great reason
216  template <typename T>
217  static boost::asio::ip::udp::socket
218  connect(T &params, boost::asio::io_service &io_service_,
219  boost::asio::ip::udp::endpoint &sender_endpoint) {
220  std::string localhost(std::get<localhost>(params));
221  std::string lp(std::get<localport>(params));
222  short localport = boost::lexical_cast<short>(lp);
223  std::string remotehost(std::get<remotehost>(params));
224  std::string rp(std::get<remoteport>(params));
225  short remoteport = boost::lexical_cast<short>(rp);
226  std::cout << "using: "
227  << " " << localhost << " " << localport << " " << remotehost
228  << " " << remoteport << "\n";
229 
230  boost::asio::ip::udp::socket s(
231  io_service_,
232  boost::asio::ip::udp::endpoint(
233  boost::asio::ip::address::from_string(localhost), localport));
234 
235  boost::asio::ip::udp::resolver resolver(io_service_);
236  sender_endpoint =
237  *resolver.resolve({boost::asio::ip::udp::v4(), remotehost, rp});
238  s.connect(sender_endpoint);
239 
240  return std::move(s);
241  }
242 
243  static void add_details_to_connection_error(boost::exception &e,
244  Params &params) {
245  e << errmsg_info(
246  "KukaUDP: Unable to connect to Kuka FRI Koni UDP device "
247  "using boost::asio::udp::socket configured with localhost:localport "
248  "@ " +
249  std::get<localhost>(params) + ":" + std::get<localport>(params) +
250  " and remotehost:remoteport @ " + std::get<remotehost>(params) + ":" +
251  std::get<remoteport>(params) + "\n");
252  }
253 };
254 
255 }
256 }
257 } // namespace grl::robot::arm
258 
259 #endif
joint_state velocity_limits
Definition: Kuka.hpp:97
cartesian_state externalForce
Definition: Kuka.hpp:50
cartesian_state wrenchJava
Definition: Kuka.hpp:53
joint velocity constraint (ex: max velocity)
Definition: tags.hpp:58
flatbuffer::ESafetyState safetyState
Definition: Kuka.hpp:67
joint_state commandedPosition
Definition: Kuka.hpp:51
time_point_type timestamp
Definition: Kuka.hpp:74
Internal implementation class for driver use only, stores all the kuka state data in a simple object...
Definition: Kuka.hpp:40
std::chrono::time_point< std::chrono::high_resolution_clock > time_point_type
Definition: Kuka.hpp:45
boost::container::static_vector< double, 7 > cartesian_state
Definition: Kuka.hpp:43
cartesian_state commandedCartesianWrenchFeedForward
Definition: Kuka.hpp:52
OutputIterator copy(std::string model, OutputIterator it, grl::revolute_joint_velocity_open_chain_state_constraint_tag)
copy vector of joint velocity limits in radians/s
Definition: Kuka.hpp:131
Definition: Kuka.hpp:16
double goal_position_command_time_duration
Definition: Kuka.hpp:91
defines tag dispatching types used throughout grl
const int NUM_DOF
Definition: Kuka.hpp:19
joint_state ipoJointPosition
Definition: Kuka.hpp:56
static void add_details_to_connection_error(boost::exception &e, Params &params)
Definition: Kuka.hpp:243
constexpr auto KUKA_LBR_IIWA_14_R820
Definition: Kuka.hpp:121
flatbuffer::EOperationMode operationMode
Definition: Kuka.hpp:68
joint_state ipoJointPositionOffsets
Definition: Kuka.hpp:57
boost::error_info< struct tag_errmsg, std::string > errmsg_info
Definition: exception.hpp:7
joint_state position
Definition: Kuka.hpp:47
constexpr auto KUKA_LBR_IIWA_7_R800
Definition: Kuka.hpp:122
joint_state commandedPosition_goal
we need to mind arm constraints, so we set a goal then work towards it
Definition: Kuka.hpp:82
const int LBRMONITORMESSAGEID
Definition: Kuka.hpp:20
joint_state torque
Definition: Kuka.hpp:48
flatbuffer::EDriveState driveState
Definition: Kuka.hpp:69
boost::container::static_vector< double, KUKA::LBRState::NUM_DOF > joint_state
Definition: Kuka.hpp:42
flatbuffer::ESessionState sessionState
Definition: Kuka.hpp:64
joint_state externalTorque
Definition: Kuka.hpp:49
static boost::asio::ip::udp::socket connect(T &params, boost::asio::io_service &io_service_, boost::asio::ip::udp::endpoint &sender_endpoint)
Advanced functionality, do not use without a great reason.
Definition: Kuka.hpp:218
const int LBRCOMMANDMESSAGEID
Definition: Kuka.hpp:24
std::tuple< std::string, std::string, std::string, std::string, std::string, ThreadingRunMode > Params
Definition: Kuka.hpp:207
flatbuffer::EConnectionQuality connectionQuality
Definition: Kuka.hpp:66
joint_state commandedTorque
Definition: Kuka.hpp:54
static const Params defaultParams()
Definition: Kuka.hpp:209
Internal class, defines some default status variables.
Definition: Kuka.hpp:189
std::chrono::milliseconds sendPeriod
Definition: Kuka.hpp:58