-
Notifications
You must be signed in to change notification settings - Fork 178
/
position_control.cpp
152 lines (123 loc) · 4.83 KB
/
position_control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
* Copyright 2016 ROBOTIS CO., LTD.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
/* Authors: Taehoon Lim (Darby) */
#include "dynamixel_workbench_controllers/position_control.h"
PositionControl::PositionControl()
:node_handle_("")
{
std::string device_name = node_handle_.param<std::string>("device_name", "/dev/ttyUSB0");
uint32_t dxl_baud_rate = node_handle_.param<int>("baud_rate", 57600);
uint8_t scan_range = node_handle_.param<int>("scan_range", 200);
uint32_t profile_velocity = node_handle_.param<int>("profile_velocity", 200);
uint32_t profile_acceleration = node_handle_.param<int>("profile_acceleration", 50);
dxl_wb_ = new DynamixelWorkbench;
dxl_wb_->begin(device_name.c_str(), dxl_baud_rate);
if (dxl_wb_->scan(dxl_id_, &dxl_cnt_, scan_range) != true)
{
ROS_ERROR("Not found Motors, Please check scan range and baud rate");
ros::shutdown();
return;
}
initMsg();
for (int index = 0; index < dxl_cnt_; index++)
dxl_wb_->jointMode(dxl_id_[index], profile_velocity, profile_acceleration);
initPublisher();
initServer();
}
PositionControl::~PositionControl()
{
for (int index = 0; index < dxl_cnt_; index++)
dxl_wb_->itemWrite(dxl_id_[index], "Torque_Enable", 0);
ros::shutdown();
}
void PositionControl::initMsg()
{
printf("-----------------------------------------------------------------------\n");
printf(" dynamixel_workbench controller; position control example \n");
printf("-----------------------------------------------------------------------\n");
printf("\n");
for (int index = 0; index < dxl_cnt_; index++)
{
printf("MODEL : %s\n", dxl_wb_->getModelName(dxl_id_[index]));
printf("ID : %d\n", dxl_id_[index]);
printf("\n");
}
printf("-----------------------------------------------------------------------\n");
}
void PositionControl::initPublisher()
{
dynamixel_state_list_pub_ = node_handle_.advertise<dynamixel_workbench_msgs::DynamixelStateList>("dynamixel_state", 10);
}
void PositionControl::initServer()
{
joint_command_server_ = node_handle_.advertiseService("joint_command", &PositionControl::jointCommandMsgCallback, this);
}
void PositionControl::dynamixelStatePublish()
{
dynamixel_workbench_msgs::DynamixelState dynamixel_state[dxl_cnt_];
dynamixel_workbench_msgs::DynamixelStateList dynamixel_state_list;
for (int index = 0; index < dxl_cnt_; index++)
{
dynamixel_state[index].model_name = std::string(dxl_wb_->getModelName(dxl_id_[index]));
dynamixel_state[index].id = dxl_id_[index];
dynamixel_state[index].torque_enable = dxl_wb_->itemRead(dxl_id_[index], "Torque_Enable");
dynamixel_state[index].present_position = dxl_wb_->itemRead(dxl_id_[index], "Present_Position");
dynamixel_state[index].present_velocity = dxl_wb_->itemRead(dxl_id_[index], "Present_Velocity");
dynamixel_state[index].goal_position = dxl_wb_->itemRead(dxl_id_[index], "Goal_Position");
dynamixel_state[index].goal_velocity = dxl_wb_->itemRead(dxl_id_[index], "Goal_Velocity");
dynamixel_state[index].moving = dxl_wb_->itemRead(dxl_id_[index], "Moving");
dynamixel_state_list.dynamixel_state.push_back(dynamixel_state[index]);
}
dynamixel_state_list_pub_.publish(dynamixel_state_list);
}
void PositionControl::controlLoop()
{
dynamixelStatePublish();
}
bool PositionControl::jointCommandMsgCallback(dynamixel_workbench_msgs::JointCommand::Request &req,
dynamixel_workbench_msgs::JointCommand::Response &res)
{
int32_t goal_position = 0;
int32_t present_position = 0;
if (req.unit == "rad")
{
goal_position = dxl_wb_->convertRadian2Value(req.id, req.goal_position);
}
else if (req.unit == "raw")
{
goal_position = req.goal_position;
}
else
{
goal_position = req.goal_position;
}
bool ret = dxl_wb_->goalPosition(req.id, goal_position);
res.result = ret;
}
int main(int argc, char **argv)
{
// Init ROS node
ros::init(argc, argv, "position_control");
PositionControl pos_ctrl;
ros::Rate loop_rate(250);
while (ros::ok())
{
pos_ctrl.controlLoop();
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}