2013-05-29 20:54:53 -03:00
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2012-06-13 16:00:20 -03:00
# include <AP_Camera.h>
# include <AP_Relay.h>
2012-12-12 15:48:01 -04:00
# include <AP_Math.h>
# include <RC_Channel.h>
# include <AP_HAL.h>
2012-06-13 16:00:20 -03:00
2012-12-12 15:48:01 -04:00
extern const AP_HAL : : HAL & hal ;
2012-06-13 16:00:20 -03:00
// ------------------------------
# define CAM_DEBUG DISABLED
const AP_Param : : GroupInfo AP_Camera : : var_info [ ] PROGMEM = {
2012-08-17 03:09:29 -03:00
// @Param: TRIGG_TYPE
// @DisplayName: Camera shutter (trigger) type
// @Description: how to trigger the camera to take a picture
2013-07-14 20:55:38 -03:00
// @Values: 0:Servo,1:Relay
2012-08-17 03:09:29 -03:00
// @User: Standard
2012-12-06 04:46:09 -04:00
AP_GROUPINFO ( " TRIGG_TYPE " , 0 , AP_Camera , _trigger_type , AP_CAMERA_TRIGGER_DEFAULT_TRIGGER_TYPE ) ,
// @Param: DURATION
// @DisplayName: Duration that shutter is held open
// @Description: How long the shutter will be held open in 10ths of a second (i.e. enter 10 for 1second, 50 for 5seconds)
// @Range: 0 50
// @User: Standard
AP_GROUPINFO ( " DURATION " , 1 , AP_Camera , _trigger_duration , AP_CAMERA_TRIGGER_DEFAULT_DURATION ) ,
// @Param: SERVO_ON
// @DisplayName: Servo ON PWM value
// @Description: PWM value to move servo to when shutter is activated
// @Range: 1000 2000
// @User: Standard
AP_GROUPINFO ( " SERVO_ON " , 2 , AP_Camera , _servo_on_pwm , AP_CAMERA_SERVO_ON_PWM ) ,
// @Param: SERVO_OFF
// @DisplayName: Servo OFF PWM value
// @Description: PWM value to move servo to when shutter is deactivated
// @Range: 1000 2000
// @User: Standard
AP_GROUPINFO ( " SERVO_OFF " , 3 , AP_Camera , _servo_off_pwm , AP_CAMERA_SERVO_OFF_PWM ) ,
2013-06-24 09:39:50 -03:00
// @Param: TRIGG_DIST
// @DisplayName: Camera trigger distance
// @Description: Distance in meters between camera triggers. If this value is non-zero then the camera will trigger whenever the GPS position changes by this number of meters regardless of what mode the APM is in
// @User: Standard
// @Range: 0 1000
AP_GROUPINFO ( " TRIGG_DIST " , 4 , AP_Camera , _trigg_dist , 0 ) ,
2012-08-17 03:09:29 -03:00
AP_GROUPEND
2012-06-13 16:00:20 -03:00
} ;
2012-06-17 17:53:54 -03:00
/// Servo operated camera
2012-06-13 16:00:20 -03:00
void
2012-06-17 17:53:54 -03:00
AP_Camera : : servo_pic ( )
2012-06-13 16:00:20 -03:00
{
2012-12-06 04:46:09 -04:00
RC_Channel_aux : : set_radio ( RC_Channel_aux : : k_cam_trigger , _servo_on_pwm ) ;
// leave a message that it should be active for this many loops (assumes 50hz loops)
2012-12-18 22:36:35 -04:00
_trigger_counter = constrain_int16 ( _trigger_duration * 5 , 0 , 255 ) ;
2012-06-13 16:00:20 -03:00
}
2012-06-17 17:53:54 -03:00
/// basic relay activation
2012-06-13 16:00:20 -03:00
void
2012-06-17 17:53:54 -03:00
AP_Camera : : relay_pic ( )
2012-06-13 16:00:20 -03:00
{
2012-12-17 11:03:51 -04:00
_apm_relay - > on ( ) ;
2012-12-06 04:46:09 -04:00
// leave a message that it should be active for this many loops (assumes 50hz loops)
2012-12-18 22:36:35 -04:00
_trigger_counter = constrain_int16 ( _trigger_duration * 5 , 0 , 255 ) ;
2012-06-13 16:00:20 -03:00
}
2012-06-17 17:53:54 -03:00
/// single entry point to take pictures
2012-06-13 16:00:20 -03:00
void
AP_Camera : : trigger_pic ( )
{
2012-12-06 04:46:09 -04:00
switch ( _trigger_type )
2012-08-17 03:09:29 -03:00
{
2012-12-06 04:46:09 -04:00
case AP_CAMERA_TRIGGER_TYPE_SERVO :
2012-08-17 03:09:29 -03:00
servo_pic ( ) ; // Servo operated camera
break ;
2012-12-06 04:46:09 -04:00
case AP_CAMERA_TRIGGER_TYPE_RELAY :
2012-08-17 03:09:29 -03:00
relay_pic ( ) ; // basic relay activation
break ;
}
2012-06-13 16:00:20 -03:00
}
2012-06-17 17:53:54 -03:00
/// de-activate the trigger after some delay, but without using a delay() function
2012-12-06 04:46:09 -04:00
/// should be called at 50hz
2012-06-13 16:00:20 -03:00
void
AP_Camera : : trigger_pic_cleanup ( )
{
2012-12-06 04:46:09 -04:00
if ( _trigger_counter ) {
_trigger_counter - - ;
} else {
switch ( _trigger_type ) {
case AP_CAMERA_TRIGGER_TYPE_SERVO :
RC_Channel_aux : : set_radio ( RC_Channel_aux : : k_cam_trigger , _servo_off_pwm ) ;
break ;
case AP_CAMERA_TRIGGER_TYPE_RELAY :
2012-12-17 11:03:51 -04:00
_apm_relay - > off ( ) ;
2012-12-06 04:46:09 -04:00
break ;
2012-08-17 03:09:29 -03:00
}
}
2012-06-13 16:00:20 -03:00
}
2012-06-17 17:53:54 -03:00
/// decode MavLink that configures camera
2012-06-13 16:00:20 -03:00
void
AP_Camera : : configure_msg ( mavlink_message_t * msg )
{
2012-08-17 03:09:29 -03:00
__mavlink_digicam_configure_t packet ;
mavlink_msg_digicam_configure_decode ( msg , & packet ) ;
if ( mavlink_check_target ( packet . target_system , packet . target_component ) ) {
// not for us
return ;
}
2012-12-17 11:03:51 -04:00
// This values may or not be used by APM
// They are bypassed as "echo" to a external specialized board
2012-08-17 03:09:29 -03:00
/*
* packet . aperture
* packet . command_id
* packet . engine_cut_off
* packet . exposure_type
* packet . extra_param
* packet . extra_value
* packet . iso
* packet . mode
* packet . shutter_speed
*/
2012-06-13 16:00:20 -03:00
}
2012-06-17 17:53:54 -03:00
/// decode MavLink that controls camera
2012-06-13 16:00:20 -03:00
void
AP_Camera : : control_msg ( mavlink_message_t * msg )
{
2012-08-17 03:09:29 -03:00
__mavlink_digicam_control_t packet ;
mavlink_msg_digicam_control_decode ( msg , & packet ) ;
if ( mavlink_check_target ( packet . target_system , packet . target_component ) ) {
// not for us
return ;
}
2012-12-17 11:03:51 -04:00
// This values may or not be used by APM (the shot is)
// They are bypassed as "echo" to a external specialized board
2012-08-17 03:09:29 -03:00
/*
* packet . command_id
* packet . extra_param
* packet . extra_value
* packet . focus_lock
* packet . session
* packet . shot
* packet . zoom_pos
* packet . zoom_step
*/
if ( packet . shot )
{
trigger_pic ( ) ;
}
2012-06-13 16:00:20 -03:00
}
2013-07-09 19:31:25 -03:00
/* update location, for triggering by GPS distance moved
This function returns true if a picture should be taken
The caller is responsible for taking the picture based on the return value of this function .
The caller is also responsible for logging the details about the photo
*/
bool AP_Camera : : update_location ( const struct Location & loc )
2013-06-24 09:39:50 -03:00
{
if ( _trigg_dist = = 0.0f ) {
2013-07-09 19:31:25 -03:00
return false ;
2013-06-24 09:39:50 -03:00
}
if ( _last_location . lat = = 0 & & _last_location . lng = = 0 ) {
_last_location = loc ;
2013-07-09 19:31:25 -03:00
return false ;
2013-06-24 09:39:50 -03:00
}
if ( _last_location . lat = = loc . lat & & _last_location . lng = = loc . lng ) {
// we haven't moved - this can happen as update_location() may
// be called without a new GPS fix
2013-07-09 19:31:25 -03:00
return false ;
2013-06-24 09:39:50 -03:00
}
2013-08-04 21:16:00 -03:00
if ( get_distance ( loc , _last_location ) < _trigg_dist ) {
2013-07-09 19:31:25 -03:00
return false ;
2013-06-24 09:39:50 -03:00
}
_last_location = loc ;
2013-07-09 19:31:25 -03:00
return true ;
2013-06-24 09:39:50 -03:00
}