-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathcarlikebot_system.cpp
279 lines (235 loc) · 10.3 KB
/
carlikebot_system.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2021 ros2_control Development Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ros2_control_demo_example_11/carlikebot_system.hpp"
#include <chrono>
#include <cmath>
#include <cstddef>
#include <iomanip>
#include <limits>
#include <memory>
#include <sstream>
#include <vector>
#include "hardware_interface/types/hardware_interface_type_values.hpp"
#include "rclcpp/rclcpp.hpp"
namespace ros2_control_demo_example_11
{
hardware_interface::CallbackReturn CarlikeBotSystemHardware::on_init(
const hardware_interface::HardwareInfo & info)
{
if (
hardware_interface::SystemInterface::on_init(info) !=
hardware_interface::CallbackReturn::SUCCESS)
{
return hardware_interface::CallbackReturn::ERROR;
}
// Check if the number of joints is correct based on the mode of operation
if (info_.joints.size() != 2)
{
RCLCPP_ERROR(
get_logger(),
"CarlikeBotSystemHardware::on_init() - Failed to initialize, "
"because the number of joints %ld is not 2.",
info_.joints.size());
return hardware_interface::CallbackReturn::ERROR;
}
for (const hardware_interface::ComponentInfo & joint : info_.joints)
{
bool joint_is_steering = joint.name.find("front") != std::string::npos;
// Steering joints have a position command interface and a position state interface
if (joint_is_steering)
{
steering_joint_ = joint.name;
RCLCPP_INFO(get_logger(), "Joint '%s' is a steering joint.", joint.name.c_str());
if (joint.command_interfaces.size() != 1)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %zu command interfaces found. 1 expected.",
joint.name.c_str(), joint.command_interfaces.size());
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.command_interfaces[0].name != hardware_interface::HW_IF_POSITION)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %s command interface. '%s' expected.", joint.name.c_str(),
joint.command_interfaces[0].name.c_str(), hardware_interface::HW_IF_POSITION);
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.state_interfaces.size() != 1)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %zu state interface. 1 expected.", joint.name.c_str(),
joint.state_interfaces.size());
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.state_interfaces[0].name != hardware_interface::HW_IF_POSITION)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %s state interface. '%s' expected.", joint.name.c_str(),
joint.state_interfaces[0].name.c_str(), hardware_interface::HW_IF_POSITION);
return hardware_interface::CallbackReturn::ERROR;
}
}
else
{
RCLCPP_INFO(get_logger(), "Joint '%s' is a drive joint.", joint.name.c_str());
traction_joint_ = joint.name;
// Drive joints have a velocity command interface and a velocity state interface
if (joint.command_interfaces.size() != 1)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %zu command interfaces found. 1 expected.",
joint.name.c_str(), joint.command_interfaces.size());
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.command_interfaces[0].name != hardware_interface::HW_IF_VELOCITY)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %s command interface. '%s' expected.", joint.name.c_str(),
joint.command_interfaces[0].name.c_str(), hardware_interface::HW_IF_VELOCITY);
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.state_interfaces.size() != 2)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %zu state interface. 2 expected.", joint.name.c_str(),
joint.state_interfaces.size());
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.state_interfaces[0].name != hardware_interface::HW_IF_VELOCITY)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %s state interface. '%s' expected.", joint.name.c_str(),
joint.state_interfaces[1].name.c_str(), hardware_interface::HW_IF_VELOCITY);
return hardware_interface::CallbackReturn::ERROR;
}
if (joint.state_interfaces[1].name != hardware_interface::HW_IF_POSITION)
{
RCLCPP_FATAL(
get_logger(), "Joint '%s' has %s state interface. '%s' expected.", joint.name.c_str(),
joint.state_interfaces[1].name.c_str(), hardware_interface::HW_IF_POSITION);
return hardware_interface::CallbackReturn::ERROR;
}
}
}
// // BEGIN: This part here is for exemplary purposes - Please do not copy to your production
// code
hw_start_sec_ = std::stod(info_.hardware_parameters["example_param_hw_start_duration_sec"]);
hw_stop_sec_ = std::stod(info_.hardware_parameters["example_param_hw_stop_duration_sec"]);
// // END: This part here is for exemplary purposes - Please do not copy to your production code
return hardware_interface::CallbackReturn::SUCCESS;
}
hardware_interface::CallbackReturn CarlikeBotSystemHardware::on_configure(
const rclcpp_lifecycle::State & /*previous_state*/)
{
RCLCPP_INFO(get_logger(), "Configuring ...please wait...");
for (auto i = 0; i < hw_start_sec_; i++)
{
rclcpp::sleep_for(std::chrono::seconds(1));
RCLCPP_INFO(get_logger(), "%.1f seconds left...", hw_start_sec_ - i);
}
// reset values always when configuring hardware
for (const auto & [name, descr] : joint_state_interfaces_)
{
set_state(name, 0.0);
}
for (const auto & [name, descr] : joint_command_interfaces_)
{
set_command(name, 0.0);
}
RCLCPP_INFO(get_logger(), "Successfully configured!");
return hardware_interface::CallbackReturn::SUCCESS;
}
hardware_interface::CallbackReturn CarlikeBotSystemHardware::on_activate(
const rclcpp_lifecycle::State & /*previous_state*/)
{
RCLCPP_INFO(get_logger(), "Activating ...please wait...");
for (auto i = 0; i < hw_start_sec_; i++)
{
rclcpp::sleep_for(std::chrono::seconds(1));
RCLCPP_INFO(get_logger(), "%.1f seconds left...", hw_start_sec_ - i);
}
// command and state should be equal when starting
for (const auto & [name, descr] : joint_command_interfaces_)
{
set_command(name, get_state(name));
}
RCLCPP_INFO(get_logger(), "Successfully activated!");
return hardware_interface::CallbackReturn::SUCCESS;
}
hardware_interface::CallbackReturn CarlikeBotSystemHardware::on_deactivate(
const rclcpp_lifecycle::State & /*previous_state*/)
{
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
RCLCPP_INFO(get_logger(), "Deactivating ...please wait...");
for (auto i = 0; i < hw_stop_sec_; i++)
{
rclcpp::sleep_for(std::chrono::seconds(1));
RCLCPP_INFO(get_logger(), "%.1f seconds left...", hw_stop_sec_ - i);
}
// END: This part here is for exemplary purposes - Please do not copy to your production code
RCLCPP_INFO(get_logger(), "Successfully deactivated!");
return hardware_interface::CallbackReturn::SUCCESS;
}
hardware_interface::return_type CarlikeBotSystemHardware::read(
const rclcpp::Time & /*time*/, const rclcpp::Duration & period)
{
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
// update states from commands and integrate velocity to position
set_state(
steering_joint_ + "/" + hardware_interface::HW_IF_POSITION,
get_command(steering_joint_ + "/" + hardware_interface::HW_IF_POSITION));
set_state(
traction_joint_ + "/" + hardware_interface::HW_IF_VELOCITY,
get_command(traction_joint_ + "/" + hardware_interface::HW_IF_VELOCITY));
set_state(
traction_joint_ + "/" + hardware_interface::HW_IF_POSITION,
get_state(traction_joint_ + "/" + hardware_interface::HW_IF_POSITION) +
get_command(traction_joint_ + "/" + hardware_interface::HW_IF_VELOCITY) * period.seconds());
std::stringstream ss;
ss << "Reading states:";
ss << std::fixed << std::setprecision(2) << std::endl
<< "\t"
<< "position: " << get_state(steering_joint_ + "/" + hardware_interface::HW_IF_POSITION)
<< " for joint '" << steering_joint_ << "'" << std::endl
<< "\t"
<< "position: " << get_state(traction_joint_ + "/" + hardware_interface::HW_IF_POSITION)
<< " for joint '" << traction_joint_ << "'" << std::endl
<< "\t"
<< "velocity: " << get_state(traction_joint_ + "/" + hardware_interface::HW_IF_VELOCITY)
<< " for joint '" << traction_joint_ << "'";
RCLCPP_INFO_THROTTLE(get_logger(), *get_clock(), 500, "%s", ss.str().c_str());
// END: This part here is for exemplary purposes - Please do not copy to your production code
return hardware_interface::return_type::OK;
}
hardware_interface::return_type ros2_control_demo_example_11 ::CarlikeBotSystemHardware::write(
const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
{
// BEGIN: This part here is for exemplary purposes - Please do not copy to your production code
std::stringstream ss;
ss << "Writing commands:";
ss << std::fixed << std::setprecision(2) << std::endl
<< "\t"
<< "position: " << get_command(steering_joint_ + "/" + hardware_interface::HW_IF_POSITION)
<< " for joint '" << steering_joint_ << "'" << std::endl
<< "\t"
<< "velocity: " << get_command(traction_joint_ + "/" + hardware_interface::HW_IF_VELOCITY)
<< " for joint '" << traction_joint_ << "'";
RCLCPP_INFO_THROTTLE(get_logger(), *get_clock(), 500, "%s", ss.str().c_str());
// END: This part here is for exemplary purposes - Please do not copy to your production code
return hardware_interface::return_type::OK;
}
} // namespace ros2_control_demo_example_11
#include "pluginlib/class_list_macros.hpp"
PLUGINLIB_EXPORT_CLASS(
ros2_control_demo_example_11::CarlikeBotSystemHardware, hardware_interface::SystemInterface)