Skip to content

Commit a8ca44b

Browse files
committed
adjust example 1 with new export of interfaces
1 parent 97c5ad8 commit a8ca44b

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

example_1/hardware/include/ros2_control_demo_example_1/rrbot.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ class RRBotSystemPositionOnlyHardware : public hardware_interface::SystemInterfa
4343
hardware_interface::CallbackReturn on_configure(
4444
const rclcpp_lifecycle::State & previous_state) override;
4545

46-
ROS2_CONTROL_DEMO_EXAMPLE_1_PUBLIC
47-
std::vector<hardware_interface::StateInterface> export_state_interfaces() override;
48-
49-
ROS2_CONTROL_DEMO_EXAMPLE_1_PUBLIC
50-
std::vector<hardware_interface::CommandInterface> export_command_interfaces() override;
51-
5246
ROS2_CONTROL_DEMO_EXAMPLE_1_PUBLIC
5347
hardware_interface::CallbackReturn on_activate(
5448
const rclcpp_lifecycle::State & previous_state) override;
@@ -70,10 +64,6 @@ class RRBotSystemPositionOnlyHardware : public hardware_interface::SystemInterfa
7064
double hw_start_sec_;
7165
double hw_stop_sec_;
7266
double hw_slowdown_;
73-
74-
// Store the command for the simulated robot
75-
std::vector<double> hw_commands_;
76-
std::vector<double> hw_states_;
7767
};
7868

7969
} // namespace ros2_control_demo_example_1

example_1/hardware/rrbot.cpp

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ hardware_interface::CallbackReturn RRBotSystemPositionOnlyHardware::on_init(
4040
hw_stop_sec_ = stod(info_.hardware_parameters["example_param_hw_stop_duration_sec"]);
4141
hw_slowdown_ = stod(info_.hardware_parameters["example_param_hw_slowdown"]);
4242
// END: This part here is for exemplary purposes - Please do not copy to your production code
43-
hw_states_.resize(info_.joints.size(), std::numeric_limits<double>::quiet_NaN());
44-
hw_commands_.resize(info_.joints.size(), std::numeric_limits<double>::quiet_NaN());
4543

4644
for (const hardware_interface::ComponentInfo & joint : info_.joints)
4745
{
@@ -103,41 +101,17 @@ hardware_interface::CallbackReturn RRBotSystemPositionOnlyHardware::on_configure
103101
// END: This part here is for exemplary purposes - Please do not copy to your production code
104102

105103
// reset values always when configuring hardware
106-
for (uint i = 0; i < hw_states_.size(); i++)
104+
for (uint i = 0; i < joint_pos_states_.size(); i++)
107105
{
108-
hw_states_[i] = 0;
109-
hw_commands_[i] = 0;
106+
joint_pos_states_[i] = 0.0;
110107
}
111-
112-
RCLCPP_INFO(rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Successfully configured!");
113-
114-
return hardware_interface::CallbackReturn::SUCCESS;
115-
}
116-
117-
std::vector<hardware_interface::StateInterface>
118-
RRBotSystemPositionOnlyHardware::export_state_interfaces()
119-
{
120-
std::vector<hardware_interface::StateInterface> state_interfaces;
121-
for (uint i = 0; i < info_.joints.size(); i++)
108+
for (uint i = 0; i < joint_pos_commands_.size(); i++)
122109
{
123-
state_interfaces.emplace_back(hardware_interface::StateInterface(
124-
info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_states_[i]));
125-
}
126-
127-
return state_interfaces;
128-
}
129-
130-
std::vector<hardware_interface::CommandInterface>
131-
RRBotSystemPositionOnlyHardware::export_command_interfaces()
132-
{
133-
std::vector<hardware_interface::CommandInterface> command_interfaces;
134-
for (uint i = 0; i < info_.joints.size(); i++)
135-
{
136-
command_interfaces.emplace_back(hardware_interface::CommandInterface(
137-
info_.joints[i].name, hardware_interface::HW_IF_POSITION, &hw_commands_[i]));
110+
joint_pos_commands_[i] = 0.0;
138111
}
112+
RCLCPP_INFO(rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Successfully configured!");
139113

140-
return command_interfaces;
114+
return hardware_interface::CallbackReturn::SUCCESS;
141115
}
142116

143117
hardware_interface::CallbackReturn RRBotSystemPositionOnlyHardware::on_activate(
@@ -157,9 +131,9 @@ hardware_interface::CallbackReturn RRBotSystemPositionOnlyHardware::on_activate(
157131
// END: This part here is for exemplary purposes - Please do not copy to your production code
158132

159133
// command and state should be equal when starting
160-
for (uint i = 0; i < hw_states_.size(); i++)
134+
for (uint i = 0; i < joint_pos_states_.size(); i++)
161135
{
162-
hw_commands_[i] = hw_states_[i];
136+
joint_pos_commands_[i] = joint_pos_states_[i];
163137
}
164138

165139
RCLCPP_INFO(rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Successfully activated!");
@@ -194,13 +168,14 @@ hardware_interface::return_type RRBotSystemPositionOnlyHardware::read(
194168
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
195169
RCLCPP_INFO(rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Reading...");
196170

197-
for (uint i = 0; i < hw_states_.size(); i++)
171+
for (uint i = 0; i < joint_pos_states_.size(); i++)
198172
{
199173
// Simulate RRBot's movement
200-
hw_states_[i] = hw_states_[i] + (hw_commands_[i] - hw_states_[i]) / hw_slowdown_;
174+
joint_pos_states_[i] =
175+
joint_pos_states_[i] + (joint_pos_commands_[i] - joint_pos_states_[i]) / hw_slowdown_;
201176
RCLCPP_INFO(
202177
rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Got state %.5f for joint %d!",
203-
hw_states_[i], i);
178+
joint_pos_states_[i], i);
204179
}
205180
RCLCPP_INFO(rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Joints successfully read!");
206181
// END: This part here is for exemplary purposes - Please do not copy to your production code
@@ -214,12 +189,12 @@ hardware_interface::return_type RRBotSystemPositionOnlyHardware::write(
214189
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
215190
RCLCPP_INFO(rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Writing...");
216191

217-
for (uint i = 0; i < hw_commands_.size(); i++)
192+
for (uint i = 0; i < joint_pos_commands_.size(); i++)
218193
{
219194
// Simulate sending commands to the hardware
220195
RCLCPP_INFO(
221196
rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Got command %.5f for joint %d!",
222-
hw_commands_[i], i);
197+
joint_pos_commands_[i], i);
223198
}
224199
RCLCPP_INFO(
225200
rclcpp::get_logger("RRBotSystemPositionOnlyHardware"), "Joints successfully written!");

0 commit comments

Comments
 (0)