Monday, May 31, 2010

Vision for Robots 1 of n: Setup

This is going to be a mini-series of posts that helps you build a practical computer vision application for your robot. We will be using the ROS robotics framework and OpenCV computer vision library.

To complete this you will need:
  • A computer
  • Some programming experience
  • Optional USB Webcam

The first step is going to be to install ROS. On Ubuntu, if you don't like reading instructions, you can try copy and pasting this and it might work. It may also cause your computer to catch on fire, but these are the risks you take when you refuse to read the instructions.

sudo apt-get install build-essential python-yaml cmake subversion wget
wget --no-check-certificate http://ros.org/rosinstall -O ~/rosinstall
chmod 755 ~/rosinstall
~/rosinstall ~/ros http://ros.org/rosinstalls/boxturtle_base.rosinstall
echo "source ~/ros/setup.sh" >> ~/.bashrc
. ~/.bashrc
rosdep install ros
rosmake install ros
rosdep install image_view
rosmake install image_view

Once you manage to get ROS installed you may want to spend some time going through the tutorials. They are well worth your time.

If you have a USB webcam that supports the uvc video class you can install one of the drivers.

cd ~/ros/stacks
svn co https://bosch-ros-pkg.svn.sourceforge.net/svnroot/bosch-ros-pkg
cd ~/ros/stacks/bosch-ros-pkg/trunk/bosch_drivers/usb_cam
rosdep install usb_cam
rosmake usb_cam

To test the usb_cam driver create a launch file directory.

mkdir ~/ros/launch

Next, create a launch file called ~/ros/launch/usb_cam.launch that contains the following.
<launch>
  <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" >
    <param name="video_device" value="/dev/video0" />
    <param name="image_width" value="320" />
    <param name="image_height" value="240" />
    <param name="pixel_format" value="mjpeg" />
    <param name="camera_frame_id" value="usb_cam" />
    <param name="io_method" value="mmap"/>
  </node>
  <node name="image_view" pkg="image_view" type="image_view" respawn="false" output="screen">
    <remap from="image" to="/usb_cam/image_raw"/>
  </node>
</launch>

Launching the file should display images from the camera.

roslaunch ~/ros/launch/usb_cam.launch


After that you can install the I Heart Robotics ROS Packages

cd ~/ros/stacks
git clone git://github.com/IHeartRobotics/iheart-ros-pkg.git
cd ~/ros/stacks/iheart-ros-pkg/ihr_demos/ihr_opencv
rosdep install ihr_opencv
rosmake ihr_opencv


The prerecorded video is stored in .bag files and can be viewed by running the launch file.

roslaunch ~/ros/stacks/iheart-ros-pkg/ihr_demos/ihr_demo_bags/launch/demo.launch




Next we will be showing how to convert the video from blue, green, red (BGR) to the hue, saturation, value (HSV) color-space.

Please post any problems with these instructions in the comments.

12 comments:

Anonymous said...

I am completely new to linux and want to make the right choice of computer and OS flavor to support this vision project, but I would like my computer setup to be capable of supporting long term "robot-building" growth.
Please provide/suggest/recommend a computer and Linux OS flavor to purchase. I'm considering the latest Ubuntu 10.x.x Lucid Lynx as the OS flavor choice running on a 64 bit computer.
Please suggest a good webCam to purchase for this project.
Thanks
Email = robosq@hotmail.com

I Heart Robotics said...

I think the Dell latitude 2100 is perfect for what you want. It can be configured with a webcam, and a touch screen. The touchscreen could be setup to act as an emergency stop. The webcam provides reasonable results. Also there are two slots on the side of the latitude 2100 that could be used to secure the computer to a robot base.

The other direction to go is to buy the fastest computer you can with an Nvidia Fermi graphics chip set like the Tesla C2070. This will probably give you the best results if you want to write computer vision code that uses GPU acceleration. This computer will also be expensive.
http://www.nvidia.com/object/imaging_comp_vision.html

For software I suggest Ubuntu 10.04 LTS.

If you have enough money to buy the expensive computer look at the point grey cameras. http://www.ptgrey.com/
Otherwise I like the Logitech C600 as a cheap webcam you can destroy for the sake of science.

Anonymous said...

i tried to find the usb_cam file in "bosch-ros-pkg/trunk/stacks/bosch_drivers/usb_cam" and tried the command "rosdep install usb_cam" & i got "Warning: could not identify ['usb_cam'] as a package". What should i do??
email:RINOSH1989_at_MSN.COM

paul philip said...

i am unable to install the usb_cam
"/opt/ros/cturtle/ros/bosch-ros-pkg/trunk/stacks/bosch_drivers/usb_cam"-my installation location
wen i run the command "rosdep install usb_cam" i get:
"Failed to find stack for package [self_test]
Failed to load rosdep.yaml for package [self_test]:Cannot locate installation of package self_test: [rospack] opendir error [No such file or directory] while crawling YOUR_BEGINNER_TUTORIALS_PATH
[rospack] couldn't find package [self_test]. ROS_ROOT[/opt/ros/cturtle/ros] ROS_PACKAGE_PATH[YOUR_BEGINNER_TUTORIALS_PATH:/opt/ros/cturtle/stacks]
[rosstack] opendir error [No such file or directory] while crawling YOUR_BEGINNER_TUTORIALS_PATH
Failed to find stack for package [image_transport]
Failed to load rosdep.yaml for package [image_transport]:Cannot locate installation of package image_transport: [rospack] opendir error [No such file or directory] while crawling YOUR_BEGINNER_TUTORIALS_PATH
[rospack] couldn't find package [image_transport]. ROS_ROOT[/opt/ros/cturtle/ros] ROS_PACKAGE_PATH[YOUR_BEGINNER_TUTORIALS_PATH:/opt/ros/cturtle/stacks]
Traceback (most recent call last):
File "/opt/ros/cturtle/ros/bin/rosdep", line 35, in
sys.exit(rosdep.main())
File "/opt/ros/cturtle/ros/tools/rosdep/src/rosdep/main.py", line 141, in main
r.install(options.include_duplicates, options.default_yes);
File "/opt/ros/cturtle/ros/tools/rosdep/src/rosdep/core.py", line 377, in install
script = self.generate_script(include_duplicates, default_yes)
File "/opt/ros/cturtle/ros/tools/rosdep/src/rosdep/core.py", line 340, in generate_script
native_packages, scripts = self.get_packages_and_scripts()
File "/opt/ros/cturtle/ros/tools/rosdep/src/rosdep/core.py", line 312, in get_packages_and_scripts
rdlp = RosdepLookupPackage(self.osi.get_name(), self.osi.get_version(), p, yc)
File "/opt/ros/cturtle/ros/tools/rosdep/src/rosdep/core.py", line 168, in __init__
self.load_for_package(package, yaml_cache.rp)
File "/opt/ros/cturtle/ros/tools/rosdep/src/rosdep/core.py", line 194, in load_for_package
paths.add( os.path.join(roslib.stacks.get_stack_dir(stack), "rosdep.yaml"))
File "/opt/ros/cturtle/ros/core/roslib/src/roslib/stacks.py", line 152, in get_stack_dir
raise InvalidROSStackException("Cannot location installation of stack %s. ROS_ROOT[%s] ROS_PACKAGE_PATH[%s]"%(stack, env[ROS_ROOT], env.get(ROS_PACKAGE_PATH, '')))
roslib.stacks.InvalidROSStackException: Cannot location installation of stack bosch_drivers. ROS_ROOT[/opt/ros/cturtle/ros] ROS_PACKAGE_PATH[YOUR_BEGINNER_TUTORIALS_PATH:/opt/ros/cturtle/stacks]please help at RINOSH1989_at_MSN.COM

I Heart Robotics said...

You need to download compile the usb camera driver.

http://www.ros.org/wiki/usb_cam

if usb_cam doesn't work you can try uvc_camera or gscam.

http://www.ros.org/wiki/uvc_camera
http://www.ros.org/wiki/gscam

Anonymous said...

when i run i get the following:-

root@paul-desktop:/opt/ros/cturtle/ros# roslaunch launch/usb_cam.launch ... logging to /root/.ros/log/aa352354-1156-11e0-872c-00e020c4b0be/roslaunch-paul-desktop-14879.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://paul-desktop:42055/

SUMMARY
========

PARAMETERS
* /usb_cam/image_width
* /usb_cam/video_device
* /usb_cam/pixel_format
* /usb_cam/io_method
* /usb_cam/camera_frame_id
* /usb_cam/image_height

NODES
/
usb_cam (usb_cam/usb_cam_node)
image_view (image_view/image_view)

starting new master (master configured for auto start)
process[master]: started with pid [14893]
ROS_MASTER_URI=http://paul-desktop:11311/

setting /run_id to aa352354-1156-11e0-872c-00e020c4b0be
process[rosout-1]: started with pid [14906]
started core service [/rosout]
process[usb_cam-2]: started with pid [14909]
process[image_view-3]: started with pid [14910]
usb_cam video_device set to [/dev/video0]
usb_cam io_method set to [mmap]
usb_cam image_width set to [320]
usb_cam image_height set to [240]
usb_cam pixel_format set to [mjpeg]
[mjpeg @ 0x8695500]codec type or id mismatches
Could not open MJPEG Decoder
[usb_cam-2] process has died [pid 14909, exit code -11].
log files: /root/.ros/log/aa352354-1156-11e0-872c-00e020c4b0be/usb_cam-2*.log
^C[image_view-3] killing on exit
[rosout-1] killing on exit
[master] killing on exit
exiting...
shutting down processing monitor...
... shutting down processing monitor complete
done

what should i do?
pls reply at riinosh1989 at msn.com

I Heart Robotics said...

Try setting the pixel format to yuyv
<param name="pixel_format" value="yuyv"/>

Another option is to try the gscam driver.
http://www.ros.org/wiki/gscam

Anonymous said...

what u said didn't workout
i then got gscam,
i got it running but got this error

Gstreamer Version: GStreamer 0.10.30
stream is PAUSED.
[ INFO] [1293443027.467762537]: Successfully read camera calibration. Rerun camera calibrator if it is incorrect.
Processing...
libv4lconvert: Error decompressing JPEG: fill_nbits error: need 7 more bits
etc...
what should i do
reply at rinosh1989 at msn.com
i am a complete newbie to ROS

Anonymous said...

i changed parameters to GSCAM_CONFIG="v4l2src device=/dev/video0 ! videoscale !
video/x-raw-yuv,width=320,height=240 ! ffmpegcolorspace !
video/x-raw-rgb ! identity name=ros ! jpegenc ! fakesink"
got it running
what should i do next?
rinosh1989 at msn.com

I Heart Robotics said...

It might be your camera, do you have another camera to try?

You should try to get image_view working first. http://www.ros.org/wiki/image_view

Also if you have not done them yet I strongly suggest doing the tutorials. Yes some of them are a little boring, but they show you what you will need to know later.
http://www.ros.org/wiki/ROS/Tutorials

Anonymous said...

i got it running! used GSCAM_CONFIG="v4l2src device=/dev/video0 ! videoscale !
video/x-raw-yuv,width=320,height=240 ! ffmpegcolorspace !
video/x-raw-rgb ! identity name=ros ! jpegenc ! fakesink"
now what should i do to proceed with your tutorials?

I Heart Robotics said...

Once you have image_view running you should be able to run the demo code. The onlything you may need to change is the camera startup in the launch file.

Look in iheart-ros-pkg/ihr_demos/ihr_opencv/launch for thee files names live*.launch and replace the usb_cam part with the gscam part.