update_dynamic_reconfigure.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2010 by Markus Bader *
3  * markus.bader@tuwien.ac.at *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 
22 
23 #include <tuw_uvc/uvc.h>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/program_options.hpp>
26 
27 extern "C" {
28 #include <libv4l2.h>
29 #include "luvcview/v4l2uvc.h"
30 #include <linux/videodev2.h>
31 }
32 
36 void updateDynamicReconfigureFile(const std::string &filename, const std::vector<V4RCam::ControlEntryPtr > &controlEntries, V4RCam::FD fd)
37 {
38  v4l2_queryctrl queryctrl;
39  v4l2_control control_s;
40  memset(&queryctrl, 0, sizeof(queryctrl));
41  memset(&control_s, 0, sizeof(control_s));
42  std::string package_name("tuw_uvc");
43  FILE *configfile;
44  configfile = fopen(filename.c_str(), "w");
45  fprintf(configfile, "#! /usr/bin/env python\n");
46  fprintf(configfile, "#Autogenerated V4L Dynamic Control\n\n");
47  fprintf(configfile, "PACKAGE='%s'\n", package_name.c_str());
48  fprintf(configfile, "from dynamic_reconfigure.parameter_generator_catkin import *\n");
49  fprintf(configfile, "gen = ParameterGenerator()\n\n");
50 
51 
52  fprintf(configfile, "gen.add(\"show_camera_image\", bool_t, 0, \"Show camera image\", True)\n");
53  fprintf(configfile, "gen.add(\"camera_freeze\", bool_t, 0, \"Pulbishes the last image over and over again\", True)\n");
54  fprintf(configfile, "enum_convert_image = gen.enum([gen.const(\"raw_data\", int_t, 0, \"raw camera Data\"),");
55  fprintf(configfile, " gen.const(\"YUV422toRGB\", int_t, 1, \"converts image to rgb first\"),");
56  fprintf(configfile, " gen.const(\"YUV422toBRG\", int_t, 2, \"converts image to bgr first\"),");
57  fprintf(configfile, " gen.const(\"YUV422toGray\", int_t, 3, \"converts image to gray first\")], \"Convert image\")\n");
58  fprintf(configfile, "gen.add(\"convert_image_first\", int_t, 3, \"Convets the raw image first to an other format\", 1, 0, 3, edit_method=enum_convert_image)\n");
59 
60 
61  fprintf(configfile, "\n#Autogenerated Controls\n\n");
62  for(std::vector<V4RCam::ControlEntryPtr>::const_iterator it = controlEntries.begin(); it != controlEntries.end(); it++) {
63  queryctrl.id = (*it)->queryctrl->id;
64 
65  if(0 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
66  if(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
67  continue;
68  } else {
69  if(queryctrl.type == V4L2_CTRL_TYPE_INTEGER) {
70  fprintf(configfile, "gen.add(\"%s\", int_t, 0, \"%s\", %i, %i, %i)\n",
71  (*it)->varName.c_str(), queryctrl.name, queryctrl.default_value, queryctrl.minimum, queryctrl.maximum);
72  } else if(queryctrl.type == V4L2_CTRL_TYPE_BOOLEAN) {
73  fprintf(configfile, "gen.add(\"%s\", bool_t, 0, \"%s\", %s)\n",
74  (*it)->varName.c_str(), queryctrl.name, queryctrl.default_value ? "True" : "False");
75  } else if(queryctrl.type == V4L2_CTRL_TYPE_MENU) {
76  fprintf(configfile, "enum_%s = gen.enum([", (*it)->varName.c_str());
77 
78  for(int i = queryctrl.minimum; i <= queryctrl.maximum; i++) {
79  struct v4l2_querymenu qm;
80  qm.id = queryctrl.id;
81  qm.index = i;
82 
83  boost::this_thread::sleep(boost::posix_time::milliseconds(10));
84  if(v4l2_ioctl(fd, VIDIOC_QUERYMENU, &qm) == 0) {
85  std::string menuEntryName = (const char *)qm.name;
86  std::transform(menuEntryName.begin(), menuEntryName.end(), menuEntryName.begin(), V4RCam::removeNonAlNum);
87  boost::algorithm::trim_left_if(menuEntryName, boost::algorithm::is_any_of("_"));
88  boost::algorithm::trim_right_if(menuEntryName, boost::algorithm::is_any_of("_"));
89  fprintf(configfile, "gen.const(\"%s\", int_t, %i, \"%s\")",
90  menuEntryName.c_str(), i, (const char *)qm.name);
91  } else {
92  fprintf(configfile, "gen.const(\"Unkown%i\", int_t, %i, \"Unkown\")",
93  i, i);
94  }
95  if(i != queryctrl.maximum) fprintf(configfile, ", ");
96  }
97  fprintf(configfile, "], \"%s\")\n", queryctrl.name);
98 
99  fprintf(configfile, "gen.add(\"%s\", int_t, 0, \"%s\", %i, %i, %i, edit_method=enum_%s)\n",
100  (*it)->varName.c_str(), queryctrl.name, queryctrl.default_value, queryctrl.minimum, queryctrl.maximum, (*it)->varName.c_str());
101  } else if(queryctrl.type == V4L2_CTRL_TYPE_BUTTON) {
102  } else if(queryctrl.type == V4L2_CTRL_TYPE_INTEGER64) {
103  } else if(queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
104  } else if(queryctrl.type == V4L2_CTRL_TYPE_STRING) {
105  } else if(queryctrl.type == V4L2_CTRL_TYPE_BITMASK) {
106  } else {
107  }
108  }
109  }
110  boost::this_thread::sleep(boost::posix_time::milliseconds(10));
111  }
112  fprintf(configfile, "\nexit(gen.generate(PACKAGE, \"%s\", \"CameraParameters\"))\n", package_name.c_str());
113  fflush(configfile);
114  fclose(configfile);
115  std::cout << "Wrote file :" << filename << std::endl;
116  boost::this_thread::sleep(boost::posix_time::milliseconds(100));
117 }
118 
119 
120 int main(int argc, char **argv)
121 {
122  namespace po = boost::program_options;
123  std::string filename;
124  std::string device;
125  po::options_description desc("Allowed Parameters");
126  desc.add_options()
127  ("help", "get this help message")
128  ("device,d", po::value<std::string>(&device)->default_value("/dev/video0"), "Video device")
129  ("file,f", po::value<std::string>(&filename), "File to genrate: like cfg/CameraParameters.cfg");
130 
131  po::variables_map vm;
132  try {
133  po::store(po::parse_command_line(argc, argv, desc), vm);
134  } catch(const std::exception &ex) {
135  std::cout << desc << std::endl;;
136  exit(1);
137  }
138  po::notify(vm);
139 
140  if(vm.count("help") || (vm.count("file") == 0)) {
141  std::cout << desc << std::endl;
142  exit(1);
143  }
144 
145  std::cout << "File: " << filename << std::endl;
146  V4RCam v4lCam;
147  V4RCam::FD fd = v4lCam.initCamera(device);
148  const std::vector<V4RCam::ControlEntryPtr > &controlEntries = v4lCam.detectControlEnties();
149  for(unsigned int i = 0; i < controlEntries.size(); i++) {
150  std::cout << controlEntries[i]->getQueryCtrlInfo() << std::endl;
151  }
152  boost::this_thread::sleep(boost::posix_time::milliseconds(100));
153  updateDynamicReconfigureFile(filename, controlEntries, fd);
154  boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
155 }
int FD
Definition: uvc.h:48
v4l2 camera abstraction
Definition: uvc.h:40
const std::vector< ControlEntryPtr > & detectControlEnties()
Definition: uvc.cpp:352
int main(int argc, char **argv)
static char removeNonAlNum(char in)
Definition: uvc.cpp:51
void updateDynamicReconfigureFile(const std::string &filename, const std::vector< V4RCam::ControlEntryPtr > &controlEntries, V4RCam::FD fd)
FD initCamera(const std::string &videoDevice="")
vector of the current supported control entries
Definition: uvc.cpp:80


tuw_uvc
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:39:24