[ROS Q&A] 133 – How to get the position and angular acceleration of a robot?

In this video we are going to see how can we subscribe to topics so that we can get the position and acceleration of a Sphero robot.

This video is especially good for beginners who have already understood the basics, and want to start writing their own code. We will try to properly structure our Python code, as well as follow some programming good practices.

This is a video based on the following post on The Construct’s Forum: http://forum.theconstructsim.com/questions/1188/print-position-angular-acceleration

// RELATED LINKS

▸ Original question: http://forum.theconstructsim.com/questions/1188/print-position-angular-acceleration
ROS Development Studio (ROSDS)
Robot Ignite Academy

Step 1. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s create a new project and call it Print_Position_QA.

Step 2. Create a package

To reproduce the question, we’ll use the original code from the forum. Let’s create a package for the code first with the following command.

cd ~/catkin_ws/src
catkin_create package print_pos rospy

Then we create a script file called main.py and put it in the scripts folder under the print_pos package with the following content from the forum.

! /usr/bin/env python
import rospy

from geometry_msgs.msg import Twist

from nav_msgs.msg import Odometry

from sensor_msgs.msg import Imu

rospy.init_node('sphero')

pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1) #topic publisher that allows you to move the sphero

rate = rospy.Rate(0.5)

def odom (msg):

go = Odometry()

print "pose x = " + str(go.pose.pose.position.x)

print "pose y = " + str(go.pose.pose.position.y)

print "orientacion x = " + str(go.pose.pose.orientation.x)

print "orientacion y = " + str(go.pose.pose.orientation.y)

rate.sleep()

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

def imu (msg):

allez = Imu()

print "veloc angular z = " + str(allez.angular_velocity.z)

print "veloc angular y = " + str(allez.angular_velocity.y)

print "aceleracion linear x = " + str(allez.linear_acceleration.x)

print "aceleracion linear y = " + str(allez.linear_acceleration.y)

rate.sleep()

sub = rospy.Subscriber('sphero/imu/data3', Imu, imu)

def twist (msg):

move = Twist()

print "velocidad linear x = " + str(move.linear.x)

print "velocidad angular z = " + str (move.angular.z)

rate.sleep()

sub=rospy.Subscriber('cmd_vel', Twist, twist)

while not rospy.is_shutdown():

move = Twist()

move.linear.x = 2

move.angular.z= 0.5

pub.publish(move)

rospy.spin()

Some part of the code is not correct(e.g. the subscriber is not subscribing correctly so there is no reading), let’s correct it as follows.

#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from sensor_msgs.msg import Imu

def odom_callback(msg):
    # go = Odometry() is not needed
    print "------------------------------------------------"
    print "pose x = " + str(msg.pose.pose.position.x)
    print "pose y = " + str(msg.pose.pose.position.y)
    print "orientacion x = " + str(msg.pose.pose.orientation.x)
    print "orientacion y = " + str(msg.pose.pose.orientation.y)
    rate.sleep()

def imu_callback(msg):
    # allez = Imu()
    print "------------------------------------------------"
    print "veloc angular z = " + str(msg.angular_velocity.z)
    print "veloc angular y = " + str(msg.angular_velocity.y)
    print "aceleracion linear x = " + str(msg.linear_acceleration.x)
    print "aceleracion linear y = " + str(msg.linear_acceleration.y)
    rate.sleep()

def twist (msg):
    # move = Twist()
    print "velocidad linear x = " + str(move.linear.x)
    print "velocidad angular z = " + str (move.angular.z)
    rate.sleep()
    #sub=rospy.Subscriber('cmd_vel', Twist, twist)

rospy.init_node('sphero_monitor') # the original name sphero might be the same as other node.
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1) #topic publisher that allows you to move the sphero
sub_odom = rospy.Subscriber('/odom', Odometry, odom_callback) # the original name odom might be the same as other function.
sub_imu = rospy.Subscriber('/sphero/imu/data3', Imu, imu_callback)
rate = rospy.Rate(0.5)

while not rospy.is_shutdown():
    move = Twist()
    move.linear.x = 0.1 # m/s. The original value 2 is too large
    move.angular.z= 0.5 # rad/s
    pub.publish(move)
    rate.sleep() # Instead of using rospy.spin(), we should use rate.sleep because we are in a loop

Before executing the code, we have to give it permission first with chmod +x main.py  and we also need the simulation(start it from Simulations->Sphero).

Then we can run the code with rosrun print_pos main.py

You should see the readings from sensors and the robot also starts to move around.

If you are interested in this topic, please check our ROS Basics in 5 Days course. You’ll learn how to control the Sphero robot to exit the maze in this course.

 

Edit by: Tony Huang


Feedback

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

[ROS Q&A] 131 – Compilation error in ROS Action Server

[ROS Q&A] 131 – Compilation error in ROS Action Server

 

In this video we are going to see why a user gets a compilation error when trying to compile his ROS Action Server node.

This is a video trying to answer the following question posted at the ROS answers forum: https://answers.ros.org/question/294052/compilation-error-in-ros-action-server-code/

Step 1. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s create a new project and call it testing_action.

Step 2. Create package

At first, let’s create the package with dependencies

cd ~/catkin_ws/src
catkin_create_pkg test_action roscpp actionlib actionlib_msgs robot_calibration_msgs

Then we create a file called action_code.cpp under test_action/src folder with the following content from the question

#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include <robot_calibration_msgs/GripperLedCommandAction.h>

typedef actionlib::SimpleActionServer<robot_calibration_msgs::GripperLedCommandAction> led_actn_srvr_t;

class led_action
{

ros::NodeHandle nh;
led_actn_srvr_t ls;
std::string action_name;
public:

led_action(std::string name):
    ls(nh, name, boost::bind(&led_action::execute_cb, this, _1), false),
    action_name(name)
{
    ls.start();
}
void execute_cb(const robot_calibration_msgs::GripperLedCommandActionConstPtr &action)
{
}
};

int main(int argc, char** argv)
{
    return 0;
}

To compile the code, we have to add the following code into BUILD part of the CMakeLists.txt file

add_executable(action_code src_action_code.cpp)
add_dependencies(action_code ${action_code_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(action_code
  ${catkin_LIBRARIES}
)

If we try to compile the code with the following command

cd ~/catkin_ws
catkin_make

We got some error.

Step 3. Solve the problem

It turns out the actionlib generates topics for us in the following format

GripperLedCommandAction

But inside the code, we have to directly use

GripperLedCommand…

So we only have to change one line in the source file to make it work

...
void execute_cb(const robot_calibration_msgs::GripperLedCommandGoalConstPtr &action)
...

Then it compiles successfully!

 

 

Edit by: Tony Huang

// RELATED LINKS

▸ Original question: https://answers.ros.org/question/294052/compilation-error-in-ros-action-server-code/
Robot Ignite Academy
ROS Basics in 5 days (C++)
ROS Development Studio (RDS)


Feedback

Did you like this video? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

ROS Developers LIVE-Class #24:  How to create basic markers in ROS Rviz

ROS Developers LIVE-Class #24: How to create basic markers in ROS Rviz

 

In this ROS LIVE-Class we’re going to learn how to create a marker to be displayed in ROS Rviz. Markers are special graphics that we can use in Rviz to display data generated by the robot. For example, we can create a marker that shows a person figure at the proper distance of the robot when the robot detects a person. By using markers, we can debug and understand better what the robot program is doing. Markers are basic tools for robot program debug!

We will see:

  •  How to get the information we want to display from a ROS program
  • How to create a custom marker that contains that information
  •  How to display the marker on the Rviz
  •  How to build a mesh marker and show it on Rviz

 

Every Tuesday at 18:00 CET/CEST.

This is a LIVE Class on how to develop with ROS. In Live Classes you practice with me at the same time that I explain, with the provided free ROS material.

IMPORTANT: Remember to be on time for the class because at the beginning of the class we will share the code with the attendants.

IMPORTANT 2: in order to start practicing quickly, we are using the ROS Development Studio for doing the practice. You will need a free account to attend the class. Go to http://rds.theconstructsim.com and create an account prior to the class.

// RELATED LINKS

[ROS Q&A] 130 – How to launch multiple robots in Gazebo simulator?

 

In this video, we are going to see how can we launch multiple robots in a single Gazebo simulation.

We will be introduced to the concept of namespace and tf_prefix, which are essential to make sure that the robots will be able to work correctly. We will create a series of launch files that will enable us to easily add robots into our Gazebo simulation.

This is a video based on the following post on ROS Answers: https://answers.ros.org/question/41433/multiple-robots-simulation-and-navigation/

NOTICE: most of the code would be the same in the ROS Answer, however, we’ll make some change to make sure it is compatible with ROS kinetic. Please use the code below.

Step 1. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here.

Step 2. Create a package

We’ll create a package to put our source code for the project with the following command

cd ~/catkin_ws/src
catkin_create_pkg multi_robot rospy gazebo_ros

Then we’ll create a folder called launch under the package directory and create 3 launch files that we need.

<launch>
    <arg name="robot_name"/>
    <arg name="init_pose"/>

    <node name="spawn_minibot_model" pkg="gazebo_ros" type="spawn_model"
     args="$(arg init_pose) -urdf -param /robot_description -model $(arg robot_name)"
     respawn="false" output="screen" />

    <node pkg="robot_state_publisher" type="state_publisher" 
          name="robot_state_publisher" output="screen"/>

    <!-- The odometry estimator, throttling, fake laser etc. go here -->
    <!-- All the stuff as from usual robot launch file -->
</launch>

This launch file will launch one robot in the simulation.

<launch>
  <!-- No namespace here as we will share this description. 
       Access with slash at the beginning -->
  <param name="robot_description"
    command="$(find xacro)/xacro.py $(find turtlebot_description)/robots/kobuki_hexagons_asus_xtion_pro.urdf.xacro" />

  <!-- BEGIN ROBOT 1-->
  <group ns="robot1">
    <param name="tf_prefix" value="robot1_tf" />
    <include file="$(find multi_robot)/launch/one_robot.launch" >
      <arg name="init_pose" value="-x 1 -y 1 -z 0" />
      <arg name="robot_name"  value="Robot1" />
    </include>
  </group>

  <!-- BEGIN ROBOT 2-->
  <group ns="robot2">
    <param name="tf_prefix" value="robot2_tf" />
    <include file="$(find multi_robot)/launch/one_robot.launch" >
      <arg name="init_pose" value="-x -1 -y 1 -z 0" />
      <arg name="robot_name"  value="Robot2" />
    </include>
  </group>
</launch>

Please notice that it should have a different namespace and tf_prefix for each robot.

<launch>
  <param name="/use_sim_time" value="true" />

  <!-- start world -->
  <node name="gazebo" pkg="gazebo_ros" type="gazebo" 
   args="$(find turtlebot_gazebo)/worlds/empty_wall.world" respawn="false" output="screen" />

  <!-- start gui -->
  <!-- <node name="gazebo_gui" pkg="gazebo" type="gui" respawn="false" output="screen"/> -->

  <!-- include our robots -->
  <include file="$(find multi_robot)/launch/robots.launch"/>
</launch>

You can launch the simulation with the following command

roslaunch multi_robot main.launch

Then you have to open the gazebo window from Tools->Gazebo

You should see 2 robots are spawned in the simulation. You can spawn more by changing the robots.launch if you want.

Step 3. Move the robot

Open another terminal, if you type rostopic list , you’ll see there are 2 cmd_vel topics for the robots. The simplest way to control them is with the teleop_twist. We can run it and remap the cmd_vel with the robot you want to control. For example

rosrun teleop_twist_keyboard teleop_twist_keyboard.py /cmd_vel:=/robot1/cmd_vel

Now you can control the robot you want.

 

Edit by: Tony Huang

 

 

// RELATED LINKS

▸ Original question: https://answers.ros.org/question/41433/multiple-robots-simulation-and-navigation/
ROS Development Studio (RDS)
Robot Ignite Academy


Feedback

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

[ROS Q&A] 128 – Can’t access messages of the ROS topic Publisher

[ROS Q&A] 128 – Can’t access messages of the ROS topic Publisher

In this video we are going to see why a user can’t access the message that he is publishing into a topic using a ROS Publisher.

This is a video trying to answer the following question posted at the ROS answers forum: https://answers.ros.org/question/293898/init_node-need-locating-after-publisher/

Step 1. Create a project in ROS Development Studio(ROSDS)

We can do any ROS development we want easily, without setting up environment locally in ROSDS and that’s the reason why we will use ROSDS for this tutorial. If you haven’t had an account yet. You can create one here for free now. After logging in, let’s begin our journey by clicking on the create new project and call it pub_example.

Step 2. Create a new package for testing

Now, let’s create a new package for testing with the following command

cd ~/catkin_ws/src/
catkin_create_pkg pub_example rospy

Then we create a new file called pub_ex.py under /pub_example/src with copying and pasting the following code.

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    rospy.init_node('talker')
    pub = rospy.Publisher('chatter', String, queue_size=10)    
    hello_str = "hello world %s" % rospy.get_time()
    pub.publish(hello_str)
    rospy.loginfo(hello_str)

if __name__ == '__main__':
    talker()

NOTICE: We no longer run ROS master for you in the backend, please launch it with the command roscore in a new terminal.

Now you can run it with the command rosrun pub_example pub_ex.py

You will get the output hello world, however, if you check with rostopic list , there is no chatter topic.

It turns out the talker function only publish once and was killed. If we want to access it, we have to keep publishing it. Let’s change our code.

def talker():
    rospy.init_node('talker')
    rate = rospy.Rate(1) # this defines the publish rate
    pub = rospy.Publisher('chatter', String, queue_size=10)    
    hello_str = "hello world %s" % rospy.get_time()
    while not rospy.is_shutdown(): 
        # put everything in a while loop to keep publishiing topic
        pub.publish(hello_str)
        rospy.loginfo(hello_str)
        rate.sleep() # this make sure that the topic is publishing at 1 Hz

When you run it again, you can see that the topic is publishing every 1 second and you can access it from other node.

 

 

Edit by: Tony Huang

 

RELATED LINKS & RESOURCES

▸ Original question: https://answers.ros.org/question/293898/init_node-need-locating-after-publisher/
Robot Ignite Academy
ROS Basics in 5 days Course (Python)
ROS Development Studio (RDS)

 


If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

[ROS IN 5 MIN] 011 – What is catkin_make & how do you use it?

[ROS IN 5 MIN] 011 – What is catkin_make & how do you use it?

Hello ROS Developers! In this ROS in 5 min tutorial, we’re going to see what catkin_make is, how it works, and what its advantages are.

For that, we are going to use Robot Ignite Academy.

But before we start, if you are new to ROS and want to Learn ROS fast, I recommend that you take any of the following courses in Robot Ignite Academy:

Whether you like the video or not, or if you want to learn about specific ROS subjects, please leave a comment in the comments section below, so we can interact and learn from each other.

ROS Basics python Course Cover - ROS Online Courses - Robot Ignite Academy

ROS Basics for Beginners (Python)

ROS Basics C++ Course Cover - ROS Online Courses - Robot Ignite Academy

ROS Basics for Beginners (C++)

Question: What is catkin_make

Step1. Create a project in Robot Ignite Academy(RIA)

We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now!

Step2. Create a package

Almost all the package you will need is already installed in RIA. We’ll create the package in the ROS Autonomous Vehicles 101 course as an example in this video, but you can choose any course you want to do this.

Please type the following command in shell to create a new package

cd ~/catkin_ws/src
catkin_create_pkg test roscpp

Step3. Compile the package with catkin_make

Now you can type catkin_make to compile the package.

cd ..
catkin_make

It shows a lot of outputs, but what did it really do?

The catkin_make is actually a macro which creates directories and runs cmake command for you. If you want to do it yourself, just delete the build and devel directories first.

rm -rf build/ devel/

Now you can create a build directory and run the cmake command by yourself to get the exact same result as catkin_make did.

mkdir build&&cd build
cmake /home/user/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install -G "Unix Makefiles"

Take away today- The Answer:

catkin_make is a macro which creates source directories and runs the cmake commands for you. You can test out with the following step.

 

Edit by Tony Huang

Pin It on Pinterest