ExternalCalUtility.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 
59 
60 using namespace crl::multisense;
61 
62 namespace { // anonymous
63 
64 void usage(const char *programNameP)
65 {
66  fprintf(stderr,
67  "USAGE: %s -f <calibration_file> [<options>]\n",
68  programNameP);
69  fprintf(stderr, "Where <options> are:\n");
70  fprintf(stderr, "\t-a <ip_address> : ip address (default=10.66.171.21)\n");
71  fprintf(stderr, "\t-s : set the external calibration (default is query)\n");
72  fprintf(stderr, "\t-y : disable confirmation prompts\n");
73 
74  exit(-1);
75 }
76 
77 bool fileExists(const std::string& name)
78 {
79  struct stat sbuf;
80  return (0 == stat(name.c_str(), &sbuf));
81 }
82 
83 const char *externalCalibrationNameP = "external_calibration";
84 
85 
86 std::ostream& writeCal (std::ostream& stream, system::ExternalCalibration const& calibration)
87 {
88  stream << "%YAML:1.0\n";
89 
90  float tmpCal[6];
91  tmpCal[0] = calibration.x;
92  tmpCal[1] = calibration.y;
93  tmpCal[2] = calibration.z;
94  tmpCal[3] = calibration.roll;
95  tmpCal[4] = calibration.pitch;
96  tmpCal[5] = calibration.yaw;
97 
98  writeMatrix (stream, externalCalibrationNameP, 1, 6, &tmpCal[0]);
99  return stream;
100 }
101 
102 
103 }; // anonymous
104 
105 int main(int argc,
106  char **argvPP)
107 {
108  std::string ipAddress = "10.66.171.21";
109  std::string calFile;
110  bool setCal=false;
111  bool prompt=true;
112 
113  //
114  // Parse args
115 
116  int c;
117 
118  while(-1 != (c = getopt(argc, argvPP, "a:f:sy")))
119  switch(c) {
120  case 'a': ipAddress = std::string(optarg); break;
121  case 'f': calFile = std::string(optarg); break;
122  case 's': setCal = true; break;
123  case 'y': prompt = false; break;
124  default: usage(*argvPP); break;
125  }
126 
127  //
128  // Verify options
129 
130  if (calFile.empty()) {
131  fprintf(stderr, "Must provide a file argument\n");
132  usage(*argvPP);
133  }
134 
135  if (true == setCal && false == fileExists(calFile)) {
136 
137  fprintf(stderr, "file not found: \"%s\"\n", calFile.c_str());
138  usage(*argvPP);
139  }
140 
141  if (false == setCal && true == prompt &&
142  true == fileExists(calFile)) {
143 
144  fprintf(stdout,
145  "\"%s\" already exists.\n\n"
146  "Really overwrite this file? (y/n): ",
147  calFile.c_str());
148  fflush(stdout);
149 
150  int c = getchar();
151  if ('Y' != c && 'y' != c) {
152  fprintf(stdout, "Aborting\n");
153  return 0;
154  }
155  }
156 
157  //
158  // Initialize communications.
159 
160  Channel *channelP = Channel::Create(ipAddress);
161  if (NULL == channelP) {
162  fprintf(stderr, "Failed to establish communications with \"%s\"\n",
163  ipAddress.c_str());
164  return -1;
165  }
166 
167  //
168  // Query version
169 
170  Status status;
171  VersionType version;
172 
173  status = channelP->getSensorVersion(version);
174  if (Status_Ok != status) {
175  fprintf(stderr, "failed to query sensor version: %s\n",
176  Channel::statusString(status));
177  goto clean_out;
178  }
179 
180  //
181  // Query
182 
183  if (false == setCal) {
184 
186 
187  status = channelP->getExternalCalibration(c);
188  if (Status_Ok != status) {
189  fprintf(stderr, "failed to query external calibration: %s\n",
190  Channel::statusString(status));
191  goto clean_out;
192  }
193 
194  std::ofstream cvFile (calFile.c_str (), std::ios_base::out | std::ios_base::trunc);
195 
196  if (!cvFile) {
197  fprintf(stderr, "failed to open '%s' for writing\n",
198  calFile.c_str());
199  goto clean_out;
200  }
201 
202  writeCal (cvFile, c);
203 
204  cvFile.flush ();
205 
206  } else {
207 
208  std::map<std::string, std::vector<float> > data;
209  std::ifstream cvFile (calFile.c_str ());
210 
211  if (!cvFile) {
212  fprintf(stderr, "failed to open '%s' for reading\n",
213  calFile.c_str());
214  goto clean_out;
215  }
216 
217  parseYaml (cvFile, data);
218 
219  cvFile.close ();
220 
221  if (data[externalCalibrationNameP].size () != 6 ) {
222 
223  fprintf(stderr, "calibration matrices incomplete in %s, "
224  "expecting two 4x4 matrices\n", calFile.c_str());
225  goto clean_out;
226  }
227 
229 
230  c.x = data[externalCalibrationNameP][0];
231  c.y = data[externalCalibrationNameP][1];
232  c.z = data[externalCalibrationNameP][2];
233  c.roll = data[externalCalibrationNameP][3];
234  c.pitch = data[externalCalibrationNameP][4];
235  c.yaw = data[externalCalibrationNameP][5];
236 
237  status = channelP->setExternalCalibration(c);
238  if (Status_Ok != status) {
239  fprintf(stderr, "failed to set external calibration: %s\n",
240  Channel::statusString(status));
241  goto clean_out;
242  }
243 
244  fprintf(stdout, "External calibration successfully updated\n");
245  }
246 
247 clean_out:
248 
249  Channel::Destroy(channelP);
250  return 0;
251 }
static const char * statusString(Status status)
Definition: channel.cc:609
virtual Status getExternalCalibration(system::ExternalCalibration &calibration)=0
static Channel * Create(const std::string &sensorAddress)
Definition: channel.cc:583
std::ostream & writeMatrix(std::ostream &stream, std::string const &name, uint32_t rows, uint32_t columns, T const *data)
virtual Status setExternalCalibration(const system::ExternalCalibration &calibration)=0
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
int main(int argc, char **argvPP)
static CRL_CONSTEXPR Status Status_Ok
def usage(progname)
std::istream & parseYaml(std::istream &stream, std::map< std::string, std::vector< float > > &data)
static void Destroy(Channel *instanceP)
Definition: channel.cc:596
char * optarg
Definition: getopt.c:32
virtual Status getSensorVersion(VersionType &version)=0


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