Skip to content

Commit 019217e

Browse files
committed
Most of the tests are working!
1 parent 8e8ff0f commit 019217e

File tree

4 files changed

+264
-84
lines changed

4 files changed

+264
-84
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/joint_trajectory_controller.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 Open Source Robotics Foundation, Inc.
1+
// Copyright (c) 2021 ros2_control Development Team
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -42,10 +42,6 @@
4242
#include "trajectory_msgs/msg/joint_trajectory.hpp"
4343
#include "trajectory_msgs/msg/joint_trajectory_point.hpp"
4444

45-
namespace hardware_interface
46-
{
47-
class RobotHardware;
48-
} // namespace hardware_interface
4945
namespace rclcpp_action
5046
{
5147
template<typename ActionT>
@@ -219,9 +215,12 @@ class JointTrajectoryController : public controller_interface::ControllerInterfa
219215
const JointTrajectoryPoint & state_error);
220216

221217
private:
222-
void assign_point_value(
223-
const std::vector<std::string> & interface_types,
224-
std::vector<double> & point_values);
218+
bool check_if_interface_type_exist(
219+
const std::vector<std::string> & interface_type_list, const std::string & interface_type)
220+
{
221+
return std::find(interface_type_list.begin(), interface_type_list.end(), interface_type) !=
222+
interface_type_list.end();
223+
}
225224
};
226225

227226
} // namespace joint_trajectory_controller

joint_trajectory_controller/src/joint_trajectory_controller.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 Open Source Robotics Foundation, Inc.
1+
// Copyright (c) 2021 ros2_control Development Team
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -117,20 +117,34 @@ JointTrajectoryController::update()
117117
}
118118

119119
auto resize_joint_trajectory_point =
120-
[](trajectory_msgs::msg::JointTrajectoryPoint & point, size_t size)
120+
[&](trajectory_msgs::msg::JointTrajectoryPoint & point, size_t size)
121121
{
122122
point.positions.resize(size);
123123
point.velocities.resize(size);
124124
point.accelerations.resize(size);
125+
if (check_if_interface_type_exist(
126+
state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
127+
point.velocities.resize(size);
128+
}
129+
if (check_if_interface_type_exist(
130+
state_interface_types_, hardware_interface::HW_IF_ACCELERATION)) {
131+
point.accelerations.resize(size);
132+
}
125133
};
126-
auto compute_error_for_joint = [](JointTrajectoryPoint & error, int index,
134+
auto compute_error_for_joint = [&](JointTrajectoryPoint & error, int index,
127135
const JointTrajectoryPoint & current, const JointTrajectoryPoint & desired)
128136
{
129137
// error defined as the difference between current and desired
130138
error.positions[index] = angles::shortest_angular_distance(
131139
current.positions[index], desired.positions[index]);
132-
error.velocities[index] = desired.velocities[index] - current.velocities[index];
133-
error.accelerations[index] = 0.0;
140+
if (check_if_interface_type_exist(
141+
state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
142+
error.velocities[index] = desired.velocities[index] - current.velocities[index];
143+
}
144+
if (check_if_interface_type_exist(
145+
state_interface_types_, hardware_interface::HW_IF_ACCELERATION)) {
146+
error.accelerations[index] = desired.accelerations[index] - current.accelerations[index];
147+
}
134148
};
135149

136150
// Check if a new external message has been received from nonRT threads
@@ -146,12 +160,6 @@ JointTrajectoryController::update()
146160
const auto joint_num = joint_names_.size();
147161
resize_joint_trajectory_point(state_current, joint_num);
148162

149-
auto check_if_interface_type_exist =
150-
[](const std::vector<std::string> & interface_type_list, const std::string & interface_type)
151-
{
152-
return std::find(interface_type_list.begin(), interface_type_list.end(), interface_type) !=
153-
interface_type_list.end();
154-
};
155163
auto assign_point_from_interface = [&, joint_num](
156164
std::vector<double> & trajectory_point_interface, const auto & joint_inteface)
157165
{
@@ -179,10 +187,8 @@ JointTrajectoryController::update()
179187
if (check_if_interface_type_exist(state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
180188
assign_point_from_interface(state_current.velocities, joint_state_interface_[1]);
181189
// Acceleration is used only in combination with velocity
182-
if (check_if_interface_type_exist(
183-
state_interface_types_,
184-
hardware_interface::HW_IF_ACCELERATION))
185-
{
190+
if (check_if_interface_type_exist(state_interface_types_,
191+
hardware_interface::HW_IF_ACCELERATION)) {
186192
assign_point_from_interface(state_current.accelerations, joint_state_interface_[2]);
187193
} else {
188194
// Make empty so the property is ignored during interpolation
@@ -360,12 +366,6 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
360366
// 1. effort
361367
// 2. velocity
362368
// 2. position [velocity, [acceleration]]
363-
auto check_if_interface_type_exist =
364-
[](const std::vector<std::string> & interface_type_list, const std::string & interface_type)
365-
{
366-
return std::find(interface_type_list.begin(), interface_type_list.end(), interface_type) !=
367-
interface_type_list.end();
368-
};
369369

370370
// effort can't be combined with other interfaces
371371
if (check_if_interface_type_exist(command_interface_types_, hardware_interface::HW_IF_EFFORT)) {
@@ -489,7 +489,7 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
489489
const double state_publish_rate =
490490
node_->get_parameter("state_publish_rate").get_value<double>();
491491
RCLCPP_INFO(
492-
logger, "Controller state will be published at %f.2 Hz.", state_publish_rate);
492+
logger, "Controller state will be published at %.2f Hz.", state_publish_rate);
493493
if (state_publish_rate > 0.0) {
494494
state_publisher_period_ =
495495
rclcpp::Duration::from_seconds(1.0 / state_publish_rate);
@@ -509,9 +509,16 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
509509
state_publisher_->msg_.desired.velocities.resize(n_joints);
510510
state_publisher_->msg_.desired.accelerations.resize(n_joints);
511511
state_publisher_->msg_.actual.positions.resize(n_joints);
512-
state_publisher_->msg_.actual.velocities.resize(n_joints);
513512
state_publisher_->msg_.error.positions.resize(n_joints);
514-
state_publisher_->msg_.error.velocities.resize(n_joints);
513+
if (check_if_interface_type_exist(state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
514+
state_publisher_->msg_.actual.velocities.resize(n_joints);
515+
state_publisher_->msg_.error.velocities.resize(n_joints);
516+
}
517+
if (check_if_interface_type_exist(state_interface_types_, hardware_interface::HW_IF_ACCELERATION))
518+
{
519+
state_publisher_->msg_.actual.accelerations.resize(n_joints);
520+
state_publisher_->msg_.error.accelerations.resize(n_joints);
521+
}
515522
state_publisher_->unlock();
516523

517524
last_state_publish_time_ = node_->now();
@@ -527,7 +534,7 @@ JointTrajectoryController::on_configure(const rclcpp_lifecycle::State &)
527534
.get_value<double>();
528535

529536
RCLCPP_INFO(
530-
logger, "Action status changes will be monitored at %f.2 Hz.", action_monitor_rate);
537+
logger, "Action status changes will be monitored at %.2f Hz.", action_monitor_rate);
531538
action_monitor_period_ = rclcpp::Duration::from_seconds(1.0 / action_monitor_rate);
532539

533540
using namespace std::placeholders;
@@ -577,7 +584,7 @@ JointTrajectoryController::on_activate(const rclcpp_lifecycle::State &)
577584
command_interfaces_, joint_names_, interface, joint_command_interface_[index]))
578585
{
579586
RCLCPP_ERROR(
580-
node_->get_logger(), "Expected %u %s command interfaces, got %u",
587+
node_->get_logger(), "Expected %u '%s' command interfaces, got %u.",
581588
joint_names_.size(), interface.c_str(), joint_command_interface_[index].size());
582589
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
583590
}
@@ -590,7 +597,7 @@ JointTrajectoryController::on_activate(const rclcpp_lifecycle::State &)
590597
state_interfaces_, joint_names_, interface, joint_state_interface_[index]))
591598
{
592599
RCLCPP_ERROR(
593-
node_->get_logger(), "Expected %u %s state interfaces, got %u",
600+
node_->get_logger(), "Expected %u '%s' state interfaces, got %u.",
594601
joint_names_.size(), interface.c_str(), joint_state_interface_[index].size());
595602
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
596603
}
@@ -711,9 +718,16 @@ void JointTrajectoryController::publish_state(
711718
state_publisher_->msg_.desired.velocities = desired_state.velocities;
712719
state_publisher_->msg_.desired.accelerations = desired_state.accelerations;
713720
state_publisher_->msg_.actual.positions = current_state.positions;
714-
state_publisher_->msg_.actual.velocities = current_state.velocities;
715721
state_publisher_->msg_.error.positions = state_error.positions;
716-
state_publisher_->msg_.error.velocities = state_error.velocities;
722+
if (check_if_interface_type_exist(state_interface_types_, hardware_interface::HW_IF_VELOCITY)) {
723+
state_publisher_->msg_.actual.velocities = current_state.velocities;
724+
state_publisher_->msg_.error.velocities = state_error.velocities;
725+
}
726+
if (check_if_interface_type_exist(
727+
state_interface_types_, hardware_interface::HW_IF_ACCELERATION)) {
728+
state_publisher_->msg_.actual.accelerations = current_state.accelerations;
729+
state_publisher_->msg_.error.accelerations = state_error.accelerations;
730+
}
717731

718732
state_publisher_->unlockAndPublish();
719733
}

0 commit comments

Comments
 (0)