Writing Your First Script
Let’s write our first script to get a feel for writing scripts for ROS. If you don’t understand what every line means, don’t worry- we’ll get there! Our example is designed to be as simple as possible.
Close all terminal windows on TurtleBot. Open a new one and run:
Now moving to the workstation, let’s clone the github repository for these articles and run a sample script:
TurtleBot should be moving forward now. Press CTRL + C to stop it.
Now let’s have a look at the code. Open it by running:
You can also view it on GitHub if you prefer.
If you scroll all the way to the bottom you’ll see:
NOTE: If you haven’t looked at Python code before, Learning Python by Mark Lutz is a good resource.
The code above simply means “when we start this script try running GoForward’s init (initialize) function”. From here the documentation in the code is pretty straightforward but not designed to be all-inclusive. To fully understand the publisher, etc., we’ve linked to some great books.
For fun let’s modify the command so TurleBot goes in circles. In a terminal window run:
Modify linear.x = 0.2 to 0 and angular.z = 0 to 0.5. Save and exit.
In a new terminal window run:
NOTE: TurtleBot only uses linear.x and angular.z values because it works in a planar (2D) world, but for drones, marine robots and other robots that occupy 3D environments, linear.y, angular.x and angular.y values are available.
Now let’s try another script which is designed for TurtleBot to draw squares. In a terminal window run:
TurtleBot starts drawing squares on the floor but you’ll see that it quickly starts to drift away from its starting path. This is where robots and computers act very differently. If you ask a computer to do 1 + 1 you’ll always receive 2. If you ask a robot to move forward 1 meter it will go roughly a meter and not perfectly straight. Knowing where the robot is (localization) is one of the classic robotic challenges, especially in indoor environments where GPS isn’t reliable or accurate enough.
You might think that because you can tell the robot to go forward at 0.2 m/s, it would be easy to program a robot so that it goes forward one meter by publishing a linear.x = 0.2 m/s for 5 seconds. If the robot were a computer this is exactly right, but for robots this is very inaccurate due to slippage, imperfect calibration, and other factors. If you write a script like this to drive in a square and run for 10 cycles, you’ll end up somewhere entirely different from where you started! Luckily, amazing scientists and researchers are way ahead of us on this.
In the next article we will start to introduce solutions to these complex but fascinating challenges as we begin to leverage some of the really powerful tools ROS provides us.