Skip to content
This repository was archived by the owner on Oct 17, 2025. It is now read-only.

Commit a332f0c

Browse files
committed
Fix vector reallocation issue
1 parent 4ced175 commit a332f0c

File tree

1 file changed

+113
-36
lines changed

1 file changed

+113
-36
lines changed

gazebo_ros2_control/src/gazebo_system.cpp

Lines changed: 113 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -472,58 +472,126 @@ void GazeboSystem::registerGazeboTopics(
472472
const hardware_interface::HardwareInfo & hardware_info,
473473
gazebo::physics::ModelPtr parent_model)
474474
{
475-
for (unsigned int j = 0; j < hardware_info.joints.size(); j++) {
476-
auto & info = hardware_info.joints[j];
475+
// This is split in two steps: Count the number and type of sensor and associate the interfaces
476+
// So we have resize only once the structures where the data will be stored, and we can safely
477+
// use pointers to the structures
478+
479+
int n_commands = 0;
480+
int n_states = 0;
481+
482+
// STEP 1
483+
for (unsigned int j = 0; j < hardware_info.joints.size(); j++)
484+
{
485+
auto &info = hardware_info.joints[j];
486+
487+
for (unsigned int i = 0; i < info.command_interfaces.size(); i++)
488+
{
489+
std::string if_name = info.command_interfaces[i].name;
490+
491+
if (if_name != "position" && if_name != "velocity" && if_name != "effort")
492+
{
493+
++n_commands;
494+
}
495+
}
496+
497+
for (unsigned int i = 0; i < info.state_interfaces.size(); i++)
498+
{
499+
std::string if_name = info.state_interfaces[i].name;
500+
501+
if (if_name != "position" && if_name != "velocity" && if_name != "effort")
502+
{
503+
++n_states;
504+
}
505+
}
506+
}
507+
508+
for (unsigned int j = 0; j < hardware_info.sensors.size(); j++)
509+
{
510+
auto &info = hardware_info.sensors[j];
511+
512+
for (unsigned int i = 0; i < info.state_interfaces.size(); i++)
513+
{
514+
std::string if_name = info.state_interfaces[i].name;
515+
516+
if (if_name != "position" && if_name != "velocity" && if_name != "effort"
517+
&& if_name != "orientation.x" && if_name != "orientation.y" && if_name != "orientation.z"
518+
&& if_name != "orientation.w" && if_name != "angular_velocity.x" && if_name != "angular_velocity.y"
519+
&& if_name != "angular_velocity.z" && if_name != "linear_acceleration.x" && if_name != "linear_acceleration.y"
520+
&& if_name != "linear_acceleration.z" && if_name != "force.x" && if_name != "force.y" && if_name != "force.z"
521+
&& if_name != "torque.x" && if_name != "torque.y" && if_name != "torque.z")
522+
{
523+
++n_states;
524+
}
525+
}
526+
}
527+
528+
dataPtr->gazebo_topic_cmd_.resize(n_commands);
529+
dataPtr->gz_pubs_.reserve(n_commands);
530+
dataPtr->gazebo_topic_fbk_.resize(n_states);
531+
dataPtr->gz_subs_.reserve(n_states);
532+
unsigned int cc = 0;
533+
unsigned int cs = 0;
534+
535+
// STEP 2
536+
for (unsigned int j = 0; j < hardware_info.joints.size(); j++)
537+
{
538+
auto &info = hardware_info.joints[j];
477539
std::string joint_name = this->dataPtr->joint_names_[j] = info.name;
478540

479-
for (unsigned int i = 0; i < info.command_interfaces.size(); i++) {
541+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "Registering gazebo topics for joint: " << joint_name);
542+
543+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\tCommand:");
544+
545+
for (unsigned int i = 0; i < info.command_interfaces.size(); i++)
546+
{
480547
std::string if_name = info.command_interfaces[i].name;
481548
std::string initial_v = info.command_interfaces[i].initial_value;
482549

483-
if (if_name != "position" && if_name != "velocity" && if_name != "effort") {
484-
auto & ref = this->dataPtr->gazebo_topic_cmd_.emplace_back(0.0);
550+
if (if_name != "position" && if_name != "velocity" && if_name != "effort")
551+
{
552+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\t\t " << if_name);
485553

486554
this->dataPtr->command_interfaces_.emplace_back(
487-
joint_name,
488-
if_name,
489-
&ref);
555+
joint_name,
556+
if_name,
557+
&(this->dataPtr->gazebo_topic_cmd_[cc]));
490558

491-
if (!initial_v.empty()) {
492-
ref = std::stod(initial_v);
559+
if (!initial_v.empty())
560+
{
561+
this->dataPtr->gazebo_topic_cmd_[cc] = std::stod(initial_v);
493562
}
494563

495564
dataPtr->gz_pubs_.push_back(
496565
this->dataPtr->transport_nh_->Advertise<gazebo::msgs::Any>(
497566
"~/" + joint_name + "/" +
498567
if_name)
499568
);
569+
cc++;
500570
}
501571
}
502572

503-
this->dataPtr->gazebo_topic_fbk_.reserve(100);
504-
this->dataPtr->gz_subs_.reserve(100);
573+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\tState:");
505574

506575
for (unsigned int i = 0; i < info.state_interfaces.size(); i++) {
507576
std::string if_name = info.state_interfaces[i].name;
508577

509-
if (if_name != "position" && if_name != "velocity" && if_name != "effort") {
510-
this->dataPtr->gazebo_topic_fbk_.emplace_back(0.0);
511-
512-
int idx = this->dataPtr->gazebo_topic_fbk_.size() - 1;
578+
if (if_name != "position" && if_name != "velocity" && if_name != "effort"){
579+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\t\t " << if_name);
513580

514581
this->dataPtr->state_interfaces_.emplace_back(
515-
joint_name,
516-
if_name,
517-
&this->dataPtr->gazebo_topic_fbk_[idx]);
518-
582+
joint_name,
583+
if_name,
584+
&(this->dataPtr->gazebo_topic_fbk_[cs]));
585+
519586
dataPtr->gz_subs_.push_back(
520587
this->dataPtr->transport_nh_->Subscribe<gazebo::msgs::Any>(
521-
"~/" + joint_name + "/" + if_name,
522-
[this, idx](ConstAnyPtr & msg) mutable -> void {
523-
this->dataPtr->gazebo_topic_fbk_[idx] = msg->double_value();
588+
"~/" + joint_name + "/" + if_name,
589+
[this, cs](ConstAnyPtr & msg) mutable -> void {
590+
this->dataPtr->gazebo_topic_fbk_[cs] = msg->double_value();
524591
}
525592
)
526593
);
594+
cs++;
527595
}
528596
}
529597
}
@@ -532,27 +600,37 @@ void GazeboSystem::registerGazeboTopics(
532600
auto & info = hardware_info.sensors[j];
533601
std::string sensor_name = info.name;
534602

535-
for (unsigned int i = 0; i < info.state_interfaces.size(); i++) {
536-
std::string if_name = info.state_interfaces[i].name;
603+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "Registering gazebo topics for sensor: " << sensor_name);
604+
605+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\tState:");
537606

538-
if (if_name != "position" && if_name != "velocity" && if_name != "effort") {
539-
this->dataPtr->gazebo_topic_fbk_.emplace_back(0.0);
607+
for (unsigned int i = 0; i < info.state_interfaces.size(); i++)
608+
{
609+
std::string if_name = info.state_interfaces[i].name;
540610

541-
int idx = this->dataPtr->gazebo_topic_fbk_.size() - 1;
611+
if (if_name != "position" && if_name != "velocity" && if_name != "effort"
612+
&& if_name != "orientation.x" && if_name != "orientation.y" && if_name != "orientation.z"
613+
&& if_name != "orientation.w" && if_name != "angular_velocity.x" && if_name != "angular_velocity.y"
614+
&& if_name != "angular_velocity.z" && if_name != "linear_acceleration.x" && if_name != "linear_acceleration.y"
615+
&& if_name != "linear_acceleration.z" && if_name != "force.x" && if_name != "force.y" && if_name != "force.z"
616+
&& if_name != "torque.x" && if_name != "torque.y" && if_name != "torque.z")
617+
{
618+
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\t\t " << if_name);
542619

543620
this->dataPtr->state_interfaces_.emplace_back(
544-
sensor_name,
545-
if_name,
546-
&this->dataPtr->gazebo_topic_fbk_[idx]);
547-
621+
sensor_name,
622+
if_name,
623+
&(this->dataPtr->gazebo_topic_fbk_[cs]));
624+
548625
dataPtr->gz_subs_.push_back(
549626
this->dataPtr->transport_nh_->Subscribe<gazebo::msgs::Any>(
550-
"~/" + sensor_name + "/" + if_name,
551-
[this, idx](ConstAnyPtr & msg) mutable -> void {
552-
this->dataPtr->gazebo_topic_fbk_[idx] = msg->double_value();
627+
"~/" + sensor_name + "/" + if_name,
628+
[this, cs](ConstAnyPtr & msg) mutable -> void {
629+
this->dataPtr->gazebo_topic_fbk_[cs] = msg->double_value();
553630
}
554631
)
555632
);
633+
cs++;
556634
}
557635
}
558636
}
@@ -733,7 +811,6 @@ hardware_interface::return_type GazeboSystem::write(
733811
counterMsg.set_double_value(this->dataPtr->gazebo_topic_cmd_[j]);
734812
dataPtr->gz_pubs_[j]->Publish(counterMsg);
735813
}
736-
737814
this->dataPtr->last_update_sim_time_ros_ = sim_time_ros;
738815

739816
return hardware_interface::return_type::OK;

0 commit comments

Comments
 (0)