generate_nerian_config_cpp.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2019 Nerian Vision GmbH
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 
15 ##############
16 #
17 # This script is called by CMake in order to generate C++ initialization
18 # and update code for the parameter server and dynamic_reconfigure
19 # (it reuses the .cfg file the dynamic_reconfigure build operates on).
20 #
21 
22 import sys
23 
24 CODE_TEMPLATE = '''// This file is AUTOGENERATED CODE produced by generate_nerian_config_cpp.py
25 
26 #include <ros/ros.h>
27 #include <dynamic_reconfigure/server.h>
28 #include <nerian_stereo/NerianStereoConfig.h>
29 #include <visiontransfer/scenescanparameters.h>
30 
31 #include "nerian_stereo_node_base.h"
32 
33 using namespace visiontransfer;
34 
35 namespace nerian_stereo {
36 
37 // Callback that receives an updated config from ROS
38 void StereoNodeBase::autogen_dynamicReconfigureCallback(nerian_stereo::NerianStereoConfig &config, uint32_t level) {
39  std::stringstream ss;
40  // == START of autogenerated parameter blocks ==
41 %s
42  // == END of autogenerated parameter blocks ==
43 }
44 
45 // Obtain current parameter values from device and copy them to parameter server
46 void StereoNodeBase::autogen_updateParameterServerFromDevice(std::map<std::string, ParameterInfo>& cfg) {
47  ROS_INFO("Setting initial parameters in the parameter server");
48  std::string node_name = ros::this_node::getName();
49  // Publish reboot flag to definitely be set to false in the parameter server
50  getNH().setParam(node_name + "/reboot", false);
51  // Publish the current config to the parameter server
52  // == START of autogenerated parameter blocks ==
53 %s
54  // == END of autogenerated parameter blocks ==
55 }
56 
57 // Override the default parameter bounds with current (run-time) config
58 void StereoNodeBase::autogen_updateDynamicReconfigureFromDevice(std::map<std::string, ParameterInfo>& cfg) {
59  nerian_stereo::NerianStereoConfig config_default, config_min, config_max;
60  ROS_INFO("Updating dynamic_reconfigure defaults and limits");
61  // Set defaults and min/max values according to SceneScan API
62  // == START of autogenerated parameter blocks ==
63 %s
64 
65  // == END of autogenerated parameter blocks ==
66  // Publish them
67  dynReconfServer->setConfigMin(config_min);
68  dynReconfServer->setConfigMax(config_max);
69  dynReconfServer->setConfigDefault(config_default);
70 }
71 
72 } // namespace
73 '''
74 
75 TEMPLATE_PARAMETER_CHANGE = ''' if (config.{varname} != lastKnownConfig.{varname}) {{
76  ROS_INFO("Request to set {varname} = %s", std::to_string(config.{varname}).c_str());
77  sceneScanParameters->setNamedParameter("{varname}", config.{varname});
78  }}'''
79 
80 TEMPLATE_SETPARAM = ''' getNH().setParam(node_name + "/{varname}", cfg["{varname}"].getValue<{typ}>());'''
81 
82 TEMPLATE_SETDEFAULTS = '''
83  config_default.{varname} = cfg["{varname}"].getValue<{typ}>();
84  config_min.{varname} = cfg["{varname}"].getMin<{typ}>();
85  config_max.{varname} = cfg["{varname}"].getMax<{typ}>();'''
86 
87 
88 if __name__ == '__main__':
89  # Default to filter
90  infile = sys.stdin
91  outfile = sys.stdout
92  if len(sys.argv) >= 3:
93  infile = open(sys.argv[1], 'r')
94  outfile = open(sys.argv[2], 'w')
95 
96  # Parse cfg file contents and extract the parameter lines (name + type)
97  varnames_and_types = []
98  for line in infile.readlines():
99  if line.startswith('gen.add('):
100  varname = line.split('"')[1]
101  typ = line.split(',')[1].strip().split('_')[0]
102  varnames_and_types.append([varname, typ])
103 
104  # Dump code for each parameter. 'reboot' is handled specially (must not be True initially)
105  paramchange = '\n'.join(TEMPLATE_PARAMETER_CHANGE.format(varname=vt[0]) for vt in varnames_and_types)
106  setparam = '\n'.join(TEMPLATE_SETPARAM.format(varname=vt[0], typ=vt[1]) for vt in varnames_and_types if vt[0] != 'reboot')
107  setdefaults = '\n'.join(TEMPLATE_SETDEFAULTS.format(varname=vt[0], typ=vt[1]) for vt in varnames_and_types if vt[0] != 'reboot')
108  outfile.write(CODE_TEMPLATE % (paramchange, setparam, setdefaults))
109 


nerian_stereo
Author(s): Nerian Vision Technologies
autogenerated on Thu Jun 20 2019 19:12:46