Robotics StackExchange | Archived questions

subscriber's callback in C++

I want to extract data from a topic in which ticks from encoders are published but i dont know the syntax for this process

So for example i have written this callback :

void left_ecn_cb(const std_msgs::UInt16::ConstPtr& lTick_msg)
{
  // here i need to store the values coming from topic 
 // into the variable lTick  
}

which is the proper syntax to do that ?

Also is there any book or pdf or web page which using a lot of examples to learn c++ and ros

Asked by Kostas Tzias on 2022-09-29 14:41:52 UTC

Comments

Answers

which is the proper syntax to do that ?

void left_ecn_cb(const std_msgs::msg::UInt16::ConstPtr& ltick_msg) {
  auto ltick = ltick_msg->data;
}

Also is there any book or pdf or web page which using a lot of examples to learn c++ and ros

A good place to start with tons of examples is the official documentation page. However, if you are interested in more structured learning then maybe look at TheConstruct. There are some books out there but I don't know if they are any good.

Asked by JustinBlack02 on 2022-09-29 21:39:25 UTC

Comments

The callback prototype also seems to suggest ROS 1. The example code you show appears to be ROS 2.

The OP labelled his question with noetic, which would be ROS 1.

The book link also appears to link to a ROS 2 book.

Asked by gvdhoorn on 2022-09-30 00:52:55 UTC

Could you explain to me what auto does . I used this syntax without auto and it worked perfectly . Is auto important ? Do i have to add it too ? Also i am doing this in ROS1

Asked by Kostas Tzias on 2022-10-03 04:07:20 UTC