forked from tiiuae/PX4-SITL_gazebo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgazebo_camera_manager_plugin.cpp
781 lines (711 loc) · 28.6 KB
/
gazebo_camera_manager_plugin.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
* Copyright (C) 2016 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "gazebo_camera_manager_plugin.h"
#include <math.h>
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace gazebo;
using namespace cv;
// #define DEBUG_MESSAGE_IO
GZ_REGISTER_SENSOR_PLUGIN(CameraManagerPlugin)
static void* start_thread(void* param) {
CameraManagerPlugin* plugin = (CameraManagerPlugin*)param;
plugin->cameraThread();
return nullptr;
}
CameraManagerPlugin::CameraManagerPlugin()
: SensorPlugin()
{
}
CameraManagerPlugin::~CameraManagerPlugin()
{
_parentSensor.reset();
_camera.reset();
}
void CameraManagerPlugin::Load(sensors::SensorPtr sensor, sdf::ElementPtr sdf)
{
if (!sensor)
gzerr << "Invalid sensor pointer.\n";
_parentSensor =
#if GAZEBO_MAJOR_VERSION >= 7
std::dynamic_pointer_cast<sensors::CameraSensor>(sensor);
#else
boost::dynamic_pointer_cast<sensors::CameraSensor>(sensor);
#endif
if (!_parentSensor)
{
gzerr << "CameraManagerPlugin requires a CameraSensor.\n";
}
#if GAZEBO_MAJOR_VERSION >= 7
_camera = _parentSensor->Camera();
#else
_camera = _parentSensor->GetCamera();
#endif
if (!_parentSensor)
{
gzerr << "CameraManagerPlugin not attached to a camera sensor\n";
return;
}
_scene = _camera->GetScene();
#if GAZEBO_MAJOR_VERSION >= 8
_lastImageTime = _scene->SimTime();
#else
_lastImageTime = _scene->GetSimTime();
#endif
#if GAZEBO_MAJOR_VERSION >= 7
_width = _camera->ImageWidth();
_height = _camera->ImageHeight();
_depth = _camera->ImageDepth();
_format = _camera->ImageFormat();
#else
_width = _camera->GetImageWidth();
_height = _camera->GetImageHeight();
_depth = _camera->GetImageDepth();
_format = _camera->GetImageFormat();
#endif
if (sdf->HasElement("robotNamespace")) {
_namespace = sdf->GetElement("robotNamespace")->Get<std::string>();
} else {
gzwarn << "[gazebo_geotagging_images_camera_plugin] Please specify a robotNamespace.\n";
}
_destWidth = _width;
if (sdf->HasElement("width")) {
_destWidth = sdf->GetElement("width")->Get<int>();
}
_destHeight = _height;
if (sdf->HasElement("height")) {
_destHeight = sdf->GetElement("height")->Get<int>();
}
if (sdf->HasElement("maximum_zoom")) {
_maxZoom = sdf->GetElement("maximum_zoom")->Get<float>();
}
if (sdf->HasElement("video_uri")) {
_videoURI = sdf->GetElement("video_uri")->Get<int>();
}
if (sdf->HasElement("system_id")) {
_systemID = sdf->GetElement("system_id")->Get<int>();
}
if (sdf->HasElement("cam_component_id")) {
_componentID = sdf->GetElement("cam_component_id")->Get<int>();
}
if (sdf->HasElement("mavlink_cam_udp_port")) {
_mavlinkCamPort = sdf->GetElement("mavlink_cam_udp_port")->Get<int>();
}
//check if exiftool exists
if (system("exiftool -ver &>/dev/null") != 0) {
gzerr << "exiftool not found. geotagging_images plugin will be disabled" << endl;
gzerr << "On Ubuntu, use 'sudo apt-get install libimage-exiftool-perl' to install" << endl;
return;
}
_node_handle = transport::NodePtr(new transport::Node());
_node_handle->Init();
_parentSensor->SetActive(true);
// Get the root model name
const string scopedName = _parentSensor->ParentName();
vector<string> names_splitted;
boost::split(names_splitted, scopedName, boost::is_any_of("::"));
names_splitted.erase(std::remove_if(begin(names_splitted), end(names_splitted),
[](const string& name)
{ return name.size() == 0; }), end(names_splitted));
std::string rootModelName = names_splitted.front(); // The first element is the name of the root model
// the second to the last name is the model name
const std::string parentSensorModelName = names_splitted.rbegin()[1];
_newFrameConnection = _camera->ConnectNewImageFrame(
boost::bind(&CameraManagerPlugin::OnNewFrame, this, _1));
_gpsSub = _node_handle->Subscribe("~/" + rootModelName + "/link/gps", &CameraManagerPlugin::OnNewGpsPosition, this);
_storageDir = "frames";
boost::filesystem::remove_all(_storageDir); //clear existing images
boost::filesystem::create_directory(_storageDir);
if (_init_udp(sdf)) {
// Start UDP thread
pthread_t threadId;
pthread_create(&threadId, NULL, start_thread, this);
}
}
void CameraManagerPlugin::OnNewGpsPosition(GpsPtr& gps_msg) {
_lastGpsPosition.X() = gps_msg->latitude_deg();
_lastGpsPosition.Y() = gps_msg->longitude_deg();
_lastGpsPosition.Z() = gps_msg->altitude();
//gzdbg << "got gps pos: "<<_lastGpsPosition.X() <<", "<<lastGpsPosition.Y() <<endl;
}
void CameraManagerPlugin::OnNewFrame(const unsigned char * image)
{
_captureMutex.lock();
// Are we capturing at all?
if (_captureMode == CAPTURE_DISABLED) {
_captureMutex.unlock();
return;
}
_captureMutex.unlock();
// Check for time lapse capture
#if GAZEBO_MAJOR_VERSION >= 8
common::Time currentTime = _scene->SimTime();
#else
common::Time currentTime = _scene->GetSimTime();
#endif
double elapsed = currentTime.Double() - _lastImageTime.Double();
if (_captureMode == CAPTURE_ELAPSED && (elapsed < _captureInterval)) {
return;
}
_lastImageTime = currentTime;
#if GAZEBO_MAJOR_VERSION >= 7
image = _camera->ImageData(0);
#else
image = _camera->GetImageData(0);
#endif
Mat frame = Mat(_height, _width, CV_8UC3);
Mat frameBGR = Mat(_height, _width, CV_8UC3);
frame.data = (uchar*)image; //frame has not the right color format yet -> convert
cvtColor(frame, frameBGR, COLOR_RGB2BGR);
char file_name[256];
snprintf(file_name, sizeof(file_name), "%s/DSC%05i.jpg", _storageDir.c_str(), _imageCounter);
if (_destWidth != _width || _destHeight != _height) {
Mat frameResized;
cv::Size size(_destWidth, _destHeight);
cv::resize(frameBGR, frameResized, size);
imwrite(file_name, frameResized);
} else {
imwrite(file_name, frameBGR);
}
char gps_tag_command[1024];
double lat = _lastGpsPosition.X();
char north_south = 'N', east_west = 'E';
double lon = _lastGpsPosition.Y();
if (lat < 0.) {
lat = -lat;
north_south = 'S';
}
if (lon < 0.) {
lon = -lon;
east_west = 'W';
}
snprintf(gps_tag_command, sizeof(gps_tag_command),
"exiftool -gpslatituderef=%c -gpslongituderef=%c -gpsaltituderef=above -gpslatitude=%.9lf -gpslongitude=%.9lf"
// " -gpsdatetime=now -gpsmapdatum=WGS-84"
" -datetimeoriginal=now -gpsdop=0.8"
" -gpsmeasuremode=3-d -gpssatellites=13 -gpsaltitude=%.3lf -overwrite_original %s &>/dev/null",
north_south, east_west, lat, lon, _lastGpsPosition.Z(), file_name);
if (system(gps_tag_command) < 0) {
gzerr << "gps tag command failed" << endl;
return;
}
gzmsg << "Took picture: " << file_name << endl;
// Send indication to GCS
mavlink_message_t msg;
mavlink_msg_camera_image_captured_pack_chan(
_systemID,
_componentID,
MAVLINK_COMM_1,
&msg,
currentTime.Double() * 1e3, // time boot ms
currentTime.Double() * 1e6, // time UTC
1, // camera ID
lat * 1e7,
lon * 1e7,
_lastGpsPosition.Z(),
0, // relative alt
0, // q[4]
_imageCounter,
1, // result
0 // file_url
);
// Send to GCS port directly
_send_mavlink_message(&msg);
++_imageCounter;
_captureMutex.lock();
if (_captureMode == CAPTURE_SINGLE) {
_captureMode = CAPTURE_DISABLED;
} else {
// _captureCount == 0 is infinite
if (_captureCount && --_captureCount < 1) {
_captureCount = 0;
_captureMode = CAPTURE_DISABLED;
}
}
if (_captureMode == CAPTURE_DISABLED) {
gzdbg << "Done with image capture\n";
}
_captureMutex.unlock();
// Send Capture Status
_send_capture_status();
}
void CameraManagerPlugin::_handle_message(mavlink_message_t *msg, struct sockaddr* srcaddr)
{
#if defined(DEBUG_MESSAGE_IO)
sockaddr_in* foo = (sockaddr_in*)srcaddr;
if (msg->sysid == 255) {
gzdbg << "Message " << std::to_string(msg->msgid).c_str() <<
" " << std::to_string(msg->sysid).c_str() << " " << std::to_string(msg->compid).c_str() <<
" from: " << inet_ntoa(foo->sin_addr) << ":" << std::to_string(ntohs(foo->sin_port)).c_str() << endl;
}
#endif
switch (msg->msgid) {
case MAVLINK_MSG_ID_COMMAND_LONG:
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(msg, &cmd);
mavlink_command_long_t digicam_ctrl_cmd;
if (cmd.target_component == _componentID) {
switch (cmd.command) {
case MAV_CMD_IMAGE_START_CAPTURE:
_handle_take_photo(msg, srcaddr);
break;
case MAV_CMD_IMAGE_STOP_CAPTURE:
_handle_stop_take_photo(msg, srcaddr);
break;
case MAV_CMD_REQUEST_CAMERA_INFORMATION:
_handle_camera_info(msg, srcaddr);
break;
case MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS:
_handle_request_camera_capture_status(msg, srcaddr);
break;
case MAV_CMD_REQUEST_STORAGE_INFORMATION:
_handle_storage_info(msg, srcaddr);
break;
case MAV_CMD_REQUEST_CAMERA_SETTINGS:
_handle_request_camera_settings(msg, srcaddr);
break;
case MAV_CMD_REQUEST_VIDEO_STREAM_INFORMATION:
_handle_request_video_stream_information(msg, srcaddr);
break;
case MAV_CMD_REQUEST_VIDEO_STREAM_STATUS:
_handle_request_video_stream_status(msg, srcaddr);
break;
case MAV_CMD_SET_CAMERA_MODE:
_handle_set_camera_mode(msg, srcaddr);
break;
case MAV_CMD_SET_CAMERA_ZOOM:
//Control the Zoom of the camera
_handle_camera_zoom(msg, srcaddr);
break;
case MAV_CMD_RESET_CAMERA_SETTINGS:
// Just ACK and ignore
_send_cmd_ack(msg->sysid, msg->compid, MAV_CMD_RESET_CAMERA_SETTINGS, MAV_RESULT_ACCEPTED, srcaddr);
break;
case MAV_CMD_STORAGE_FORMAT:
// Just ACK and ignore
_send_cmd_ack(msg->sysid, msg->compid, MAV_CMD_STORAGE_FORMAT, MAV_RESULT_ACCEPTED, srcaddr);
break;
case MAV_CMD_VIDEO_START_STREAMING:
// Just ACK and ignore
_send_cmd_ack(msg->sysid, msg->compid, MAV_CMD_VIDEO_START_STREAMING, MAV_RESULT_ACCEPTED, srcaddr);
break;
case MAV_CMD_VIDEO_STOP_STREAMING:
// Just ACK and ignore
_send_cmd_ack(msg->sysid, msg->compid, MAV_CMD_VIDEO_STOP_STREAMING, MAV_RESULT_ACCEPTED, srcaddr);
break;
}
}
break;
}
}
void CameraManagerPlugin::_send_mavlink_message(const mavlink_message_t *message, struct sockaddr* srcaddr)
{
struct sockaddr* target = srcaddr ? srcaddr : (struct sockaddr *)&_gcsaddr;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
int packetlen = mavlink_msg_to_send_buffer(buffer, message);
ssize_t len = sendto(_fd, buffer, packetlen, 0, target, sizeof(_gcsaddr));
if (len <= 0) {
gzerr << "Failed sending mavlink message" << endl;
#if defined(DEBUG_MESSAGE_IO)
} else {
sockaddr_in* foo = (sockaddr_in*)target;
gzdbg << "Message " << std::to_string(message->msgid).c_str() <<
" " << std::to_string(message->sysid).c_str() << " " << std::to_string(message->compid).c_str() <<
" to: " << inet_ntoa(foo->sin_addr) << ":" << std::to_string(ntohs(foo->sin_port)).c_str() << endl;
#endif
}
}
void CameraManagerPlugin::_send_cmd_ack(uint8_t target_sysid, uint8_t target_compid, uint16_t cmd, unsigned char result, struct sockaddr* srcaddr)
{
mavlink_message_t msg;
mavlink_msg_command_ack_pack_chan(
_systemID,
_componentID,
MAVLINK_COMM_1,
&msg,
cmd,
result,
100,
0,
target_sysid,
target_compid);
_send_mavlink_message(&msg, srcaddr);
}
void CameraManagerPlugin::_send_heartbeat()
{
mavlink_message_t msg;
mavlink_msg_heartbeat_pack_chan(_systemID, _componentID, MAVLINK_COMM_1, &msg, MAV_TYPE_GENERIC, MAV_AUTOPILOT_GENERIC, 0, 0, 0);
// Send to GCS port directly
_send_mavlink_message(&msg);
// Gazebo log output is using buffered IO for some reason
fflush(stdout);
fflush(stderr);
}
void CameraManagerPlugin::cameraThread() {
mavlink_status_t status;
mavlink_message_t msg;
unsigned char buffer[16 * 1024];
while (true) {
::poll(&_fds[0], (sizeof(_fds[0]) / sizeof(_fds[0])), 1000);
if (_fds[0].revents & POLLIN) {
struct sockaddr srcaddr;
socklen_t addrlen = sizeof(srcaddr);
int len = recvfrom(_fd, buffer, sizeof(buffer), 0, (struct sockaddr *)&srcaddr, &addrlen);
if (len > 0) {
for (unsigned i = 0; i < len; ++i)
{
if (mavlink_parse_char(MAVLINK_COMM_1, buffer[i], &msg, &status))
{
// have a message, handle it
_handle_message(&msg, &srcaddr);
memset(&status, 0, sizeof(status));
memset(&msg, 0, sizeof(msg));
}
}
}
}
// Heartbeat
#if GAZEBO_MAJOR_VERSION >= 8
common::Time current_time = _scene->SimTime();
#else
common::Time current_time = _scene->GetSimTime();
#endif
double elapsed = (current_time - _last_heartbeat).Double();
if (elapsed > 1.0) {
_last_heartbeat = current_time;
_send_heartbeat();
}
//Move camera zoom incase of continuous zoom
// _zoom_cmd is set by MAV_CMD_SET_CAMERA_ZOOM
if (_zoom_cmd!=0) {
_zoom = std::max(std::min(float(_zoom + 0.05 * _zoom_cmd), _maxZoom), 1.0f);
_camera->SetHFOV(_hfov / _zoom);
}
}
}
bool CameraManagerPlugin::_init_udp(sdf::ElementPtr sdf) {
//-- Target
uint32_t mavlink_addr = htonl(INADDR_LOOPBACK);
/* TODO: We need to find if the system is setup for broadcast
and add the proper socket options and broadcast address
if that's the case. It's hardcoded to loopback for now.
if (sdf->HasElement("mavlink_telem_addr")) {
std::string mavlink_addr = sdf->GetElement("mavlink_telem_addr")->Get<std::string>();
if (mavlink_addr != "INADDR_ANY") {
mavlink_addr_ = inet_addr(mavlink_addr.c_str());
if (mavlink_addr_ == INADDR_NONE) {
fprintf(stderr, "invalid mavlink_addr \"%s\"\n", mavlink_addr.c_str());
return;
}
}
}
*/
//Create socket
if ((_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
gzerr << "Create camera plugin UDP socket failed" << endl;
return false;
}
memset((char *)&_myaddr, 0, sizeof(_myaddr));
_myaddr.sin_family = AF_INET;
_myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
// Choose the default cam port
_myaddr.sin_port = htons(_mavlinkCamPort);
if (::bind(_fd, (struct sockaddr *)&_myaddr, sizeof(_myaddr)) < 0) {
gzerr << "Bind failed for camera UDP plugin" << endl;
return false;
}
_gcsaddr.sin_family = AF_INET;
_gcsaddr.sin_addr.s_addr = mavlink_addr;
_gcsaddr.sin_port = htons(14550);
_fds[0].fd = _fd;
_fds[0].events = POLLIN;
mavlink_status_t* chan_state = mavlink_get_channel_status(MAVLINK_COMM_1);
chan_state->flags &= ~(MAVLINK_STATUS_FLAG_OUT_MAVLINK1);
gzmsg << "[Camera manager plugin]: Camera on udp port " + std::to_string(_mavlinkCamPort) + "\n";
return true;
}
void CameraManagerPlugin::_handle_take_photo(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Handle Start Capture" << endl;
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
std::lock_guard<std::mutex> guard(_captureMutex);
//-- We we busy?
if (_captureMode != CAPTURE_DISABLED) {
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_IMAGE_START_CAPTURE, MAV_RESULT_TEMPORARILY_REJECTED, srcaddr);
return;
}
//-- Single capture?
if (cmd.param3 == 1) {
_captureMode = CAPTURE_SINGLE;
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_IMAGE_START_CAPTURE, MAV_RESULT_ACCEPTED, srcaddr);
//-- Time lapse?
} else if (cmd.param3 >= 0 && cmd.param2 > 0.0) {
gzdbg << "Start time lapse of " << (int)cmd.param3 << " shots every " << cmd.param2 << " seconds.\n";
_captureInterval = cmd.param2;
_captureCount = cmd.param3;
_captureMode = CAPTURE_ELAPSED;
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_IMAGE_START_CAPTURE, MAV_RESULT_ACCEPTED, srcaddr);
} else {
gzerr << "Bad Start Capture argments: " << cmd.param2 << " " << cmd.param3 << endl;
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_IMAGE_START_CAPTURE, MAV_RESULT_UNSUPPORTED, srcaddr);
}
}
void CameraManagerPlugin::_handle_stop_take_photo(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Handle Stop Capture" << endl;
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
std::lock_guard<std::mutex> guard(_captureMutex);
_captureMode = CAPTURE_DISABLED;
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_IMAGE_STOP_CAPTURE, MAV_RESULT_ACCEPTED, srcaddr);
}
void CameraManagerPlugin::_handle_request_camera_capture_status(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
// Should we execute the command
if (cmd.param1 != 1)
{
gzwarn << "Camera capture status request ignored" << endl;
return;
}
// ACK command received and accepted
_send_cmd_ack(pMsg->sysid, pMsg->compid, MAV_CMD_REQUEST_CAMERA_CAPTURE_STATUS, MAV_RESULT_ACCEPTED, srcaddr);
//-- Specs call for recording time in ms. get_recording_time returns seconds.
_send_capture_status(srcaddr);
}
void CameraManagerPlugin::_handle_camera_info(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Send camera info" << endl;
_send_cmd_ack(pMsg->sysid, pMsg->compid, MAV_CMD_REQUEST_CAMERA_INFORMATION, MAV_RESULT_ACCEPTED, srcaddr);
static const char vendor[MAVLINK_MSG_CAMERA_INFORMATION_FIELD_VENDOR_NAME_LEN] = "PX4.io";
static const char model[MAVLINK_MSG_CAMERA_INFORMATION_FIELD_MODEL_NAME_LEN] = "Gazebo";
static const char uri[MAVLINK_MSG_CAMERA_INFORMATION_FIELD_CAM_DEFINITION_URI_LEN] = {};
uint32_t camera_capabilities = CAMERA_CAP_FLAGS_CAPTURE_IMAGE | CAMERA_CAP_FLAGS_CAPTURE_VIDEO |
CAMERA_CAP_FLAGS_HAS_MODES | CAMERA_CAP_FLAGS_HAS_BASIC_ZOOM |
CAMERA_CAP_FLAGS_HAS_VIDEO_STREAM;
mavlink_message_t msg;
mavlink_msg_camera_information_pack_chan(
_systemID,
_componentID,
MAVLINK_COMM_1,
&msg,
0, // time_boot_ms
(const uint8_t *)vendor, // const uint8_t * vendor_name
(const uint8_t *)model, // const uint8_t * model_name
0x01, // uint32_t firmware_version
50.0f, // float focal_lenth
35.0f, // float sensor_size_h
24.0f, // float sensor_size_v
_width, // resolution_h
_height, // resolution_v
0, // lens_id
camera_capabilities, // CAP_FLAGS
0, // Camera Definition Version
uri // URI
);
_send_mavlink_message(&msg, srcaddr);
}
void CameraManagerPlugin::_handle_request_camera_settings(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Send camera settings" << endl;
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
//-- Should we execute the command
if ((int)cmd.param1 != 1)
{
gzwarn << "Request ignored" << endl;
return;
}
// ACK command received and accepted
_send_cmd_ack(pMsg->sysid, pMsg->compid, MAV_CMD_REQUEST_CAMERA_SETTINGS, MAV_RESULT_ACCEPTED, srcaddr);
mavlink_message_t msg;
mavlink_msg_camera_settings_pack_chan(
_systemID,
_componentID,
MAVLINK_COMM_1,
&msg,
0, // time_boot_ms
_mode, // Camera Mode
1.0E2 * (_zoom - 1.0)/ (_maxZoom - 1.0), // Zoom level
NAN); // Focus level
_send_mavlink_message(&msg, srcaddr);
}
void CameraManagerPlugin::_handle_request_video_stream_status(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Send videostream status" << endl;
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
int sid = static_cast<int>(cmd.param1);
//-- Should we execute the command
if ((int)cmd.param1 != 1)
{
gzwarn << "Request ignored" << endl;
return;
}
// ACK command received and accepted
_send_cmd_ack(pMsg->sysid, pMsg->compid, MAV_CMD_REQUEST_VIDEO_STREAM_STATUS, MAV_RESULT_ACCEPTED, srcaddr);
mavlink_message_t msg;
mavlink_msg_video_stream_status_pack_chan(
_systemID,
_componentID, // Component ID
MAVLINK_COMM_1,
&msg,
static_cast<uint8_t>(sid), // Stream ID
VIDEO_STREAM_STATUS_FLAGS_RUNNING, // Flags (It's always running)
30, // Frame rate
_width, // Horizontal resolution
_height, // Vertical resolution
2048, // Bit rate (made up)
0, // Rotation (none)
90 // FOV (made up)
);
_send_mavlink_message(&msg, srcaddr);
}
void CameraManagerPlugin::_handle_request_video_stream_information(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Send videostream information" << endl;
char name[MAVLINK_MSG_VIDEO_STREAM_INFORMATION_FIELD_NAME_LEN] = {};
snprintf(name, MAVLINK_MSG_VIDEO_STREAM_INFORMATION_FIELD_NAME_LEN, "Visual Spectrum");
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
//-- Should we execute the command
if ((int)cmd.param1 != 1)
{
gzwarn << "Request ignored" << endl;
return;
}
// ACK command received and accepted
_send_cmd_ack(pMsg->sysid, pMsg->compid, MAV_CMD_REQUEST_VIDEO_STREAM_INFORMATION, MAV_RESULT_ACCEPTED, srcaddr);
std::string uri = std::to_string(_videoURI);
mavlink_message_t msg;
mavlink_msg_video_stream_information_pack_chan(
_systemID,
_componentID, // Component ID
MAVLINK_COMM_1,
&msg,
1, // Stream ID
1, // Stream count
VIDEO_STREAM_TYPE_RTPUDP, // Stream type
VIDEO_STREAM_STATUS_FLAGS_RUNNING, // Flags (It's always running)
30, // Frame rate
_width, // Horizontal resolution
_height, // Vertical resolution
2048, // Bit rate
0, // Rotation (none)
90, // FOV (made up)
name,
uri.c_str()
);
_send_mavlink_message(&msg, srcaddr);
}
void CameraManagerPlugin::_handle_set_camera_mode(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_SET_CAMERA_MODE, MAV_RESULT_ACCEPTED, srcaddr);
if (_mode == CAMERA_MODE_VIDEO) {
_mode = CAMERA_MODE_IMAGE;
} else {
_mode = CAMERA_MODE_VIDEO;
}
}
void CameraManagerPlugin::_handle_camera_zoom(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
mavlink_command_long_t cmd;
mavlink_msg_command_long_decode(pMsg, &cmd);
_send_cmd_ack(pMsg->sysid, pMsg->compid,
MAV_CMD_SET_CAMERA_ZOOM, MAV_RESULT_ACCEPTED, srcaddr);
if (cmd.param1 == ZOOM_TYPE_CONTINUOUS) {
_zoom = std::max(std::min(float(_zoom + 0.1 * cmd.param2), _maxZoom), 1.0f);
_zoom_cmd = cmd.param2;
} else {
_zoom = std::max(std::min(float(_zoom + 0.1 * cmd.param2), _maxZoom), 1.0f);
_camera->SetHFOV(_hfov / _zoom);
}
}
void CameraManagerPlugin::_send_capture_status(struct sockaddr* srcaddr)
{
_captureMutex.lock();
int status = _captureMode == CAPTURE_DISABLED ? 0 : (_captureMode == CAPTURE_SINGLE ? 1 : 3);
float interval = CAPTURE_ELAPSED ? (float)_captureInterval : 0.0f;
_captureMutex.unlock();
gzdbg << "Send capture status" << endl;
float available_mib = 0.0f;
boost::filesystem::space_info si = boost::filesystem::space(".");
available_mib = (float)((double)si.available / (1024.0 * 1024.0));
#if GAZEBO_MAJOR_VERSION >= 9
common::Time current_time = _scene->SimTime();
#else
common::Time current_time = _scene->GetSimTime();
#endif
mavlink_message_t msg;
mavlink_msg_camera_capture_status_pack_chan(
_systemID,
_componentID,
MAVLINK_COMM_1,
&msg,
current_time.Double() * 1e3,
status, // image status
0, // video status (Idle)
interval, // image interval
0, // recording time in ms
available_mib, // available storage capacity
_imageCounter); // total number of images
_send_mavlink_message(&msg, srcaddr);
}
void CameraManagerPlugin::_handle_storage_info(const mavlink_message_t *pMsg, struct sockaddr* srcaddr)
{
gzdbg << "Send storage info" << endl;
float total_mib = 0.0f;
float available_mib = 0.0f;
boost::filesystem::space_info si = boost::filesystem::space(".");
const std::string storage_name = "SITL Camera Storage";
available_mib = (float)((double)si.available / (1024.0 * 1024.0));
total_mib = (float)((double)si.capacity / (1024.0 * 1024.0));
_send_cmd_ack(pMsg->sysid, pMsg->compid, MAV_CMD_REQUEST_STORAGE_INFORMATION, MAV_RESULT_ACCEPTED, srcaddr);
mavlink_message_t msg;
mavlink_msg_storage_information_pack_chan(
_systemID,
_componentID,
MAVLINK_COMM_1,
&msg,
0, // time_boot_ms
1, // storage_id,
1, // storage_count,
2, // status: (formatted)
total_mib,
total_mib - available_mib, // used_capacity,
available_mib,
NAN, // read_speed,
NAN, // write_speed
STORAGE_TYPE_OTHER, // storage type
storage_name.c_str() // storage name
);
_send_mavlink_message(&msg, srcaddr);
}