update_dynamic_reconfigure.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Markus Bader                                    *
00003  *   markus.bader@tuwien.ac.at                                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 
00022 
00023 #include <tuw_uvc/uvc.h>
00024 #include <boost/algorithm/string.hpp>
00025 #include <boost/program_options.hpp>
00026 
00027 extern "C" {
00028 #include <libv4l2.h>
00029 #include "luvcview/v4l2uvc.h"
00030 #include <linux/videodev2.h>
00031 }
00032 
00036 void updateDynamicReconfigureFile(const std::string &filename, const std::vector<V4RCam::ControlEntryPtr > &controlEntries, V4RCam::FD fd)
00037 {
00038     v4l2_queryctrl queryctrl;
00039     v4l2_control   control_s;
00040     memset(&queryctrl, 0, sizeof(queryctrl));
00041     memset(&control_s, 0, sizeof(control_s));
00042         std::string package_name("tuw_uvc");
00043     FILE *configfile;
00044     configfile = fopen(filename.c_str(), "w");
00045     fprintf(configfile, "#! /usr/bin/env python\n");
00046     fprintf(configfile, "#Autogenerated V4L Dynamic Control\n\n");
00047     fprintf(configfile, "PACKAGE='%s'\n", package_name.c_str());
00048     fprintf(configfile, "from dynamic_reconfigure.parameter_generator_catkin import *\n");
00049     fprintf(configfile, "gen = ParameterGenerator()\n\n");
00050 
00051 
00052     fprintf(configfile, "gen.add(\"show_camera_image\", bool_t, 0, \"Show camera image\", True)\n");
00053     fprintf(configfile, "gen.add(\"camera_freeze\", bool_t, 0, \"Pulbishes the last image over and over again\", True)\n");
00054     fprintf(configfile, "enum_convert_image = gen.enum([gen.const(\"raw_data\", int_t, 0, \"raw camera Data\"),");
00055     fprintf(configfile, "  gen.const(\"YUV422toRGB\", int_t, 1, \"converts image to rgb first\"),");
00056     fprintf(configfile, "  gen.const(\"YUV422toBRG\", int_t, 2, \"converts image to bgr first\"),");
00057     fprintf(configfile, "  gen.const(\"YUV422toGray\", int_t, 3, \"converts image to gray first\")], \"Convert image\")\n");
00058     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");
00059 
00060     
00061     fprintf(configfile, "\n#Autogenerated Controls\n\n");
00062     for(std::vector<V4RCam::ControlEntryPtr>::const_iterator it = controlEntries.begin(); it != controlEntries.end(); it++) {
00063         queryctrl.id = (*it)->queryctrl->id;
00064 
00065         if(0 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
00066             if(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
00067                 continue;
00068             } else {
00069                 if(queryctrl.type == V4L2_CTRL_TYPE_INTEGER) {
00070                     fprintf(configfile, "gen.add(\"%s\", int_t, 0, \"%s\", %i, %i, %i)\n",
00071                             (*it)->varName.c_str(), queryctrl.name, queryctrl.default_value, queryctrl.minimum, queryctrl.maximum);
00072                 } else if(queryctrl.type == V4L2_CTRL_TYPE_BOOLEAN) {
00073                     fprintf(configfile, "gen.add(\"%s\", bool_t, 0, \"%s\", %s)\n",
00074                             (*it)->varName.c_str(), queryctrl.name, queryctrl.default_value ? "True" : "False");
00075                 } else if(queryctrl.type == V4L2_CTRL_TYPE_MENU) {
00076                     fprintf(configfile, "enum_%s = gen.enum([", (*it)->varName.c_str());
00077 
00078                     for(int i = queryctrl.minimum; i <= queryctrl.maximum; i++) {
00079                         struct v4l2_querymenu qm;
00080                         qm.id = queryctrl.id;
00081                         qm.index = i;
00082 
00083                         boost::this_thread::sleep(boost::posix_time::milliseconds(10));
00084                         if(v4l2_ioctl(fd, VIDIOC_QUERYMENU, &qm) == 0) {
00085                             std::string menuEntryName = (const char *)qm.name;
00086                             std::transform(menuEntryName.begin(), menuEntryName.end(), menuEntryName.begin(), V4RCam::removeNonAlNum);
00087                             boost::algorithm::trim_left_if(menuEntryName, boost::algorithm::is_any_of("_"));
00088                             boost::algorithm::trim_right_if(menuEntryName, boost::algorithm::is_any_of("_"));
00089                             fprintf(configfile, "gen.const(\"%s\", int_t, %i, \"%s\")",
00090                                     menuEntryName.c_str(), i, (const char *)qm.name);
00091                         } else {
00092                             fprintf(configfile, "gen.const(\"Unkown%i\", int_t, %i, \"Unkown\")",
00093                                     i, i);
00094                         }
00095                         if(i != queryctrl.maximum) fprintf(configfile, ", ");
00096                     }
00097                     fprintf(configfile, "], \"%s\")\n", queryctrl.name);
00098 
00099                     fprintf(configfile, "gen.add(\"%s\", int_t, 0, \"%s\", %i, %i, %i, edit_method=enum_%s)\n",
00100                             (*it)->varName.c_str(), queryctrl.name, queryctrl.default_value, queryctrl.minimum, queryctrl.maximum, (*it)->varName.c_str());
00101                 } else if(queryctrl.type == V4L2_CTRL_TYPE_BUTTON) {
00102                 } else if(queryctrl.type == V4L2_CTRL_TYPE_INTEGER64) {
00103                 } else if(queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS) {
00104                 } else if(queryctrl.type == V4L2_CTRL_TYPE_STRING) {
00105                 } else if(queryctrl.type == V4L2_CTRL_TYPE_BITMASK) {
00106                 } else {
00107                 }
00108             }
00109         }
00110         boost::this_thread::sleep(boost::posix_time::milliseconds(10));
00111     }
00112     fprintf(configfile, "\nexit(gen.generate(PACKAGE, \"%s\", \"CameraParameters\"))\n", package_name.c_str());
00113     fflush(configfile);
00114     fclose(configfile);
00115     std::cout << "Wrote file :" << filename << std::endl;
00116     boost::this_thread::sleep(boost::posix_time::milliseconds(100));
00117 }
00118 
00119 
00120 int main(int argc, char **argv)
00121 {
00122     namespace po = boost::program_options;
00123     std::string filename;
00124     std::string device;
00125     po::options_description desc("Allowed Parameters");
00126     desc.add_options()
00127     ("help", "get this help message")
00128     ("device,d", po::value<std::string>(&device)->default_value("/dev/video0"), "Video device")
00129     ("file,f", po::value<std::string>(&filename), "File to genrate: like cfg/CameraParameters.cfg");
00130 
00131     po::variables_map vm;
00132     try {
00133         po::store(po::parse_command_line(argc, argv, desc), vm);
00134     } catch(const std::exception &ex) {
00135         std::cout << desc << std::endl;;
00136         exit(1);
00137     }
00138     po::notify(vm);
00139 
00140     if(vm.count("help") || (vm.count("file") == 0))  {
00141         std::cout << desc << std::endl;
00142         exit(1);
00143     }
00144 
00145     std::cout << "File: " << filename << std::endl;
00146     V4RCam v4lCam;
00147     V4RCam::FD fd = v4lCam.initCamera(device);
00148     const std::vector<V4RCam::ControlEntryPtr > &controlEntries = v4lCam.detectControlEnties();
00149     for(unsigned int  i = 0; i  < controlEntries.size(); i++) {
00150         std::cout << controlEntries[i]->getQueryCtrlInfo() << std::endl;
00151     }
00152     boost::this_thread::sleep(boost::posix_time::milliseconds(100));
00153     updateDynamicReconfigureFile(filename, controlEntries, fd);
00154     boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
00155 }


tuw_uvc
Author(s):
autogenerated on Sun May 29 2016 02:50:28