-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBASLER-EXEC.cpp
408 lines (327 loc) · 9.35 KB
/
BASLER-EXEC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Control Basler camera using pylon SDK and use OpenCV library
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <cstddef>
#include <unistd.h>
// Include Serial Communication
#include <wiringPi.h>
#include <wiringSerial.h>
// Include Basler API
#include <pylon/PylonIncludes.h>
#include <pylon/gige/BaslerGigEInstantCamera.h>
#include "include/SampleImageCreator.h"
// Include JSON
#include "include/json.hpp"
// Include files OpenCV
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
struct INFO{
std::string DATE;
std::string LAT;
std::string LNG;
std::string BIN;
std::string JPG;
std::string SN;
std::string MODEL;
std::string BATT;
};
uint32_t imageWidth;
uint32_t imageHeight;
cv::Mat Img, roiImg;
bool enableGPS(int fd, bool onoff);
INFO GetGPS(int fd);
int CameraConfig();
INFO CameraFrame(INFO gps);
void MatWrite(const std::string& filename, const cv::Mat& mat);
cv::Mat MatRead(const std::string& filename);
std::string exec(std::string cmd);
// Namespace for using pylon objects.
using namespace GenApi;
using namespace Pylon;
using namespace std;
using json = nlohmann::json ;
// The name of the pylon feature stream file.
std::string version = "1.0";
const char Fileconfig[] = "ConfigCamera.pfs";
std::string path = "/media/usb-drive/";
const int ledPin = 13; // FLASH LED pin 13,
const int butPin = 26; // TRIGGER button pin 26,
int main(int argc, char* argv[]){
// The exit code of the sample application.
int fd;
wiringPiSetupGpio(); // Initialize wiringPi -- using Broadcom pin numbers
pinMode(ledPin, OUTPUT); // Set regular LED as output
pinMode(butPin, INPUT); // Set button as INPUT
pullUpDnControl(butPin, PUD_UP); // Enable pull-up resistor on button
while ((fd = serialOpen ("/dev/ttyS0", 115200)) < 1)
{
printf ("Connecting to serial port:\n") ;
delay (1000) ;
}
printf ("\nConnect port successful !\n::ttyS0 to 115200 baud::\n");
/* Habilitar GPS via UART */
enableGPS(fd, true);
/* Configurar Camara */
CameraConfig();
std::cout << "\n --- TRIGGER: Trigger Mode ---" << endl;
while(1)
{
if (digitalRead(butPin)) // Button is released if this returns 1
{
std::cout << "Trigger adquisition OFF" << std::endl;
digitalWrite(ledPin, LOW);
delay(2500);
}
else // If digitalRead returns 0, button is pressed
{
delay(100);
std::cout << "Trigger adquisition ON" << std::endl;
/* OBTENER GPS: DATE, LAT y LNG */
INFO gps = GetGPS(fd);
delay(100);
std::cout << "DATE: " << gps.DATE << std::endl;
std::cout << "Latitude: " << gps.LAT << std::endl;
std::cout << "Longitude: " << gps.LNG << std::endl;
digitalWrite(ledPin, HIGH); // Turn LED ON
// ESTRUCTURA JSON PARA METADATOS
INFO MD5 =CameraFrame(gps);
delay(100);
std::cout << "LED OFF" << std::endl;
digitalWrite(ledPin, LOW); // Turn LED FLASH ON, via RELE
// ESTRUCTURA JSON PARA METADATOS
json js;
js["id"] = gps.DATE;
js["SWVersion"] = version;
js["image"]["MD5"]["JPG"] = MD5.JPG;
js["image"]["MD5"]["BIN"] = MD5.BIN;
js["image"]["LAT"] = gps.LAT;
js["image"]["LNG"] = gps.LNG;
js["device"]["SN"] = MD5.SN;
js["device"]["Model"] = MD5.MODEL;
// GUARDAR JSON
std::cout << "\n --- JSON: DATA---" << std::endl;
std::ofstream ofs;
ofs.open (path + gps.DATE + ".json", std::ofstream::out | std::ofstream::app);
ofs << js;
ofs.close();
cout << "JSON info Saved"<< endl;
delay(500);
}
}
return 0;
}
bool enableGPS(int fd, bool onoff){
if (onoff){
printf ("\n --- GPS: ENABLE---\n") ;
serialPuts(fd,"AT+CGNSPWR=1\r") ;
delay (100);
} else if (!onoff){
printf ("\nGPS:DISABLE\n") ;
serialPuts(fd, "AT+CGPS=0\r") ;
delay (100) ;
}
while (serialDataAvail (fd))
{
printf ("%c", (char)serialGetchar (fd)) ;
fflush (stdout) ;
}
}
INFO GetGPS(int fd){
char GPS[300];
/* Obtener GPS mediante AT+CGNSINF */
printf ("\n --- GPS:Get GPS --- \n") ;
fflush (stdout) ;
serialPuts(fd, "AT+CGNSINF\r") ;
delay (100) ;
int k=0;
while (serialDataAvail (fd))
{
GPS[k] = (char)serialGetchar(fd);
fflush (stdout);
k++;
}
delay(100);
int j=0;
std::string date;
std::string lat;
std::string lon;
char *token = std::strtok(GPS, ",");
while (token)
{
token = std::strtok(NULL, ",");
if (j==1){
date = token; //.find(".");
}
if (j==2){
lat = token;
}
if (j==3){
lon = token;
}
j++;
}
date = date.substr(0,14);
INFO gps;
gps.DATE = date;
gps.LAT = lat;
gps.LNG = lon;
return gps;
}
int CameraConfig(){
std::cout << "\n --- CAMERA: CONFIG---" << std::endl;
int exitCode = 0;
PylonInitialize();
try
{
CTlFactory& tlFactory = CTlFactory::GetInstance();
// Get all attached devices and exit application if no device is found.
DeviceInfoList_t devices;
if ( tlFactory.EnumerateDevices(devices) == 0 ){
throw RUNTIME_EXCEPTION( "No camera present.");
}
int sensorid=-1;
for ( size_t i = 0; i < devices.size(); ++i){
if (devices[i].GetModelName().compare("acA4096-11gc")==0){
sensorid=i;
break;
}
}
CInstantCamera camera(tlFactory.CreateDevice( devices[ sensorid ]));
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
//Open the camera and save config.
camera.Open();
cout << "Loading file config to camera's..."<< endl;
CFeaturePersistence::Load(Fileconfig, &camera.GetNodeMap(), true );
cout << "Saved Config \n"<< endl;
camera.Close();
}
catch (const GenericException &e)
{
// Error handling
cerr << "An exception occurred." << endl
<< e.GetDescription() << endl;
exitCode = 1;
}
// Releases all pylon resources.
PylonTerminate();
return exitCode;
}
INFO CameraFrame(INFO gps){
//INFO gps
std::cout << "\n --- CAMERA: FRAME---" << std::endl;
INFO MD5;
int exitCode = 0;
PylonInitialize();
try
{
CGrabResultPtr ptrGrabResult;
CTlFactory& tlFactory = CTlFactory::GetInstance();
//Get all attached devices and exit application if no device is found.
DeviceInfoList_t devices;
if ( tlFactory.EnumerateDevices(devices) == 0 ){
throw RUNTIME_EXCEPTION( "No camera present.");
}
int sensorid=-1;
for ( size_t i = 0; i < devices.size(); ++i){
if (devices[i].GetModelName().compare("acA4096-11gc")==0){
sensorid=i;
break;
}
}
CInstantCamera camera(tlFactory.CreateDevice(devices[ sensorid ]));
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
MD5.MODEL = camera.GetDeviceInfo().GetModelName();
MD5.SN = camera.GetDeviceInfo().GetSerialNumber();
camera.GrabOne(5000, ptrGrabResult);
if (ptrGrabResult->GrabSucceeded())
{
//CImagePersistence::Save(ImageFileFormat_Tiff, "GrabbedImage.tiff", ptrGrabResult);
imageWidth = ptrGrabResult->GetWidth();
imageHeight = ptrGrabResult->GetHeight();
uint16_t *pImageBuffer = (uint16_t *)ptrGrabResult->GetBuffer();
cv::Mat ImgBayer16UC1(imageHeight, imageWidth, CV_16UC1, pImageBuffer, cv::Mat::AUTO_STEP);
cv::Mat Img16UC3(imageHeight, imageWidth, CV_16UC3);
cv::cvtColor(ImgBayer16UC1,Img16UC3,cv::COLOR_BayerBG2BGR);
Img16UC3.convertTo(Img, CV_8UC3, 1.0/16);
cv::imwrite(path + gps.DATE + ".jpg", Img);
cout << "JPG frame Saved"<< endl;
MD5.JPG = exec("md5sum " + path + gps.DATE + ".jpg");
MD5.JPG = MD5.JPG.substr(0,32);
MatWrite(path + gps.DATE + ".bin", Img);
cout << "BIN frame Saved"<< endl;
MD5.BIN = exec("md5sum " + path + gps.DATE + ".bin");
MD5.BIN = MD5.BIN.substr(0,32);
}
}
catch (const GenericException &e)
{
// Error handling
cerr << "An exception occurred." << endl
<< e.GetDescription() << endl;
exitCode = 1;
}
// Releases all pylon resources.
PylonTerminate();
return MD5;
}
void MatWrite(const std::string& filename, const cv::Mat& mat){
std::ofstream fs(filename, std::fstream::binary);
// Header
int type = mat.type();
int channels = mat.channels();
fs.write((char*)&mat.rows, sizeof(int)); // rows
fs.write((char*)&mat.cols, sizeof(int)); // cols
fs.write((char*)&type, sizeof(int)); // type
fs.write((char*)&channels, sizeof(int)); // channels
// Data
if (mat.isContinuous())
{
fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart));
}
else
{
int rowsz = CV_ELEM_SIZE(type) * mat.cols;
for (int r = 0; r < mat.rows; ++r)
{
fs.write(mat.ptr<char>(r), rowsz);
}
}
}
cv::Mat MatRead(const std::string& filename){
std::ifstream fs(filename, std::fstream::binary);
// Header
int rows, cols, type, channels;
fs.read((char*)&rows, sizeof(int)); // rows
fs.read((char*)&cols, sizeof(int)); // cols
fs.read((char*)&type, sizeof(int)); // type
fs.read((char*)&channels, sizeof(int)); // channels
// Data
cv::Mat mat(rows, cols, type);
fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);
cv::imwrite("bin2jpg.jpg", mat);
return mat;
}
std::string exec(std::string cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}