Skip to content

Commit f28b6a1

Browse files
author
Felix Exner
committed
Formatting as requested in this repo
1 parent f1d293e commit f28b6a1

File tree

4 files changed

+163
-98
lines changed

4 files changed

+163
-98
lines changed

scaled_controllers/include/scaled_controllers/scaled_joint_trajectory_controller.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,32 @@
2626
#include "angles/angles.h"
2727
#include "joint_trajectory_controller/joint_trajectory_controller.hpp"
2828
#include "joint_trajectory_controller/trajectory.hpp"
29-
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
30-
#include "rclcpp/time.hpp"
3129
#include "rclcpp/duration.hpp"
30+
#include "rclcpp/time.hpp"
31+
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
3232

3333
namespace scaled_controllers
3434
{
3535
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
3636

37-
class ScaledJointTrajectoryController : public joint_trajectory_controller::JointTrajectoryController
37+
class ScaledJointTrajectoryController
38+
: public joint_trajectory_controller::JointTrajectoryController
3839
{
3940
public:
4041
ScaledJointTrajectoryController() = default;
4142
~ScaledJointTrajectoryController() override = default;
4243

4344
controller_interface::InterfaceConfiguration state_interface_configuration() const override;
4445

45-
CallbackReturn on_activate(const rclcpp_lifecycle::State& state) override;
46+
CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
4647

47-
controller_interface::return_type update(const rclcpp::Time& time, const rclcpp::Duration& period) override;
48+
controller_interface::return_type update(
49+
const rclcpp::Time & time, const rclcpp::Duration & period) override;
4850

4951
protected:
5052
struct TimeData
5153
{
52-
TimeData() : time(0.0), period(rclcpp::Duration::from_nanoseconds(0.0)), uptime(0.0)
53-
{
54-
}
54+
TimeData() : time(0.0), period(rclcpp::Duration::from_nanoseconds(0.0)), uptime(0.0) {}
5555
rclcpp::Time time;
5656
rclcpp::Duration period;
5757
rclcpp::Time uptime;

scaled_controllers/include/scaled_controllers/speed_scaling_state_broadcaster.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
#include <vector>
3030

3131
#include "controller_interface/controller_interface.hpp"
32+
#include "rclcpp/duration.hpp"
33+
#include "rclcpp/time.hpp"
3234
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"
3335
#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
34-
#include "rclcpp/time.hpp"
35-
#include "rclcpp/duration.hpp"
3636
#include "std_msgs/msg/float64.hpp"
3737

3838
namespace scaled_controllers
@@ -48,13 +48,14 @@ class SpeedScalingStateBroadcaster : public controller_interface::ControllerInte
4848

4949
controller_interface::InterfaceConfiguration state_interface_configuration() const override;
5050

51-
controller_interface::return_type update(const rclcpp::Time& time, const rclcpp::Duration& period) override;
51+
controller_interface::return_type update(
52+
const rclcpp::Time & time, const rclcpp::Duration & period) override;
5253

53-
CallbackReturn on_configure(const rclcpp_lifecycle::State& previous_state) override;
54+
CallbackReturn on_configure(const rclcpp_lifecycle::State & previous_state) override;
5455

55-
CallbackReturn on_activate(const rclcpp_lifecycle::State& previous_state) override;
56+
CallbackReturn on_activate(const rclcpp_lifecycle::State & previous_state) override;
5657

57-
CallbackReturn on_deactivate(const rclcpp_lifecycle::State& previous_state) override;
58+
CallbackReturn on_deactivate(const rclcpp_lifecycle::State & previous_state) override;
5859

5960
CallbackReturn on_init() override;
6061

scaled_controllers/src/scaled_joint_trajectory_controller.cpp

Lines changed: 113 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828

2929
namespace scaled_controllers
3030
{
31-
controller_interface::InterfaceConfiguration ScaledJointTrajectoryController::state_interface_configuration() const
31+
controller_interface::InterfaceConfiguration
32+
ScaledJointTrajectoryController::state_interface_configuration() const
3233
{
3334
controller_interface::InterfaceConfiguration conf;
3435
conf = JointTrajectoryController::state_interface_configuration();
3536
conf.names.push_back("speed_scaling/speed_scaling_factor");
3637
return conf;
3738
}
3839

39-
CallbackReturn ScaledJointTrajectoryController::on_activate(const rclcpp_lifecycle::State& state)
40+
CallbackReturn ScaledJointTrajectoryController::on_activate(const rclcpp_lifecycle::State & state)
4041
{
4142
TimeData time_data;
4243
time_data.time = node_->now();
@@ -46,44 +47,58 @@ CallbackReturn ScaledJointTrajectoryController::on_activate(const rclcpp_lifecyc
4647
return JointTrajectoryController::on_activate(state);
4748
}
4849

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*/)
5152
{
52-
if (state_interfaces_.back().get_name() == "speed_scaling") {
53+
if (state_interfaces_.back().get_name() == "speed_scaling")
54+
{
5355
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.");
5661
}
5762

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+
{
5965
return controller_interface::return_type::OK;
6066
}
6167

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) {
7384
// 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+
{
7689
error.velocities[index] = desired.velocities[index] - current.velocities[index];
7790
}
78-
if (has_acceleration_state_interface_ && has_acceleration_command_interface_) {
91+
if (has_acceleration_state_interface_ && has_acceleration_command_interface_)
92+
{
7993
error.accelerations[index] = desired.accelerations[index] - current.accelerations[index];
8094
}
8195
};
8296

8397
// Check if a new external message has been received from nonRT threads
8498
auto current_external_msg = traj_external_point_ptr_->get_trajectory_msg();
8599
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+
{
87102
fill_partial_goal(*new_external_msg);
88103
sort_to_local_joint_order(*new_external_msg);
89104
traj_external_point_ptr_->update(*new_external_msg);
@@ -94,96 +109,120 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
94109
resize_joint_trajectory_point(state_current, joint_num);
95110

96111
// 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+
};
109126

110127
state_current.time_from_start.set__sec(0);
111128

112129
// Assign values from the hardware
113130
// Position states always exist
114131
assign_point_from_interface(state_current.positions, joint_state_interface_[0]);
115132
// velocity and acceleration states are optional
116-
if (has_velocity_state_interface_) {
133+
if (has_velocity_state_interface_)
134+
{
117135
assign_point_from_interface(state_current.velocities, joint_state_interface_[1]);
118136
// Acceleration is used only in combination with velocity
119-
if (has_acceleration_state_interface_) {
137+
if (has_acceleration_state_interface_)
138+
{
120139
assign_point_from_interface(state_current.accelerations, joint_state_interface_[2]);
121-
} else {
140+
}
141+
else
142+
{
122143
// Make empty so the property is ignored during interpolation
123144
state_current.accelerations.clear();
124145
}
125-
} else {
146+
}
147+
else
148+
{
126149
// Make empty so the property is ignored during interpolation
127150
state_current.velocities.clear();
128151
state_current.accelerations.clear();
129152
}
130153

131154
// 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+
{
133157
// Main Speed scaling difference...
134158
// Adjust time with scaling factor
135159
TimeData time_data;
136160
time_data.time = time;
137161
rcl_duration_value_t period = (time_data.time - time_data_.readFromRT()->time).nanoseconds();
138162
time_data.period = rclcpp::Duration::from_nanoseconds(scaling_factor_ * period);
139163
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);
141166
time_data_.writeFromNonRT(time_data);
142167

143168
// 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+
{
145171
(*traj_point_active_ptr_)->set_point_before_trajectory_msg(traj_time, state_current);
146172
}
147173
resize_joint_trajectory_point(state_error, joint_num);
148174

149175
// find segment for current timestamp
150176
joint_trajectory_controller::TrajectoryPointConstIter start_segment_itr, end_segment_itr;
151177
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);
153180

154-
if (valid_point) {
181+
if (valid_point)
182+
{
155183
bool abort = false;
156184
bool outside_goal_tolerance = false;
157185
const bool before_last_point = end_segment_itr != (*traj_point_active_ptr_)->end();
158186

159187
// set values for next hardware write()
160-
if (has_position_command_interface_) {
188+
if (has_position_command_interface_)
189+
{
161190
assign_interface_from_point(joint_command_interface_[0], state_desired.positions);
162191
}
163-
if (has_velocity_command_interface_) {
192+
if (has_velocity_command_interface_)
193+
{
164194
assign_interface_from_point(joint_command_interface_[1], state_desired.velocities);
165195
}
166-
if (has_acceleration_command_interface_) {
196+
if (has_acceleration_command_interface_)
197+
{
167198
assign_interface_from_point(joint_command_interface_[2], state_desired.accelerations);
168199
}
169200

170-
for (size_t index = 0; index < joint_num; ++index) {
201+
for (size_t index = 0; index < joint_num; ++index)
202+
{
171203
// set values for next hardware write()
172204
compute_error_for_joint(state_error, index, state_current, state_desired);
173205

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+
{
176211
abort = true;
177212
}
178213
// 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+
{
181219
outside_goal_tolerance = true;
182220
}
183221
}
184222

185223
const auto active_goal = *rt_active_goal_.readFromRT();
186-
if (active_goal) {
224+
if (active_goal)
225+
{
187226
// send feedback
188227
auto feedback = std::make_shared<FollowJTrajAction::Feedback>();
189228
feedback->header.stamp = time;
@@ -195,13 +234,17 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
195234
active_goal->setFeedback(feedback);
196235

197236
// check abort
198-
if (abort || outside_goal_tolerance) {
237+
if (abort || outside_goal_tolerance)
238+
{
199239
auto result = std::make_shared<FollowJTrajAction::Result>();
200240

201-
if (abort) {
241+
if (abort)
242+
{
202243
RCLCPP_WARN(node_->get_logger(), "Aborted due to state tolerance violation");
203244
result->set__error_code(FollowJTrajAction::Result::PATH_TOLERANCE_VIOLATED);
204-
} else if (outside_goal_tolerance) {
245+
}
246+
else if (outside_goal_tolerance)
247+
{
205248
RCLCPP_WARN(node_->get_logger(), "Aborted due to goal tolerance violation");
206249
result->set__error_code(FollowJTrajAction::Result::GOAL_TOLERANCE_VIOLATED);
207250
}
@@ -210,28 +253,35 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
210253
}
211254

212255
// 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+
{
215260
auto res = std::make_shared<FollowJTrajAction::Result>();
216261
res->set__error_code(FollowJTrajAction::Result::SUCCESSFUL);
217262
active_goal->setSucceeded(res);
218263
rt_active_goal_.writeFromNonRT(RealtimeGoalHandlePtr());
219264

220265
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+
{
222269
// if we exceed goal_time_toleralance set it to aborted
223270
const rclcpp::Time traj_start = (*traj_point_active_ptr_)->get_trajectory_start_time();
224271
const rclcpp::Time traj_end = traj_start + start_segment_itr->time_from_start;
225272

226273
// TODO(anyone): This will break in speed scaling we have to discuss how to handle the goal
227274
// time when the robot scales itself down.
228275
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+
{
230278
auto result = std::make_shared<FollowJTrajAction::Result>();
231279
result->set__error_code(FollowJTrajAction::Result::GOAL_TOLERANCE_VIOLATED);
232280
active_goal->setAborted(result);
233281
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);
235285
}
236286
}
237287
}
@@ -246,4 +296,5 @@ controller_interface::return_type ScaledJointTrajectoryController::update(const
246296
} // namespace scaled_controllers
247297

248298
#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

Comments
 (0)