ROS Q&A | Publish and subscribe array of vector as message


Q: Hi all, I am trying to send an array of vector points from one node to another node as a message. The message contains this array and other message fields as well.

My cpp file has the following array vector:

Vector Point distancePoints[11];
The above array of vector is the one I need to publish and subscribe.

For example, distancePoints[0] contains vector Point from distance 1m and so on. Point contains x and y coordinates. Point is an opencv 2D point class.

Is it possible to publish and receive such array of vector using ROS messages? Does anyone have an idea about how it can be done? What will be the structure of message header?

A: Hello,

I’ve created a new message to do that, including an array and another field, just to show how you can do that.

[ROS Q&A] How to convert quaternions to Euler angles

[ROS Q&A] How to convert quaternions to Euler angles

About

In this tutorial, we are going to answer a question found at ROS answers – How to convert quaternions to Euler angles?

We’ll explain this with the following example in ROS Development Studio (ROSDS), where you can easily follow the steps and understand how to use the conversion from quaternions provided by an Odometry message to Euler angles (Roll, Pitch, and Yaw).


Step1. Create a Project in RDS

If you haven’t had an account yet, register here for free. 

We’ll use a simulation we built previously, you can find some information here and build it in 5 mins with RDS.

Launch the Turtlebot 3 empty world simulation with the following command

$ roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch


Step2. Create Package

We then create another package called my_quaternion_pkg for this example  under the ~/catkin_ws/src using

$ catkin_create_pkg my_quaternion_pkg rospy

Create a quaternion_to_euler.py file under my_quaternion_pkg. Now the source tree may look like the following picture


Step3. Transform Quaternion to Euler

As our first attempt, copy the following code into the quaternion_to_euler.py file

#!/usr/bin/env python
import rospy
from nav_msgs.msg import Odometry

def get_rotation (msg):
    print msg.pose.pose.orientation

rospy.init_node('my_quaternion_to_euler')

sub = rospy.Subscriber ('/odom', Odometry, get_rotation)

r = rospy.Rate(1)
while not rospy.is_shutdown():
    r.sleep()

To run the file, simply type

$ rosrun my_quaternion_pkg quaternion_to_euler.py

Now you can see the code prints the odometry message in quaternion format. Now, we’d like to transform it to Euler angles. We use the euler_from_quaternion function provided by tf.transformations, you can find more detail here

#!/usr/bin/env python
import rospy
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion, quaternion_from_euler

def get_rotation (msg):
    global roll, pitch, yaw
    orientation_q = msg.pose.pose.orientation
    orientation_list = [orientation_q.x, orientation_q.y, orientation_q.z, orientation_q.w]
    (roll, pitch, yaw) = euler_from_quaternion (orientation_list)
    print yaw

rospy.init_node('my_quaternion_to_euler')

sub = rospy.Subscriber ('/odom', Odometry, get_rotation)

r = rospy.Rate(1)
while not rospy.is_shutdown():
    r.sleep()

We only print yaw values here. You can check it by typing the following command to control the Turtlebot.

$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch

If you turn the robot, you’ll see the yaw angle change accordingly.


Step4. Transform Euler to Quaternion

Similarly, we can use the quaternion_from_euler function provided by tf.transformations to transform the Euler back to the quaternion with the following code.

#!/usr/bin/env python
import rospy
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion, quaternion_from_euler

roll = pitch = yaw = 0.0

def get_rotation (msg):
    global roll, pitch, yaw
    orientation_q = msg.pose.pose.orientation
    orientation_list = [orientation_q.x, orientation_q.y, orientation_q.z, orientation_q.w]
    (roll, pitch, yaw) = euler_from_quaternion (orientation_list)
    print yaw

rospy.init_node('my_quaternion_to_euler')

sub = rospy.Subscriber ('/odom', Odometry, get_rotation)

r = rospy.Rate(1)
while not rospy.is_shutdown():
    quat = quaternion_from_euler (roll, pitch,yaw)
    print quat
    r.sleep()

The quaternion will print every 1 second since we specified the rate for 1 Hz and the odometry topic is published in 100 Hz.


Takeaway Today:

You can transform between Euler and quaternion using the quaternion_from_euler and euler_from_quaternion function provided by the tf.transform.

 

Edit by Tony Huang

Related Courses

TF ROS Course Cover - ROS Online Courses - Robot Ignite Academy

TF ROS Course

ROS for Beginners (Python)

MASTERING WITH ROS: TurtleBot 3 (Python)

ROS Q&A | How to retrieve the robot name from the URDF file?

ROS Q&A | How to retrieve the robot name from the URDF file?

 

Q: How to retrieve the robot name from the URDF file?

A: This can be accomplished by loading a URDF file, parsing it and calling the getName() method of the C++ Urdf model.

To see how to load and parse a URDF file, you can follow the Parse a urdf file tutorial  (http://wiki.ros.org/urdf/Tutorials/Parse%20a%20urdf%20file).

After compile the code and generate the executable called “parser” following the “Parse a urdf file” tutorial, you can call it with the path of the URDF file as argument. Something like: rosrun your_package_name parser /path/to/your/file.urdf

ROS Q&A | Use different message types

ROS Q&A | Use different message types

 

Learn how to detect the message type and create multiple input message type functions: https://answers.ros.org/question/60281/how-to-get-massage-type-in-rospy/

Q:How to get massage type in rospy?
A:The cleanest way is to use: `msg._type`

Here I leave an example of two susbcribers in ros using the same callback. The callback called **common_callback** will process the messages differently depending on their type. In this case it processes type LaserScan or Odometry, but this can be used for anything really because the output of the `msg._type` is `sensor_msgs/LaserScan`or `nav_msgs/Odometry`. So its easy too work with that.

example code in this the ROSAnswer: https://answers.ros.org/question/60281/how-to-get-massage-type-in-rospy/

Hope this helps ;).

ROS Q&A | How to know the Pose of a robot (Python) ?

ROS Q&A | How to know the Pose of a robot (Python) ?

 

Here you have an example of how to read the Pose of a robot in Python, answering a question made in ROS Answers

Q: Hello ! I want to know the pose of turtlebot (x,y,z, z rotation) respect to the point from which it started. What is the best way to do that in python?

A:Hello! As you suggest in your last message, the best practice for doing that is to use the odom topic. For that, you can create a simple subscriber in Python that gets the data you need, which is, position and orientation respect to an starting point. The code for a simple subscriber could be like this:

#! /usr/bin/env python

import rospy
from nav_msgs.msg import Odometry

def callback(msg):
    print msg.pose.pose

rospy.init_node('check_odometry')
odom_sub = rospy.Subscriber('/odom', Odometry, callback)
rospy.spin()

This code basically subscribes to the odom topic, and prints the pose component of the message, which contains the position and orientation values.

 

The following steps take throught the development as shown in the video.

We can check the names of all the published topics with the following command (Enter in a Shell)

$ rostopic list

Check for /odom topic in the output of the command. We can now check the information being published on this topic using the following command

$ rostopic echo /odom -n1

The output of the above command should be as shown in the image below (x0x≈0, y0y≈0)

  • Next we will create a subscriber. Use the following commands to create a new package and a subscriber script file.

cd ~/catkin_ws/src catkin_create_pkg check_odometry cd check_odometry mkdir src launch

  • Now we will create a script named inside the check_odometry/src/ directory. Add the following code to the script file
#!/usr/bin/env python

import rospy
from nav_msgs.msg import Odometry

def callback(msg):
    print(msg.pose.pose)
    
rospy.init_node('check_odometry')
odom_sub = rospy.Subscriber('/odom', Odometry, callback)
rospy.spin()
  • We will now create a launch file with name inside the check_odometry/launch/ directory with the following content
<launch>
    <node pkg="check_odometry" type="check_odom.py" name="check_odometry" output="screen" />
</launch>
  • Lets launch this launch file with the following command in SHELL

$ roslaunch check_odometry check_odom.launch

The above command should start publishing a stream of Odometry messages (the pose part) as shown in this image

  • Now let us move the robot slightly using the keyboard_teleop and observe the /odom message

$ roslaunch turtlebot_teleop keyboard_teleop.launch

  • Now we can start the launch file again see the change in the location of the robot

This finishes the steps in the Video tutorial.

Worldwide ROS Events

Worldwide ROS Events

This article shares all Non-profit ROS Evens around the world to promote the exchange ideas and communication between ROS learners.

 

The largest and most active ROS Meetups in the world


1. Silicon Valley Robotics

ros-meetups-group-1

Host City: Silicon Valley, San Francisco

About: Silicon Valley Robotics is a non-profit business association that promotes the innovation and commercialization of robotics technologies. They hold an event every two weeks. You can share your robot or robotics-related project with the robotics students at Silicon Valley CTE, offer your ideas to robotics startups or get the opportunity to be hired by a robotics-related company.

 

2. Robot Developer Group 

ros-meetups-groups-2

Host City: San Francisco, CA

About: Their sessions will showcase the latest in new robot platforms, software, hardware and sensor technologies.  A community dedicated to moving the robotics industry forward by bringing the best robotics minds in the area together to network, brainstorm, collaborate, and take real action to execute ideas and visions.

 

3. Learn/Teach ROS Fast

ros-meetups-groups-3

Host City: Barcelona, Kontich (Belgium), Vancouver, Murrieta (USA), Cambridge, Punjab (India), Cairo (Egypt)…more than 30 cities

About: A ROS learning community for students or teachers or anybody who interested in learn or teach ROS in an effective way.  In their events you can to meet other ROS enthusiasts, join together to learn how to smooth the ROS learning curve and start ROS project quickly.

Meetups topics include: ROS Navigation, ROS introduction, Using OpenAI with ROS , Self-driving cars using ROS, RPS-Industrial, Apply ROS to real robots, etc.

 

4. ROS in Chicago 

Host City: Chicago (University of Illinois – Chicago campus)

About: A group for people interested in making robots who use/want to use ROS (Robotic Operating System) in the Chicago-land area. Topic will be covered from software development to hardware implementation, as well as all aspects in artificial intelligence

 

5. Vancouver ROS User Group

Host City: Vancouver

About: A ROS group in Vancouver which is open to anyone interested in using ROS to design and develop robots for real world applications. Their prime focus is on collaborative service robots that can safety interact with humans and share what they have learned.

Meetups topics include: Sensors, Intro to Robot Simulators, Drones, Application of Computer Vision, Robot mapping using SLAM, etc.

 

6. Tokyo ROS Meetup 

Host City: Tokyo

About: This group is for people interested in robotics who want to use and learn more about ROS and is open to all interested and will try to strengthen the network between the international and local ROS community.

 

7. ROS on the Rocks

Host City: Boulder, Loveland

About: ROS Users Group in the Rocky Mountain region. The group will focus specifically on ROS, and ROS-Industrial.

Meetups topcis include: Introduction to ROS through a Robotic Manipulator

 

8. ROS Seattle Meetup 

Host City: Seattle

About: This is a bi-monthly user group for developers learning or using the ROS. Most meetings consist of community show and tell and a featured talk.

 

 

Other Events


 

About: Learn a method to teach ROS fast with no hassle. The aim of this webinar is to show you how to change your classes from passive listening to active practising.

 

About: A webinar series designed to help you equip your team with ROS fast.

 

About: ROS Japan Users Google Group. (Group email: ros-japan-users@googlegroups.com) They will hold ROS Events from time to time.

 

About:The seminar covered overview for introduction, navigation, moveit, UAV, and community briefly by speakers from various groups in Korea. (*This is a past event, but you can still find the presentation slides and other useful ROS materials on their website.)

Pin It on Pinterest