SensorCalUtility.cc
Go to the documentation of this file.
1 
40 #ifdef WIN32
41 #ifndef WIN32_LEAN_AND_MEAN
42 #define WIN32_LEAN_AND_MEAN 1
43 #endif
44 
45 #include <windows.h>
46 #include <winsock2.h>
47 #else
48 #include <unistd.h>
49 #endif
50 
51 #include <fstream>
52 #include <iostream>
53 #include <map>
54 #include <string>
55 #include <sstream>
56 #include <stdlib.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 
62 
63 using namespace crl::multisense;
64 
65 namespace { // anonymous
66 
67 std::string byte_to_binary(uint8_t x)
68 {
69  std::stringstream b;
70 
71  uint8_t z;
72  for (z = 128; z > 0; z >>= 1)
73  {
74  b << (((x & z) == z) ? "1" : "0");
75  }
76 
77  return b.str();
78 }
79 
80 std::string bytes_to_binary(uint64_t x, int bits)
81 {
82  std::string b;
83  uint8_t currentByte;
84  for(currentByte = 0; currentByte*8<bits; currentByte++)
85  {
86  b = byte_to_binary(static_cast<uint8_t> (x >> (currentByte*8))) + " " + b;
87  }
88 
89  return b.erase(0,(currentByte*8 - bits));
90 }
91 
92 void usage(const char *programNameP)
93 {
94  std::cerr << "USAGE: " << programNameP << " [<options>]" << std::endl;
95  std::cerr << "Where <options> are:" << std::endl;
96  std::cerr << "\t-a <ip_address> : ip address (default=10.66.171.21)" << std::endl;
97  std::cerr << "\t-s : set the calibration (default is query)" << std::endl;
98  std::cerr << "\t-l : set the left calibration" << std::endl;
99  std::cerr << "\t-r : set the right calibration" << std::endl;
100  std::cerr << "\t-e : set the right black level" << std::endl;
101  std::cerr << "\t-k : set the left black level" << std::endl;
102 
103  exit(-1);
104 }
105 
106 } // anonymous
107 
108 int main(int argc,
109  char **argvPP)
110 {
111  Status status = Status_Ok;
112  std::string ipAddress = "10.66.171.21";
113  std::string leftIn = "50";
114  std::string rightIn = "50";
115 
116  // The default value of the dark level offset as given in section 5.12.1 of the CMV2000 data sheet.
117  std::string leftBlackIn = "-61";
118  std::string rightBlackIn = "-61";
119  int left;
120  int right;
121  int leftBlack;
122  int rightBlack;
123  bool setCal=false;
124  bool setLeft=false;
125  bool setRight=false;
126  bool setLeftBlack=false;
127  bool setRightBlack=false;
128 
129  //
130  // Parse args
131 
132  int inArgument;
133 
134  while(-1 != (inArgument = getopt(argc, argvPP, "l:r:k:e:a:sy")))
135  switch(inArgument) {
136  case 'a': ipAddress = std::string(optarg); break;
137  case 'l': leftIn = std::string(optarg); setLeft=true; break;
138  case 'r': rightIn = std::string(optarg); setRight=true; break;
139  case 'k': leftBlackIn = std::string(optarg); setLeftBlack=true; break;
140  case 'e': rightBlackIn = std::string(optarg); setRightBlack=true; break;
141  case 's': setCal = true; break;
142  default: usage(*argvPP); break;
143  }
144 
145  //
146  // Verify options
147 
148  if (setCal && (!setLeft && !setRight && !setLeftBlack && !setRightBlack)) {
149  std::cerr << "Please specify a value to set using -l, -r, -e, or -k" << std::endl;
150  usage(*argvPP);
151  }
152 
153  //
154  // Initialize communications.
155  std::cout << "Attempting to establish communications with: " << ipAddress << std::endl;
156 
157  Channel *channelP = Channel::Create(ipAddress);
158  if (NULL == channelP) {
159  std::cerr << "Failed to establish communications with: " << ipAddress << std::endl;
160  return -1;
161  }
162 
163  //
164  // Query the current device calibration
165  image::SensorCalibration sensorCalibration;
166 
167  status = channelP->getSensorCalibration(sensorCalibration);
168  if (Status_Ok != status) {
169  std::cerr << "failed to query sensor calibration: " << Channel::statusString(status);
170  goto clean_out;
171  }
172 
173  //
174  // Modify the elements of the queried calibration depending on which
175  // parameters will be set
176  if (setCal && setLeft) {
177  left = atoi(leftIn.c_str());
178 
179  if (left > 255 || left < 0)
180  {
181  std::cerr << "Left sensor gain range is 0-255" << std::endl;
182  usage(*argvPP);
183  goto clean_out;
184  }
185 
186  sensorCalibration.adc_gain[0] = static_cast<uint8_t> (left);
187  }
188 
189  if (setCal && setRight) {
190  right = atoi(rightIn.c_str());
191 
192  if (right > 255 || right < 0)
193  {
194  std::cerr << "Right sensor gain range is 0-255" << std::endl;
195  usage(*argvPP);
196  goto clean_out;
197  }
198 
199  sensorCalibration.adc_gain[1] = static_cast<uint8_t> (right);
200  }
201 
202  if (setCal && setLeftBlack)
203  {
204  leftBlack = atoi(leftBlackIn.c_str());
205 
206  if (leftBlack > 8191 || leftBlack < -8192)
207  {
208  std::cerr << "Left black offset range is -8192 to 8191" << std::endl;
209  usage (*argvPP);
210  goto clean_out;
211  }
212 
213  // Per the CMV2000 data sheet, bl_offset is a 14-bit 2-complement integer
214  sensorCalibration.bl_offset[0] = static_cast<uint16_t> (leftBlack) & 0x7FFF;
215  }
216 
217  if (setCal && setRightBlack)
218  {
219  rightBlack = atoi(rightBlackIn.c_str());
220 
221  if (rightBlack > 8191 || rightBlack < -8192)
222  {
223  std::cerr << "Right black offset range is -8192 to 8191" << std::endl;
224  usage (*argvPP);
225  goto clean_out;
226  }
227 
228  // Per the CMV2000 data sheet, bl_offset is a 14-bit 2-complement integer
229  sensorCalibration.bl_offset[1] = static_cast<uint16_t> (rightBlack) & 0x7FFF;
230  }
231 
232 
233  if (false == setCal) {
234 
235  std::cout << "left sensor gain: " << static_cast<uint32_t>(sensorCalibration.adc_gain[0]) << std::endl;
236  std::cout << "right sensor gain: " << static_cast<uint32_t>(sensorCalibration.adc_gain[1]) << std::endl;
237  std::cout << "left sensor black level: " << static_cast<uint32_t>(sensorCalibration.bl_offset[0]) << std::endl;
238  std::cout << "right sensor black level: " << static_cast<uint32_t>(sensorCalibration.bl_offset[1]) << std::endl;
239  std::cout << "left sensor vramp: " << static_cast<uint32_t>(sensorCalibration.vramp[0]) << std::endl;
240  std::cout << "right sensor vramp: " << static_cast<uint32_t>(sensorCalibration.vramp[1]) << std::endl;
241  } else {
242  std::cout << "Setting :" << std::endl;
243  std::cout << "left sensor gain: " << static_cast<uint32_t>(sensorCalibration.adc_gain[0]) << " binary: " << byte_to_binary(sensorCalibration.adc_gain[0]) << std::endl;
244  std::cout << "right sensor gain: " << static_cast<uint32_t>(sensorCalibration.adc_gain[1]) << " binary: " << byte_to_binary(sensorCalibration.adc_gain[1]) << std::endl;
245  std::cout << "left sensor black level: " << static_cast<uint32_t>(sensorCalibration.bl_offset[0]) << " binary: " << bytes_to_binary(sensorCalibration.bl_offset[0], 14) << std::endl;
246  std::cout << "right sensor black level: " << static_cast<uint32_t>(sensorCalibration.bl_offset[1]) << " binary: " << bytes_to_binary(sensorCalibration.bl_offset[1], 14) << std::endl;
247 
248  status = channelP->setSensorCalibration(sensorCalibration);
249  if (Status_Ok != status) {
250  std::cerr << "failed to set sensor calibration: " << Channel::statusString(status) << std::endl;
251  goto clean_out;
252  }
253 
254  std::cout << "Sensor calibration successfully updated" << std::endl;
255  }
256 
257 clean_out:
258 
259  Channel::Destroy(channelP);
260  return 0;
261 }
static const char * statusString(Status status)
Definition: channel.cc:649
virtual Status getSensorCalibration(image::SensorCalibration &c)=0
static Channel * Create(const std::string &sensorAddress)
Definition: channel.cc:623
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:31
static CRL_CONSTEXPR Status Status_Ok
int main(int argc, char **argvPP)
def usage(progname)
virtual Status setSensorCalibration(const image::SensorCalibration &c)=0
static void Destroy(Channel *instanceP)
Definition: channel.cc:636
char * optarg
Definition: getopt.c:29


multisense_lib
Author(s):
autogenerated on Sun Mar 14 2021 02:34:50