SaveImageUtility.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 #include <arpa/inet.h> // htons
47 #endif
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <signal.h>
52 #include <string.h>
53 #include <string>
54 #include <fstream>
55 #include <iostream>
56 #include <iomanip>
57 
59 
62 
63 using namespace crl::multisense;
64 
65 namespace { // anonymous
66 
67 volatile bool doneG = false;
68 
69 void usage(const char *programNameP)
70 {
71  std::cerr << "USAGE: " << programNameP << " [<options>]" << std::endl;
72  std::cerr << "Where <options> are:" << std::endl;
73  std::cerr << "\t-a <current_address> : CURRENT IPV4 address (default=10.66.171.21)" << std::endl;
74 
75  exit(-1);
76 }
77 
78 #ifdef WIN32
79 BOOL WINAPI signalHandler(DWORD dwCtrlType)
80 {
81  std::cerr << "Shutting down on signal: CTRL-C" << std::endl;
82  doneG = true;
83  return TRUE;
84 }
85 #else
86 void signalHandler(int sig)
87 {
88  std::cerr << "Shutting down on signal: " << strsignal(sig) << std::endl;
89  doneG = true;
90 }
91 #endif
92 
93 bool savePgm(const std::string& fileName,
94  uint32_t width,
95  uint32_t height,
96  uint32_t bitsPerPixel,
97  const void *dataP)
98 {
99  std::ofstream outputStream(fileName.c_str(), std::ios::binary | std::ios::out);
100 
101  if (false == outputStream.good()) {
102  std::cerr << "Failed to open \"" << fileName << "\"" << std::endl;
103  return false;
104  }
105 
106  const uint32_t imageSize = height * width;
107 
108  switch(bitsPerPixel) {
109  case 8:
110  {
111 
112  outputStream << "P5\n"
113  << width << " " << height << "\n"
114  << 0xFF << "\n";
115 
116  outputStream.write(reinterpret_cast<const char*>(dataP), imageSize);
117 
118  break;
119  }
120  case 16:
121  {
122  outputStream << "P5\n"
123  << width << " " << height << "\n"
124  << 0xFFFF << "\n";
125 
126  const uint16_t *imageP = reinterpret_cast<const uint16_t*>(dataP);
127 
128  for (uint32_t i=0; i<imageSize; ++i) {
129  uint16_t o = htons(imageP[i]);
130  outputStream.write(reinterpret_cast<const char*>(&o), sizeof(uint16_t));
131  }
132 
133  break;
134  }
135  }
136 
137  outputStream.close();
138  return true;
139 }
140 
141 void ppsCallback(const pps::Header& header,
142  void *userDataP)
143 {
144  std::cerr << "PPS: " << header.sensorTime << " ns" << std::endl;
145 }
146 
147 void laserCallback(const lidar::Header& header,
148  void *userDataP)
149 {
150 // std::cerr << "lidar: " << header.pointCount << std::endl;
151 }
152 
153 void imageCallback(const image::Header& header,
154  void *userDataP)
155 {
156  Channel *channelP = reinterpret_cast<Channel*>(userDataP);
157 
158 // double timeStamp = header.timeSeconds + 1e-6 * header.timeMicroSeconds;
159 
160  static int64_t lastFrameId = -1;
161 
162  if (-1 == lastFrameId)
163  {
164  savePgm("test.pgm",
165  header.width,
166  header.height,
167  header.bitsPerPixel,
168  header.imageDataP);
169  }
170 
171  lastFrameId = header.frameId;
172 
173  image::Histogram histogram;
174 
175  if (Status_Ok != channelP->getImageHistogram(header.frameId, histogram))
176  std::cerr << "failed to get histogram for frame " << header.frameId << std::endl;
177 }
178 
179 }; // anonymous
180 
181 int main(int argc,
182  char **argvPP)
183 {
184  std::string currentAddress = "10.66.171.21";
185  int32_t mtu = 7200;
186 
187 #if WIN32
188  SetConsoleCtrlHandler (signalHandler, TRUE);
189 #else
190  signal(SIGINT, signalHandler);
191 #endif
192 
193  //
194  // Parse args
195 
196  int c;
197 
198  while(-1 != (c = getopt(argc, argvPP, "a:m:")))
199  switch(c) {
200  case 'a': currentAddress = std::string(optarg); break;
201  case 'm': mtu = atoi(optarg); break;
202  default: usage(*argvPP); break;
203  }
204 
205  //
206  // Initialize communications.
207 
208  Channel *channelP = Channel::Create(currentAddress);
209  if (NULL == channelP) {
210  std::cerr << "Failed to establish communications with \"" << currentAddress << "\"" << std::endl;
211  return -1;
212  }
213 
214  //
215  // Query version
216 
217  Status status;
219  VersionType version;
220  status = channelP->getSensorVersion(version);
221  status = channelP->getVersionInfo(v);
222  if (Status_Ok != status) {
223  std::cerr << "Failed to query sensor version: " << Channel::statusString(status) << std::endl;
224  goto clean_out;
225  }
226 
227  std::cout << "API build date : " << v.apiBuildDate << "\n";
228  std::cout << "API version : 0x" << std::hex << std::setw(4) << std::setfill('0') << v.apiVersion << "\n";
229  std::cout << "Firmware build date : " << v.sensorFirmwareBuildDate << "\n";
230  std::cout << "Firmware version : 0x" << std::hex << std::setw(4) << std::setfill('0') << v.sensorFirmwareVersion << "\n";
231  std::cout << "Hardware version : 0x" << std::hex << v.sensorHardwareVersion << "\n";
232  std::cout << "Hardware magic : 0x" << std::hex << v.sensorHardwareMagic << "\n";
233  std::cout << "FPGA DNA : 0x" << std::hex << v.sensorFpgaDna << "\n";
234  std::cout << std::dec;
235 
236  //
237  // Change framerate
238 
239  {
240  image::Config cfg;
241 
242  status = channelP->getImageConfig(cfg);
243  if (Status_Ok != status) {
244  std::cerr << "Failed to get image config: " << Channel::statusString(status) << std::endl;
245  goto clean_out;
246  } else {
247 
248  cfg.setResolution(1024, 544);
249  cfg.setFps(30.0);
250 
251  status = channelP->setImageConfig(cfg);
252  if (Status_Ok != status) {
253  std::cerr << "Failed to configure sensor: " << Channel::statusString(status) << std::endl;
254  goto clean_out;
255  }
256  }
257  }
258 
259  //
260  // Change MTU
261 
262  status = channelP->setMtu(mtu);
263  if (Status_Ok != status) {
264  std::cerr << "Failed to set MTU to " << mtu << ": " << Channel::statusString(status) << std::endl;
265  goto clean_out;
266  }
267 
268  //
269  // Change trigger source
270 
271  status = channelP->setTriggerSource(Trigger_Internal);
272  if (Status_Ok != status) {
273  std::cerr << "Failed to set trigger source: " << Channel::statusString(status) << std::endl;
274  goto clean_out;
275  }
276 
277  //
278  // Add callbacks
279 
280  channelP->addIsolatedCallback(imageCallback, Source_All, channelP);
281  channelP->addIsolatedCallback(laserCallback, channelP);
282  channelP->addIsolatedCallback(ppsCallback, channelP);
283 
284  //
285  // Start streaming
286 
288  if (Status_Ok != status) {
289  std::cerr << "Failed to start streams: " << Channel::statusString(status) << std::endl;
290  goto clean_out;
291  }
292 
293  while(!doneG)
294  {
295  system::StatusMessage statusMessage;
296  status = channelP->getDeviceStatus(statusMessage);
297 
298  if (Status_Ok == status) {
299  std::cout << "Uptime: " << statusMessage.uptime << ", " <<
300  "SystemOk: " << statusMessage.systemOk << ", " <<
301  "FPGA Temp: " << statusMessage.fpgaTemperature << ", " <<
302  "Left Imager Temp: " << statusMessage.leftImagerTemperature << ", " <<
303  "Right Imager Temp: " << statusMessage.rightImagerTemperature << ", " <<
304  "Input Voltage: " << statusMessage.inputVoltage << ", " <<
305  "Input Current: " << statusMessage.inputCurrent << ", " <<
306  "FPGA Power: " << statusMessage.fpgaPower << ", " <<
307  "Logic Power: " << statusMessage.logicPower << ", " <<
308  "Imager Power: " << statusMessage.imagerPower << std::endl;
309  }
310 
311 
312  usleep(1e6);
313  }
314 
315  status = channelP->stopStreams(Source_All);
316  if (Status_Ok != status) {
317  std::cerr << "Failed to stop streams: " << Channel::statusString(status) << std::endl;
318  }
319 
320 clean_out:
321 
322  Channel::Destroy(channelP);
323  return 0;
324 }
virtual Status startStreams(DataSource mask)=0
static const char * statusString(Status status)
Definition: channel.cc:609
static CRL_CONSTEXPR TriggerSource Trigger_Internal
virtual Status stopStreams(DataSource mask)=0
static Channel * Create(const std::string &sensorAddress)
Definition: channel.cc:583
int main(int argc, char **argvPP)
virtual Status getImageHistogram(int64_t frameId, image::Histogram &histogram)=0
void setResolution(uint32_t w, uint32_t h)
virtual Status getVersionInfo(system::VersionInfo &v)=0
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
static CRL_CONSTEXPR DataSource Source_Lidar_Scan
virtual Status addIsolatedCallback(image::Callback callback, DataSource imageSourceMask, void *userDataP=NULL)=0
virtual Status setTriggerSource(TriggerSource s)=0
static CRL_CONSTEXPR Status Status_Ok
virtual Status setMtu(int32_t mtu)=0
virtual Status setImageConfig(const image::Config &c)=0
def usage(progname)
virtual Status getDeviceStatus(system::StatusMessage &status)=0
static void Destroy(Channel *instanceP)
Definition: channel.cc:596
char * optarg
Definition: getopt.c:32
virtual Status getImageConfig(image::Config &c)=0
virtual Status getSensorVersion(VersionType &version)=0
static CRL_CONSTEXPR DataSource Source_All
static CRL_CONSTEXPR DataSource Source_Luma_Rectified_Left


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