libvisiontransfer  10.8.0
parameter_enumeration_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <visiontransfer/deviceenumeration.h>
16 #include <visiontransfer/imagetransfer.h>
17 #include <visiontransfer/imageset.h>
18 #include <visiontransfer/deviceparameters.h>
19 #include <iostream>
20 #include <exception>
21 #include <iomanip>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <thread>
25 #include <chrono>
26 
27 using namespace visiontransfer;
28 using namespace visiontransfer::param;
29 
30 int main(int argc, const char** argv) {
31  try {
32  // Search for Nerian stereo devices
33  DeviceEnumeration deviceEnum;
34 
35  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
36  if(devices.size() == 0) {
37  std::cout << "No devices discovered!" << std::endl;
38  return -1;
39  }
40 
41  // Print devices
42  std::cout << "Discovered devices:" << std::endl;
43  for(unsigned int i = 0; i< devices.size(); i++) {
44  std::cout << devices[i].toString() << std::endl;
45  }
46  std::cout << std::endl;
47 
48  // Create a DeviceParameters object that connects to the parameter
49  // service on the device to set / receive current device parameters.
50  // This uses a TCP connection with auto-reconnect enabled by default.
51  DeviceParameters parameters(devices[0]);
52 
53  // Output the current parameterization
54 
55  const int colW = 40;
56  const int valueW = 8;
57  std::cout << std::boolalpha << std::left;
58  std::cout << "Server-side Parameter Enumeration" << std::endl;
59  std::cout << "=================================" << std::endl << std::endl;
60  ParameterSet allParams = parameters.getParameterSet();
61  std::cout << "All " << allParams.size() << " parameters reported by server:" << std::endl;
62  for (ParameterSet::iterator it = allParams.begin(); it != allParams.end(); ++it) {
63  Parameter& param = it->second;
64  switch (param.getType()) {
65  case ParameterValue::TYPE_INT: {
66  std::cout << std::setw(colW) << (param.getUid()+" (int)") << " = " << std::setw(valueW) << param.getCurrent<int>();
67  if (param.hasRange()) {
68  std::cout << " range " << param.getMin<int>() << "-" << param.getMax<int>();
69  }
70  if (param.getIncrement<int>() != 1) {
71  std::cout << " increment " << param.getIncrement<int>();
72  }
73  if (param.hasOptions()) {
74  auto opts = param.getOptions<std::string>();
75  auto descr = param.getOptionDescriptions();
76  std::cout << " options: ";
77  for (int i=0; i<(int) opts.size(); ++i) {
78  std::cout << opts[i] << "(" << descr[i] << ") ";
79  }
80  }
81  std::cout << std::endl;
82  break;
83  }
84  case ParameterValue::TYPE_BOOL: {
85  std::cout << std::setw(colW) << (param.getUid() + " (bool)") << " = " << (param.getCurrent<bool>()?"true":"false") << std::endl;
86  break;
87  }
88  case ParameterValue::TYPE_DOUBLE: {
89  std::cout << std::setw(colW) << (param.getUid()+" (double)") << " = " << std::setw(valueW) << param.getCurrent<double>();
90  if (param.hasRange()) {
91  std::cout << " range " << param.getMin<double>() << "-" << param.getMax<double>();
92  }
93  if (param.hasIncrement()) {
94  std::cout << " increment " << param.getIncrement<double>();
95  }
96  std::cout << std::endl;
97  break;
98  }
99  case ParameterValue::TYPE_STRING: {
100  std::cout << std::setw(colW) << (param.getUid() + " (string)") << " = \"" << param.getCurrent<std::string>() << "\"" << std::endl;
101  break;
102  }
103  case ParameterValue::TYPE_SAFESTRING: {
104  std::cout << std::setw(colW) << (param.getUid() + " (safestring)") << " = \"" << param.getCurrent<std::string>() << "\"" << std::endl;
105  break;
106  }
107  case ParameterValue::TYPE_TENSOR: {
108  std::cout << std::setw(colW) << (param.getUid() + " (tensor) - shape: ");
109  for (unsigned int i=0; i<param.getTensorDimension(); ++i) {
110  if (i) std::cout << "x";
111  std::cout << param.getTensorShape()[i];
112  }
113  std::cout << std::endl;
114 
115  std::vector<double> data = param.getTensorData();
116  std::cout << std::setw(colW) << (" ");
117  std::cout << " - data: ";
118  int perline = (param.getTensorDimension()==2) ? param.getTensorShape()[1] : param.getTensorNumElements();
119  for (unsigned int i=0; i<param.getTensorNumElements(); ++i) {
120  std::cout << data[i] << " ";
121  if (((i+1)%perline)==0) {
122  std::cout << std::endl << std::setw(colW) << (" ") << " ";
123  }
124  }
125  std::cout << std::endl;
126  break;
127  }
128  case ParameterValue::TYPE_COMMAND: {
129  std::cout << std::setw(colW) << (param.getUid() + " (-> command trigger)") << std::endl;
130  break;
131  default:
132  break;
133  }
134  }
135  /*
136  // Show description texts, where defined
137  auto descr = param.getDescription();
138  if (descr != "") {
139  std::cout << "\tDescription: " << descr << std::endl;
140  }
141  */
142  }
143  std::cout << std::endl;
144 
145  // Setting an enumerated parameter
146  if (argc > 1) {
147  if (std::string(argv[1]) == "LOG") {
148  std::cout << "(Showing incoming parameter events - terminate with Ctrl-C ...)" << std::endl;
149  parameters.setParameterUpdateCallback([&parameters](const std::string& uid) {
150  // Gets the up-to-date value (not the one from the frozen ParameterSet we got earlier!)
151  std::cout << uid << " := " << parameters.getParameter(uid).getCurrent<std::string>() << std::endl;
152  });
153  while (true) std::this_thread::sleep_for(std::chrono::seconds(1));
154  } else {
155  std::string argname(argv[1]);
156  if (argc > 2) {
157  std::string val(argv[2]);
158  std::cout << "Sending request to set " << argname << " to " << val << std::endl;
159  parameters.setParameter(argname, val);
160  } else {
161  std::cout << "Requesting single parameter " << argname << std::endl;
162  std::cout << "-> cast as a string: " << parameters.getParameter(argname).getCurrent<std::string>() << std::endl;
163  }
164  }
165  } else {
166  std::cout << "You can launch this with a parameter name to get (and a value to set it)" << std::endl;
167  std::cout << " e.g. " << argv[0] << " operation_mode [2]" << std::endl;
168  std::cout << "or with 'LOG' as first argument to see continuous parameter updates" << std::endl;
169  std::cout << std::endl;
170  }
171 
172  return 0;
173  } catch(const std::exception& ex) {
174  // Note: for setting parameters, there are two relevant exceptions that
175  // should be handled. ParameterException indicates an invalid UID,
176  // lack of access rights, or unacceptable value, while
177  // TransferException likely means that the connection has been lost
178  // and could not yet be reconnected automatically in the background.
179  // You might want to retry the set operation later in the latter case.
180  std::cerr << "Exception occurred: " << ex.what() << std::endl;
181  }
182 
183  return 0;
184 }
185 
visiontransfer::param::Parameter::getIncrement
VT_EXPORT T getIncrement() const
visiontransfer::param::Parameter::getType
VT_EXPORT ParameterValue::ParameterType getType() const
Definition: parameter.cpp:752
visiontransfer::param::Parameter::getTensorDimension
VT_EXPORT unsigned int getTensorDimension() const
Definition: parameter.cpp:824
visiontransfer::DeviceEnumeration::discoverDevices
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Definition: deviceenumeration.h:66
visiontransfer::DeviceEnumeration
Allows for the discovery of devices in the network.
Definition: deviceenumeration.h:42
visiontransfer::param::Parameter::getTensorNumElements
VT_EXPORT unsigned int getTensorNumElements() const
Definition: parameter.cpp:830
visiontransfer::param::Parameter::hasRange
VT_EXPORT bool hasRange() const
Definition: parameter.cpp:1003
visiontransfer::param::Parameter::hasIncrement
VT_EXPORT bool hasIncrement() const
Definition: parameter.cpp:1006
visiontransfer::DeviceParameters
Allows for configuration of the parameters of a Nerian stereo device through a network connection.
Definition: deviceparameters.h:64
visiontransfer::param::Parameter::getOptionDescriptions
VT_EXPORT std::vector< std::string > getOptionDescriptions() const
Definition: parameter.cpp:957
visiontransfer::param::ParameterSet
Definition: parameterset.h:59
visiontransfer::param::Parameter::getTensorData
VT_EXPORT std::vector< double > getTensorData() const
Definition: parameter.cpp:833
visiontransfer::param::Parameter::getMin
VT_EXPORT T getMin() const
visiontransfer::param::Parameter
Definition: parameter.h:71
visiontransfer::param::Parameter::getMax
VT_EXPORT T getMax() const
visiontransfer::param::Parameter::getTensorShape
VT_EXPORT std::vector< unsigned int > getTensorShape() const
Definition: parameter.cpp:827
visiontransfer::param::Parameter::getOptions
VT_EXPORT std::vector< T > getOptions() const
visiontransfer::param::Parameter::hasOptions
VT_EXPORT bool hasOptions() const
Definition: parameter.cpp:994
visiontransfer::param::Parameter::getCurrent
VT_EXPORT T getCurrent() const
visiontransfer::param::Parameter::getUid
VT_EXPORT std::string getUid() const
Definition: parameter.cpp:734
Allied Vision