Taking a Photo Using Code
We have learned how to take a photo manually, but it is not very convenient. In this lesson we will learn how to take a photo using a script.
We will work with the files from github repository.
You can find the information how to clone the github repository in
“Writing Your First Script” lesson.
We assume that your files are situated here ~/helloworld/turtlebot
.
Launching Script
1. Launch Gazebo.
roslaunch turtlebot_gazebo turtlebot_world.launch
2. Open a new terminal and change directory.
cd ~/helloworld/turtlebot
3. Launch script.
python take_photo.py
You will see this string in terminal:
[INFO] [WallTime: 1456885227.493792] [646.730000] Saved image photo.jpg
4. See the results. Open the image file.
ristretto photo.jpg
NOTE: You can use image viewer which you prefer. For instance, you can use eog which is GNOME image viewer.
eog photo.jpg
5. Interrupt Gazebo. Close the terminals.
You can watch these steps in the video:
Taking a Photo Using Code
Using ROS Parameter
You can use ros parameter to change the title of the image. You can find the
line in the take_photo.py
:
img_title = rospy.get_param('~image_title', 'photo.jpg')
The program fetches value from the parameter server. If the parameter is not set
it uses the default value photo.jpg
.
Let’s check if the parameter is set. We will look for /take_photo/image_title
parameter, because image_title
parameter is a private parameter (~
before
image title
) for take_photo
node. Launch.
rosparam list | grep image_title
# Output:
The parameter is not set. Launch the program with parameter.
python take_photo.py _image_title:="new_title.jpg"
The title of the saved photo is new_title.jpg
. This command sets the
/take_photo/image_title
parameter. Let’s check if the parameter set.
rosparam list | grep image_title
# Output:
/take_photo/image_title
Now the program will use the title from parameter server. Check the current value.
rosparam get /take_photo/image_title
# Output:
new_title.jpg
Launch the program without a parameter.
python take_photo.py
The title of the saved photo is new_title.jpg
. new_title.jpg
is the value
from ros parameter server.
There is a brief introduction of ros parameters. You can read more about ros parameters on wiki ros.