gripper_module.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2018 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 #include <stdio.h>
19 
20 using namespace thormang3;
21 
23  : control_cycle_sec_(0.008),
24  is_moving_(false)
25 {
26  enable_ = false;
27  module_name_ = "gripper_module";
29 
30  /* gripper */
31  result_["r_arm_grip"] = new robotis_framework::DynamixelState();
32  result_["l_arm_grip"] = new robotis_framework::DynamixelState();
33 
34  /* gripper */
35  joint_name_to_id_["r_arm_grip"] = 0;
36  joint_name_to_id_["l_arm_grip"] = 1;
37 
38  /* ----- parameter initialization ----- */
39  present_joint_position_ = Eigen::VectorXd::Zero(NUM_GRIPPER_JOINTS);
40  present_joint_velocity_ = Eigen::VectorXd::Zero(NUM_GRIPPER_JOINTS);
41  goal_joint_position_ = Eigen::VectorXd::Zero(NUM_GRIPPER_JOINTS);
42 }
43 
45 {
46  queue_thread_.join();
47 }
48 
49 void GripperModule::initialize(const int control_cycle_msec, robotis_framework::Robot *robot)
50 {
51  control_cycle_sec_ = control_cycle_msec * 0.001;
52  queue_thread_ = boost::thread(boost::bind(&GripperModule::queueThread, this));
53 }
54 
56 {
57  ros::NodeHandle ros_node;
58  ros::CallbackQueue callback_queue;
59 
60  ros_node.setCallbackQueue(&callback_queue);
61 
62  /* publish topics */
63  status_msg_pub_ = ros_node.advertise<robotis_controller_msgs::StatusMsg>("/robotis/status", 1);
64  set_ctrl_module_pub_ = ros_node.advertise<std_msgs::String>("/robotis/enable_ctrl_module", 1);
65  goal_torque_limit_pub_ = ros_node.advertise<robotis_controller_msgs::SyncWriteItem>("/robotis/sync_write_item", 1);
66  movement_done_pub_ = ros_node.advertise<std_msgs::String>("/robotis/movement_done", 1);
67 
68  /* subscribe topics */
69  ros::Subscriber set_mode_msg_sub = ros_node.subscribe("/robotis/gripper/set_mode_msg", 5,
71  ros::Subscriber joint_pose_msg_sub = ros_node.subscribe("/robotis/gripper/joint_pose_msg", 5,
73 
74  /* service */
75 
77  while(ros_node.ok())
78  callback_queue.callAvailable(duration);
79 }
80 
81 void GripperModule::setModeMsgCallback(const std_msgs::String::ConstPtr& msg)
82 {
83  // ROS_INFO("--- Set Torque Control Mode ---");
84  std_msgs::String str_msg;
85  str_msg.data = "gripper_module";
87  return;
88 }
89 
90 void GripperModule::setJointPoseMsgCallback(const sensor_msgs::JointState::ConstPtr& msg)
91 {
92  if(enable_ == false)
93  return;
94 
95  goal_joint_pose_msg_ = *msg;
96 
97  if (is_moving_ == false)
98  {
100 
101  movement_done_msg_.data = "gripper";
102 
103  tra_gene_tread_ = new boost::thread(boost::bind(&GripperModule::traGeneProcJointSpace, this));
104  delete tra_gene_tread_;
105  }
106  else
107  ROS_INFO("previous task is alive");
108 
109  return;
110 }
111 
113 {
114  mov_time_ = 1.5;
115  int all_time_steps = int(floor((mov_time_/control_cycle_sec_) + 1 ));
116  mov_time_ = double (all_time_steps - 1) * control_cycle_sec_;
117 
120 
121  /* calculate joint trajectory */
122  for (int dim=0; dim<NUM_GRIPPER_JOINTS; dim++)
123  {
124  double ini_value = goal_joint_position_(dim);
125  double tar_value = goal_joint_position_(dim);
126 
127  Eigen::MatrixXd tra =
128  robotis_framework::calcMinimumJerkTra(ini_value, 0.0, 0.0,
129  tar_value , 0.0 , 0.0 ,
131 
132  goal_joint_tra_.block(0, dim, all_time_steps_, 1) = tra;
133  }
134 
135  for (int dim=0; dim<goal_joint_pose_msg_.name.size(); dim++)
136  {
137  std::string joint_name = goal_joint_pose_msg_.name[dim];
138  int id = joint_name_to_id_[joint_name];
139 
140  double ini_value = goal_joint_position_(id);
141  double tar_value = goal_joint_pose_msg_.position[dim];
142 
143  Eigen::MatrixXd tra =
144  robotis_framework::calcMinimumJerkTra(ini_value, 0.0, 0.0,
145  tar_value , 0.0 , 0.0 ,
147 
148  goal_joint_tra_.block(0, id, all_time_steps_, 1) = tra;
149  }
150 
151  cnt_ = 0;
152  is_moving_ = true;
153 
154  ROS_INFO("[start] send trajectory");
155 }
156 
158 {
159  robotis_controller_msgs::SyncWriteItem sync_write_msg;
160  sync_write_msg.item_name = "goal_torque";
161 
162  for (int dim=0; dim<goal_joint_pose_msg_.name.size(); dim++)
163  {
164  std::string joint_name = goal_joint_pose_msg_.name[dim];
165  int torque_limit = (int) goal_joint_pose_msg_.effort[dim];
166 
167  sync_write_msg.joint_name.push_back(joint_name);
168  sync_write_msg.value.push_back(torque_limit);
169  }
170 
171  goal_torque_limit_pub_.publish(sync_write_msg);
172 }
173 
175 {
176  if (is_moving_ == true)
177  {
178  if (cnt_ >= all_time_steps_)
179  {
180  ROS_INFO("[end] send trajectory");
181  publishStatusMsg(robotis_controller_msgs::StatusMsg::STATUS_INFO, "End Trajectory");
182 
184  movement_done_msg_.data = "";
185 
186  is_moving_ = false;
187  cnt_ = 0;
188  }
189  }
190 }
191 
192 void GripperModule::process(std::map<std::string, robotis_framework::Dynamixel *> dxls,
193  std::map<std::string, double> sensors)
194 {
195  if (enable_ == false)
196  return;
197 
198  /*----- Get Joint Data & Sensor Data-----*/
199  for (std::map<std::string, robotis_framework::DynamixelState *>::iterator state_iter = result_.begin();
200  state_iter != result_.end(); state_iter++)
201  {
202  std::string joint_name = state_iter->first;
203 
204  robotis_framework::Dynamixel *dxl = NULL;
205  std::map<std::string, robotis_framework::Dynamixel*>::iterator dxl_it = dxls.find(joint_name);
206  if (dxl_it != dxls.end())
207  dxl = dxl_it->second;
208  else
209  continue;
210 
211  // Get Joint Data
214 
216  }
217 
218  /* ----- Movement Event -----*/
219  if (is_moving_ == true)
220  {
221  if (cnt_ == 0)
222  publishStatusMsg(robotis_controller_msgs::StatusMsg::STATUS_INFO, "Start Trajectory");
223 
224  // joint space control
225  for (int dim=0; dim<NUM_GRIPPER_JOINTS; dim++)
227 
228  cnt_++;
229  }
230 
231  /* ---- Send Goal Joint Data -----*/
232  for (std::map<std::string, robotis_framework::DynamixelState *>::iterator state_iter = result_.begin();
233  state_iter != result_.end(); state_iter++)
234  {
235  std::string joint_name = state_iter->first;
236  result_[joint_name]->goal_position_ = goal_joint_position_(joint_name_to_id_[joint_name]);
237  }
238 
239  /*---------- Movement End Event ----------*/
241 }
242 
244 {
245  is_moving_ = false;
246 
247  return;
248 }
249 
251 {
252  return is_moving_;
253 }
254 
255 void GripperModule::publishStatusMsg(unsigned int type, std::string msg)
256 {
257  robotis_controller_msgs::StatusMsg status_msg;
258  status_msg.header.stamp = ros::Time::now();
259  status_msg.type = type;
260  status_msg.module_name = "Gripper";
261  status_msg.status_msg = msg;
262 
263  status_msg_pub_.publish(status_msg);
264 }
Eigen::MatrixXd goal_joint_tra_
std::map< std::string, DynamixelState * > result_
Eigen::MatrixXd calcMinimumJerkTra(double pos_start, double vel_start, double accel_start, double pos_end, double vel_end, double accel_end, double smp_time, double mov_time)
void publish(const boost::shared_ptr< M > &message) const
ros::Publisher movement_done_pub_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std_msgs::String movement_done_msg_
boost::thread * tra_gene_tread_
Eigen::VectorXd present_joint_position_
sensor_msgs::JointState goal_joint_pose_msg_
Eigen::VectorXd present_joint_velocity_
#define ROS_INFO(...)
void setCallbackQueue(CallbackQueueInterface *queue)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher status_msg_pub_
void setModeMsgCallback(const std_msgs::String::ConstPtr &msg)
#define NUM_GRIPPER_JOINTS
Eigen::VectorXd goal_joint_position_
DynamixelState * dxl_state_
ros::Publisher goal_torque_limit_pub_
static Time now()
ros::Publisher set_ctrl_module_pub_
bool ok() const
void setJointPoseMsgCallback(const sensor_msgs::JointState::ConstPtr &msg)
void initialize(const int control_cycle_msec, robotis_framework::Robot *robot)
void publishStatusMsg(unsigned int type, std::string msg)
void process(std::map< std::string, robotis_framework::Dynamixel * > dxls, std::map< std::string, double > sensors)
std::map< std::string, int > joint_name_to_id_
boost::thread queue_thread_


thormang3_gripper_module
Author(s): SCH
autogenerated on Mon Jun 10 2019 15:37:46