Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Camera: Add Camera Tracking functionality (WIP) #27773

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion libraries/AP_Camera/AP_Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ bool AP_Camera::send_mavlink_message(GCS_MAVLINK &link, const enum ap_message ms
CHECK_PAYLOAD_SIZE2(CAMERA_CAPTURE_STATUS);
send_camera_capture_status(chan);
break;

#if AP_CAMERA_TRACKING_ENABLED
case MSG_CAMERA_TRACKING_IMAGE_STATUS:
CHECK_PAYLOAD_SIZE2(CAMERA_TRACKING_IMAGE_STATUS);
send_camera_tracking_image_status(chan);
break;
#endif
default:
// should not reach this; should only be called for specific IDs
break;
Expand Down Expand Up @@ -621,6 +626,19 @@ void AP_Camera::send_camera_capture_status(mavlink_channel_t chan)
}
}

// send camera tracking image status message to GCS
void AP_Camera::send_camera_tracking_image_status(mavlink_channel_t chan)
{
WITH_SEMAPHORE(_rsem);

// call each instance
for (uint8_t instance = 0; instance < AP_CAMERA_MAX_INSTANCES; instance++) {
if (_backends[instance] != nullptr) {
_backends[instance]->send_camera_tracking_image_status(chan);
}
}
}

/*
update; triggers by distance moved and camera trigger
*/
Expand Down Expand Up @@ -733,6 +751,20 @@ bool AP_Camera::set_tracking(uint8_t instance, TrackingType tracking_type, const
// call each instance
return backend->set_tracking(tracking_type, top_left, bottom_right);
}

bool AP_Camera::is_tracking_object_visible(uint8_t instance)
{
WITH_SEMAPHORE(_rsem);

auto *backend = get_instance(instance);
if (backend == nullptr) {
return false;
}

// call each instance
return backend->is_tracking_object_visible();
}

#endif

#if AP_CAMERA_SET_CAMERA_SOURCE_ENABLED
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_Camera/AP_Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class AP_Camera {
// p1,p2 are in range 0 to 1. 0 is left or top, 1 is right or bottom
bool set_tracking(TrackingType tracking_type, const Vector2f& top_left, const Vector2f& bottom_right);
bool set_tracking(uint8_t instance, TrackingType tracking_type, const Vector2f& top_left, const Vector2f& bottom_right);
bool is_tracking_object_visible(uint8_t instance);
#endif

#if AP_CAMERA_SET_CAMERA_SOURCE_ENABLED
Expand Down Expand Up @@ -239,6 +240,9 @@ class AP_Camera {
// send camera capture status message to GCS
void send_camera_capture_status(mavlink_channel_t chan);

// send camera tracking image status message
void send_camera_tracking_image_status(mavlink_channel_t chan);

HAL_Semaphore _rsem; // semaphore for multi-thread access
AP_Camera_Backend *primary; // primary camera backed
bool _is_in_auto_mode; // true if in AUTO mode
Expand Down
25 changes: 25 additions & 0 deletions libraries/AP_Camera/AP_Camera_Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ void AP_Camera_Backend::send_camera_capture_status(mavlink_channel_t chan) const
image_index); // total number of images captured
}

// send camera tracking image status message to GCS
void AP_Camera_Backend::send_camera_tracking_image_status(mavlink_channel_t chan) const
{
mavlink_msg_camera_tracking_image_status_send(
chan,
camera_settings._cam_tracking_status.tracking_status, // uint8_t tracking_status
camera_settings._cam_tracking_status.tracking_mode, // uint8_t tracking_mode
camera_settings._cam_tracking_status.target_data, // uint8_t target_data
camera_settings._cam_tracking_status.point_x, // float point_x
camera_settings._cam_tracking_status.point_y, // float point_y
camera_settings._cam_tracking_status.radius, // float radius
camera_settings._cam_tracking_status.rec_top_x, // float rec_top_x
camera_settings._cam_tracking_status.rec_top_y, // float rec_top_y
camera_settings._cam_tracking_status.rec_bottom_x, // float rec_bottom_x
camera_settings._cam_tracking_status.rec_bottom_y // float rec_bottom_y
);
}

// setup a callback for a feedback pin. When on PX4 with the right FMU
// mode we can use the microsecond timer.
void AP_Camera_Backend::setup_feedback_callback()
Expand Down Expand Up @@ -476,6 +494,11 @@ void AP_Camera_Backend::handle_message_camera_information(mavlink_channel_t chan
camera_settings._got_camera_info = true;
}

void AP_Camera_Backend::handle_message_camera_tracking_image_status(mavlink_channel_t chan, const mavlink_message_t &msg)
{
mavlink_msg_camera_tracking_image_status_decode(&msg, &camera_settings._cam_tracking_status);
}

// handle MAVLink messages from the camera
void AP_Camera_Backend::handle_message(mavlink_channel_t chan, const mavlink_message_t &msg)
{
Expand All @@ -484,6 +507,8 @@ void AP_Camera_Backend::handle_message(mavlink_channel_t chan, const mavlink_mes
case MAVLINK_MSG_ID_CAMERA_INFORMATION:
handle_message_camera_information(chan,msg);
break;
case MAVLINK_MSG_ID_CAMERA_TRACKING_IMAGE_STATUS:
handle_message_camera_tracking_image_status(chan,msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing break. Compilation with clang fails.

default:
break;
}
Expand Down
21 changes: 21 additions & 0 deletions libraries/AP_Camera/AP_Camera_Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class AP_Camera_Backend
// p1,p2 are in range 0 to 1. 0 is left or top, 1 is right or bottom
bool set_tracking(TrackingType tracking_type, const Vector2f& top_left, const Vector2f& bottom_right);

// returns true if the objkect to be tracked is visible in the frame
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp. objkect -> object

bool is_tracking_object_visible() {
if (camera_settings._cam_tracking_status.tracking_status & CAMERA_TRACKING_STATUS_FLAGS::CAMERA_TRACKING_STATUS_FLAGS_ACTIVE) {
return true;
}
return false;
}

#if AP_CAMERA_TRACKING_ENABLED
// default tracking supported by camera
virtual bool set_tracking_internal(TrackingType tracking_type, const Vector2f& top_left, const Vector2f& bottom_right) { return false; }
Expand Down Expand Up @@ -113,6 +121,9 @@ class AP_Camera_Backend
// handle camera information message
void handle_message_camera_information(mavlink_channel_t chan, const mavlink_message_t &msg);

// handle image tracking status messages
void handle_message_camera_tracking_image_status(mavlink_channel_t chan, const mavlink_message_t &msg);

// configure camera
virtual void configure(float shooting_mode, float shutter_speed, float aperture, float ISO, int32_t exposure_type, int32_t cmd_id, float engine_cutoff_time) {}

Expand All @@ -139,6 +150,9 @@ class AP_Camera_Backend
// send camera capture status message to GCS
virtual void send_camera_capture_status(mavlink_channel_t chan) const;

// send camera tracking image status message to GCS
void send_camera_tracking_image_status(mavlink_channel_t chan) const;

#if AP_CAMERA_SCRIPTING_ENABLED
// accessor to allow scripting backend to retrieve state
// returns true on success and cam_state is filled in
Expand Down Expand Up @@ -207,9 +221,16 @@ class AP_Camera_Backend
struct {
bool _got_camera_info; // true once camera has provided CAMERA_INFORMATION
mavlink_camera_information_t _cam_info {}; // latest camera information received from camera
mavlink_camera_tracking_image_status_t _cam_tracking_status {};
uint8_t _sysid_camera; // sysid of camera
uint8_t _compid_camera; // component id of gimbal
} camera_settings;

enum CAMERA_TRACKING_STATUS_FLAGS : uint8_t {
CAMERA_TRACKING_STATUS_FLAGS_IDLE,
CAMERA_TRACKING_STATUS_FLAGS_ACTIVE,
CAMERA_TRACKING_STATUS_FLAGS_ERROR
};
Comment on lines +229 to +233
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shadows

#define HAVE_ENUM_CAMERA_TRACKING_STATUS_FLAGS
typedef enum CAMERA_TRACKING_STATUS_FLAGS
{
   CAMERA_TRACKING_STATUS_FLAGS_IDLE=0, /* Camera is not tracking | */
   CAMERA_TRACKING_STATUS_FLAGS_ACTIVE=1, /* Camera is tracking | */
   CAMERA_TRACKING_STATUS_FLAGS_ERROR=2, /* Camera tracking in error state | */
   CAMERA_TRACKING_STATUS_FLAGS_ENUM_END=3, /*  | */
} CAMERA_TRACKING_STATUS_FLAGS;
#endif

in mavlink/v2.0/common/common.h

};

#endif // AP_CAMERA_ENABLED