Difficulty with Exercise 2.2


I am struggling with exercise 2.2. I am asked to modify the code from exercise 2.1 so that it now publishes data to the /cmd_vel topic. How would I go about starting this as I cant seem to make any progress with it. Sorry if this is quite simple!

Thanks in advance

Hey @hayatua!

The hints under the heading Data for Excercice 2.2 is the best place to start! Here is summarizing what you need to do (and leaving you to do it :wink:):

  • You need to publish to another topic. The code was publishing to /counter but now it needs to publish to /cmd_vel.
ros::Publisher pub = nh.advertise<std_msgs::Int32>("counter", 1000);
  • You need to use a different message for the /cmd_vel topic. As they say, different strokes for different folks…The challenge here is you need to (similar to what we have in the code block below):
    • Figure out the kind of message /cmd_vel accepts.
    • Include it in place of <std_msgs/Int32.h>
    • Create and initialize a message of that type, setting the right parameters.
    • Publish the message.
    std_msgs::Int32 count; // Create a variable of type Int32
    count.data = 0; // Initialize 'count' variable
    while (ros::ok()) // Create a loop that will go until someone stops the program execution
        pub.publish(count); // Publish the message within the 'count' variable
       // ...

I hope this helps.

By the way, welcome to the Community!

1 Like

Thanks for the tips! Hopefully I’ll manage to figure it out now :slight_smile:

1 Like

2 posts were split to a new topic: Exercise 2.2: /cmd_vel does not have the expected message type