open_manipulator_p.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2019 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Darby Lim, Hye-Jong KIM, Ryan Shim, Yong-Ho Na */
18 
19 #include "../include/open_manipulator_p_libs/open_manipulator_p.h"
20 
22 
24 {
25  delete kinematics_;
26  delete actuator_;
27  delete tool_;
28  for(uint8_t index = 0; index < CUSTOM_TRAJECTORY_SIZE; index++)
29  delete custom_trajectory_[index];
30 }
31 
32 void OpenManipulator::initOpenManipulator(bool using_actual_robot_state, STRING usb_port, STRING baud_rate, float control_loop_time, bool with_gripper)
33 {
34  /*****************************************************************************
35  ** Initialize Manipulator Parameter
36  *****************************************************************************/
37  addWorld("world", // world name
38  "joint1"); // child name
39 
40  addJoint("joint1", // my name
41  "world", // parent name
42  "joint2", // child name
43  math::vector3(0.0, 0.0, 0.126), // relative position
44  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
45  Z_AXIS, // axis of rotation
46  1, // actuator id
47  M_PI, // max joint limit (3.14 rad)
48  -M_PI, // min joint limit (-3.14 rad)
49  1.0, // coefficient
50  9.8406837e-02, // mass
51  math::inertiaMatrix(3.4543422e-05, -1.6031095e-08, -3.8375155e-07,
52  3.2689329e-05, 2.8511935e-08,
53  1.8850320e-05), // inertial tensor
54  math::vector3(-3.0184870e-04, 5.4043684e-04, 0.018 + 2.9433464e-02) // COM
55  );
56 
57  addJoint("joint2", // my name
58  "joint1", // parent name
59  "joint3", // child name
60  math::vector3(0.0, 0.0, 0.033), // relative position
61  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
62  Y_AXIS, // axis of rotation
63  2, // actuator id
64  M_PI, // max joint limit (3.14 rad)
65  -M_PI, // min joint limit (-3.14 rad)
66  1.0, // coefficient
67  1.3850917e-01, // mass
68  math::inertiaMatrix(3.3055381e-04, 9.7940978e-08, -3.8505711e-05,
69  3.4290447e-04, -1.5717516e-06,
70  6.0346498e-05), // inertial tensor
71  math::vector3(1.0308393e-02, 3.7743363e-04, 1.0170197e-01) // COM
72  );
73 
74  addJoint("joint3", // my name
75  "joint2", // parent name
76  "joint4", // child name
77  math::vector3(0.030, 0.0, 0.264), // relative position
78  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
79  Y_AXIS, // axis of rotation
80  3, // actuator id
81  M_PI, // max joint limit (3.14 rad)
82  -M_PI, // min joint limit (-3.14 rad)
83  1.0, // coefficient
84  1.3274562e-01, // mass
85  math::inertiaMatrix(3.0654178e-05, -1.2764155e-06, -2.6874417e-07,
86  2.4230292e-04, 1.1559550e-08,
87  2.5155057e-04), // inertial tensor
88  math::vector3(9.0909590e-02, 3.8929816e-04, 2.2413279e-04) // COM
89  );
90 
91  addJoint("joint4", // my name
92  "joint3", // parent name
93  "joint5", // child name
94  math::vector3(0.195, 0.0, 0.030), // relative position
95  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
96  X_AXIS, // axis of rotation
97  4, // actuator id
98  M_PI, // max joint limit (3.14 rad)
99  -M_PI, // min joint limit (-3.14 rad)
100  1.0, // coefficient
101  1.4327573e-01, // mass
102  math::inertiaMatrix(8.0870749e-05, 0.0, -1.0157896e-06,
103  7.5980465e-05, 0.0,
104  9.3127351e-05), // inertial tensor
105  math::vector3(4.4206755e-02, 3.6839985e-07, 8.9142216e-03) // COM
106  );
107 
108  addJoint("joint5", // my name
109  "joint4", // parent name
110  "joint6", // child name
111  math::vector3(0.063, 0.0, 0.0), // relative position
112  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
113  Y_AXIS, // axis of rotation
114  5, // actuator id
115  M_PI, // max joint limit (3.14 rad)
116  -M_PI, // min joint limit (-3.14 rad)
117  1.0, // coefficient
118  1.4327573e-01, // mass
119  math::inertiaMatrix(8.0870749e-05, 0.0, -1.0157896e-06,
120  7.5980465e-05, 0.0,
121  9.3127351e-05), // inertial tensor
122  math::vector3(4.4206755e-02, 3.6839985e-07, 8.9142216e-03) // COM
123  );
124 
125  addJoint("joint6", // my name
126  "joint5", // parent name
127  "gripper", // child name
128  math::vector3(0.123, 0.0, 0.0), // relative position
129  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
130  X_AXIS, // axis of rotation
131  6, // actuator id
132  M_PI, // max joint limit (3.14 rad)
133  -M_PI, // min joint limit (-3.14 rad)
134  1.0, // coefficient
135  1.4327573e-01, // mass
136  math::inertiaMatrix(8.0870749e-05, 0.0, -1.0157896e-06,
137  7.5980465e-05, 0.0,
138  9.3127351e-05), // inertial tensor
139  math::vector3(4.4206755e-02, 3.6839985e-07, 8.9142216e-03) // COM
140  );
141 
142  int gripper_id = -1;
143  double gripper_len = 0.0;
144  if (with_gripper)
145  {
146  gripper_id = 7;
147  gripper_len = 0.1223;
148  }
149  addTool("gripper", // my name
150  "joint6", // parent name
151  math::vector3(gripper_len, 0.0, 0.0), // relative position
152  // math::vector3(0.150, 0.0, 0.0), // relative position
153  math::convertRPYToRotationMatrix(0.0, 0.0, 0.0), // relative orientation
154  gripper_id, // actuator id
155  1.1351, // max gripper limit (0.01 m)
156  -0.001, // min gripper limit (-0.01 m)
157  1.0, // Change unit from `meter` to `radian`
158  3.2218127e-02 * 2, // mass
159  math::inertiaMatrix(9.5568826e-06, 2.8424644e-06, -3.2829197e-10,
160  2.2552871e-05, -3.1463634e-10,
161  1.7605306e-05), // inertial tensor
162  math::vector3(0.028 + 8.3720668e-03, 0.0246, -4.2836895e-07) // COM
163  );
164 
165  /*****************************************************************************
166  ** Initialize Kinematics
167  *****************************************************************************/
169 
171 
172  // Set kinematics arguments
173  void *p_with_gripper = &with_gripper;
174  setKinematicsOption(p_with_gripper);
175 
176  if (using_actual_robot_state)
177  {
178  /*****************************************************************************
179  ** Initialize Joint Actuator
180  *****************************************************************************/
182  // actuator_ = new dynamixel::JointDynamixelProfileControl(control_loop_time);
183 
184  // Set communication arguments
185  STRING dxl_comm_arg[2] = {usb_port, baud_rate};
186  void *p_dxl_comm_arg = &dxl_comm_arg;
187 
188  // Set joint actuator id
189  std::vector<uint8_t> jointDxlId;
190  jointDxlId.push_back(1);
191  jointDxlId.push_back(2);
192  jointDxlId.push_back(3);
193  jointDxlId.push_back(4);
194  jointDxlId.push_back(5);
195  jointDxlId.push_back(6);
196  addJointActuator(JOINT_DYNAMIXEL, actuator_, jointDxlId, p_dxl_comm_arg);
197 
198  // Set joint actuator control mode
199  STRING joint_dxl_mode_arg = "position_mode";
200  void *p_joint_dxl_mode_arg = &joint_dxl_mode_arg;
201  setJointActuatorMode(JOINT_DYNAMIXEL, jointDxlId, p_joint_dxl_mode_arg);
202 
203  /*****************************************************************************
204  ** Initialize Tool Actuator
205  *****************************************************************************/
206  if (with_gripper)
207  {
209 
210  uint8_t gripperDxlId = 7;
211  addToolActuator(TOOL_DYNAMIXEL, tool_, gripperDxlId, p_dxl_comm_arg);
212 
213  // Set gripper actuator control mode
214  STRING gripper_dxl_mode_arg = "current_based_position_mode";
215  void *p_gripper_dxl_mode_arg = &gripper_dxl_mode_arg;
216  setToolActuatorMode(TOOL_DYNAMIXEL, p_gripper_dxl_mode_arg);
217 
218  // Set gripper actuator parameter
219  // STRING gripper_dxl_opt_arg[2];
220  // void *p_gripper_dxl_opt_arg = &gripper_dxl_opt_arg;
221  // gripper_dxl_opt_arg[0] = "Profile_Acceleration";
222  // gripper_dxl_opt_arg[1] = "20";
223  // setToolActuatorMode(TOOL_DYNAMIXEL, p_gripper_dxl_opt_arg);
224 
225  // gripper_dxl_opt_arg[0] = "Profile_Velocity";
226  // gripper_dxl_opt_arg[1] = "20";
227  // setToolActuatorMode(TOOL_DYNAMIXEL, p_gripper_dxl_opt_arg);
228  }
229 
230  // Enable All Actuators
232 
233  // Receive current angles from all actuators
235 
236  if (with_gripper) receiveAllToolActuatorValue();
237  }
238 
239  /*****************************************************************************
240  ** Initialize Custom Trajectory
241  *****************************************************************************/
246 
251 }
252 
253 void OpenManipulator::processOpenManipulator(double present_time, bool using_actual_robot_state, bool with_gripper)
254 {
255  // Planning (ik)
256  JointWaypoint goal_joint_value = getJointGoalValueFromTrajectory(present_time);
257  JointWaypoint goal_tool_value;
258 
259  static double init_tool_value = getToolGoalValue().at(0).position;
260  static bool onoff = false;
261  if (with_gripper)
262  {
263  // switch to on if the value changed different from the init value
264  if (init_tool_value != getToolGoalValue().at(0).position)
265  onoff = true;
266 
267  if (!onoff)
268  goal_tool_value = getToolGoalValue();
269  else
270  goal_tool_value = distanceToAngle(getToolGoalValue());
271  }
272 
273  // Control (motor)
275  if (with_gripper)
276  {
277  std::vector<Name> tool_component_name;
278  tool_component_name = getManipulator()-> getAllToolComponentName();
279 
280  if (using_actual_robot_state)
281  {
282  getManipulator()->setJointValue(tool_component_name.at(0),
284  }
285  }
286 
287  if (goal_joint_value.size() != 0) sendAllJointActuatorValue(goal_joint_value);
288  if (with_gripper) {if (goal_tool_value.size() != 0) {sendAllToolActuatorValue(goal_tool_value);}}
289 
290  // Perception (fk)
292 }
293 
295 {
296  // distance (m) -> angle (rad)
297  double angle = distance.at(0).position;
298  // double angle = 1.135 - distance.at(0).position; // / 0.109 * 1.135;
299 
300  JointValue result;
301  result.position = angle;
302 
303  JointWaypoint result_vector;
304  result_vector.push_back(result);
305 
306  return result_vector;
307 }
308 
310 {
311  // angle (rad) -> distance (m)
312  double distance = angle.at(0).position;
313  // double distance = 1.135 - angle.at(0).position; /// 1.135 * 0.109;
314 
315  JointValue result;
316  result.position = distance;
317 
318  JointWaypoint result_vector;
319  result_vector.push_back(result);
320 
321  return result_vector;
322 }
#define CUSTOM_TRAJECTORY_HEART
void addTool(Name my_name, Name parent_name, Eigen::Vector3d relative_position, Eigen::Matrix3d relative_orientation, int8_t tool_id=-1, double max_position_limit=M_PI, double min_position_limit=-M_PI, double coefficient=1.0, double object_mass=0.0, Eigen::Matrix3d object_inertia_tensor=Eigen::Matrix3d::Identity(), Eigen::Vector3d object_center_of_mass=Eigen::Vector3d::Zero())
std::vector< JointValue > getToolGoalValue()
std::vector< JointValue > receiveAllToolActuatorValue()
std::vector< JointValue > getJointGoalValueFromTrajectory(double present_time, int option=DYNAMICS_ALL_SOVING)
#define Z_AXIS
#define Y_AXIS
#define JOINT_DYNAMIXEL
robotis_manipulator::ToolActuator * tool_
void addJoint(Name my_name, Name parent_name, Name child_name, Eigen::Vector3d relative_position, Eigen::Matrix3d relative_orientation, Eigen::Vector3d axis_of_rotation=Eigen::Vector3d::Zero(), int8_t joint_actuator_id=-1, double max_position_limit=M_PI, double min_position_limit=-M_PI, double coefficient=1.0, double mass=0.0, Eigen::Matrix3d inertia_tensor=Eigen::Matrix3d::Identity(), Eigen::Vector3d center_of_mass=Eigen::Vector3d::Zero(), double torque_coefficient=1.0)
void setKinematicsOption(const void *arg)
robotis_manipulator::JointActuator * actuator_
#define CUSTOM_TRAJECTORY_LINE
std::vector< JointValue > JointWaypoint
std::vector< JointValue > receiveAllJointActuatorValue()
void setToolActuatorMode(Name actuator_name, const void *arg)
#define CUSTOM_TRAJECTORY_SIZE
#define TOOL_DYNAMIXEL
void processOpenManipulator(double present_time, bool using_actual_robot_state, bool with_gripper=false)
#define CUSTOM_TRAJECTORY_CIRCLE
#define X_AXIS
robotis_manipulator::Kinematics * kinematics_
bool sendAllToolActuatorValue(std::vector< JointValue > value_vector)
bool sendAllJointActuatorValue(std::vector< JointValue > value_vector)
void setJointValue(Name name, JointValue joint_value)
void initOpenManipulator(bool using_actual_robot_state, STRING usb_port="/dev/ttyUSB0", STRING baud_rate="1000000", float control_loop_time=0.010, bool with_gripper=false)
void addCustomTrajectory(Name trajectory_name, CustomJointTrajectory *custom_trajectory)
#define CUSTOM_TRAJECTORY_RHOMBUS
void addKinematics(Kinematics *kinematics)
void addJointActuator(Name actuator_name, JointActuator *joint_actuator, std::vector< uint8_t > id_array, const void *arg)
void setJointActuatorMode(Name actuator_name, std::vector< uint8_t > id_array, const void *arg)
JointWaypoint distanceToAngle(JointWaypoint distance)
robotis_manipulator::CustomTaskTrajectory * custom_trajectory_[CUSTOM_TRAJECTORY_SIZE]
std::string STRING
JointWaypoint angleToDistance(JointWaypoint angle)
void addWorld(Name world_name, Name child_name, Eigen::Vector3d world_position=Eigen::Vector3d::Zero(), Eigen::Matrix3d world_orientation=Eigen::Matrix3d::Identity())
void addToolActuator(Name tool_name, ToolActuator *tool_actuator, uint8_t id, const void *arg)


open_manipulator_p_libs
Author(s): Ryan Shim , Yong-Ho Na , Hye-Jong KIM
autogenerated on Thu Oct 22 2020 03:16:37