Topics Quiz Autograder: not subscribed to topic /scan

Hi,

I am creating a single node that is both a producer and a subscriber. The autograder is telling me that I am not subscribed to the /scan subscription, but I am. I have also double checked the name of my node. Could someone give me a tip as to where I am going wrong? I do not wish to expend any more attempts… Code is below. Thanks in advance!

#include “geometry_msgs/msg/twist.hpp”
#include “nav_msgs/msg/odometry.hpp”
#include “rclcpp/rclcpp.hpp”
#include “rclcpp/subscription.hpp”
#include “sensor_msgs/msg/laser_scan.hpp”
#include
using std::placeholders::1;
using namespace std::chrono_literals;
class TopicsQuiz : public rclcpp::Node {
public:
TopicsQuiz() : Node(“topics_quiz_node”) {
publisher
=
this->create_publisher<geometry_msgs::msg::Twist>(“cmd_vel”, 10);
subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>(
“scan”, 10, std::bind(&TopicsQuiz::topic_callback, this, 1));
timer
= this->create_wall_timer(
500ms, std::bind(&TopicsQuiz::timer_callback, this));
}

private:
void topic_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg) {
msg_ = *msg;
}
void timer_callback() {
auto toPublish = geometry_msgs::msg::Twist();
auto left = msg_.ranges[0];
for (int i = 0; i < (int)(msg_.ranges.size() / 3); i++) {
if (msg_.ranges[i] < left) {
left = msg_.ranges[i];
}
}
auto forward = msg_.ranges[(int)(msg_.ranges.size() / 3)];
for (int i = (int)(msg_.ranges.size() / 3);
i < 2 * (int)(msg_.ranges.size() / 3); i++) {
if (msg_.ranges[i] < forward) {
forward = msg_.ranges[i];
}
}
auto right = msg_.ranges[msg_.ranges.size() - 1];
for (int i = 2 * (int)(msg_.ranges.size() / 3);
i < (int)(msg_.ranges.size() - 1); i++) {
if (msg_.ranges[i] < right) {
right = msg_.ranges[i];
}
}
printf(“Left: %f\nRight: %f\nForward: %f\n--------------\n”, left, right,
forward);
if (forward > 1) {
toPublish.linear.x = 0.5f;
} else {
toPublish.linear.x = 0.0f;
toPublish.angular.z = 0.5f;
}
if (left < 1) {
toPublish.angular.z = -0.5f;
}
if (right < 1 && toPublish.angular.z == 0.0f) {
toPublish.angular.z = 0.5f;
}
publisher_->publish(toPublish);
}
sensor_msgs::msg::LaserScan msg_;
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr subscription_;
};

int main(int argc, char *argv) {
rclcpp::init(argc, argv);
// Create a loop that will keep the program in execution
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}

The problem was that after launching your quiz package,

ros2 launch topics_quiz topics_quiz.launch.py

and trying to check information about the /scan topic, the grader got the follow error:

ros2 topic info /scan -v
Unknown topic '/scan'

Please check that on your end.

  • Is the subscriber node started by that node?
  • Is the topic name correct?

Hi, when I run the launch command and then in another terminal perform the second command, I get this result. My node is visible by the looks of it

user:~$ ros2 topic info /scan -vType: sensor_msgs/msg/LaserScan
Publisher count: 1

Node name: lidar_1
Node namespace: /
Topic type: sensor_msgs/msg/LaserScan
Endpoint type: PUBLISHER
GID: 01.0f.9c.34.12.01.6a.bc.01.00.00.00.00.00.3b.03.00.00.00.00.00.00.00.00
QoS profile:
Reliability: RELIABLE
History (Depth): UNKNOWN
Durability: VOLATILE
Lifespan: Infinite
Deadline: Infinite
Liveliness: AUTOMATIC
Liveliness lease duration: Infinite

Subscription count: 1

Node name: topics_quiz_node
Node namespace: /
Topic type: sensor_msgs/msg/LaserScan
Endpoint type: SUBSCRIPTION
GID: 01.0f.a7.3c.b0.03.03.a9.01.00.00.00.00.00.13.04.00.00.00.00.00.00.00.00
QoS profile:
Reliability: RELIABLE
History (Depth): UNKNOWN
Durability: VOLATILE
Lifespan: Infinite
Deadline: Infinite
Liveliness: AUTOMATIC
Liveliness lease duration: Infinite

Then I suggest you try again and let’s see. It’s possible you corrected the code after the last submission.

I have not changed my code since I started this thread, but upon running it a third time, it works. Now, it is saying that I am not publishing to the /cmd_vel topic, but I believe I am. Upon running the following command, I get the following output which shows that my node is publishing to the /cmd_vel topic. I also checked the output of the /cmd_vel topic, and the data is being written to (and the robot is moving). I am hesitant to use a 4th attempt since I only have 5.

user:~$ ros2 node info /topics_quiz_node
/topics_quiz_node
Subscribers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/scan: sensor_msgs/msg/LaserScan
Publishers:
/cmd_vel: geometry_msgs/msg/Twist
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
Service Servers:
/topics_quiz_node/describe_parameters: rcl_interfaces/srv/DescribeParameters
/topics_quiz_node/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
/topics_quiz_node/get_parameters: rcl_interfaces/srv/GetParameters
/topics_quiz_node/list_parameters: rcl_interfaces/srv/ListParameters
/topics_quiz_node/set_parameters: rcl_interfaces/srv/SetParameters
/topics_quiz_node/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
Service Clients:

Action Servers:

Action Clients:

I can’t say when the change happened, but gradebot does not change the code. I’m glad it worked!

You’re publishing to the /cmd_vel right, but under what node name? I see moving_service in the logs, and it should be topics_quiz_node.

To check the node name your package starts, run

# Terminal 1
ros2 launch topics_quiz topics_quiz.launch.py

# Terminal 2
ros2 topic info /scan -v

Hint: the node name specified in the launch file overrides the one specified in the code.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.