Go to the documentation of this file.00001
00008 #include <sys/time.h>
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <math.h>
00012 #include <unistd.h>
00013 #include <string.h>
00014 #include <assert.h>
00015 #include <ros/ros.h>
00016 #include <sensor_msgs/Image.h>
00017 #include <image_transport/image_transport.h>
00018
00019 #include <dynamic_reconfigure/server.h>
00020 #include <canon_vbc50i/CanonParamsConfig.h>
00021 #include <canon_vbc50i/PTZ.h>
00022
00023
00024 #include <string>
00025 #include <vector>
00026 #include "canon_vbc50i/libCanon/JpegReader.h"
00027 #include "canon_vbc50i/libCanon/CanonDriver.h"
00028 #include "canon_vbc50i/libCanon/XString.h"
00029
00030 using namespace std;
00031
00032 #if 0
00033 #warning Saving path specific to eddie-ph
00034 #define RECORDING_DEST "/scratch/stream"
00035 #else
00036 #define RECORDING_DEST "/tmp"
00037 #endif
00038
00039 canon_vbc50i::CanonParamsConfig currentConfig;
00040 boost::recursive_mutex param_mutex;
00041 CanonDriver * driver = NULL;
00042
00043 void received_frame_callback(void * arg, const JpegReader * jpeg, unsigned int frame)
00044 {
00045
00046 #if 1
00047 image_transport::Publisher *pub = (image_transport::Publisher*)arg;
00048 sensor_msgs::Image output;
00049 assert (jpeg->getOutputColorSpace() == JpegReader::cmRGB);
00050 frame += 1;
00051 output.width = jpeg->width;
00052 output.height = jpeg->height;
00053 output.header.stamp = ros::Time::now();
00054 output.header.seq = frame;
00055 output.header.frame_id = currentConfig.frame_id;
00056 output.encoding = "rgb8";
00057 output.is_bigendian = 0;
00058 output.step = output.width*3;
00059 output.data.resize(3*jpeg->width*jpeg->height);
00060 std::copy(jpeg->buffer,jpeg->buffer+3*jpeg->width*jpeg->height,output.data.begin());
00061 pub->publish(output);
00062 #endif
00063 }
00064
00065 void callback(canon_vbc50i::CanonParamsConfig &config, uint32_t level)
00066 {
00067 if (config.infrared != currentConfig.infrared) {
00068 driver->setInfraRed(config.infrared);
00069 }
00070 if (config.night_mode != currentConfig.night_mode) {
00071 driver->setNightMode(config.night_mode);
00072 }
00073 if (config.fps != currentConfig.fps) {
00074 driver->setFrameRate(config.fps);
00075 }
00076 bool pos_change = false;
00077 if (config.pan_ang != currentConfig.pan_ang) {
00078 pos_change = true;
00079 }
00080 if (config.tilt_ang != currentConfig.tilt_ang) {
00081 pos_change = true;
00082 }
00083 if (config.zoom_ang != currentConfig.zoom_ang) {
00084 pos_change = true;
00085 }
00086 if (config.pan_speed != currentConfig.pan_speed)
00087 driver->setPanSpeed(config.pan_speed);
00088 if (config.tilt_speed != currentConfig.tilt_speed)
00089 driver->setTiltSpeed(config.tilt_speed);
00090 if (config.zoom_speed != currentConfig.zoom_speed)
00091 driver->setZoomSpeed(config.zoom_speed);
00092
00093 if (pos_change) {
00094 driver->moveTo(config.pan_ang,config.tilt_ang,config.zoom_ang);
00095 }
00096 #if 0
00097
00098
00099
00100
00101
00102 if (config.focus_mode != currentConfig.focus_mode) {
00103 switch(config.focus_mode) {
00104 case 0:
00105 driver->setFocusMode(CanonDriver::FMAuto, CanonDriver::FMUndef);
00106 break;
00107 case 1:
00108 driver->setFocusMode(CanonDriver::FMManual, CanonDriver::FMManualFar);
00109 break;
00110 case 2:
00111 driver->setFocusMode(CanonDriver::FMManual, CanonDriver::FMManualNear);
00112 break;
00113 case 3:
00114 driver->setFocusMode(CanonDriver::FMAutoDomes, CanonDriver::FMUndef);
00115 break;
00116 case 4:
00117 driver->setFocusMode(CanonDriver::FMInfinity, CanonDriver::FMUndef);
00118 break;
00119 }
00120 }
00121
00122 if (config.autoexp != currentConfig.autoexp) {
00123 driver->setAutoExposure(CanonDriver::AXPmode1);
00124 printf("Switched to auto exposure\n");
00125 } else if ((config.aperture != currentConfig.aperture) ||
00126 (config.inv_shutter != currentConfig.inv_shutter) ||
00127 (config.gain != currentConfig.gain)) {
00128 driver->setExposureParameters(config.aperture,
00129 config.inv_shutter,config.gain);
00130 printf("Changed exposure parameters\n");
00131 }
00132
00133 if (config.digital_zoom != currentConfig.digital_zoom) {
00134 driver->setDigitalZoom(config.digital_zoom);
00135 }
00136 if (config.pause != currentConfig.pause) {
00137 driver->setVideoReceptionPauseStatus(config.pause);
00138 }
00139 if (config.record != currentConfig.record) {
00140 driver->setRecordingDestination(config.record_dir.c_str());
00141 driver->setVideoRecordingMode(config.record);
00142 if (config.record) {
00143 fprintf(stderr,"Warning: video recording started\n");
00144 }
00145 }
00146 currentConfig = config;
00147 #endif
00148 }
00149
00150
00151
00152
00153
00154 int main(int argc, char* argv[])
00155 {
00156 if (argc < 2) {
00157 return 1;
00158 }
00159
00160 currentConfig.hostname = argv[1];
00161 currentConfig.subsampling = 0;
00162 currentConfig.fps = 10;
00163
00164 ros::init(argc, argv, "canon_vbc50i");
00165 ros::NodeHandle nh("~");
00166 ros::Publisher ptzpub = nh.advertise<canon_vbc50i::PTZ>("ptz", 1);
00167 image_transport::ImageTransport it(nh);
00168 image_transport::Publisher image_pub = it.advertise("image",1);
00169
00170
00171 char* loginf = NULL;
00172 if (argc >= 3) {
00173 loginf = argv[2];
00174 }
00175 try {
00176 double p,t,z;
00177 unsigned int width = 768, height = 576;
00178
00179 CanonDriver canon(loginf,currentConfig.hostname.c_str());
00180 driver = &canon;
00181 if (!canon.connect()) {
00182 return 1;
00183 }
00184
00185 canon.setVideoOutputColorSpace(JpegReader::cmRGB);
00186 width >>= currentConfig.subsampling;
00187 height >>= currentConfig.subsampling;
00188
00189 canon.setFrameRate(currentConfig.fps);
00190 canon.startVideoReception(received_frame_callback,&image_pub);
00191 canon.getCurrentPos(&p,&t,&z);
00192 currentConfig.pan_ang = p;
00193 currentConfig.tilt_ang = t;
00194 currentConfig.zoom_ang = z;
00195 ros::Rate loop_rate(10);
00196
00197 dynamic_reconfigure::Server<canon_vbc50i::CanonParamsConfig> srv(param_mutex,nh);
00198 dynamic_reconfigure::Server<canon_vbc50i::CanonParamsConfig>::CallbackType f = boost::bind(&callback, _1, _2);
00199 srv.setCallback(f);
00200 srv.updateConfig(currentConfig);
00201
00202
00203 while (ros::ok()) {
00204 canon_vbc50i::PTZ ptz;
00205 canon.getCurrentPos(&p,&t,&z);
00206 ptz.pan = p;
00207 ptz.tilt = t;
00208 ptz.zoom = z;
00209 ptzpub.publish(ptz);
00210
00211 ros::spinOnce();
00212 loop_rate.sleep();
00213 }
00214
00215 canon.disconnect();
00216 driver = NULL;
00217
00218 } catch (const std::exception & e) {
00219 fprintf(stderr,"Failed to connect to '%s'\n",argv[1]);
00220 fprintf(stderr,"\t%s\n",e.what());
00221 }
00222
00223 printf("\n");
00224 return 0;
00225 }