Create an array of subscribers in ROS 2
Hello. Sorry for all mistakes. English is not my native tongue. In ROS 1, I have code that looks something like this:
std::vector<ros::Subscriber> sub_sensors_front;
void EnableSensors(bool enable)
{
if (enable)
{
sub_sensors_front = {
pNodeHandle->subscribe("/minicar/sensor/front_0", 10, SensorInput),
pNodeHandle->subscribe("/minicar/sensor/front_1", 10, SensorInput),
pNodeHandle->subscribe("/minicar/sensor/front_2", 10, SensorInput),
pNodeHandle->subscribe("/minicar/sensor/front_3", 10, SensorInput),
pNodeHandle->subscribe("/minicar/sensor/front_4", 10, SensorInput),
};
}
}
It is working fine. Now I need to migrate my code to ROS 2 foxy, and I am stuck. All I could achieve looks like this, and it is not working.
std::vector<rclcpp::Subscription<sensor_msg::msg::Range>> sub_sensors_front;
void EnableSensors(bool enable)
{
if (enable)
{
sub_sensors_front = {
pNodeHandle->create_subscription<sensor_msg::msg::Range>("/minicar/sensor/front_0", 10, SensorInput),
pNodeHandle->create_subscription<sensor_msg::msg::Range>("/minicar/sensor/front_1", 10, SensorInput),
pNodeHandle->create_subscription<sensor_msg::msg::Range>("/minicar/sensor/front_2", 10, SensorInput),
pNodeHandle->create_subscription<sensor_msg::msg::Range>("/minicar/sensor/front_3", 10, SensorInput),
pNodeHandle->create_subscription<sensor_msg::msg::Range>("/minicar/sensor/front_4", 10, SensorInput),
};
}
}
VS code following error:
no operator '=' matches these operands
I would appreciate all possible help. If there is a working example, I would be very grateful for it.