base_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 /* Author: zerom */
18 
19 #include <stdio.h>
21 
22 namespace rh_p12_rn
23 {
24 
26  : control_cycle_msec_(0),
27  torque_enable_(0),
28  goal_position_(0),
29  goal_velocity_(0),
30  goal_current_(30),
31  goal_acceleration_(0),
32  is_moving_(false),
33  present_position_(0),
34  present_velocity_(0),
35  present_current_(0)
36 {
37  enable_ = true;
38  module_name_ = "rh_p12_rn_base_module";
40 
41  result_["gripper"] = new robotis_framework::DynamixelState();
42  result_["gripper"]->goal_position_ = 0;
43 }
44 
46 {
47  queue_thread_.join();
48 }
49 
50 void BaseModule::initialize(const int control_cycle_msec, robotis_framework::Robot *robot)
51 {
52  control_cycle_msec_ = control_cycle_msec;
53 
54  // setting of queue thread
55  queue_thread_ = boost::thread(boost::bind(&BaseModule::queueThread, this));
56 
57  robot_ = robot;
58  //robot->dxls_[0]->ctrl_module_name_ = "rh_p12_rn_base_module";
59 }
60 
62 {
63  ros::NodeHandle ros_node;
64  ros::CallbackQueue callback_queue;
65 
66  ros_node.setCallbackQueue(&callback_queue);
67 
68  /* subscriber */
69  ros::Subscriber sync_write_item_sub = ros_node.subscribe("/robotis/direct/sync_write_item", 1,
71  //ros::Subscriber set_head_joint_sub = ros_node.subscribe("/robotis/direct_control/set_joint_states", 1,
72  // &BaseModule::setJointCallback, this);
73 
74  /* publisher */
75  present_position_pub_ = ros_node.advertise<std_msgs::Int32>("/robotis/rh_p12_rn_base/present_position", 1, true);
76  present_current_pub_ = ros_node.advertise<std_msgs::Int32>("/robotis/rh_p12_rn_base/present_current", 1, true);
77 
78 
79  /* service callback */
80  ros::ServiceServer get_item_value_server = ros_node.advertiseService("/robotis/rh_p12_rn_base/get_item_value",
82 
83  ros::WallDuration duration(control_cycle_msec_ / 1000.0);
84  while(ros_node.ok())
85  callback_queue.callAvailable(duration);
86 }
87 
88 void BaseModule::setPosition(const robotis_controller_msgs::SyncWriteItem::ConstPtr &msg)
89 {
90  if(msg->joint_name[0] == "gripper" && msg->item_name == "goal_position")
91  {
92  result_["gripper"]->goal_position_ = robot_->dxls_["gripper"]->convertValue2Radian(msg->value[0]);
93  ROS_WARN("BASE_MODULE: goalposition : %d -> %f", msg->value[0], result_["gripper"]->goal_position_);
94  }
95 }
96 
97 bool BaseModule::getItemValueCallback(rh_p12_rn_base_module_msgs::GetItemValue::Request &req,
98  rh_p12_rn_base_module_msgs::GetItemValue::Response &res)
99 {
100  if(req.joint_name != "gripper")
101  return false;
102 
103  if(req.item_name == "torque_enable")
104  res.value = torque_enable_;
105  else if(req.item_name == "goal_position")
106  res.value = goal_position_;
107  else if(req.item_name == "goal_velocity")
108  res.value = goal_velocity_;
109  else if(req.item_name == "goal_current")
110  res.value = goal_current_;
111  else if(req.item_name == "goal_acceleration")
112  res.value = goal_acceleration_;
113  else if(req.item_name == "is_moving")
114  res.value = is_moving_;
115  else if(req.item_name == "present_position")
116  res.value = present_position_;
117  else if(req.item_name == "present_velocity")
118  res.value = present_velocity_;
119  else if(req.item_name == "present_current")
120  res.value = present_current_;
121  else
122  return false;
123 
124  //ROS_INFO("[getItemValue] %s : %d", req.item_name.c_str(), res.value);
125 
126  return true;
127 }
128 
129 void BaseModule::process(std::map<std::string, robotis_framework::Dynamixel *> dxls,
130  std::map<std::string, double> sensors)
131 {
132  std::map<std::string, uint32_t> _table = dxls["gripper"]->dxl_state_->bulk_read_table_;
133  torque_enable_ = _table["torque_enable"];
134  goal_position_ = _table["goal_position"];
135  goal_velocity_ = _table["goal_velocity"];
136  goal_current_ = _table["goal_current"];
137  goal_acceleration_ = _table["goal_acceleration"];
138  is_moving_ = _table["is_moving"];
139  present_position_ = _table["present_position"];
140  present_velocity_ = _table["present_velocity"];
141  present_current_ = _table["present_current"];
142 
143  std_msgs::Int32 _pos;
144  _pos.data = present_position_;
146 
147  std_msgs::Int32 _curr;
148  _curr.data = present_current_;
150 
151  if (enable_ == false)
152  return;
153 
154  // get joint data from robot
155  for (std::map<std::string, robotis_framework::DynamixelState *>::iterator state_it = result_.begin();
156  state_it != result_.end(); state_it++)
157  {
158  std::string joint_name = state_it->first;
159 
160  robotis_framework::Dynamixel *_dxl = NULL;
161  std::map<std::string, robotis_framework::Dynamixel*>::iterator dxl_it = dxls.find(joint_name);
162  if (dxl_it != dxls.end())
163  _dxl = dxl_it->second;
164  else
165  continue;
166 
167  //ROS_INFO("Present Position: %d", _dxl->dxl_state_->bulk_read_table_["present_position"]);
168  }
169 
170  // set joint data to robot
171  //result_["gripper"]->goal_position_ = 1;
172 }
173 
175 {
176  return;
177 }
178 
180 {
181  return false;
182 }
183 
184 } // namespace
ros::Publisher present_current_pub_
Definition: base_module.h:59
void setPosition(const robotis_controller_msgs::SyncWriteItem::ConstPtr &msg)
Definition: base_module.cpp:88
boost::thread queue_thread_
Definition: base_module.h:43
std::map< std::string, DynamixelState * > result_
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
#define ROS_WARN(...)
std::map< std::string, Dynamixel * > dxls_
void setCallbackQueue(CallbackQueueInterface *queue)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher present_position_pub_
Definition: base_module.h:58
void process(std::map< std::string, robotis_framework::Dynamixel * > dxls, std::map< std::string, double > sensors)
robotis_framework::Robot * robot_
Definition: base_module.h:45
bool ok() const
void initialize(const int control_cycle_msec, robotis_framework::Robot *robot)
Definition: base_module.cpp:50
bool getItemValueCallback(rh_p12_rn_base_module_msgs::GetItemValue::Request &req, rh_p12_rn_base_module_msgs::GetItemValue::Response &res)
Definition: base_module.cpp:97


rh_p12_rn_base_module
Author(s): Zerom
autogenerated on Mon Jun 10 2019 14:42:05