# Adding a sonar Range subscriber c++

**URL:** <https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813>\
**Category:** Uncategorized\
**Created:** [April 30, 2021, 7:49pm UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813 "2021-04-30T19:49:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [April 30, 2021, 7:49pm UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/1 "2021-04-30T19:49:37Z")

</div>

So I am trying to add a subscriber to a specific node.  
The purpose of the subscriber is to get range messages from the pi\_sonar topic and use it in the code.  
This is the code here

 ![image](https://forum.ubiquityrobotics.com/uploads/default/original/1X/18288bfc090640ec4c422988fa32cb5f7bfb6364.png)  
so, if I wanted to add the sonar messages, should it look like this:

```
void turtlebot::range_sub('pacakge name of sonars'::Range msg){
        pi_sonar_x::Range = msg.Range;
 }

```

Ofcourse however, I realized there is another node which is responsible for receiving these messages which are built here then sent over to the motion node. So, what I realize I should do is I would want to have the subscriber to the range messages there, and then use them here?

questions would be:

- do I need to add the range message to the package xml file
- do I need to add the range message to the cmakelist file?
- do I need to add the messages in the messages folder? (I don’t think so based on my understanding)
- what are other things I need to look out for?

If any further clarifications are need, let me know.  
here is the actual code I am using:

> **[GitHub - sudrag/line\_follower\_turtlebot: A repo created to simulate a line following...](https://github.com/sudrag/line_follower_turtlebot)**
>
> A repo created to simulate a line following turtlebot in Gazebo environment using ROS

thanks!

---

<div class="post-metadata">

**Author:** ![MoffKalast](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/moffkalast/32/214_2.png) [@MoffKalast](https://forum.ubiquityrobotics.com/u/MoffKalast)\
**Post date:** [May 1, 2021, 12:08am UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/2 "2021-05-01T00:08:45Z")

</div>

Hmm, I think these should be of type [sensor\_msgs/Range](http://docs.ros.org/en/melodic/api/sensor_msgs/html/msg/Range.html), so you can treat them exactly the same as you do the geometry\_msgs/Twist. I’m not seeing why this would be any different.

I don’t quite get what you’re trying to do.

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 1, 2021, 1:14am UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/3 "2021-05-01T01:14:56Z")

</div>

Please see this example for C++ sonar usage. I think it answers fully your questions.

> **[GitHub - UbiquityRobotics/sonar\_detector](https://github.com/UbiquityRobotics/sonar_detector)**
>
> Contribute to UbiquityRobotics/sonar\_detector development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [May 1, 2021, 8:53am UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/4 "2021-05-01T08:53:10Z")

</div>

Thank you for your response!

So to clarify, what I am trying to do is I want to use the sonar messages in my code alongside the direction messages, which are generated by the package.

So, I would just want to make the robot check the ranges, and then check the direction. basically a nested if.  
So, I am just like trying to see how do I add to the node those messages to be read and used.

---

<div class="post-metadata">

**Author:** ![MoffKalast](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/moffkalast/32/214_2.png) [@MoffKalast](https://forum.ubiquityrobotics.com/u/MoffKalast)\
**Post date:** [May 1, 2021, 10:52am UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/5 "2021-05-01T10:52:50Z")

</div>

Ah I see what you mean, in that case you have three options depending on which message you want to parse quicker:

### Store twist, wait for sonar:

Create a class variable to store the Twist message, then in the vel\_cmd callback only do a `stored_twist = msg;` or something like that.

Afterwards in the sonar callback node you can then always check the `stored_twist` and process it along with the sonar data. The `stored_twist` will get updated every time and the sonar message will work with the latest version received.

Since roscpp processes callbacks single threaded (by default) you have no danger of the `vel_cmd` changing the `stored_twist` while you’re working on it in the `range_sub`. Still, I’d make a deep copy at the very first line before using it just to be safe in case that ever changes.

### Store sonar, wait for twist:

This is probably the better option, since sonar messages should arrive at a lower frequency, but the principle is the same.

You make a variable to store the Range message, something like `stored_range = msg;` and then access it in the `vel_cmd` callback when processing.

### Process at a fixed frequency

The third option is to store both the range and twist message in each callback and do nothing else, so that you have the latest messages stored.

Then you can process both of them at the same time in `int main`, where it’s doing the spins:

```
ros::Rate r(20);
while (ros::ok())
{
	ros::spinOnce(); //callbacks update the stored vars here in theory

	geometry_msgs::Twist latest_twist;
	latest_twist = yourclass.stored_twist;
	
	sensor_msgs::Range latest_range;
	latest_range = yourclass.stored_range;
	
	//TODO process data using both 

	r.sleep();
}

```

This won’t react to a brand new message until the rate timer comes around of course, but it’ll be giving updates at a constant frequency which is sometimes better. It’s a more “game loop” style approach.

Of course this one will run even if no new data is received from both callbacks, so if that’s something you need I’d add a boolean “has\_changed” that gets set to true by the callbacks and to false by the loop, and the loop only processes if the value is true.

Anyhow my cpp is a bit rusty, I mainly write python these days so take that as more of a pseudocode.

---

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [May 1, 2021, 11:11am UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/6 "2021-05-01T11:11:29Z")

</div>

So, should be something like this, based on what I understood, to store the range value, it would be something like this:

 ![image](https://forum.ubiquityrobotics.com/uploads/default/original/1X/b12a52c6934fe322653a0ebbad17203d7f6c07a0.png)  
here I think the direction is stored in the callback function and is being used in the vel\_cmd function

Is this what I should do for the Sonar messages?

---

<div class="post-metadata">

**Author:** ![MoffKalast](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/moffkalast/32/214_2.png) [@MoffKalast](https://forum.ubiquityrobotics.com/u/MoffKalast)\
**Post date:** [May 1, 2021, 11:30am UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/7 "2021-05-01T11:30:32Z")

</div>

Yep, that should work I think.

---

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [May 1, 2021, 4:14pm UTC](https://forum.ubiquityrobotics.com/t/adding-a-sonar-range-subscriber-c/813/8 "2021-05-01T16:14:20Z")

</div>

Thank you so much!! I got it to work eventually. I’ll post later about this project for anyone who wants to build this project as well!
