How to stop recording by camera node through an user input?
I would like to stop the camera node through a different node. I was trying to implement this by sending a message through another node which is read by the camera node But, the problem is that the camera node runs on an infinite loop. Can I get a cpp implementation to stop this while loop through a subscribed message.
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sstream> // for converting the command line parameter to integer
#include "std_msgs/Int32.h"
bool camOn = true;
void sendCamera(const std_msgs::Int32::ConstPtr& msg)
{
if (msg->data == 1){
camOn = true;}
else if (msg->data == 2){
camOn = false;}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("/camera_status", 1000, sendCamera);
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("camera/image", 1);
cv::VideoCapture cap(0);
if(!cap.isOpened()) return 1;
cv::Mat frame;
sensor_msgs::ImagePtr msg;
ros::Rate loop_rate(10);
while(nh.ok()){
while (camOn) {
ros::Subscriber sub = nh.subscribe("/camera_status", 1000, sendCamera);
cap >> frame;
// Check if grabbed frame is actually full with some content
if(!frame.empty()) {
msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
pub.publish(msg);
cv::waitKey(1);
}
ros::spin();
loop_rate.sleep();
}
}
}
You could make the loop non-infinite.
Instead of
you could use
and have a subscriber callback change to keeprunning = 0.
I have edited my question to show you my attempt. It's still not working.