libvisiontransfer  10.6.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 an image transfer object that receives data from
49  // the first detected Nerian stereo device
50  DeviceParameters parameters(devices[0]);
51 
52  // Output the current parameterization
53 
54  const int colW = 40;
55  const int valueW = 8;
56  std::cout << std::boolalpha << std::left;
57  std::cout << "Server-side Parameter Enumeration" << std::endl;
58  std::cout << "=================================" << std::endl << std::endl;
59  ParameterSet allParams = parameters.getParameterSet();
60  std::cout << "All " << allParams.size() << " parameters reported by server:" << std::endl;
61  for (ParameterSet::iterator it = allParams.begin(); it != allParams.end(); ++it) {
62  Parameter& param = it->second;
63  switch (param.getType()) {
64  case ParameterValue::TYPE_INT: {
65  std::cout << std::setw(colW) << (param.getUid()+" (int)") << " = " << std::setw(valueW) << param.getCurrent<int>();
66  if (param.hasRange()) {
67  std::cout << " range " << param.getMin<int>() << "-" << param.getMax<int>();
68  }
69  if (param.getIncrement<int>() != 1) {
70  std::cout << " increment " << param.getIncrement<int>();
71  }
72  std::cout << std::endl;
73  break;
74  }
75  case ParameterValue::TYPE_BOOL: {
76  std::cout << std::setw(colW) << (param.getUid() + " (bool)") << " = " << (param.getCurrent<bool>()?"true":"false") << std::endl;
77  break;
78  }
79  case ParameterValue::TYPE_DOUBLE: {
80  std::cout << std::setw(colW) << (param.getUid()+" (double)") << " = " << std::setw(valueW) << param.getCurrent<double>();
81  if (param.hasRange()) {
82  std::cout << " range " << param.getMin<double>() << "-" << param.getMax<double>();
83  }
84  if (param.hasIncrement()) {
85  std::cout << " increment " << param.getIncrement<double>();
86  }
87  std::cout << std::endl;
88  break;
89  }
90  case ParameterValue::TYPE_STRING: {
91  std::cout << std::setw(colW) << (param.getUid() + " (string)") << " = \"" << param.getCurrent<std::string>() << "\"" << std::endl;
92  break;
93  }
94  case ParameterValue::TYPE_SAFESTRING: {
95  std::cout << std::setw(colW) << (param.getUid() + " (safestring)") << " = \"" << param.getCurrent<std::string>() << "\"" << std::endl;
96  break;
97  }
98  case ParameterValue::TYPE_TENSOR: {
99  std::cout << std::setw(colW) << (param.getUid() + " (tensor) - shape: ");
100  for (unsigned int i=0; i<param.getTensorDimension(); ++i) {
101  if (i) std::cout << "x";
102  std::cout << param.getTensorShape()[i];
103  }
104  std::cout << std::endl;
105 
106  std::vector<double> data = param.getTensorData();
107  std::cout << std::setw(colW) << (" ");
108  std::cout << " - data: ";
109  int perline = (param.getTensorDimension()==2) ? param.getTensorShape()[1] : param.getTensorNumElements();
110  for (unsigned int i=0; i<param.getTensorNumElements(); ++i) {
111  std::cout << data[i] << " ";
112  if (((i+1)%perline)==0) {
113  std::cout << std::endl << std::setw(colW) << (" ") << " ";
114  }
115  }
116  std::cout << std::endl;
117  break;
118  }
119  case ParameterValue::TYPE_COMMAND: {
120  std::cout << std::setw(colW) << (param.getUid() + " (-> command trigger)") << std::endl;
121  break;
122  default:
123  break;
124  }
125  }
126  /*
127  // Show description texts, where defined
128  auto descr = param.getDescription();
129  if (descr != "") {
130  std::cout << "\tDescription: " << descr << std::endl;
131  }
132  */
133  }
134  std::cout << std::endl;
135 
136  // Setting an enumerated parameter
137  if (argc > 1) {
138  if (std::string(argv[1]) == "LOG") {
139  std::cout << "(Showing incoming parameter events - terminate with Ctrl-C ...)" << std::endl;
140  parameters.setParameterUpdateCallback([&parameters](const std::string& uid) {
141  // Gets the up-to-date value (not the one from the frozen ParameterSet we got earlier!)
142  std::cout << uid << " := " << parameters.getParameter(uid).getCurrent<std::string>() << std::endl;
143  });
144  while (true) std::this_thread::sleep_for(std::chrono::seconds(1));
145  } else {
146  std::string argname(argv[1]);
147  if (argc > 2) {
148  std::string val(argv[2]);
149  std::cout << "Sending request to set " << argname << " to " << val << std::endl;
150  parameters.setParameter(argname, val);
151  } else {
152  std::cout << "Requesting single parameter " << argname << std::endl;
153  std::cout << "-> cast as a string: " << parameters.getParameter(argname).getCurrent<std::string>() << std::endl;
154  }
155  }
156  } else {
157  std::cout << "You can launch this with a parameter name to get (and a value to set it)" << std::endl;
158  std::cout << " e.g. " << argv[0] << " operation_mode [2]" << std::endl;
159  std::cout << "or with 'LOG' as first argument to see continuous parameter updates" << std::endl;
160  std::cout << std::endl;
161  }
162 
163  return 0;
164  } catch(const std::exception& ex) {
165  // Note: for setting parameters, there are two relevant exceptions that
166  // should be handled. ParameterException indicates an invalid UID,
167  // lack of access rights, or unacceptable value, while
168  // TransferException likely means that the connection has been lost
169  // and could not yet be reconnected automatically in the background.
170  // You might want to retry the set operation later in the latter case.
171  std::cerr << "Exception occurred: " << ex.what() << std::endl;
172  }
173 
174  return 0;
175 }
176 
visiontransfer::param::Parameter::getType
ParameterValue::ParameterType getType() const
Definition: parameter.h:120
visiontransfer::param::Parameter::getTensorDimension
unsigned int getTensorDimension() const
Definition: parameter.h:158
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
unsigned int getTensorNumElements() const
Definition: parameter.h:164
visiontransfer::param::Parameter::hasRange
bool hasRange() const
Definition: parameter.h:463
visiontransfer::param::Parameter::hasIncrement
bool hasIncrement() const
Definition: parameter.h:467
visiontransfer::DeviceParameters
Allows for configuration of the parameters of a Nerian stereo device through a network connection.
Definition: deviceparameters.h:62
visiontransfer::param::Parameter::getTensorShape
std::vector< unsigned int > getTensorShape() const
Definition: parameter.h:161
visiontransfer::param::Parameter::getIncrement
T getIncrement() const
Definition: parameter.h:437
visiontransfer::param::ParameterSet
Definition: parameterset.h:59
visiontransfer::param::Parameter::getMax
T getMax() const
Definition: parameter.h:432
visiontransfer::param::Parameter::getTensorData
std::vector< double > getTensorData() const
Definition: parameter.cpp:266
visiontransfer::param::Parameter
Definition: parameter.h:71
visiontransfer::param::Parameter::getCurrent
T getCurrent() const
Definition: parameter.h:406
visiontransfer::param::Parameter::getUid
std::string getUid() const
Definition: parameter.h:108
visiontransfer::param::Parameter::getMin
T getMin() const
Definition: parameter.h:427
Allied Vision