4 #include <sensor_msgs/SetCameraInfo.h> 8 #include <opencv2/opencv.hpp> 11 #include <dynamic_reconfigure/server.h> 12 #include <opt_camera/OptNM33CameraConfig.h> 46 nh.
getParam(
"camera_index", camera_index);
49 nh.
getParam(
"serial_id", serial_id_);
57 img_.header.frame_id = std::string(
"head_camera");
58 img_omni_.header.frame_id = std::string(
"head_camera");
59 img_wide_.header.frame_id = std::string(
"head_camera");
60 img_middle_.header.frame_id = std::string(
"head_camera");
61 img_narrow_.header.frame_id = std::string(
"head_camera");
62 img_panorama_.header.frame_id = std::string(
"head_camera");
63 nh.
getParam(
"frame_id", img_.header.frame_id);
64 nh.
getParam(
"omni_frame_id", img_omni_.header.frame_id);
65 nh.
getParam(
"wide_frame_id", img_wide_.header.frame_id);
66 nh.
getParam(
"middle_frame_id", img_middle_.header.frame_id);
67 nh.
getParam(
"narrow_frame_id", img_narrow_.header.frame_id);
68 nh.
getParam(
"panorama_frame_id", img_panorama_.header.frame_id);
71 info_.header.frame_id = img_.header.frame_id;
72 info_omni_.header.frame_id = img_omni_.header.frame_id;
73 info_wide_.header.frame_id = img_wide_.header.frame_id;
74 info_middle_.header.frame_id = img_middle_.header.frame_id;
75 info_narrow_.header.frame_id = img_narrow_.header.frame_id;
76 info_panorama_.header.frame_id = img_panorama_.header.frame_id;
86 info_srv_ = node.
advertiseService(
"set_camera_info", &OptCamNode::set_camera_info,
this);
87 info_omni_srv_ =
ros::NodeHandle(node,
"omni").advertiseService(
"set_camera_info", &OptCamNode::set_camera_info_omni,
this);
88 info_wide_srv_ =
ros::NodeHandle(node,
"wide").advertiseService(
"set_camera_info", &OptCamNode::set_camera_info_wide,
this);
89 info_middle_srv_ =
ros::NodeHandle(node,
"middle").advertiseService(
"set_camera_info", &OptCamNode::set_camera_info_middle,
this);
90 info_narrow_srv_ =
ros::NodeHandle(node,
"narrow").advertiseService(
"set_camera_info", &OptCamNode::set_camera_info_narrow,
this);
91 info_panorama_srv_ =
ros::NodeHandle(node,
"panorama").advertiseService(
"set_camera_info", &OptCamNode::set_camera_info_panorama,
this);
103 reconfigure_server_.setCallback(f);
115 IplImage *frame = NULL;
118 if ( ( frame = camera_->
queryFrame() ) == NULL ) {
123 img_.header.stamp = now;
124 img_omni_.header.stamp = now;
125 img_wide_.header.stamp = now;
126 img_middle_.header.stamp = now;
127 img_narrow_.header.stamp = now;
128 img_panorama_.header.stamp = now;
129 info_.header.stamp = now;
130 info_omni_.header.stamp = now;
131 info_wide_.header.stamp = now;
132 info_middle_.header.stamp = now;
133 info_narrow_.header.stamp = now;
134 info_panorama_.header.stamp = now;
137 fillImage(img_,
"bgr8", frame->height, frame->width, frame->width * frame->nChannels, frame->imageData);
141 fillImage(img_omni_,
"bgr8", tmp_img_->height, tmp_img_->width, tmp_img_->width * tmp_img_->nChannels, tmp_img_->imageData);
142 img_omni_pub_.
publish(img_omni_, info_omni_);
146 fillImage(img_wide_,
"bgr8", tmp_img_->height, tmp_img_->width, tmp_img_->width * tmp_img_->nChannels, tmp_img_->imageData);
147 img_wide_pub_.
publish(img_wide_, info_wide_);
151 fillImage(img_middle_,
"bgr8", tmp_img_->height, tmp_img_->width, tmp_img_->width * tmp_img_->nChannels, tmp_img_->imageData);
152 img_middle_pub_.
publish(img_middle_, info_middle_);
156 fillImage(img_narrow_,
"bgr8", tmp_img_->height, tmp_img_->width, tmp_img_->width * tmp_img_->nChannels, tmp_img_->imageData);
157 img_narrow_pub_.
publish(img_narrow_, info_narrow_);
159 if(config_.mode == 2) {
160 IplImage* tmp_img_ = cvCreateImage(cvSize(frame->width*2,frame->height/2),frame->depth,frame->nChannels);
162 if ( 0 < pan && pan < 180 ) {
163 int offset = frame->width * pan / 180;
164 cvSetImageROI(frame,cvRect(0,frame->height/2,frame->width,frame->height/2));
165 cvSetImageROI(tmp_img_,cvRect(offset,0,frame->width,frame->height/2));
166 cvCopy(frame,tmp_img_);
167 cvSetImageROI(frame,cvRect(frame->width-offset,0,offset,frame->height/2));
168 cvSetImageROI(tmp_img_,cvRect(0,0,offset,frame->height/2));
169 cvCopy(frame,tmp_img_);
170 cvSetImageROI(frame,cvRect(0,0,frame->width-offset,frame->height/2));
171 cvSetImageROI(tmp_img_,cvRect(frame->width+offset,0,frame->width-offset,frame->height/2));
172 cvCopy(frame,tmp_img_);
173 }
else if ( -180 < pan && pan < 0 ) {
174 int offset = frame->width * pan / -180;
175 cvSetImageROI(frame,cvRect(0,0,frame->width,frame->height/2));
176 cvSetImageROI(tmp_img_,cvRect(frame->width-offset,0,frame->width,frame->height/2));
177 cvCopy(frame,tmp_img_);
178 cvSetImageROI(frame,cvRect(offset,frame->height/2,frame->width-offset,frame->height/2));
179 cvSetImageROI(tmp_img_,cvRect(0,0,frame->width-offset,frame->height/2));
180 cvCopy(frame,tmp_img_);
181 cvSetImageROI(frame,cvRect(0,frame->height/2,offset,frame->height/2));
182 cvSetImageROI(tmp_img_,cvRect(frame->width*2-offset,0,offset,frame->height/2));
183 cvCopy(frame,tmp_img_);
185 cvSetImageROI(frame,cvRect(0,frame->height/2,frame->width,frame->height/2));
186 cvSetImageROI(tmp_img_,cvRect(0,0,frame->width,frame->height/2));
187 cvCopy(frame,tmp_img_);
188 cvSetImageROI(frame,cvRect(0,0,frame->width,frame->height/2));
189 cvSetImageROI(tmp_img_,cvRect(frame->width,0,frame->width,frame->height/2));
190 cvCopy(frame,tmp_img_);
192 fillImage(img_panorama_,
"bgr8", tmp_img_->height, tmp_img_->width, tmp_img_->width * tmp_img_->nChannels, tmp_img_->imageData);
193 img_panorama_pub_.
publish(img_panorama_, info_panorama_);
194 cvResetImageROI(frame);
195 cvReleaseImage(&tmp_img_);
200 ROS_ERROR(
"Could not convert from opencv to message.");
205 void config_cb(opt_camera::OptNM33CameraConfig &config, uint32_t level)
207 #define SET_CAMERA(methodname, paramname) \ 208 if (config_.paramname != config.paramname ) { \ 209 camera_->methodname(config.paramname); \ 213 if ( config.autoexposure ) {
219 if ( config.mode != 4 && config.mode != 0 ) {
226 if ( config.autoexposure ) {
230 if ( ! config.autowhitebalance ) {
233 SET_CAMERA(setAutoWhitebalance, autowhitebalance);
237 SET_CAMERA(setSmallHemisphere, smallhemisphere);
242 if ( config.capturefps != config_.capturefps) {
246 if ( config.lenstype != config_.lenstype) {
249 if ( config.mode == 4 ) {
255 if ( ( config.camera1_pan != config_.camera1_pan ) ||
256 ( config.camera1_tilt != config_.camera1_tilt ) ||
257 ( config.camera1_roll != config_.camera1_roll ) ||
258 ( config.camera1_zoom != config_.camera1_zoom ) ) {
259 camera_->
setLocationAbsolute(1,config.camera1_pan, config.camera1_tilt, config.camera1_roll, config.camera1_zoom);
267 if ( ( config.camera2_pan != config_.camera2_pan ) ||
268 ( config.camera2_tilt != config_.camera2_tilt ) ||
269 ( config.camera2_roll != config_.camera2_roll ) ||
270 ( config.camera2_zoom != config_.camera2_zoom ) ) {
271 camera_->
setLocationAbsolute(2,config.camera2_pan, config.camera2_tilt, config.camera2_roll, config.camera2_zoom);
275 if ( ( config.camera3_pan != config_.camera3_pan ) ||
276 ( config.camera3_tilt != config_.camera3_tilt ) ||
277 ( config.camera3_roll != config_.camera3_roll ) ||
278 ( config.camera3_zoom != config_.camera3_zoom ) ) {
279 camera_->
setLocationAbsolute(0,config.camera3_pan, config.camera3_tilt, config.camera3_roll, config.camera3_zoom);
286 if ( config.camera3_pan != config_.camera3_pan ) {
289 if ( config.camera3_tilt != config_.camera3_tilt ) {
292 if ( config.camera3_roll != config_.camera3_roll ) {
295 if ( config.camera3_zoom != config_.camera3_zoom ) {
304 std::string cam_name = serial_id_+view_name;
305 std::string ini_name = cam_name+
".ini";
307 #ifdef ROSPACK_EXPORT 309 rospack::Package *p = rp.get_pkg(
"opt_camera");
310 if (p!=NULL) ini_name = p->path +
"/cfg/" + ini_name;
313 std::vector<std::string> search_path;
315 rp.
crawl(search_path, 1);
317 if (rp.
find(
"opt_camera",path)==
true) ini_name = path +
"/cfg" + ini_name;
319 }
catch (std::runtime_error &e) {
324 if ( info.P[0] == 0.0 ) {
332 #ifdef ROSPACK_EXPORT 333 #define set_camera_info_method(set_camera_info_method, view_name) \ 334 bool set_camera_info_method(sensor_msgs::SetCameraInfo::Request& req, \ 335 sensor_msgs::SetCameraInfo::Response& rsp) { \ 336 ROS_INFO("New camera info received"); \ 337 sensor_msgs::CameraInfo &info = req.camera_info; \ 339 std::string cam_name = serial_id_+view_name; \ 340 std::string ini_name = cam_name+".ini"; \ 341 rospack::ROSPack rp; \ 343 rospack::Package *p = rp.get_pkg("opt_camera"); \ 344 if (p!=NULL) ini_name = p->path + "/cfg/" + ini_name; \ 345 } catch (std::runtime_error &e) { \ 347 if (!camera_calibration_parsers::writeCalibration(ini_name, cam_name.c_str(), info)) { \ 348 rsp.status_message = "Error writing camera_info to " + cam_name + ".ini"; \ 349 rsp.success = false; \ 352 rsp.success = true; \ 356 #define set_camera_info_method(set_camera_info_method, view_name) \ 357 bool set_camera_info_method(sensor_msgs::SetCameraInfo::Request& req, \ 358 sensor_msgs::SetCameraInfo::Response& rsp) { \ 359 ROS_INFO("New camera info received"); \ 360 sensor_msgs::CameraInfo &info = req.camera_info; \ 362 std::string cam_name = serial_id_+view_name; \ 363 std::string ini_name = cam_name+".ini"; \ 364 rospack::Rospack rp; \ 366 std::vector<std::string> search_path; \ 367 rp.getSearchPathFromEnv(search_path); \ 368 rp.crawl(search_path, 1); \ 370 if(rp.find("opt_camera", path)==true) ini_name = path + "/cfg/" + ini_name; \ 371 } catch (std::runtime_error &e) { \ 373 if (!camera_calibration_parsers::writeCalibration(ini_name, cam_name.c_str(), info)) { \ 374 rsp.status_message = "Error writing camera_info to " + cam_name + ".ini"; \ 375 rsp.success = false; \ 378 rsp.success = true; \ 398 if (now_time > next_time_) {
399 std::cout << count_ <<
" frames/sec at " << now_time << std::endl;
415 int main (
int argc,
char **argv)
std::string getFirmwareVersion()
ros::ServiceServer info_wide_srv_
bool setTiltAbsolute(double value)
bool setRollAbsolute(double value)
ros::ServiceServer info_narrow_srv_
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
IplImage * queryOmniFrame()
sensor_msgs::Image img_panorama_
def readCalibration(file_name)
image_transport::CameraPublisher img_panorama_pub_
IplImage * queryWideFrame()
ros::ServiceServer info_middle_srv_
image_transport::CameraPublisher img_narrow_pub_
image_transport::CameraPublisher img_pub_
sensor_msgs::CameraInfo info_middle_
sensor_msgs::CameraInfo info_narrow_
sensor_msgs::CameraInfo info_wide_
dynamic_reconfigure::Server< opt_camera::OptNM33CameraConfig > ReconfigureServer
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
sensor_msgs::CameraInfo info_
sensor_msgs::CameraInfo info_omni_
std::string getSerialID()
IplImage * queryNarrowFrame()
opt_camera::OptNM33CameraConfig config_
sensor_msgs::Image img_wide_
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
sensor_msgs::Image img_narrow_
image_transport::CameraPublisher img_omni_pub_
OptCamNode(ros::NodeHandle &node)
image_transport::CameraPublisher img_wide_pub_
omni middle panorama bool spin()
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
sensor_msgs::Image img_middle_
bool setZoomAbsolute(double value)
ros::ServiceServer info_srv_
#define SET_CAMERA(methodname, paramname)
bool setPanAbsolute(double value)
const std::string & getNamespace() const
ros::ServiceServer info_omni_srv_
bool getSearchPathFromEnv(std::vector< std::string > &sp)
set_camera_info_method(set_camera_info,"") set_camera_info_method(set_camera_info_omni
bool take_and_send_image()
void config_cb(opt_camera::OptNM33CameraConfig &config, uint32_t level)
bool setLocationAbsolute(int no, int pan, int tilt, int roll, int zoom)
sensor_msgs::CameraInfo info_panorama_
void crawl(std::vector< std::string > search_path, bool force)
ros::ServiceServer info_panorama_srv_
bool getParam(const std::string &key, std::string &s) const
bool setSmallHemisphere(char value)
bool find(const std::string &name, std::string &path)
sensor_msgs::Image img_omni_
int main(int argc, char **argv)
image_transport::CameraPublisher img_middle_pub_
ReconfigureServer reconfigure_server_
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
bool get_camera_info_method(std::string view_name, sensor_msgs::CameraInfo &info)
IplImage * queryMiddleFrame()