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


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46