Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <image_transport/image_transport.h>
00008 #include <math.h>
00009 #include <ros/ros.h>
00010 #include <ros/time.h>
00011
00012
00013 #include <sensor_msgs/Image.h>
00014 #include <sensor_msgs/image_encodings.h>
00015 #include <sys/param.h>
00016 #include <sys/times.h>
00017
00018 #include "Camera.h"
00019 #include "CameraContext.h"
00020 #include "CameraException.h"
00021
00022 using std::string;
00023
00024 using nifti::dc1394::Camera;
00025 using nifti::dc1394::CameraContext;
00026 using nifti::dc1394::CameraException;
00027
00028 namespace {
00029
00030 const std::string rawTopic = "raw";
00031 }
00032
00033 int main(int argc, char** argv) {
00034
00035 ros::init(argc, argv, "raw_image_publisher");
00036 ros::NodeHandle nodeHandle;
00037
00038 image_transport::ImageTransport it(nodeHandle);
00039 image_transport::Publisher pub = it.advertise(rawTopic, 4);
00040
00041
00042 bool exceptionOccurred = true;
00043 try {
00044
00045 CameraContext context;
00046
00047 Camera camera = context.getCamera();
00048 camera.resetBus();
00049
00050 camera.setup();
00051 camera.setupCapture();
00052 camera.startVideoTransmission();
00053
00054 struct tms t1;
00055 long r1 = 0l;
00056 uint32_t seq = 0;
00057 while (nodeHandle.ok()) {
00058
00059 r1 = times(&t1);
00060 ROS_DEBUG("Dequeuing frame...\n");
00061 dc1394video_frame_t* dequeuedFrame = NULL;
00062 dequeuedFrame = camera.dequeueFrame(DC1394_CAPTURE_POLICY_WAIT);
00063
00064 if (!dequeuedFrame) {
00065 continue;
00066 } else if (camera.isFrameCorrupt(dequeuedFrame)) {
00067 ROS_WARN("Frame corrupt.\n");
00068 camera.enqueueFrameLater(dequeuedFrame);
00069 camera.enqueueFrames();
00070 continue;
00071 }
00072
00073
00074
00075 sensor_msgs::Image imageMsg;
00076
00077
00078 imageMsg.header.frame_id = "omni_frame";
00079
00080 imageMsg.header.seq = seq++;
00081 imageMsg.header.stamp = ros::Time((double) dequeuedFrame->timestamp * 1000000);
00082
00083
00084 imageMsg.data.assign(dequeuedFrame->image, dequeuedFrame->image + dequeuedFrame->image_bytes);
00085
00086
00087
00088 imageMsg.encoding = sensor_msgs::image_encodings::MONO8;
00089 imageMsg.width = dequeuedFrame->size[0];
00090 imageMsg.height = dequeuedFrame->size[1];
00091 imageMsg.is_bigendian = false;
00092
00093 imageMsg.step = dequeuedFrame->stride;
00094
00095
00096
00097 pub.publish(imageMsg);
00098
00099 camera.enqueueFrameLater(dequeuedFrame);
00100 camera.enqueueFrames();
00101 }
00102
00103
00104 camera.stopVideoTransmission();
00105 camera.stopCapture();
00106 camera.releaseAll();
00107
00108 exceptionOccurred = false;
00109
00110 } catch (CameraException& e) {
00111 ROS_ERROR("Camera exception: %s %s", e.what(), e.getErrorString().data());
00112 } catch (std::runtime_error& e) {
00113 ROS_ERROR("Runtime error: %s", e.what());
00114 } catch (...) {
00115 ROS_ERROR("Unexpected exception occurred.");
00116 }
00117
00118 return exceptionOccurred ? -1 : 1;
00119 }