CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Camera/AP_Camera_MAVLinkCamV2.cpp
Views: 1798
1
#include "AP_Camera_MAVLinkCamV2.h"
2
3
#if AP_CAMERA_MAVLINKCAMV2_ENABLED
4
#include <GCS_MAVLink/GCS.h>
5
6
extern const AP_HAL::HAL& hal;
7
8
#define AP_CAMERA_MAVLINKCAMV2_SEARCH_MS 60000 // search for camera for 60 seconds after startup
9
10
// update - should be called at 50hz
11
void AP_Camera_MAVLinkCamV2::update()
12
{
13
// exit immediately if not initialised
14
if (!_initialised) {
15
find_camera();
16
}
17
18
// call parent update
19
AP_Camera_Backend::update();
20
}
21
22
// entry point to actually take a picture. returns true on success
23
bool AP_Camera_MAVLinkCamV2::trigger_pic()
24
{
25
// exit immediately if have not found camera or does not support taking pictures
26
if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_CAPTURE_IMAGE)) {
27
return false;
28
}
29
30
// prepare and send message
31
mavlink_command_long_t pkt {};
32
pkt.command = MAV_CMD_IMAGE_START_CAPTURE;
33
pkt.param3 = 1; // number of images to take
34
pkt.param4 = image_index+1; // starting sequence number
35
36
_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);
37
38
return true;
39
}
40
41
// start or stop video recording. returns true on success
42
// set start_recording = true to start record, false to stop recording
43
bool AP_Camera_MAVLinkCamV2::record_video(bool start_recording)
44
{
45
// exit immediately if have not found camera or does not support recording video
46
if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_CAPTURE_VIDEO)) {
47
return false;
48
}
49
50
// prepare and send message
51
mavlink_command_long_t pkt {};
52
53
if (start_recording) {
54
pkt.command = MAV_CMD_VIDEO_START_CAPTURE;
55
// param1 = 0, video stream id. 0 for all streams
56
// param2 = 0, status frequency. frequency that CAMERA_CAPTURE_STATUS messages should be sent while recording. 0 for no messages
57
} else {
58
pkt.command = MAV_CMD_VIDEO_STOP_CAPTURE;
59
// param1 = 0, video stream id. 0 for all streams
60
}
61
62
_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);
63
64
return true;
65
}
66
67
// set zoom specified as a rate or percentage
68
bool AP_Camera_MAVLinkCamV2::set_zoom(ZoomType zoom_type, float zoom_value)
69
{
70
// exit immediately if have not found camera or does not support zoom
71
if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_HAS_BASIC_ZOOM)) {
72
return false;
73
}
74
75
// prepare and send message
76
mavlink_command_long_t pkt {};
77
pkt.command = MAV_CMD_SET_CAMERA_ZOOM;
78
switch (zoom_type) {
79
case ZoomType::RATE:
80
pkt.param1 = ZOOM_TYPE_CONTINUOUS;
81
break;
82
case ZoomType::PCT:
83
pkt.param1 = ZOOM_TYPE_RANGE;
84
break;
85
}
86
pkt.param2 = zoom_value; // Zoom Value
87
88
_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);
89
90
return true;
91
}
92
93
// set focus specified as rate, percentage or auto
94
// focus in = -1, focus hold = 0, focus out = 1
95
SetFocusResult AP_Camera_MAVLinkCamV2::set_focus(FocusType focus_type, float focus_value)
96
{
97
// exit immediately if have not found camera or does not support focus
98
if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_HAS_BASIC_FOCUS)) {
99
return SetFocusResult::FAILED;
100
}
101
102
// prepare and send message
103
mavlink_command_long_t pkt {};
104
pkt.command = MAV_CMD_SET_CAMERA_FOCUS;
105
switch (focus_type) {
106
case FocusType::RATE:
107
// focus in, out or hold (focus in = -1, hold = 0, focus out = 1). Same as FOCUS_TYPE_CONTINUOUS
108
pkt.param1 = FOCUS_TYPE_CONTINUOUS;
109
break;
110
case FocusType::PCT:
111
// focus to a percentage (from 0 to 100) of the full range. Same as FOCUS_TYPE_RANGE
112
pkt.param1 = FOCUS_TYPE_RANGE;
113
break;
114
case FocusType::AUTO:
115
// focus automatically. Same as FOCUS_TYPE_AUTO
116
pkt.param1 = FOCUS_TYPE_AUTO;
117
break;
118
}
119
pkt.param2 = focus_value;
120
121
_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);
122
123
return SetFocusResult::ACCEPTED;
124
}
125
126
// handle incoming mavlink message including CAMERA_INFORMATION
127
void AP_Camera_MAVLinkCamV2::handle_message(mavlink_channel_t chan, const mavlink_message_t &msg)
128
{
129
// exit immediately if this is not our message
130
if (msg.sysid != _sysid || msg.compid != _compid) {
131
return;
132
}
133
134
// handle CAMERA_INFORMATION
135
if (msg.msgid == MAVLINK_MSG_ID_CAMERA_INFORMATION) {
136
mavlink_msg_camera_information_decode(&msg, &_cam_info);
137
138
const uint8_t fw_ver_major = _cam_info.firmware_version & 0x000000FF;
139
const uint8_t fw_ver_minor = (_cam_info.firmware_version & 0x0000FF00) >> 8;
140
const uint8_t fw_ver_revision = (_cam_info.firmware_version & 0x00FF0000) >> 16;
141
const uint8_t fw_ver_build = (_cam_info.firmware_version & 0xFF000000) >> 24;
142
143
// display camera info to user
144
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Camera: %.32s %.32s fw:%u.%u.%u.%u",
145
_cam_info.vendor_name,
146
_cam_info.model_name,
147
(unsigned)fw_ver_major,
148
(unsigned)fw_ver_minor,
149
(unsigned)fw_ver_revision,
150
(unsigned)fw_ver_build);
151
152
_got_camera_info = true;
153
}
154
}
155
156
// send camera information message to GCS
157
void AP_Camera_MAVLinkCamV2::send_camera_information(mavlink_channel_t chan) const
158
{
159
// exit immediately if we have not yet received cam info
160
if (!_got_camera_info) {
161
return;
162
}
163
164
// send CAMERA_INFORMATION message
165
mavlink_msg_camera_information_send(
166
chan,
167
AP_HAL::millis(), // time_boot_ms
168
_cam_info.vendor_name, // vendor_name uint8_t[32]
169
_cam_info.model_name, // model_name uint8_t[32]
170
_cam_info.firmware_version, // firmware version uint32_t
171
_cam_info.focal_length, // focal_length float (mm)
172
_cam_info.sensor_size_h, // sensor_size_h float (mm)
173
_cam_info.sensor_size_v, // sensor_size_v float (mm)
174
_cam_info.resolution_h, // resolution_h uint16_t (pix)
175
_cam_info.resolution_v, // resolution_v uint16_t (pix)
176
_cam_info.lens_id, // lens_id, uint8_t
177
_cam_info.flags, // flags uint32_t (CAMERA_CAP_FLAGS)
178
_cam_info.cam_definition_version, // cam_definition_version uint16_t
179
_cam_info.cam_definition_uri, // cam_definition_uri char[140]
180
get_gimbal_device_id()); // gimbal_device_id uint8_t
181
}
182
183
// search for camera in GCS_MAVLink routing table
184
void AP_Camera_MAVLinkCamV2::find_camera()
185
{
186
// do not look for camera for first 10 seconds so user may see banner
187
uint32_t now_ms = AP_HAL::millis();
188
if (now_ms < 10000) {
189
return;
190
}
191
192
// search for camera for 60 seconds or until armed
193
if ((now_ms > AP_CAMERA_MAVLINKCAMV2_SEARCH_MS) && hal.util->get_soft_armed()) {
194
return;
195
}
196
197
// search for a mavlink enabled camera
198
if (_link == nullptr) {
199
// we expect that instance 0 has compid = MAV_COMP_ID_CAMERA, instance 1 has compid = MAV_COMP_ID_CAMERA2, etc
200
uint8_t compid = MIN(MAV_COMP_ID_CAMERA + _instance, MAV_COMP_ID_CAMERA6);
201
_link = GCS_MAVLINK::find_by_mavtype_and_compid(MAV_TYPE_CAMERA, compid, _sysid);
202
if (_link == nullptr) {
203
// have not yet found a camera so return
204
return;
205
}
206
_compid = compid;
207
}
208
209
// request CAMERA_INFORMATION
210
if (!_got_camera_info) {
211
if (now_ms - _last_caminfo_req_ms > 1000) {
212
_last_caminfo_req_ms = now_ms;
213
request_camera_information();
214
}
215
return;
216
}
217
218
_initialised = true;
219
}
220
221
// request CAMERA_INFORMATION (holds vendor and model name)
222
void AP_Camera_MAVLinkCamV2::request_camera_information() const
223
{
224
if (_link == nullptr) {
225
return;
226
}
227
228
const mavlink_command_long_t pkt {
229
MAVLINK_MSG_ID_CAMERA_INFORMATION, // param1
230
0, // param2
231
0, // param3
232
0, // param4
233
0, // param5
234
0, // param6
235
0, // param7
236
MAV_CMD_REQUEST_MESSAGE,
237
_sysid,
238
_compid,
239
0 // confirmation
240
};
241
242
_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);
243
}
244
245
#endif // AP_CAMERA_MAVLINKCAMV2_ENABLED
246
247