ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

USB camera does not take any pictures when ROS is launched at system startup

asked 2018-07-25 02:51:39 -0500

erjohn gravatar image

I have been trying to take pictures using a USB cam connected to my Jetson TX2. My code works pretty well when I execute it from terminal using the command roslaunch my_ros my_launch_file.launch and takes the pictures and saves them in a particular path. However, when I run the launch file at system startup, the code runs and the USB camera is triggered (the light on the camera tuns on) but no images are generated in the given path. Here is my code along with the explanations.

import rospy
from std_msgs.msg import Float32MultiArray
import cv2
import commands
import time

no_of_cameras = 2 # define number of attached cameras
time.sleep(120) # allow some time for the system see the usb camera

# this function returns the number of connected cameras
def getCameraInfo():

   Res=commands.getoutput("ls -l /dev/video*") #search for cameras attached
   CamCnt=Res.count("video") #getting number of cameras attached the board
   return CamCnt/2 

def process_image(frame, i):
    ###
    ### Image Processing code comes here
    ###
    if i == 6:
        res = True
    if i != 6:
        res = False    
    return res      

def talker():
    pub = rospy.Publisher('servo', Float32MultiArray, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    # check for the number of cameras continuously 
    while True: 
        er = getCameraInfo()
        if er > 1:
            break 
        time.sleep(5)

    cap = cv2.VideoCapture(1) # indicator light on my usb cam is turned ON when this command is executed

    rate1 = rospy.Rate(0.5)

    forward_slow = Float32MultiArray()  
    dont_move = Float32MultiArray()
    forward_slow.data = [90,84]
    dont_move.data = [90,90]

    i = 0
    while not rospy.is_shutdown():
        image_name = "/home/nvidia/Desktop/my_ros/cam" + str(i) + ".png" # define path and name of the image
        ret,frame=cap.read()
        cv2.imwrite(image_name, frame)
        res = process_image(frame, i) # image processing function. not related to to the problem

        if res:
            rospy.loginfo(dont_move)
            pub.publish(dont_move)
            rospy.sleep(1)  

        if not res:
            rospy.loginfo(forward_slow)
            pub.publish(forward_slow)
            rospy.sleep(1)

        i = i + 1
        rate1.sleep()
    cap.release() # indicator light on my usb cam is turned OFF when this command is executed

if __name__ == '__main__':    
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

What I am basically trying to do here is to take pictures with predefined intervals and then process them. According to the result of image processing, I will control robot with some basic commands (go forward, dont move,...). But I cannot figure out what sort of problem may be preventing the program from saving images when it is run automatically at system startup. Any kind of comments are appreciated (ros distro: kinetic)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-07-27 09:03:04 -0500

erjohn gravatar image

I solved the problem by following the solution given here.

I used to call roslaunch in my rc.local file as

su ubuntu -c "source /home/nvidia/catkin_ws/devel/setup.sh ; roslaunch my_ros my_launch_file.launch > /dev/null 2>&1 &"

Now, I replaced it with /home/nvidia/auto.sh where content auto.sh file is

#!/bin/bash    
cd $home    
source /home/nvidia/catkin_ws/devel/setup.bash    
roslaunch my_ros my_launch_file.launch    
exit 0

Restarting the Jetson after making the auto.sh file executable with the command $ sudo chmod u+x /home/username/auto.sh, I managed to save the image files. They are read-only though it not a problem for me.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-07-25 02:51:39 -0500

Seen: 367 times

Last updated: Jul 27 '18