LidarCalUtility.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 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 *laserToSpindleNameP = "laser_T_spindle";
84 const char *cameraToSpindleFixedNameP = "camera_T_spindle_fixed";
85 
86 
87 std::ostream& writeLaserCal (std::ostream& stream, lidar::Calibration const& calibration)
88 {
89  stream << "%YAML:1.0\n";
90  writeMatrix (stream, laserToSpindleNameP, 4, 4, &calibration.laserToSpindle[0][0]);
91  writeMatrix (stream, cameraToSpindleFixedNameP, 4, 4, &calibration.cameraToSpindleFixed[0][0]);
92  return stream;
93 }
94 
95 
96 }; // anonymous
97 
98 int main(int argc,
99  char **argvPP)
100 {
101  std::string ipAddress = "10.66.171.21";
102  std::string calFile;
103  bool setCal=false;
104  bool prompt=true;
105 
106  //
107  // Parse args
108 
109  int c;
110 
111  while(-1 != (c = getopt(argc, argvPP, "a:f:sy")))
112  switch(c) {
113  case 'a': ipAddress = std::string(optarg); break;
114  case 'f': calFile = std::string(optarg); break;
115  case 's': setCal = true; break;
116  case 'y': prompt = false; break;
117  default: usage(*argvPP); break;
118  }
119 
120  //
121  // Verify options
122 
123  if (calFile.empty()) {
124  fprintf(stderr, "Must provide a file argument\n");
125  usage(*argvPP);
126  }
127 
128  if (true == setCal && false == fileExists(calFile)) {
129 
130  fprintf(stderr, "file not found: \"%s\"\n", calFile.c_str());
131  usage(*argvPP);
132  }
133 
134  if (false == setCal && true == prompt &&
135  true == fileExists(calFile)) {
136 
137  fprintf(stdout,
138  "\"%s\" already exists.\n\n"
139  "Really overwrite this file? (y/n): ",
140  calFile.c_str());
141  fflush(stdout);
142 
143  int c = getchar();
144  if ('Y' != c && 'y' != c) {
145  fprintf(stdout, "Aborting\n");
146  return 0;
147  }
148  }
149 
150  //
151  // Initialize communications.
152 
153  Channel *channelP = Channel::Create(ipAddress);
154  if (NULL == channelP) {
155  fprintf(stderr, "Failed to establish communications with \"%s\"\n",
156  ipAddress.c_str());
157  return -1;
158  }
159 
160  //
161  // Query version
162 
163  Status status;
164  VersionType version;
165 
166  status = channelP->getSensorVersion(version);
167  if (Status_Ok != status) {
168  fprintf(stderr, "failed to query sensor version: %s\n",
169  Channel::statusString(status));
170  goto clean_out;
171  }
172 
173  //
174  // Query
175 
176  if (false == setCal) {
177 
179 
180  status = channelP->getLidarCalibration(c);
181  if (Status_Ok != status) {
182  fprintf(stderr, "failed to query lidar calibration: %s\n",
183  Channel::statusString(status));
184  goto clean_out;
185  }
186 
187  std::ofstream cvFile (calFile.c_str (), std::ios_base::out | std::ios_base::trunc);
188 
189  if (!cvFile) {
190  fprintf(stderr, "failed to open '%s' for writing\n",
191  calFile.c_str());
192  goto clean_out;
193  }
194 
195  writeLaserCal (cvFile, c);
196 
197  cvFile.flush ();
198 
199  } else {
200 
201  std::map<std::string, std::vector<float> > data;
202  std::ifstream cvFile (calFile.c_str ());
203 
204  if (!cvFile) {
205  fprintf(stderr, "failed to open '%s' for reading\n",
206  calFile.c_str());
207  goto clean_out;
208  }
209 
210  parseYaml (cvFile, data);
211 
212  cvFile.close ();
213 
214  if (data[laserToSpindleNameP].size () != 4 * 4 ||
215  data[cameraToSpindleFixedNameP].size () != 4 * 4) {
216 
217  fprintf(stderr, "calibration matrices incomplete in %s, "
218  "expecting two 4x4 matrices\n", calFile.c_str());
219  goto clean_out;
220  }
221 
223 
224  memcpy (&c.laserToSpindle[0][0], &data[laserToSpindleNameP].front (), data[laserToSpindleNameP].size () * sizeof (float));
225  memcpy (&c.cameraToSpindleFixed[0][0], &data[cameraToSpindleFixedNameP].front (), data[cameraToSpindleFixedNameP].size () * sizeof (float));
226 
227  status = channelP->setLidarCalibration(c);
228  if (Status_Ok != status) {
229  fprintf(stderr, "failed to set lidar calibration: %s\n",
230  Channel::statusString(status));
231  goto clean_out;
232  }
233 
234  fprintf(stdout, "Lidar calibration successfully updated\n");
235  }
236 
237 clean_out:
238 
239  Channel::Destroy(channelP);
240  return 0;
241 }
static const char * statusString(Status status)
Definition: channel.cc:609
int main(int argc, char **argvPP)
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 getLidarCalibration(lidar::Calibration &c)=0
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
static CRL_CONSTEXPR Status Status_Ok
def usage(progname)
virtual Status setLidarCalibration(const lidar::Calibration &c)=0
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