Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Camera/AP_Camera_MAVLinkCamV2.cpp
Views: 1798
#include "AP_Camera_MAVLinkCamV2.h"12#if AP_CAMERA_MAVLINKCAMV2_ENABLED3#include <GCS_MAVLink/GCS.h>45extern const AP_HAL::HAL& hal;67#define AP_CAMERA_MAVLINKCAMV2_SEARCH_MS 60000 // search for camera for 60 seconds after startup89// update - should be called at 50hz10void AP_Camera_MAVLinkCamV2::update()11{12// exit immediately if not initialised13if (!_initialised) {14find_camera();15}1617// call parent update18AP_Camera_Backend::update();19}2021// entry point to actually take a picture. returns true on success22bool AP_Camera_MAVLinkCamV2::trigger_pic()23{24// exit immediately if have not found camera or does not support taking pictures25if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_CAPTURE_IMAGE)) {26return false;27}2829// prepare and send message30mavlink_command_long_t pkt {};31pkt.command = MAV_CMD_IMAGE_START_CAPTURE;32pkt.param3 = 1; // number of images to take33pkt.param4 = image_index+1; // starting sequence number3435_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);3637return true;38}3940// start or stop video recording. returns true on success41// set start_recording = true to start record, false to stop recording42bool AP_Camera_MAVLinkCamV2::record_video(bool start_recording)43{44// exit immediately if have not found camera or does not support recording video45if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_CAPTURE_VIDEO)) {46return false;47}4849// prepare and send message50mavlink_command_long_t pkt {};5152if (start_recording) {53pkt.command = MAV_CMD_VIDEO_START_CAPTURE;54// param1 = 0, video stream id. 0 for all streams55// param2 = 0, status frequency. frequency that CAMERA_CAPTURE_STATUS messages should be sent while recording. 0 for no messages56} else {57pkt.command = MAV_CMD_VIDEO_STOP_CAPTURE;58// param1 = 0, video stream id. 0 for all streams59}6061_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);6263return true;64}6566// set zoom specified as a rate or percentage67bool AP_Camera_MAVLinkCamV2::set_zoom(ZoomType zoom_type, float zoom_value)68{69// exit immediately if have not found camera or does not support zoom70if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_HAS_BASIC_ZOOM)) {71return false;72}7374// prepare and send message75mavlink_command_long_t pkt {};76pkt.command = MAV_CMD_SET_CAMERA_ZOOM;77switch (zoom_type) {78case ZoomType::RATE:79pkt.param1 = ZOOM_TYPE_CONTINUOUS;80break;81case ZoomType::PCT:82pkt.param1 = ZOOM_TYPE_RANGE;83break;84}85pkt.param2 = zoom_value; // Zoom Value8687_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);8889return true;90}9192// set focus specified as rate, percentage or auto93// focus in = -1, focus hold = 0, focus out = 194SetFocusResult AP_Camera_MAVLinkCamV2::set_focus(FocusType focus_type, float focus_value)95{96// exit immediately if have not found camera or does not support focus97if (_link == nullptr || !(_cam_info.flags & CAMERA_CAP_FLAGS_HAS_BASIC_FOCUS)) {98return SetFocusResult::FAILED;99}100101// prepare and send message102mavlink_command_long_t pkt {};103pkt.command = MAV_CMD_SET_CAMERA_FOCUS;104switch (focus_type) {105case FocusType::RATE:106// focus in, out or hold (focus in = -1, hold = 0, focus out = 1). Same as FOCUS_TYPE_CONTINUOUS107pkt.param1 = FOCUS_TYPE_CONTINUOUS;108break;109case FocusType::PCT:110// focus to a percentage (from 0 to 100) of the full range. Same as FOCUS_TYPE_RANGE111pkt.param1 = FOCUS_TYPE_RANGE;112break;113case FocusType::AUTO:114// focus automatically. Same as FOCUS_TYPE_AUTO115pkt.param1 = FOCUS_TYPE_AUTO;116break;117}118pkt.param2 = focus_value;119120_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);121122return SetFocusResult::ACCEPTED;123}124125// handle incoming mavlink message including CAMERA_INFORMATION126void AP_Camera_MAVLinkCamV2::handle_message(mavlink_channel_t chan, const mavlink_message_t &msg)127{128// exit immediately if this is not our message129if (msg.sysid != _sysid || msg.compid != _compid) {130return;131}132133// handle CAMERA_INFORMATION134if (msg.msgid == MAVLINK_MSG_ID_CAMERA_INFORMATION) {135mavlink_msg_camera_information_decode(&msg, &_cam_info);136137const uint8_t fw_ver_major = _cam_info.firmware_version & 0x000000FF;138const uint8_t fw_ver_minor = (_cam_info.firmware_version & 0x0000FF00) >> 8;139const uint8_t fw_ver_revision = (_cam_info.firmware_version & 0x00FF0000) >> 16;140const uint8_t fw_ver_build = (_cam_info.firmware_version & 0xFF000000) >> 24;141142// display camera info to user143GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Camera: %.32s %.32s fw:%u.%u.%u.%u",144_cam_info.vendor_name,145_cam_info.model_name,146(unsigned)fw_ver_major,147(unsigned)fw_ver_minor,148(unsigned)fw_ver_revision,149(unsigned)fw_ver_build);150151_got_camera_info = true;152}153}154155// send camera information message to GCS156void AP_Camera_MAVLinkCamV2::send_camera_information(mavlink_channel_t chan) const157{158// exit immediately if we have not yet received cam info159if (!_got_camera_info) {160return;161}162163// send CAMERA_INFORMATION message164mavlink_msg_camera_information_send(165chan,166AP_HAL::millis(), // time_boot_ms167_cam_info.vendor_name, // vendor_name uint8_t[32]168_cam_info.model_name, // model_name uint8_t[32]169_cam_info.firmware_version, // firmware version uint32_t170_cam_info.focal_length, // focal_length float (mm)171_cam_info.sensor_size_h, // sensor_size_h float (mm)172_cam_info.sensor_size_v, // sensor_size_v float (mm)173_cam_info.resolution_h, // resolution_h uint16_t (pix)174_cam_info.resolution_v, // resolution_v uint16_t (pix)175_cam_info.lens_id, // lens_id, uint8_t176_cam_info.flags, // flags uint32_t (CAMERA_CAP_FLAGS)177_cam_info.cam_definition_version, // cam_definition_version uint16_t178_cam_info.cam_definition_uri, // cam_definition_uri char[140]179get_gimbal_device_id()); // gimbal_device_id uint8_t180}181182// search for camera in GCS_MAVLink routing table183void AP_Camera_MAVLinkCamV2::find_camera()184{185// do not look for camera for first 10 seconds so user may see banner186uint32_t now_ms = AP_HAL::millis();187if (now_ms < 10000) {188return;189}190191// search for camera for 60 seconds or until armed192if ((now_ms > AP_CAMERA_MAVLINKCAMV2_SEARCH_MS) && hal.util->get_soft_armed()) {193return;194}195196// search for a mavlink enabled camera197if (_link == nullptr) {198// we expect that instance 0 has compid = MAV_COMP_ID_CAMERA, instance 1 has compid = MAV_COMP_ID_CAMERA2, etc199uint8_t compid = MIN(MAV_COMP_ID_CAMERA + _instance, MAV_COMP_ID_CAMERA6);200_link = GCS_MAVLINK::find_by_mavtype_and_compid(MAV_TYPE_CAMERA, compid, _sysid);201if (_link == nullptr) {202// have not yet found a camera so return203return;204}205_compid = compid;206}207208// request CAMERA_INFORMATION209if (!_got_camera_info) {210if (now_ms - _last_caminfo_req_ms > 1000) {211_last_caminfo_req_ms = now_ms;212request_camera_information();213}214return;215}216217_initialised = true;218}219220// request CAMERA_INFORMATION (holds vendor and model name)221void AP_Camera_MAVLinkCamV2::request_camera_information() const222{223if (_link == nullptr) {224return;225}226227const mavlink_command_long_t pkt {228MAVLINK_MSG_ID_CAMERA_INFORMATION, // param12290, // param22300, // param32310, // param42320, // param52330, // param62340, // param7235MAV_CMD_REQUEST_MESSAGE,236_sysid,237_compid,2380 // confirmation239};240241_link->send_message(MAVLINK_MSG_ID_COMMAND_LONG, (const char*)&pkt);242}243244#endif // AP_CAMERA_MAVLINKCAMV2_ENABLED245246247