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

Revision history [back]

click to hide/show revision 1
initial version

You can subscribe a topic only once, there's no need to subscribe to the /camera_status every iteration in the loop.

Do you want to stop the camera recording or shutdown the node? I'm assuming you only want to stop the camera, not the node.

You should use spinOnce(), spin() will not return until your node receive a shutdown signal. There's no need for the inner while, you can check camOn using an if. Using the inner while, you can't shutdown the node properly when camOn is true.

Try this way:

while(nh.ok())
{
    if (camOn) 
    {
        cap >> frame;
        if(!frame.empty()) 
        {
          msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
          pub.publish(msg);
          cv::waitKey(1);
        }        
    }
    ros::spinOnce();
    loop_rate.sleep();
}