28
28
29
29
namespace scaled_controllers
30
30
{
31
- controller_interface::InterfaceConfiguration ScaledJointTrajectoryController::state_interface_configuration () const
31
+ controller_interface::InterfaceConfiguration
32
+ ScaledJointTrajectoryController::state_interface_configuration () const
32
33
{
33
34
controller_interface::InterfaceConfiguration conf;
34
35
conf = JointTrajectoryController::state_interface_configuration ();
35
36
conf.names .push_back (" speed_scaling/speed_scaling_factor" );
36
37
return conf;
37
38
}
38
39
39
- CallbackReturn ScaledJointTrajectoryController::on_activate (const rclcpp_lifecycle::State& state)
40
+ CallbackReturn ScaledJointTrajectoryController::on_activate (const rclcpp_lifecycle::State & state)
40
41
{
41
42
TimeData time_data;
42
43
time_data.time = node_->now ();
@@ -46,44 +47,58 @@ CallbackReturn ScaledJointTrajectoryController::on_activate(const rclcpp_lifecyc
46
47
return JointTrajectoryController::on_activate (state);
47
48
}
48
49
49
- controller_interface::return_type ScaledJointTrajectoryController::update (const rclcpp::Time& time,
50
- const rclcpp::Duration& /* period*/ )
50
+ controller_interface::return_type ScaledJointTrajectoryController::update (
51
+ const rclcpp::Time & time, const rclcpp::Duration & /* period*/ )
51
52
{
52
- if (state_interfaces_.back ().get_name () == " speed_scaling" ) {
53
+ if (state_interfaces_.back ().get_name () == " speed_scaling" )
54
+ {
53
55
scaling_factor_ = state_interfaces_.back ().get_value ();
54
- } else {
55
- RCLCPP_ERROR (get_node ()->get_logger (), " Speed scaling interface not found in hardware interface." );
56
+ }
57
+ else
58
+ {
59
+ RCLCPP_ERROR (
60
+ get_node ()->get_logger (), " Speed scaling interface not found in hardware interface." );
56
61
}
57
62
58
- if (get_state ().id () == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE) {
63
+ if (get_state ().id () == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
64
+ {
59
65
return controller_interface::return_type::OK;
60
66
}
61
67
62
- auto resize_joint_trajectory_point = [&](trajectory_msgs::msg::JointTrajectoryPoint& point, size_t size) {
63
- point.positions .resize (size);
64
- if (has_velocity_state_interface_) {
65
- point.velocities .resize (size);
66
- }
67
- if (has_acceleration_state_interface_) {
68
- point.accelerations .resize (size);
69
- }
70
- };
71
- auto compute_error_for_joint = [&](JointTrajectoryPoint& error, int index, const JointTrajectoryPoint& current,
72
- const JointTrajectoryPoint& desired) {
68
+ auto resize_joint_trajectory_point =
69
+ [&](trajectory_msgs::msg::JointTrajectoryPoint & point, size_t size) {
70
+ point.positions .resize (size);
71
+ if (has_velocity_state_interface_)
72
+ {
73
+ point.velocities .resize (size);
74
+ }
75
+ if (has_acceleration_state_interface_)
76
+ {
77
+ point.accelerations .resize (size);
78
+ }
79
+ };
80
+ auto compute_error_for_joint = [&](
81
+ JointTrajectoryPoint & error, int index,
82
+ const JointTrajectoryPoint & current,
83
+ const JointTrajectoryPoint & desired) {
73
84
// error defined as the difference between current and desired
74
- error.positions [index] = angles::shortest_angular_distance (current.positions [index], desired.positions [index]);
75
- if (has_velocity_state_interface_ && has_velocity_command_interface_) {
85
+ error.positions [index] =
86
+ angles::shortest_angular_distance (current.positions [index], desired.positions [index]);
87
+ if (has_velocity_state_interface_ && has_velocity_command_interface_)
88
+ {
76
89
error.velocities [index] = desired.velocities [index] - current.velocities [index];
77
90
}
78
- if (has_acceleration_state_interface_ && has_acceleration_command_interface_) {
91
+ if (has_acceleration_state_interface_ && has_acceleration_command_interface_)
92
+ {
79
93
error.accelerations [index] = desired.accelerations [index] - current.accelerations [index];
80
94
}
81
95
};
82
96
83
97
// Check if a new external message has been received from nonRT threads
84
98
auto current_external_msg = traj_external_point_ptr_->get_trajectory_msg ();
85
99
auto new_external_msg = traj_msg_external_point_ptr_.readFromRT ();
86
- if (current_external_msg != *new_external_msg) {
100
+ if (current_external_msg != *new_external_msg)
101
+ {
87
102
fill_partial_goal (*new_external_msg);
88
103
sort_to_local_joint_order (*new_external_msg);
89
104
traj_external_point_ptr_->update (*new_external_msg);
@@ -94,96 +109,120 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
94
109
resize_joint_trajectory_point (state_current, joint_num);
95
110
96
111
// current state update
97
- auto assign_point_from_interface = [&, joint_num](std::vector<double >& trajectory_point_interface,
98
- const auto & joint_inteface) {
99
- for (auto index = 0ul ; index < joint_num; ++index) {
100
- trajectory_point_interface[index] = joint_inteface[index].get ().get_value ();
101
- }
102
- };
103
- auto assign_interface_from_point = [&, joint_num](auto & joint_inteface,
104
- const std::vector<double >& trajectory_point_interface) {
105
- for (auto index = 0ul ; index < joint_num; ++index) {
106
- joint_inteface[index].get ().set_value (trajectory_point_interface[index]);
107
- }
108
- };
112
+ auto assign_point_from_interface =
113
+ [&, joint_num](std::vector<double > & trajectory_point_interface, const auto & joint_inteface) {
114
+ for (auto index = 0ul ; index < joint_num; ++index)
115
+ {
116
+ trajectory_point_interface[index] = joint_inteface[index].get ().get_value ();
117
+ }
118
+ };
119
+ auto assign_interface_from_point =
120
+ [&, joint_num](auto & joint_inteface, const std::vector<double > & trajectory_point_interface) {
121
+ for (auto index = 0ul ; index < joint_num; ++index)
122
+ {
123
+ joint_inteface[index].get ().set_value (trajectory_point_interface[index]);
124
+ }
125
+ };
109
126
110
127
state_current.time_from_start .set__sec (0 );
111
128
112
129
// Assign values from the hardware
113
130
// Position states always exist
114
131
assign_point_from_interface (state_current.positions , joint_state_interface_[0 ]);
115
132
// velocity and acceleration states are optional
116
- if (has_velocity_state_interface_) {
133
+ if (has_velocity_state_interface_)
134
+ {
117
135
assign_point_from_interface (state_current.velocities , joint_state_interface_[1 ]);
118
136
// Acceleration is used only in combination with velocity
119
- if (has_acceleration_state_interface_) {
137
+ if (has_acceleration_state_interface_)
138
+ {
120
139
assign_point_from_interface (state_current.accelerations , joint_state_interface_[2 ]);
121
- } else {
140
+ }
141
+ else
142
+ {
122
143
// Make empty so the property is ignored during interpolation
123
144
state_current.accelerations .clear ();
124
145
}
125
- } else {
146
+ }
147
+ else
148
+ {
126
149
// Make empty so the property is ignored during interpolation
127
150
state_current.velocities .clear ();
128
151
state_current.accelerations .clear ();
129
152
}
130
153
131
154
// currently carrying out a trajectory
132
- if (traj_point_active_ptr_ && (*traj_point_active_ptr_)->has_trajectory_msg ()) {
155
+ if (traj_point_active_ptr_ && (*traj_point_active_ptr_)->has_trajectory_msg ())
156
+ {
133
157
// Main Speed scaling difference...
134
158
// Adjust time with scaling factor
135
159
TimeData time_data;
136
160
time_data.time = time;
137
161
rcl_duration_value_t period = (time_data.time - time_data_.readFromRT ()->time ).nanoseconds ();
138
162
time_data.period = rclcpp::Duration::from_nanoseconds (scaling_factor_ * period);
139
163
time_data.uptime = time_data_.readFromRT ()->uptime + time_data.period ;
140
- rclcpp::Time traj_time = time_data_.readFromRT ()->uptime + rclcpp::Duration::from_nanoseconds (period);
164
+ rclcpp::Time traj_time =
165
+ time_data_.readFromRT ()->uptime + rclcpp::Duration::from_nanoseconds (period);
141
166
time_data_.writeFromNonRT (time_data);
142
167
143
168
// if sampling the first time, set the point before you sample
144
- if (!(*traj_point_active_ptr_)->is_sampled_already ()) {
169
+ if (!(*traj_point_active_ptr_)->is_sampled_already ())
170
+ {
145
171
(*traj_point_active_ptr_)->set_point_before_trajectory_msg (traj_time, state_current);
146
172
}
147
173
resize_joint_trajectory_point (state_error, joint_num);
148
174
149
175
// find segment for current timestamp
150
176
joint_trajectory_controller::TrajectoryPointConstIter start_segment_itr, end_segment_itr;
151
177
const bool valid_point =
152
- (*traj_point_active_ptr_)->sample (traj_time, state_desired, start_segment_itr, end_segment_itr);
178
+ (*traj_point_active_ptr_)
179
+ ->sample (traj_time, state_desired, start_segment_itr, end_segment_itr);
153
180
154
- if (valid_point) {
181
+ if (valid_point)
182
+ {
155
183
bool abort = false ;
156
184
bool outside_goal_tolerance = false ;
157
185
const bool before_last_point = end_segment_itr != (*traj_point_active_ptr_)->end ();
158
186
159
187
// set values for next hardware write()
160
- if (has_position_command_interface_) {
188
+ if (has_position_command_interface_)
189
+ {
161
190
assign_interface_from_point (joint_command_interface_[0 ], state_desired.positions );
162
191
}
163
- if (has_velocity_command_interface_) {
192
+ if (has_velocity_command_interface_)
193
+ {
164
194
assign_interface_from_point (joint_command_interface_[1 ], state_desired.velocities );
165
195
}
166
- if (has_acceleration_command_interface_) {
196
+ if (has_acceleration_command_interface_)
197
+ {
167
198
assign_interface_from_point (joint_command_interface_[2 ], state_desired.accelerations );
168
199
}
169
200
170
- for (size_t index = 0 ; index < joint_num; ++index) {
201
+ for (size_t index = 0 ; index < joint_num; ++index)
202
+ {
171
203
// set values for next hardware write()
172
204
compute_error_for_joint (state_error, index, state_current, state_desired);
173
205
174
- if (before_last_point &&
175
- !check_state_tolerance_per_joint (state_error, index, default_tolerances_.state_tolerance [index], true )) {
206
+ if (
207
+ before_last_point &&
208
+ !check_state_tolerance_per_joint (
209
+ state_error, index, default_tolerances_.state_tolerance [index], true ))
210
+ {
176
211
abort = true ;
177
212
}
178
213
// past the final point, check that we end up inside goal tolerance
179
- if (!before_last_point && !check_state_tolerance_per_joint (
180
- state_error, index, default_tolerances_.goal_state_tolerance [index], true )) {
214
+ if (
215
+ !before_last_point &&
216
+ !check_state_tolerance_per_joint (
217
+ state_error, index, default_tolerances_.goal_state_tolerance [index], true ))
218
+ {
181
219
outside_goal_tolerance = true ;
182
220
}
183
221
}
184
222
185
223
const auto active_goal = *rt_active_goal_.readFromRT ();
186
- if (active_goal) {
224
+ if (active_goal)
225
+ {
187
226
// send feedback
188
227
auto feedback = std::make_shared<FollowJTrajAction::Feedback>();
189
228
feedback->header .stamp = time;
@@ -195,13 +234,17 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
195
234
active_goal->setFeedback (feedback);
196
235
197
236
// check abort
198
- if (abort || outside_goal_tolerance) {
237
+ if (abort || outside_goal_tolerance)
238
+ {
199
239
auto result = std::make_shared<FollowJTrajAction::Result>();
200
240
201
- if (abort) {
241
+ if (abort)
242
+ {
202
243
RCLCPP_WARN (node_->get_logger (), " Aborted due to state tolerance violation" );
203
244
result->set__error_code (FollowJTrajAction::Result::PATH_TOLERANCE_VIOLATED);
204
- } else if (outside_goal_tolerance) {
245
+ }
246
+ else if (outside_goal_tolerance)
247
+ {
205
248
RCLCPP_WARN (node_->get_logger (), " Aborted due to goal tolerance violation" );
206
249
result->set__error_code (FollowJTrajAction::Result::GOAL_TOLERANCE_VIOLATED);
207
250
}
@@ -210,28 +253,35 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
210
253
}
211
254
212
255
// check goal tolerance
213
- if (!before_last_point) {
214
- if (!outside_goal_tolerance) {
256
+ if (!before_last_point)
257
+ {
258
+ if (!outside_goal_tolerance)
259
+ {
215
260
auto res = std::make_shared<FollowJTrajAction::Result>();
216
261
res->set__error_code (FollowJTrajAction::Result::SUCCESSFUL);
217
262
active_goal->setSucceeded (res);
218
263
rt_active_goal_.writeFromNonRT (RealtimeGoalHandlePtr ());
219
264
220
265
RCLCPP_INFO (node_->get_logger (), " Goal reached, success!" );
221
- } else if (default_tolerances_.goal_time_tolerance != 0.0 ) {
266
+ }
267
+ else if (default_tolerances_.goal_time_tolerance != 0.0 )
268
+ {
222
269
// if we exceed goal_time_toleralance set it to aborted
223
270
const rclcpp::Time traj_start = (*traj_point_active_ptr_)->get_trajectory_start_time ();
224
271
const rclcpp::Time traj_end = traj_start + start_segment_itr->time_from_start ;
225
272
226
273
// TODO(anyone): This will break in speed scaling we have to discuss how to handle the goal
227
274
// time when the robot scales itself down.
228
275
const double difference = time.seconds () - traj_end.seconds ();
229
- if (difference > default_tolerances_.goal_time_tolerance ) {
276
+ if (difference > default_tolerances_.goal_time_tolerance )
277
+ {
230
278
auto result = std::make_shared<FollowJTrajAction::Result>();
231
279
result->set__error_code (FollowJTrajAction::Result::GOAL_TOLERANCE_VIOLATED);
232
280
active_goal->setAborted (result);
233
281
rt_active_goal_.writeFromNonRT (RealtimeGoalHandlePtr ());
234
- RCLCPP_WARN (node_->get_logger (), " Aborted due goal_time_tolerance exceeding by %f seconds" , difference);
282
+ RCLCPP_WARN (
283
+ node_->get_logger (), " Aborted due goal_time_tolerance exceeding by %f seconds" ,
284
+ difference);
235
285
}
236
286
}
237
287
}
@@ -246,4 +296,5 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
246
296
} // namespace scaled_controllers
247
297
248
298
#include " pluginlib/class_list_macros.hpp"
249
- PLUGINLIB_EXPORT_CLASS (scaled_controllers::ScaledJointTrajectoryController, controller_interface::ControllerInterface)
299
+ PLUGINLIB_EXPORT_CLASS (
300
+ scaled_controllers::ScaledJointTrajectoryController, controller_interface::ControllerInterface)
0 commit comments