2015-05-13 00:16:45 -03:00
# include "Rover.h"
2012-04-30 04:17:14 -03:00
/********************************************************************************/
// Command Event Handlers
/********************************************************************************/
2015-05-12 04:00:25 -03:00
bool Rover : : start_command ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2014-03-18 23:42:02 -03:00
// log when new commands start
if ( should_log ( MASK_LOG_CMD ) ) {
2015-06-23 23:13:24 -03:00
DataFlash . Log_Write_Mission_Cmd ( mission , cmd ) ;
2014-03-18 23:42:02 -03:00
}
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Executing command ID #%i " , cmd . id ) ;
2012-04-30 04:17:14 -03:00
2014-04-06 20:30:39 -03:00
// remember the course of our next navigation leg
next_navigation_leg_cd = mission . get_next_ground_course_cd ( 0 ) ;
2016-10-30 06:21:03 -03:00
switch ( cmd . id ) {
2016-12-20 09:32:19 -04:00
case MAV_CMD_NAV_WAYPOINT : // Navigate to Waypoint
2016-10-30 06:21:03 -03:00
do_nav_wp ( cmd ) ;
break ;
case MAV_CMD_NAV_RETURN_TO_LAUNCH :
do_RTL ( ) ;
break ;
case MAV_CMD_NAV_LOITER_UNLIM : // Loiter indefinitely
do_loiter_unlimited ( cmd ) ;
break ;
2016-10-30 07:42:46 -03:00
case MAV_CMD_NAV_LOITER_TIME :
do_loiter_time ( cmd ) ;
break ;
2016-10-30 06:21:03 -03:00
// Conditional commands
case MAV_CMD_CONDITION_DELAY :
do_wait_delay ( cmd ) ;
break ;
case MAV_CMD_CONDITION_DISTANCE :
do_within_distance ( cmd ) ;
break ;
2017-01-18 13:34:42 -04:00
case MAV_CMD_CONDITION_YAW : // 115
do_yaw ( cmd ) ;
break ;
2016-10-30 06:21:03 -03:00
// Do commands
case MAV_CMD_DO_CHANGE_SPEED :
do_change_speed ( cmd ) ;
break ;
case MAV_CMD_DO_SET_HOME :
do_set_home ( cmd ) ;
break ;
case MAV_CMD_DO_SET_SERVO :
ServoRelayEvents . do_set_servo ( cmd . content . servo . channel , cmd . content . servo . pwm ) ;
break ;
case MAV_CMD_DO_SET_RELAY :
ServoRelayEvents . do_set_relay ( cmd . content . relay . num , cmd . content . relay . state ) ;
break ;
case MAV_CMD_DO_REPEAT_SERVO :
ServoRelayEvents . do_repeat_servo ( cmd . content . repeat_servo . channel , cmd . content . repeat_servo . pwm ,
cmd . content . repeat_servo . repeat_count , cmd . content . repeat_servo . cycle_time * 1000.0f ) ;
break ;
case MAV_CMD_DO_REPEAT_RELAY :
ServoRelayEvents . do_repeat_relay ( cmd . content . repeat_relay . num , cmd . content . repeat_relay . repeat_count ,
cmd . content . repeat_relay . cycle_time * 1000.0f ) ;
break ;
2012-04-30 04:17:14 -03:00
2013-07-14 20:57:00 -03:00
# if CAMERA == ENABLED
2016-10-30 06:21:03 -03:00
case MAV_CMD_DO_CONTROL_VIDEO : // Control on-board camera capturing. |Camera ID (-1 for all)| Transmission: 0: disabled, 1: enabled compressed, 2: enabled raw| Transmission mode: 0: video stream, >0: single images every n seconds (decimal)| Recording: 0: disabled, 1: enabled compressed, 2: enabled raw| Empty| Empty| Empty|
break ;
2013-07-14 20:57:00 -03:00
2016-10-30 06:21:03 -03:00
case MAV_CMD_DO_DIGICAM_CONFIGURE : // Mission command to configure an on-board camera controller system. |Modes: P, TV, AV, M, Etc| Shutter speed: Divisor number for one second| Aperture: F stop number| ISO number e.g. 80, 100, 200, Etc| Exposure type enumerator| Command Identity| Main engine cut-off time before camera trigger in seconds/10 (0 means no cut-off)|
do_digicam_configure ( cmd ) ;
break ;
2013-07-14 20:57:00 -03:00
2016-10-30 06:21:03 -03:00
case MAV_CMD_DO_DIGICAM_CONTROL : // Mission command to control an on-board camera controller system. |Session control e.g. show/hide lens| Zoom's absolute position| Zooming step value to offset zoom from the current position| Focus Locking, Unlocking or Re-locking| Shooting Command| Command Identity| Empty|
do_digicam_control ( cmd ) ;
break ;
2013-10-11 07:36:07 -03:00
2016-10-30 06:21:03 -03:00
case MAV_CMD_DO_SET_CAM_TRIGG_DIST :
camera . set_trigger_distance ( cmd . content . cam_trigg_dist . meters ) ;
break ;
2013-07-14 20:57:00 -03:00
# endif
2012-04-30 04:17:14 -03:00
# if MOUNT == ENABLED
2016-10-30 06:21:03 -03:00
// Sets the region of interest (ROI) for a sensor set or the
// vehicle itself. This can then be used by the vehicles control
// system to control the vehicle attitude and the attitude of various
// devices such as cameras.
// |Region of interest mode. (see MAV_ROI enum)| Waypoint index/ target ID. (see MAV_ROI enum)| ROI index (allows a vehicle to manage multiple cameras etc.)| Empty| x the location of the fixed ROI (see MAV_FRAME)| y| z|
case MAV_CMD_DO_SET_ROI :
if ( cmd . content . location . alt = = 0 & & cmd . content . location . lat = = 0 & & cmd . content . location . lng = = 0 ) {
// switch off the camera tracking if enabled
if ( camera_mount . get_mode ( ) = = MAV_MOUNT_MODE_GPS_POINT ) {
camera_mount . set_mode_to_default ( ) ;
2014-07-04 03:43:08 -03:00
}
2016-10-30 06:21:03 -03:00
} else {
// send the command to the camera mount
camera_mount . set_roi_target ( cmd . content . location ) ;
}
break ;
2012-04-30 04:17:14 -03:00
# endif
2014-03-10 05:45:26 -03:00
2016-10-30 06:21:03 -03:00
case MAV_CMD_DO_SET_REVERSE :
do_set_reverse ( cmd ) ;
break ;
2016-07-14 04:50:39 -03:00
2016-10-30 06:21:03 -03:00
default :
// return false for unhandled commands
return false ;
}
2014-03-10 05:45:26 -03:00
2016-10-30 06:21:03 -03:00
// if we got this far we must have been successful
return true ;
2012-04-30 04:17:14 -03:00
}
2014-03-17 04:08:10 -03:00
// exit_mission - callback function called from ap-mission when the mission has completed
// we double check that the flight mode is AUTO to avoid the possibility of ap-mission triggering actions while we're not in AUTO mode
2015-05-12 02:03:23 -03:00
void Rover : : exit_mission ( )
2014-03-10 05:45:26 -03:00
{
2017-07-19 06:57:10 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_NOTICE , " No commands. Can't set AUTO. Setting HOLD " ) ;
set_mode ( mode_hold ) ;
2012-04-30 04:17:14 -03:00
}
2015-07-24 05:20:49 -03:00
// verify_command_callback - callback function called from ap-mission at 10hz or higher when a command is being run
// we double check that the flight mode is AUTO to avoid the possibility of ap-mission triggering actions while we're not in AUTO mode
2015-07-24 04:31:30 -03:00
bool Rover : : verify_command_callback ( const AP_Mission : : Mission_Command & cmd )
2015-07-24 05:20:49 -03:00
{
2017-07-19 06:57:10 -03:00
const bool cmd_complete = verify_command ( cmd ) ;
2015-07-24 04:31:30 -03:00
2017-07-19 06:57:10 -03:00
// send message to GCS
if ( cmd_complete ) {
gcs ( ) . send_mission_item_reached_message ( cmd . index ) ;
2015-07-24 05:20:49 -03:00
}
2017-07-19 06:57:10 -03:00
return cmd_complete ;
2015-07-24 05:20:49 -03:00
}
2016-08-22 06:00:12 -03:00
/*******************************************************************************
Verify command Handlers
Each type of mission element has a " verify " operation . The verify
operation returns true when the mission element has completed and we
should move onto the next mission element .
Return true if we do not recognize the command so that we move on to the next command
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-04-30 04:17:14 -03:00
2015-05-12 02:03:23 -03:00
bool Rover : : verify_command ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2016-10-30 06:21:03 -03:00
switch ( cmd . id ) {
case MAV_CMD_NAV_WAYPOINT :
return verify_nav_wp ( cmd ) ;
case MAV_CMD_NAV_RETURN_TO_LAUNCH :
return verify_RTL ( ) ;
case MAV_CMD_NAV_LOITER_UNLIM :
2016-11-11 22:55:37 -04:00
return verify_loiter_unlimited ( cmd ) ;
2016-10-30 06:21:03 -03:00
2016-10-30 07:42:46 -03:00
case MAV_CMD_NAV_LOITER_TIME :
return verify_loiter_time ( cmd ) ;
2016-10-30 06:21:03 -03:00
case MAV_CMD_CONDITION_DELAY :
return verify_wait_delay ( ) ;
case MAV_CMD_CONDITION_DISTANCE :
return verify_within_distance ( ) ;
2017-01-18 13:34:42 -04:00
case MAV_CMD_CONDITION_YAW :
return verify_yaw ( ) ;
2016-10-30 06:21:03 -03:00
// do commands (always return true)
case MAV_CMD_DO_CHANGE_SPEED :
case MAV_CMD_DO_SET_HOME :
case MAV_CMD_DO_SET_SERVO :
case MAV_CMD_DO_SET_RELAY :
case MAV_CMD_DO_REPEAT_SERVO :
case MAV_CMD_DO_REPEAT_RELAY :
case MAV_CMD_DO_CONTROL_VIDEO :
case MAV_CMD_DO_DIGICAM_CONFIGURE :
case MAV_CMD_DO_DIGICAM_CONTROL :
case MAV_CMD_DO_SET_CAM_TRIGG_DIST :
case MAV_CMD_DO_SET_ROI :
case MAV_CMD_DO_SET_REVERSE :
return true ;
default :
// error message
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Skipping invalid cmd #%i " , cmd . id ) ;
2016-10-30 06:21:03 -03:00
// return true if we do not recognize the command so that we move on to the next command
return true ;
}
2012-04-30 04:17:14 -03:00
}
/********************************************************************************/
// Nav (Must) commands
/********************************************************************************/
2015-05-12 02:03:23 -03:00
void Rover : : do_RTL ( void )
2012-04-30 04:17:14 -03:00
{
2017-07-19 05:03:45 -03:00
set_mode ( mode_rtl ) ;
2012-04-30 04:17:14 -03:00
}
2015-05-12 02:03:23 -03:00
void Rover : : do_nav_wp ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2016-10-30 07:42:46 -03:00
// just starting so we haven't previously reached the waypoint
previously_reached_wp = false ;
2015-07-27 09:10:37 -03:00
// this will be used to remember the time in millis after we reach or pass the WP.
2016-10-30 07:42:46 -03:00
loiter_start_time = 0 ;
2015-07-27 09:10:37 -03:00
// this is the delay, stored in seconds
2016-10-30 07:42:46 -03:00
loiter_duration = cmd . p1 ;
2015-07-27 09:10:37 -03:00
// this is the distance we travel past the waypoint - not there yet so 0 initially
distance_past_wp = 0 ;
2016-11-11 22:55:37 -04:00
Location cmdloc = cmd . content . location ;
location_sanitize ( current_loc , cmdloc ) ;
2016-08-23 08:21:28 -03:00
set_auto_WP ( cmdloc ) ;
2012-04-30 04:17:14 -03:00
}
2016-07-13 22:34:22 -03:00
void Rover : : do_loiter_unlimited ( const AP_Mission : : Mission_Command & cmd )
{
2016-11-11 22:55:37 -04:00
active_loiter = true ;
do_nav_wp ( cmd ) ;
2016-12-20 09:32:19 -04:00
loiter_duration = 100 ; // an arbitrary large loiter time
2016-07-13 22:34:22 -03:00
}
2016-10-30 07:42:46 -03:00
// do_loiter_time - initiate loitering at a point for a given time period
// if the vehicle is moved off the loiter point (i.e. a boat in a current)
// then the vehicle will actively return to the loiter coords.
void Rover : : do_loiter_time ( const AP_Mission : : Mission_Command & cmd )
{
active_loiter = true ;
do_nav_wp ( cmd ) ;
}
2012-04-30 04:17:14 -03:00
/********************************************************************************/
// Verify Nav (Must) commands
/********************************************************************************/
2015-05-12 02:03:23 -03:00
bool Rover : : verify_nav_wp ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2016-10-30 07:42:46 -03:00
// Have we reached the waypoint i.e. are we within waypoint_radius of the waypoint
2012-11-18 01:07:50 -04:00
if ( ( wp_distance > 0 ) & & ( wp_distance < = g . waypoint_radius ) ) {
2016-10-30 07:42:46 -03:00
// check if we are loitering at this waypoint - the message sent to the GCS is different
if ( loiter_duration > 0 ) {
// Check if this is the first time we have reached the waypoint
if ( ! previously_reached_wp ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Reached waypoint #%u. Loiter for %u seconds " ,
2017-03-30 11:07:24 -03:00
static_cast < uint32_t > ( cmd . index ) ,
static_cast < uint32_t > ( loiter_duration ) ) ;
2015-07-27 09:10:37 -03:00
// record the current time i.e. start timer
2016-10-30 07:42:46 -03:00
loiter_start_time = millis ( ) ;
previously_reached_wp = true ;
2015-07-27 09:10:37 -03:00
}
2016-10-30 07:42:46 -03:00
distance_past_wp = wp_distance ;
2015-07-27 09:10:37 -03:00
// Check if we have loiter long enough
2016-10-30 07:42:46 -03:00
if ( ( ( millis ( ) - loiter_start_time ) / 1000 ) < loiter_duration ) {
2015-07-27 09:10:37 -03:00
return false ;
}
} else {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Reached waypoint #%u. Distance %dm " ,
2017-03-30 11:07:24 -03:00
static_cast < uint32_t > ( cmd . index ) ,
2017-02-20 09:26:00 -04:00
static_cast < int32_t > ( fabsf ( get_distance ( current_loc , next_WP ) ) ) ) ;
2015-07-27 09:10:37 -03:00
}
2016-10-30 07:42:46 -03:00
// set loiter_duration to 0 so we know we aren't or have finished loitering
loiter_duration = 0 ;
2012-11-18 01:07:50 -04:00
return true ;
}
2012-11-27 18:20:20 -04:00
// have we gone past the waypoint?
2015-07-27 09:10:37 -03:00
// We should always go through the waypoint i.e. the above code
2016-10-30 07:42:46 -03:00
// first before we go past it but sometimes we don't.
2016-12-19 23:31:42 -04:00
// OR have we reached the waypoint previously be we aren't actively loitering
// This second check is required for when we roll past the waypoint radius
if ( location_passed_point ( current_loc , prev_WP , next_WP ) | |
( ! active_loiter & & previously_reached_wp ) ) {
2016-11-11 22:55:37 -04:00
// As we have passed the waypoint navigation needs to be done from current location
prev_WP = current_loc ;
2016-10-30 07:42:46 -03:00
// Check if this is the first time we have reached the waypoint even though we have gone past it
if ( ! previously_reached_wp ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Reached waypoint #%u. Loiter for %u seconds " ,
2017-03-30 11:07:24 -03:00
static_cast < uint32_t > ( cmd . index ) ,
static_cast < uint32_t > ( loiter_duration ) ) ;
2016-10-30 07:42:46 -03:00
// record the current time i.e. start timer
loiter_start_time = millis ( ) ;
previously_reached_wp = true ;
distance_past_wp = wp_distance ;
}
// check if distance to the WP has changed and output new message if it has
2017-01-31 06:12:26 -04:00
const float dist_to_wp = get_distance ( current_loc , next_WP ) ;
2017-01-31 05:46:32 -04:00
if ( ! is_equal ( distance_past_wp , dist_to_wp ) ) {
2016-10-30 07:42:46 -03:00
distance_past_wp = dist_to_wp ;
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Passed waypoint #%u. Distance %dm " ,
2017-03-30 11:07:24 -03:00
static_cast < uint32_t > ( cmd . index ) ,
2017-02-20 09:26:00 -04:00
static_cast < int32_t > ( fabsf ( distance_past_wp ) ) ) ;
2015-07-27 09:10:37 -03:00
}
2016-10-30 07:42:46 -03:00
2015-07-27 09:10:37 -03:00
// Check if we need to loiter at this waypoint
2016-10-30 07:42:46 -03:00
if ( loiter_duration > 0 ) {
if ( ( ( millis ( ) - loiter_start_time ) / 1000 ) < loiter_duration ) {
2015-07-27 09:10:37 -03:00
return false ;
}
}
2016-10-30 07:42:46 -03:00
// set loiter_duration to 0 so we know we aren't or have finished loitering
loiter_duration = 0 ;
2012-11-18 01:07:50 -04:00
return true ;
}
return false ;
2012-04-30 04:17:14 -03:00
}
2015-05-12 02:03:23 -03:00
bool Rover : : verify_RTL ( )
2012-04-30 04:17:14 -03:00
{
2016-10-30 06:21:03 -03:00
if ( wp_distance < = g . waypoint_radius ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Reached destination " ) ;
2016-10-30 06:21:03 -03:00
rtl_complete = true ;
return true ;
}
2013-03-28 20:24:59 -03:00
// have we gone past the waypoint?
2014-03-17 04:44:25 -03:00
if ( location_passed_point ( current_loc , prev_WP , next_WP ) ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Reached destination. Distance away %dm " ,
2017-02-20 09:26:00 -04:00
static_cast < int32_t > ( fabsf ( get_distance ( current_loc , next_WP ) ) ) ) ;
2015-05-07 21:50:16 -03:00
rtl_complete = true ;
2013-03-28 20:24:59 -03:00
return true ;
}
return false ;
2012-04-30 04:17:14 -03:00
}
2016-11-11 22:55:37 -04:00
bool Rover : : verify_loiter_unlimited ( const AP_Mission : : Mission_Command & cmd )
2016-07-13 22:34:22 -03:00
{
2016-11-11 22:55:37 -04:00
// Continually set loiter start time to now so it never finishes
loiter_start_time + = millis ( ) ;
verify_nav_wp ( cmd ) ;
2016-07-13 22:34:22 -03:00
return false ;
}
2016-10-30 07:42:46 -03:00
// verify_loiter_time - check if we have loitered long enough
bool Rover : : verify_loiter_time ( const AP_Mission : : Mission_Command & cmd )
{
2017-01-31 06:12:26 -04:00
const bool result = verify_nav_wp ( cmd ) ;
2016-10-30 07:42:46 -03:00
if ( result ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Finished active loiter \n " ) ;
2016-10-30 07:42:46 -03:00
// if we have finished active loitering - turn it off
active_loiter = false ;
}
return result ;
}
2016-09-15 09:09:45 -03:00
void Rover : : nav_set_yaw_speed ( )
{
// if we haven't received a MAV_CMD_NAV_SET_YAW_SPEED message within the last 3 seconds bring the rover to a halt
2017-02-15 15:11:08 -04:00
if ( ( millis ( ) - guided_control . msg_time_ms ) > 3000 ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " NAV_SET_YAW_SPEED not recvd last 3secs, stopping " ) ;
2017-07-06 00:06:20 -03:00
g2 . motors . set_throttle ( g . throttle_min . get ( ) ) ;
2017-07-06 07:07:32 -03:00
g2 . motors . set_steering ( 0.0f ) ;
2016-09-15 09:09:45 -03:00
return ;
}
2017-02-15 15:11:08 -04:00
const int32_t steering = steerController . get_steering_out_angle_error ( guided_control . turn_angle ) ;
2017-07-06 00:06:20 -03:00
g2 . motors . set_steering ( steering ) ;
2016-09-15 09:09:45 -03:00
// speed param in the message gives speed as a proportion of cruise speed.
// 0.5 would set speed to the cruise speed
// 1 is double the cruise speed.
2017-05-03 11:06:54 -03:00
const float target_speed = g . speed_cruise * guided_control . target_speed * 2.0f ;
2017-07-18 23:19:08 -03:00
rover . control_mode - > calc_throttle ( target_speed ) ;
2016-09-15 09:09:45 -03:00
2017-07-18 23:19:08 -03:00
Log_Write_GuidedTarget ( rover . mode_guided . guided_mode , Vector3f ( steering , 0.0f , 0.0f ) , Vector3f ( target_speed , 0.0f , 0.0f ) ) ;
2016-09-15 09:09:45 -03:00
}
2017-05-03 11:21:58 -03:00
void Rover : : nav_set_speed ( )
{
// if we haven't received a MAVLINK_MSG_ID_SET_POSITION_TARGET_LOCAL_NED message within the last 3 seconds bring the rover to a halt
2017-02-15 15:11:08 -04:00
if ( ( millis ( ) - guided_control . msg_time_ms ) > 3000 ) {
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " SET_VELOCITY not recvd last 3secs, stopping " ) ;
2017-07-06 00:06:20 -03:00
g2 . motors . set_throttle ( g . throttle_min . get ( ) ) ;
2017-07-06 07:07:32 -03:00
g2 . motors . set_steering ( 0.0f ) ;
2017-05-03 11:21:58 -03:00
prev_WP = current_loc ;
next_WP = current_loc ;
set_guided_WP ( current_loc ) ; // exit Guided_Velocity to prevent spam
return ;
}
prev_WP = current_loc ;
next_WP = current_loc ;
2017-02-15 15:11:08 -04:00
const int32_t steer_value = steerController . get_steering_out_rate ( guided_control . target_steer_speed ) ;
2017-05-03 11:21:58 -03:00
location_update ( next_WP , ( steer_value + ahrs . yaw_sensor ) * 0.01f , 4.0f ) ; // put the next wp at 4m forward at steer direction
nav_controller - > update_waypoint ( current_loc , next_WP ) ;
2017-07-06 00:06:20 -03:00
g2 . motors . set_steering ( steer_value ) ;
2017-07-18 23:19:08 -03:00
rover . control_mode - > calc_throttle ( guided_control . target_speed ) ;
2017-05-03 11:21:58 -03:00
2017-07-18 23:19:08 -03:00
Log_Write_GuidedTarget ( rover . mode_guided . guided_mode , Vector3f ( steer_value , 0.0f , 0.0f ) , Vector3f ( guided_control . target_speed , 0.0f , 0.0f ) ) ;
2017-05-03 11:21:58 -03:00
}
2012-04-30 04:17:14 -03:00
/********************************************************************************/
// Condition (May) commands
/********************************************************************************/
2015-05-12 02:03:23 -03:00
void Rover : : do_wait_delay ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2016-10-30 06:21:03 -03:00
condition_start = millis ( ) ;
2017-01-18 13:34:42 -04:00
condition_value = static_cast < int32_t > ( cmd . content . delay . seconds * 1000 ) ; // convert seconds to milliseconds
2012-04-30 04:17:14 -03:00
}
2015-05-12 02:03:23 -03:00
void Rover : : do_within_distance ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2017-01-31 05:46:32 -04:00
condition_value = cmd . content . distance . meters ;
2012-04-30 04:17:14 -03:00
}
2017-01-18 13:34:42 -04:00
void Rover : : do_yaw ( const AP_Mission : : Mission_Command & cmd )
{
// Only support target yaw for now
condition_start = condition_value ; // save condition_value from current navigation wp loaded
// get final angle, 1 = Relative, 0 = Absolute
if ( cmd . content . yaw . relative_angle = = 0 ) {
// absolute angle
condition_value = cmd . content . yaw . angle_deg * 100 ;
} else {
// relative angle
condition_value = cmd . content . yaw . angle_deg * 100 ;
if ( cmd . content . yaw . direction < 0 ) {
condition_value = - condition_value ;
}
condition_value = condition_value + ahrs . yaw_sensor ;
}
// absolute angle error
const int32_t error_to_target_yaw = abs ( ( condition_value - ahrs . yaw_sensor ) ) ;
// Calculate the steering to apply base on error calculated
const int32_t steering = steerController . get_steering_out_angle_error ( error_to_target_yaw ) ;
2017-07-06 00:06:20 -03:00
g2 . motors . set_steering ( steering ) ;
2017-01-18 13:34:42 -04:00
next_navigation_leg_cd = condition_value ;
2017-07-18 23:19:08 -03:00
control_mode - > calc_throttle ( g . speed_cruise ) ;
2017-01-18 13:34:42 -04:00
do_auto_rotation = true ;
}
bool Rover : : do_yaw_rotation ( )
{
// absolute angle error
const int32_t error_to_target_yaw = abs ( condition_value - ahrs . yaw_sensor ) ;
// check if we are within 5 degrees of the target heading
if ( error_to_target_yaw < = 500 ) {
2017-07-06 07:07:32 -03:00
g2 . motors . set_steering ( 0.0f ) ; // stop the current rotation
2017-01-18 13:34:42 -04:00
condition_value = condition_start ; // reset the condition value to its previous value
2017-07-06 07:07:32 -03:00
g2 . motors . set_throttle ( 0.0f ) ;
2017-01-18 13:34:42 -04:00
next_navigation_leg_cd = mission . get_next_ground_course_cd ( 0 ) ;
do_auto_rotation = false ;
return true ;
} else {
// Calculate the steering to apply base on error calculated
const int32_t steering = steerController . get_steering_out_angle_error ( error_to_target_yaw ) ;
2017-07-06 00:06:20 -03:00
g2 . motors . set_steering ( steering ) ;
2017-07-18 23:19:08 -03:00
control_mode - > calc_throttle ( g . speed_cruise ) ;
2017-01-18 13:34:42 -04:00
do_auto_rotation = true ;
return false ;
}
}
2012-04-30 04:17:14 -03:00
/********************************************************************************/
// Verify Condition (May) commands
/********************************************************************************/
2015-05-12 02:03:23 -03:00
bool Rover : : verify_wait_delay ( )
2012-04-30 04:17:14 -03:00
{
2017-03-30 11:07:24 -03:00
if ( static_cast < uint32_t > ( millis ( ) - condition_start ) > static_cast < uint32_t > ( condition_value ) ) {
2016-12-20 09:32:19 -04:00
condition_value = 0 ;
2016-10-30 06:21:03 -03:00
return true ;
}
return false ;
2012-04-30 04:17:14 -03:00
}
2015-05-12 02:03:23 -03:00
bool Rover : : verify_within_distance ( )
2012-04-30 04:17:14 -03:00
{
2016-10-30 06:21:03 -03:00
if ( wp_distance < condition_value ) {
condition_value = 0 ;
return true ;
}
return false ;
2012-04-30 04:17:14 -03:00
}
2017-01-18 13:34:42 -04:00
// verify_yaw - return true if we have reached the desired heading
bool Rover : : verify_yaw ( )
{
// override by do_yaw_rotation()
if ( do_auto_rotation ) {
return false ;
} else {
return true ;
}
}
2012-04-30 04:17:14 -03:00
/********************************************************************************/
// Do (Now) commands
/********************************************************************************/
2015-05-12 02:03:23 -03:00
void Rover : : do_change_speed ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2017-01-31 05:46:32 -04:00
if ( cmd . content . speed . target_ms > 0.0f ) {
2015-05-28 04:14:16 -03:00
g . speed_cruise . set ( cmd . content . speed . target_ms ) ;
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Cruise speed: %.1f m/s " , static_cast < double > ( g . speed_cruise . get ( ) ) ) ;
2015-05-28 04:14:16 -03:00
}
2012-04-30 04:17:14 -03:00
2017-01-31 05:46:32 -04:00
if ( cmd . content . speed . throttle_pct > 0.0f & & cmd . content . speed . throttle_pct < = 100.0f ) {
2016-10-30 06:21:03 -03:00
g . throttle_cruise . set ( cmd . content . speed . throttle_pct ) ;
2017-07-08 22:40:59 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Cruise throttle: %.1f " , static_cast < double > ( g . throttle_cruise . get ( ) ) ) ;
2013-05-26 22:08:25 -03:00
}
2012-04-30 04:17:14 -03:00
}
2015-05-12 02:03:23 -03:00
void Rover : : do_set_home ( const AP_Mission : : Mission_Command & cmd )
2012-04-30 04:17:14 -03:00
{
2016-10-30 06:21:03 -03:00
if ( cmd . p1 = = 1 & & have_position ) {
2017-06-05 04:55:24 -03:00
set_home_to_current_location ( false ) ;
2016-10-30 06:21:03 -03:00
} else {
2017-06-05 04:55:24 -03:00
set_home ( cmd . content . location , false ) ;
2016-10-30 06:21:03 -03:00
}
2012-04-30 04:17:14 -03:00
}
2017-01-02 00:21:14 -04:00
# if CAMERA == ENABLED
2015-03-29 15:49:34 -03:00
// do_digicam_configure Send Digicam Configure message with the camera library
2015-05-12 02:03:23 -03:00
void Rover : : do_digicam_configure ( const AP_Mission : : Mission_Command & cmd )
2015-03-29 15:49:34 -03:00
{
2015-09-06 17:44:51 -03:00
camera . configure ( cmd . content . digicam_configure . shooting_mode ,
cmd . content . digicam_configure . shutter_speed ,
cmd . content . digicam_configure . aperture ,
cmd . content . digicam_configure . ISO ,
cmd . content . digicam_configure . exposure_type ,
cmd . content . digicam_configure . cmd_id ,
cmd . content . digicam_configure . engine_cutoff_time ) ;
2015-03-29 15:49:34 -03:00
}
// do_digicam_control Send Digicam Control message with the camera library
2015-05-12 02:03:23 -03:00
void Rover : : do_digicam_control ( const AP_Mission : : Mission_Command & cmd )
2015-03-29 15:49:34 -03:00
{
2016-01-28 18:32:15 -04:00
if ( camera . control ( cmd . content . digicam_control . session ,
cmd . content . digicam_control . zoom_pos ,
cmd . content . digicam_control . zoom_step ,
cmd . content . digicam_control . focus_lock ,
cmd . content . digicam_control . shooting_cmd ,
cmd . content . digicam_control . cmd_id ) ) {
2016-10-30 06:21:03 -03:00
log_picture ( ) ;
2016-01-28 18:32:15 -04:00
}
2015-03-29 15:49:34 -03:00
}
2013-07-14 20:57:00 -03:00
// do_take_picture - take a picture with the camera library
2015-05-12 02:03:23 -03:00
void Rover : : do_take_picture ( )
2013-07-14 20:57:00 -03:00
{
2015-03-29 15:49:34 -03:00
camera . trigger_pic ( true ) ;
log_picture ( ) ;
}
// log_picture - log picture taken and send feedback to GCS
2015-05-12 02:03:23 -03:00
void Rover : : log_picture ( )
2015-03-29 15:49:34 -03:00
{
2016-01-19 01:26:14 -04:00
if ( ! camera . using_feedback_pin ( ) ) {
2017-07-07 22:16:56 -03:00
gcs ( ) . send_message ( MSG_CAMERA_FEEDBACK ) ;
2016-01-19 01:26:14 -04:00
if ( should_log ( MASK_LOG_CAMERA ) ) {
DataFlash . Log_Write_Camera ( ahrs , gps , current_loc ) ;
}
} else {
if ( should_log ( MASK_LOG_CAMERA ) ) {
DataFlash . Log_Write_Trigger ( ahrs , gps , current_loc ) ;
2016-10-30 06:21:03 -03:00
}
2015-06-07 14:22:06 -03:00
}
2016-01-19 01:26:14 -04:00
}
2016-07-14 04:50:39 -03:00
2017-01-02 00:21:14 -04:00
# endif
2016-07-14 04:50:39 -03:00
void Rover : : do_set_reverse ( const AP_Mission : : Mission_Command & cmd )
{
2016-10-30 06:21:03 -03:00
if ( cmd . p1 = = 1 ) {
2017-03-15 00:00:14 -03:00
in_auto_reverse = true ;
2016-07-14 04:50:39 -03:00
set_reverse ( true ) ;
2016-10-30 06:21:03 -03:00
} else {
2017-03-15 00:00:14 -03:00
in_auto_reverse = false ;
2016-07-14 04:50:39 -03:00
set_reverse ( false ) ;
}
}