-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathjoint_group_position_controller.cpp
193 lines (169 loc) · 6.77 KB
/
joint_group_position_controller.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* Copyright (c) 2012, hiDOF, Inc.
* Copyright (c) 2013, PAL Robotics, S.L.
* Copyright (c) 2014, Fraunhofer IPA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#include <effort_controllers/joint_group_position_controller.h>
#include <pluginlib/class_list_macros.hpp>
#include <angles/angles.h>
namespace effort_controllers
{
/**
* \brief Forward command controller for a set of effort controlled joints (torque or force).
*
* This class forwards the commanded efforts down to a set of joints.
*
* \section ROS interface
*
* \param type Must be "JointGroupEffortController".
* \param joints List of names of the joints to control.
*
* Subscribes to:
* - \b command (std_msgs::Float64MultiArray) : The joint efforts to apply
*/
JointGroupPositionController::JointGroupPositionController() {}
JointGroupPositionController::~JointGroupPositionController() {sub_command_.shutdown();}
bool JointGroupPositionController::init(hardware_interface::EffortJointInterface* hw, ros::NodeHandle &n)
{
// List of controlled joints
std::string param_name = "joints";
if(!n.getParam(param_name, joint_names_))
{
ROS_ERROR_STREAM("Failed to getParam '" << param_name << "' (namespace: " << n.getNamespace() << ").");
return false;
}
n_joints_ = joint_names_.size();
if(n_joints_ == 0){
ROS_ERROR_STREAM("List of joint names is empty.");
return false;
}
// Get URDF
urdf::Model urdf;
if (!urdf.initParamWithNodeHandle("robot_description", n))
{
ROS_ERROR("Failed to parse urdf file");
return false;
}
pid_controllers_.resize(n_joints_);
for(unsigned int i=0; i<n_joints_; i++)
{
const auto& joint_name = joint_names_[i];
try
{
joints_.push_back(hw->getHandle(joint_name));
}
catch (const hardware_interface::HardwareInterfaceException& e)
{
ROS_ERROR_STREAM("Exception thrown: " << e.what());
return false;
}
urdf::JointConstSharedPtr joint_urdf = urdf.getJoint(joint_name);
if (!joint_urdf)
{
ROS_ERROR("Could not find joint '%s' in urdf", joint_name.c_str());
return false;
}
joint_urdfs_.push_back(joint_urdf);
// Load PID Controller using gains set on parameter server
if (!pid_controllers_[i].init(ros::NodeHandle(n, joint_name + "/pid")))
{
ROS_ERROR_STREAM("Failed to load PID parameters from " << joint_name + "/pid");
return false;
}
}
commands_buffer_.writeFromNonRT(std::vector<double>(n_joints_, 0.0));
sub_command_ = n.subscribe<std_msgs::Float64MultiArray>("command", 1, &JointGroupPositionController::commandCB, this);
return true;
}
void JointGroupPositionController::update(const ros::Time& time, const ros::Duration& period)
{
std::vector<double> & commands = *commands_buffer_.readFromRT();
for(unsigned int i=0; i<n_joints_; i++)
{
double command_position = commands[i];
double error; //, vel_error;
double commanded_effort;
double current_position = joints_[i].getPosition();
// Make sure joint is within limits if applicable
enforceJointLimits(command_position, i);
// Compute position error
if (joint_urdfs_[i]->type == urdf::Joint::REVOLUTE)
{
angles::shortest_angular_distance_with_large_limits(
current_position,
command_position,
joint_urdfs_[i]->limits->lower,
joint_urdfs_[i]->limits->upper,
error);
}
else if (joint_urdfs_[i]->type == urdf::Joint::CONTINUOUS)
{
error = angles::shortest_angular_distance(current_position, command_position);
}
else //prismatic
{
error = command_position - current_position;
}
// Set the PID error and compute the PID command with nonuniform
// time step size.
commanded_effort = pid_controllers_[i].computeCommand(error, period);
joints_[i].setCommand(commanded_effort);
}
}
void JointGroupPositionController::commandCB(const std_msgs::Float64MultiArrayConstPtr& msg)
{
if(msg->data.size()!=n_joints_)
{
ROS_ERROR_STREAM("Dimension of command (" << msg->data.size() << ") does not match number of joints (" << n_joints_ << ")! Not executing!");
return;
}
commands_buffer_.writeFromNonRT(msg->data);
}
void JointGroupPositionController::enforceJointLimits(double &command, unsigned int index)
{
// Check that this joint has applicable limits
if (joint_urdfs_[index]->type == urdf::Joint::REVOLUTE || joint_urdfs_[index]->type == urdf::Joint::PRISMATIC)
{
if( command > joint_urdfs_[index]->limits->upper ) // above upper limnit
{
command = joint_urdfs_[index]->limits->upper;
}
else if( command < joint_urdfs_[index]->limits->lower ) // below lower limit
{
command = joint_urdfs_[index]->limits->lower;
}
}
}
} // namespace
PLUGINLIB_EXPORT_CLASS( effort_controllers::JointGroupPositionController, controller_interface::ControllerBase)