op3_manager.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2017 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: Kayman */
18 
19 /* ROS API Header */
20 #include <ros/ros.h>
21 #include <std_msgs/String.h>
22 
23 /* ROBOTIS Controller Header */
25 
26 /* Sensor Module Header */
28 
29 /* Motion Module Header */
36 
37 using namespace robotis_framework;
38 using namespace dynamixel;
39 using namespace robotis_op;
40 
41 const int BAUD_RATE = 2000000;
42 const double PROTOCOL_VERSION = 2.0;
43 const int SUB_CONTROLLER_ID = 200;
44 const int DXL_BROADCAST_ID = 254;
45 const int DEFAULT_DXL_ID = 1;
46 const std::string SUB_CONTROLLER_DEVICE = "/dev/ttyUSB0";
47 const int POWER_CTRL_TABLE = 24;
48 const int RGB_LED_CTRL_TABLE = 26;
49 const int TORQUE_ON_CTRL_TABLE = 64;
50 
51 bool g_is_simulation = false;
53 std::string g_offset_file;
54 std::string g_robot_file;
55 std::string g_init_file;
56 std::string g_device_name;
57 
60 
61 void buttonHandlerCallback(const std_msgs::String::ConstPtr& msg)
62 {
63  if (msg->data == "user_long")
64  {
66 
67  controller->setCtrlModule("none");
68 
69  controller->stopTimer();
70 
71  if (g_is_simulation == false)
72  {
73  // power and torque on
74  PortHandler *port_handler = (PortHandler *) PortHandler::getPortHandler(g_device_name.c_str());
75  bool set_port_result = port_handler->setBaudRate(g_baudrate);
76  if (set_port_result == false)
77  {
78  ROS_ERROR("Error Set port");
79  return;
80  }
81  PacketHandler *packet_handler = PacketHandler::getPacketHandler(PROTOCOL_VERSION);
82 
83  // check dxls torque.
84  uint8_t torque = 0;
85  packet_handler->read1ByteTxRx(port_handler, DEFAULT_DXL_ID, TORQUE_ON_CTRL_TABLE, &torque);
86 
87  if (torque != 1)
88  {
89  controller->initializeDevice(g_init_file);
90  }
91  else
92  {
93  ROS_INFO("Torque is already on!!");
94  }
95  }
96 
97  controller->startTimer();
98 
99  usleep(200 * 1000);
100 
101  // go to init pose
102  std_msgs::String init_msg;
103  init_msg.data = "ini_pose";
104 
105  g_init_pose_pub.publish(init_msg);
106  ROS_INFO("Go to init pose");
107  }
108 }
109 
110 void dxlTorqueCheckCallback(const std_msgs::String::ConstPtr& msg)
111 {
112  if (g_is_simulation == true)
113  return;
114 
115  // check dxl torque
116  uint8_t torque_result = 0;
117  bool torque_on = true;
119  //controller->robot_->port_default_device_
120 
121  for (std::map<std::string, std::string>::iterator map_it = controller->robot_->port_default_device_.begin();
122  map_it != controller->robot_->port_default_device_.end(); map_it++)
123  {
124  std::string default_device_name = map_it->second;
125  controller->read1Byte(default_device_name, TORQUE_ON_CTRL_TABLE, &torque_result);
126 
127  // if not, torque on
128  if (torque_result != 1)
129  torque_on = false;
130  }
131 
132  if(torque_on == false)
133  {
134  controller->stopTimer();
135 
136  controller->initializeDevice(g_init_file);
137 
138  controller->startTimer();
139  }
140 }
141 
142 int main(int argc, char **argv)
143 {
144  ros::init(argc, argv, "op3_manager");
145  ros::NodeHandle nh;
146 
147  ROS_INFO("manager->init");
149 
150  /* Load ROS Parameter */
151 
152  nh.param<std::string>("offset_file_path", g_offset_file, "");
153  nh.param<std::string>("robot_file_path", g_robot_file, "");
154  nh.param<std::string>("init_file_path", g_init_file, "");
155  nh.param<std::string>("device_name", g_device_name, SUB_CONTROLLER_DEVICE);
156  nh.param<int>("baud_rate", g_baudrate, BAUD_RATE);
157 
158  ros::Subscriber button_sub = nh.subscribe("/robotis/open_cr/button", 1, buttonHandlerCallback);
159  ros::Subscriber dxl_torque_sub = nh.subscribe("/robotis/dxl_torque", 1, dxlTorqueCheckCallback);
160  g_init_pose_pub = nh.advertise<std_msgs::String>("/robotis/base/ini_pose", 0);
161  g_demo_command_pub = nh.advertise<std_msgs::String>("/ball_tracker/command", 0);
162 
163  nh.param<bool>("gazebo", controller->gazebo_mode_, false);
164  g_is_simulation = controller->gazebo_mode_;
165 
166  /* real robot */
167  if (g_is_simulation == false)
168  {
169  // open port
170  PortHandler *port_handler = (PortHandler *) PortHandler::getPortHandler(g_device_name.c_str());
171  bool set_port_result = port_handler->setBaudRate(BAUD_RATE);
172  if (set_port_result == false)
173  ROS_ERROR("Error Set port");
174 
175  PacketHandler *packet_handler = PacketHandler::getPacketHandler(PROTOCOL_VERSION);
176 
177  // power on dxls
178  int torque_on_count = 0;
179 
180  while (torque_on_count < 5)
181  {
182  int _return = packet_handler->write1ByteTxRx(port_handler, SUB_CONTROLLER_ID, POWER_CTRL_TABLE, 1);
183 
184  if(_return != 0)
185  ROS_ERROR("Torque on DXLs! [%s]", packet_handler->getRxPacketError(_return));
186  else
187  ROS_INFO("Torque on DXLs!");
188 
189  if (_return == 0)
190  break;
191  else
192  torque_on_count++;
193  }
194 
195  usleep(100 * 1000);
196 
197  // set RGB-LED to GREEN
198  int led_full_unit = 0x1F;
199  int led_range = 5;
200  int led_value = led_full_unit << led_range;
201  int _return = packet_handler->write2ByteTxRx(port_handler, SUB_CONTROLLER_ID, RGB_LED_CTRL_TABLE, led_value);
202 
203  if(_return != 0)
204  ROS_ERROR("Fail to control LED [%s]", packet_handler->getRxPacketError(_return));
205 
206  port_handler->closePort();
207  }
208  /* gazebo simulation */
209  else
210  {
211  ROS_WARN("SET TO GAZEBO MODE!");
212  std::string robot_name;
213  nh.param<std::string>("gazebo_robot_name", robot_name, "");
214  if (robot_name != "")
215  controller->gazebo_robot_name_ = robot_name;
216  }
217 
218  if (g_robot_file == "")
219  {
220  ROS_ERROR("NO robot file path in the ROS parameters.");
221  return -1;
222  }
223 
224  // initialize robot
225  if (controller->initialize(g_robot_file, g_init_file) == false)
226  {
227  ROS_ERROR("ROBOTIS Controller Initialize Fail!");
228  return -1;
229  }
230 
231  // load offset
232  if (g_offset_file != "")
233  controller->loadOffset(g_offset_file);
234 
235  usleep(300 * 1000);
236 
237  /* Add Sensor Module */
238  controller->addSensorModule((SensorModule*) OpenCRModule::getInstance());
239 
240  /* Add Motion Module */
241  controller->addMotionModule((MotionModule*) ActionModule::getInstance());
242  controller->addMotionModule((MotionModule*) BaseModule::getInstance());
243  controller->addMotionModule((MotionModule*) HeadControlModule::getInstance());
244  controller->addMotionModule((MotionModule*) WalkingModule::getInstance());
245  controller->addMotionModule((MotionModule*) DirectControlModule::getInstance());
246  controller->addMotionModule((MotionModule*) OnlineWalkingModule::getInstance());
247 
248  // start timer
249  controller->startTimer();
250 
251  usleep(100 * 1000);
252 
253  // go to init pose
254  std_msgs::String init_msg;
255  init_msg.data = "ini_pose";
256 
257  g_init_pose_pub.publish(init_msg);
258  ROS_INFO("Go to init pose");
259 
260  while (ros::ok())
261  {
262  usleep(1 * 1000);
263 
264  ros::spin();
265  }
266 
267  return 0;
268 }
ros::Publisher g_init_pose_pub
Definition: op3_manager.cpp:58
virtual int read1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t *data, uint8_t *error=0)=0
const int BAUD_RATE
Definition: op3_manager.cpp:41
void initializeDevice(const std::string init_file_path)
std::string g_init_file
Definition: op3_manager.cpp:55
void publish(const boost::shared_ptr< M > &message) const
void dxlTorqueCheckCallback(const std_msgs::String::ConstPtr &msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
const double PROTOCOL_VERSION
Definition: op3_manager.cpp:42
std::string g_robot_file
Definition: op3_manager.cpp:54
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
std::string g_device_name
Definition: op3_manager.cpp:56
void buttonHandlerCallback(const std_msgs::String::ConstPtr &msg)
Definition: op3_manager.cpp:61
void addMotionModule(MotionModule *module)
const int DXL_BROADCAST_ID
Definition: op3_manager.cpp:44
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
int g_baudrate
Definition: op3_manager.cpp:52
std::map< std::string, std::string > port_default_device_
virtual void closePort()=0
virtual const char * getRxPacketError(uint8_t error)=0
bool initialize(const std::string robot_file_path, const std::string init_file_path)
#define ROS_INFO(...)
const std::string SUB_CONTROLLER_DEVICE
Definition: op3_manager.cpp:46
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void loadOffset(const std::string path)
ROSCPP_DECL bool ok()
virtual int write1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t data, uint8_t *error=0)=0
const int RGB_LED_CTRL_TABLE
Definition: op3_manager.cpp:48
const int DEFAULT_DXL_ID
Definition: op3_manager.cpp:45
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher g_demo_command_pub
Definition: op3_manager.cpp:59
const int TORQUE_ON_CTRL_TABLE
Definition: op3_manager.cpp:49
void addSensorModule(SensorModule *module)
const int SUB_CONTROLLER_ID
Definition: op3_manager.cpp:43
virtual int write2ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint16_t data, uint8_t *error=0)=0
bool g_is_simulation
Definition: op3_manager.cpp:51
const int POWER_CTRL_TABLE
Definition: op3_manager.cpp:47
int read1Byte(const std::string joint_name, uint16_t address, uint8_t *data, uint8_t *error=0)
virtual bool setBaudRate(const int baudrate)=0
#define ROS_ERROR(...)
std::string g_offset_file
Definition: op3_manager.cpp:53
void setCtrlModule(std::string module_name)


op3_manager
Author(s): Kayman
autogenerated on Mon Jun 10 2019 14:41:30