-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrosbot_system.cpp
280 lines (230 loc) · 9.71 KB
/
rosbot_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
280
#include "rosbot_hardware_interfaces/rosbot_system.hpp"
#include <string>
#include <vector>
#include <algorithm>
#include "rclcpp/logging.hpp"
#include "hardware_interface/types/hardware_interface_type_values.hpp"
namespace rosbot_hardware_interfaces
{
CallbackReturn RosbotSystem::on_init(const hardware_interface::HardwareInfo& hardware_info)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Initializing");
if (hardware_interface::SystemInterface::on_init(hardware_info) != CallbackReturn::SUCCESS)
{
return CallbackReturn::ERROR;
}
for (const hardware_interface::ComponentInfo& joint : info_.joints)
{
if (joint.command_interfaces.size() != 1)
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint '%s' has %zu command interfaces found. 1 expected.",
joint.name.c_str(), joint.command_interfaces.size());
return CallbackReturn::ERROR;
}
if (joint.command_interfaces[0].name != hardware_interface::HW_IF_VELOCITY)
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint '%s' have %s command interfaces found. '%s' expected.",
joint.name.c_str(), joint.command_interfaces[0].name.c_str(), hardware_interface::HW_IF_VELOCITY);
return CallbackReturn::ERROR;
}
if (joint.state_interfaces.size() != 2)
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint '%s' has %zu state interface. 2 expected.",
joint.name.c_str(), joint.state_interfaces.size());
return CallbackReturn::ERROR;
}
if (joint.state_interfaces[0].name != hardware_interface::HW_IF_POSITION)
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint '%s' have '%s' as first state interface. '%s' expected.",
joint.name.c_str(), joint.state_interfaces[0].name.c_str(), hardware_interface::HW_IF_POSITION);
return CallbackReturn::ERROR;
}
if (joint.state_interfaces[1].name != hardware_interface::HW_IF_VELOCITY)
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint '%s' have '%s' as second state interface. '%s' expected.",
joint.name.c_str(), joint.state_interfaces[1].name.c_str(), hardware_interface::HW_IF_VELOCITY);
return CallbackReturn::ERROR;
}
}
for (auto& j : info_.joints)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Joint '%s' found", j.name.c_str());
pos_state_[j.name] = 0.0;
vel_state_[j.name] = 0.0;
vel_commands_[j.name] = 0.0;
}
connection_timeout_ms_ = std::stoul(info_.hardware_parameters["connection_timeout_ms"]);
connection_check_period_ms_ = std::stoul(info_.hardware_parameters["connection_check_period_ms"]);
std::string velocity_command_joint_order_raw = info_.hardware_parameters["velocity_command_joint_order"];
// remove whitespaces
velocity_command_joint_order_raw.erase(
std::remove_if(velocity_command_joint_order_raw.begin(), velocity_command_joint_order_raw.end(),
[](char c) { return std::isspace(static_cast<unsigned char>(c)); }),
velocity_command_joint_order_raw.end());
std::stringstream velocity_command_joint_order_stream(velocity_command_joint_order_raw);
std::string joint_name;
while (getline(velocity_command_joint_order_stream, joint_name, ','))
{
velocity_command_joint_order_.push_back(joint_name);
}
if (velocity_command_joint_order_.size() != info_.joints.size())
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint order size is invalid");
return CallbackReturn::ERROR;
}
for (auto& j : info_.joints)
{
if (std::find(velocity_command_joint_order_.begin(), velocity_command_joint_order_.end(), j.name) ==
velocity_command_joint_order_.end())
{
RCLCPP_FATAL(rclcpp::get_logger("RosbotSystem"), "Joint '%s' missing from velocity command joint order",
j.name.c_str());
return CallbackReturn::ERROR;
}
}
node_ = std::make_shared<rclcpp::Node>("rosbot_system_node");
executor_.add_node(node_);
executor_thread_ =
std::make_unique<std::thread>(std::bind(&rclcpp::executors::MultiThreadedExecutor::spin, &executor_));
return CallbackReturn::SUCCESS;
}
CallbackReturn RosbotSystem::on_configure(const rclcpp_lifecycle::State&)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Configuring");
return CallbackReturn::SUCCESS;
}
CallbackReturn RosbotSystem::on_cleanup(const rclcpp_lifecycle::State&)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Cleaning up");
return CallbackReturn::SUCCESS;
}
CallbackReturn RosbotSystem::on_activate(const rclcpp_lifecycle::State&)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Activating");
for (const auto& x : pos_state_)
{
pos_state_[x.first] = 0.0;
vel_state_[x.first] = 0.0;
vel_commands_[x.first] = 0.0;
}
motor_command_publisher_ = node_->create_publisher<Float32MultiArray>("~/motors_cmd", rclcpp::SensorDataQoS());
realtime_motor_command_publisher_ =
std::make_shared<realtime_tools::RealtimePublisher<Float32MultiArray>>(motor_command_publisher_);
motor_state_subscriber_ =
node_->create_subscription<JointState>("~/motors_response", rclcpp::SensorDataQoS(),
std::bind(&RosbotSystem::motor_state_cb, this, std::placeholders::_1));
std::shared_ptr<JointState> motor_state;
for (uint wait_time = 0; wait_time <= connection_timeout_ms_; wait_time += connection_check_period_ms_)
{
if (!rclcpp::ok())
{
return CallbackReturn::ERROR;
}
RCLCPP_WARN_THROTTLE(rclcpp::get_logger("RosbotSystem"), *node_->get_clock(), 5000, "Feedback message from motors wasn't received yet");
received_motor_state_msg_ptr_.get(motor_state);
if (motor_state)
{
RCLCPP_DEBUG(node_->get_logger(), "Subscriber and publisher are now active.");
return CallbackReturn::SUCCESS;
}
rclcpp::sleep_for(std::chrono::milliseconds(connection_check_period_ms_));
}
RCLCPP_FATAL(node_->get_logger(), "Activation failed, timeout reached while waiting for feedback from motors");
return CallbackReturn::ERROR;
}
CallbackReturn RosbotSystem::on_deactivate(const rclcpp_lifecycle::State&)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Deactivating");
cleanup_node();
received_motor_state_msg_ptr_.set(nullptr);
return CallbackReturn::SUCCESS;
}
CallbackReturn RosbotSystem::on_shutdown(const rclcpp_lifecycle::State&)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Shutting down");
cleanup_node();
return CallbackReturn::SUCCESS;
}
CallbackReturn RosbotSystem::on_error(const rclcpp_lifecycle::State&)
{
RCLCPP_INFO(rclcpp::get_logger("RosbotSystem"), "Handling error");
cleanup_node();
return CallbackReturn::SUCCESS;
}
std::vector<StateInterface> RosbotSystem::export_state_interfaces()
{
std::vector<StateInterface> state_interfaces;
for (auto i = 0u; i < info_.joints.size(); i++)
{
state_interfaces.emplace_back(
StateInterface(info_.joints[i].name, hardware_interface::HW_IF_POSITION, &pos_state_[info_.joints[i].name]));
state_interfaces.emplace_back(
StateInterface(info_.joints[i].name, hardware_interface::HW_IF_VELOCITY, &vel_state_[info_.joints[i].name]));
}
return state_interfaces;
}
std::vector<CommandInterface> RosbotSystem::export_command_interfaces()
{
std::vector<CommandInterface> command_interfaces;
for (auto i = 0u; i < info_.joints.size(); i++)
{
command_interfaces.emplace_back(hardware_interface::CommandInterface(
info_.joints[i].name, hardware_interface::HW_IF_VELOCITY, &vel_commands_[info_.joints[i].name]));
}
return command_interfaces;
}
void RosbotSystem::cleanup_node()
{
motor_state_subscriber_.reset();
realtime_motor_command_publisher_.reset();
motor_command_publisher_.reset();
}
void RosbotSystem::motor_state_cb(const std::shared_ptr<JointState> msg)
{
RCLCPP_DEBUG(node_->get_logger(), "Received motors response");
received_motor_state_msg_ptr_.set(std::move(msg));
}
return_type RosbotSystem::read(const rclcpp::Time&, const rclcpp::Duration&)
{
std::shared_ptr<JointState> motor_state;
received_motor_state_msg_ptr_.get(motor_state);
RCLCPP_DEBUG(rclcpp::get_logger("RosbotSystem"), "Reading motors state");
if (!motor_state)
{
RCLCPP_ERROR(rclcpp::get_logger("RosbotSystem"), "Feedback message from motors wasn't received");
return return_type::ERROR;
}
for (auto i = 0u; i < motor_state->name.size(); i++)
{
if (pos_state_.find(motor_state->name[i]) == pos_state_.end() ||
vel_state_.find(motor_state->name[i]) == vel_state_.end())
{
RCLCPP_ERROR(rclcpp::get_logger("RosbotSystem"), "Position or velocity feedback not found for joint %s",
motor_state->name[i].c_str());
return return_type::ERROR;
}
pos_state_[motor_state->name[i]] = motor_state->position[i];
vel_state_[motor_state->name[i]] = motor_state->velocity[i];
RCLCPP_DEBUG(rclcpp::get_logger("RosbotSystem"), "Position feedback: %f, velocity feedback: %f",
pos_state_[motor_state->name[i]], vel_state_[motor_state->name[i]]);
}
return return_type::OK;
}
return_type RosbotSystem::write(const rclcpp::Time&, const rclcpp::Duration&)
{
if (realtime_motor_command_publisher_->trylock())
{
auto& motor_command = realtime_motor_command_publisher_->msg_;
motor_command.data.clear();
RCLCPP_DEBUG(rclcpp::get_logger("RosbotSystem"), "Wrtiting motors cmd message");
for (auto const& joint : velocity_command_joint_order_)
{
motor_command.data.push_back(vel_commands_[joint]);
}
realtime_motor_command_publisher_->unlockAndPublish();
}
return return_type::OK;
}
} // namespace rosbot_hardware_interfaces
#include "pluginlib/class_list_macros.hpp"
PLUGINLIB_EXPORT_CLASS(rosbot_hardware_interfaces::RosbotSystem, hardware_interface::SystemInterface)