Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Getting started ROS with Python

Once again, run each of these Nodes in their own terminal, make sure you also have a master Running (it can be the same one from before if you haven't shut it down yet). Now open a new terminal and enter rosnode list. You will see both the "adder" and "subtractor" nodes listed as well as "rosout" which is the logging node automatically created by the Master.

Python does not require compilation however you should still organize your code appropriately. It is standard ROS practice to place Python files in a directory inside the package named scripts. Some ROS users choose to distinguish between Python files which are intended to be run as executables by having a seperate nodes directory, however for this tutorial we will simply place all our Python code in the scripts folder.

It is worth noting here that we haven't actually used any of the ROS libraries yet. Here we are simply demonstrating an easy way to run your code when it is organized into packages. Once we start using the ROS library correctly in our code however, running such an executable will create what is called a node.

rosnode is a command line tool that lets you get information about the nodes running in your distributed system as well as shut them down. Try shutting down the subtractor node with rosnode kill subtractor. If you check the terminal in which this node was running, the process should have stopped along with giving some information on the shutdown.

As mentioned above, whilst we have now placed our code into our catkin workspace and can run them using rosrun, we have yet to actually utilize ROS at runtime. To do this, we must add some simple commands so that when we run them, they do so under the ROS framework. Then, before we run them, we start the main ROS process, roscore which facilitates all ROS's functionalites.

Any name in ROS, for example "adder", can also be used with a forward slash before it. When using a forward slash at the beginning of a name, ROS command line tools provide tab-completion functionality, so you can interact quicker. Try shutting down the "adder" node using rosnode and tab-completion with rosnode kill /adder.

You can also shutdown your roscore and try running the nodes again. You will see they will output a message indicating no master was found and that they are waiting for one. Then, once you start a roscore process, they will find it themselves, register and run as normal.

The roscore process is a necessary background process for the running of any ROS node. If you try and start a node before this process has begun it will simply wait until you start a roscore before it does anything.

Notice the source devel/setup.bash at the end. Remember from the previous chapter that we added such a line to our .bashrc so that we would always have our executable code available to ROS. The .bashrc is only called when a new shell is opened. Since we have just performed a new build, to make sure everything is made available in our current shell, we manually source the setup.bash.

To test these nodes, first make sure you have a roscore process running in a terminal. Then run rosrun myp_ros_tutorial adder in one terminal and rosrun myp_ros_tutorial subtractor.py in another. They should run exactly as before these modifications as long as there is a roscore running.

This tutorial describes some tips for writing roslaunch files for large projects. The focus is on how to structure launch files so they may be reused as much as possible in different situations. We'll use the 2dnav_pr2 package as a case study.

This tutorial will teach you how to record data from a running ROS system into a .bag file, and then to play back the data to produce similar behavior in a running system.

This tutorial introduces ROS using rqt_console and rqt_logger_level for debugging and roslaunch for starting many nodes at once. If you use ROS fuerte or ealier distros where rqt isn't fully available, please see this page with this page that uses old rx based tools.

Sourcing the setup.bash will be important shortly, when we run the code, however before we do, let's also get a python script in our workspace.

This tutorial explains how to start a ROS system using two machines. It explains the use of ROS_MASTER_URI to configure multiple machines to use a single master.

When you run each of these nodes, they exit immediately after printing their results. Often we want nodes to stay active until we decide they should shutdown.

This tutorial covers useful template and macros for writing tutorials, along with example tutorials that are available for guidance on ros.org


This post first appeared on ROS Robotics, please read the originial post: here

Share the post

Getting started ROS with Python

×

Subscribe to Ros Robotics

Get updates delivered right to your inbox!

Thank you for your subscription

×