123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- #pragma once
- #include <AP_HAL/AP_HAL.h>
- #include <AP_Vehicle/AP_Vehicle.h>
- #include <GCS_MAVLink/GCS_MAVLink.h>
- #include <AP_Math/AP_Math.h>
- #include <AP_Common/AP_Common.h>
- #include <AP_Common/Location.h>
- #include <AP_Param/AP_Param.h>
- #include <StorageManager/StorageManager.h>
- #define AP_MISSION_EEPROM_VERSION 0x65AE
- #define AP_MISSION_EEPROM_COMMAND_SIZE 15
- #define AP_MISSION_MAX_NUM_DO_JUMP_COMMANDS 15
- #define AP_MISSION_JUMP_REPEAT_FOREVER -1
- #define AP_MISSION_CMD_ID_NONE 0
- #define AP_MISSION_CMD_INDEX_NONE 65535
- #define AP_MISSION_JUMP_TIMES_MAX 32767
- #define AP_MISSION_FIRST_REAL_COMMAND 1
- #define AP_MISSION_RESTART_DEFAULT 0
- #define AP_MISSION_OPTIONS_DEFAULT 0
- #define AP_MISSION_MASK_MISSION_CLEAR (1<<0)
- class AP_Mission {
- public:
-
- struct PACKED Jump_Command {
- uint16_t target;
- int16_t num_times;
- };
-
- struct PACKED Conditional_Delay_Command {
- float seconds;
- };
-
- struct PACKED Conditional_Distance_Command {
- float meters;
- };
-
- struct PACKED Yaw_Command {
- float angle_deg;
- float turn_rate_dps;
- int8_t direction;
- uint8_t relative_angle;
- };
-
- struct PACKED Change_Speed_Command {
- uint8_t speed_type;
- float target_ms;
- float throttle_pct;
- };
-
- struct PACKED Set_Relay_Command {
- uint8_t num;
- uint8_t state;
- };
-
- struct PACKED Repeat_Relay_Command {
- uint8_t num;
- int16_t repeat_count;
- float cycle_time;
- };
-
- struct PACKED Set_Servo_Command {
- uint8_t channel;
- uint16_t pwm;
- };
-
- struct PACKED Repeat_Servo_Command {
- uint8_t channel;
- uint16_t pwm;
- int16_t repeat_count;
- float cycle_time;
- };
-
- struct PACKED Mount_Control {
- float pitch;
- float roll;
- float yaw;
- };
-
- struct PACKED Digicam_Configure {
- uint8_t shooting_mode;
- uint16_t shutter_speed;
- uint8_t aperture;
- uint16_t ISO;
- uint8_t exposure_type;
- uint8_t cmd_id;
- float engine_cutoff_time;
- };
-
- struct PACKED Digicam_Control {
- uint8_t session;
- uint8_t zoom_pos;
- int8_t zoom_step;
- uint8_t focus_lock;
- uint8_t shooting_cmd;
- uint8_t cmd_id;
- };
-
- struct PACKED Cam_Trigg_Distance {
- float meters;
- };
-
- struct PACKED Gripper_Command {
- uint8_t num;
- uint8_t action;
- };
-
- struct PACKED Altitude_Wait {
- float altitude;
- float descent_rate;
- uint8_t wiggle_time;
- };
-
- struct PACKED Guided_Limits_Command {
-
- float alt_min;
- float alt_max;
- float horiz_max;
- };
-
- struct PACKED Do_VTOL_Transition {
- uint8_t target_state;
- };
-
- struct PACKED Navigation_Delay_Command {
- float seconds;
- int8_t hour_utc;
- int8_t min_utc;
- int8_t sec_utc;
- };
-
- struct PACKED Do_Engine_Control {
- bool start_control;
- bool cold_start;
- uint16_t height_delay_cm;
- };
-
- struct PACKED Set_Yaw_Speed {
- float angle_deg;
- float speed;
- uint8_t relative_angle;
- };
-
- struct PACKED Winch_Command {
- uint8_t num;
- uint8_t action;
- float release_length;
- float release_rate;
- };
- union Content {
-
- Jump_Command jump;
-
- Conditional_Delay_Command delay;
-
- Conditional_Distance_Command distance;
-
- Yaw_Command yaw;
-
- Change_Speed_Command speed;
-
- Set_Relay_Command relay;
-
- Repeat_Relay_Command repeat_relay;
-
- Set_Servo_Command servo;
-
- Repeat_Servo_Command repeat_servo;
-
- Mount_Control mount_control;
-
- Digicam_Configure digicam_configure;
-
- Digicam_Control digicam_control;
-
- Cam_Trigg_Distance cam_trigg_dist;
-
- Gripper_Command gripper;
-
- Guided_Limits_Command guided_limits;
-
- Altitude_Wait altitude_wait;
-
- Do_VTOL_Transition do_vtol_transition;
-
- Do_Engine_Control do_engine_control;
-
- Navigation_Delay_Command nav_delay;
-
- Set_Yaw_Speed set_yaw_speed;
-
- Winch_Command winch;
-
- Location location{};
- };
-
- struct Mission_Command {
- uint16_t index;
- uint16_t id;
- uint16_t p1;
- Content content;
-
- const char *type() const;
- };
-
- FUNCTOR_TYPEDEF(mission_cmd_fn_t, bool, const Mission_Command&);
- FUNCTOR_TYPEDEF(mission_complete_fn_t, void);
-
- AP_Mission(mission_cmd_fn_t cmd_start_fn, mission_cmd_fn_t cmd_verify_fn, mission_complete_fn_t mission_complete_fn) :
- _cmd_start_fn(cmd_start_fn),
- _cmd_verify_fn(cmd_verify_fn),
- _mission_complete_fn(mission_complete_fn),
- _prev_nav_cmd_id(AP_MISSION_CMD_ID_NONE),
- _prev_nav_cmd_index(AP_MISSION_CMD_INDEX_NONE),
- _prev_nav_cmd_wp_index(AP_MISSION_CMD_INDEX_NONE),
- _last_change_time_ms(0)
- {
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- if (_singleton != nullptr) {
- AP_HAL::panic("Mission must be singleton");
- }
- #endif
- _singleton = this;
-
- AP_Param::setup_object_defaults(this, var_info);
-
- _nav_cmd.index = AP_MISSION_CMD_INDEX_NONE;
- _do_cmd.index = AP_MISSION_CMD_INDEX_NONE;
-
- _flags.state = MISSION_STOPPED;
- _flags.nav_cmd_loaded = false;
- _flags.do_cmd_loaded = false;
- }
-
- static AP_Mission *get_singleton() {
- return _singleton;
- }
-
- AP_Mission(const AP_Mission &other) = delete;
- AP_Mission &operator=(const AP_Mission&) = delete;
-
-
- enum mission_state {
- MISSION_STOPPED=0,
- MISSION_RUNNING=1,
- MISSION_COMPLETE=2
- };
-
-
-
-
- void init();
-
- mission_state state() const { return _flags.state; }
-
-
- uint16_t num_commands() const { return _cmd_total; }
-
- uint16_t num_commands_max() const;
-
-
- void start();
-
- void stop();
-
-
- void resume();
-
- void start_or_resume();
-
- bool starts_with_takeoff_cmd();
-
- void reset();
-
- bool clear();
-
- void truncate(uint16_t index);
-
-
- void update();
-
-
-
-
-
-
- bool add_cmd(Mission_Command& cmd);
-
-
-
- bool replace_cmd(uint16_t index, const Mission_Command& cmd);
-
- static bool is_nav_cmd(const Mission_Command& cmd);
-
- const Mission_Command& get_current_nav_cmd() const { return _nav_cmd; }
-
-
-
- uint16_t get_current_nav_index() const {
- return _nav_cmd.index==AP_MISSION_CMD_INDEX_NONE?0:_nav_cmd.index; }
-
-
-
- uint16_t get_prev_nav_cmd_id() const { return _prev_nav_cmd_id; }
-
-
-
- uint16_t get_prev_nav_cmd_index() const { return _prev_nav_cmd_index; }
-
-
-
- uint16_t get_prev_nav_cmd_with_wp_index() const { return _prev_nav_cmd_wp_index; }
-
-
-
- bool get_next_nav_cmd(uint16_t start_index, Mission_Command& cmd);
-
-
-
- int32_t get_next_ground_course_cd(int32_t default_angle);
-
- const Mission_Command& get_current_do_cmd() const { return _do_cmd; }
-
- bool set_current_cmd(uint16_t index);
-
-
- bool read_cmd_from_storage(uint16_t index, Mission_Command& cmd) const;
-
-
-
- bool write_cmd_to_storage(uint16_t index, const Mission_Command& cmd);
-
-
- void write_home_to_storage();
- static MAV_MISSION_RESULT convert_MISSION_ITEM_to_MISSION_ITEM_INT(const mavlink_mission_item_t &mission_item,
- mavlink_mission_item_int_t &mission_item_int) WARN_IF_UNUSED;
- static MAV_MISSION_RESULT convert_MISSION_ITEM_INT_to_MISSION_ITEM(const mavlink_mission_item_int_t &mission_item_int,
- mavlink_mission_item_t &mission_item) WARN_IF_UNUSED;
-
-
- static MAV_MISSION_RESULT mavlink_int_to_mission_cmd(const mavlink_mission_item_int_t& packet, AP_Mission::Mission_Command& cmd);
-
-
- static MAV_MISSION_RESULT mavlink_cmd_long_to_mission_cmd(const mavlink_command_long_t& packet, AP_Mission::Mission_Command& cmd);
-
-
- static bool mission_cmd_to_mavlink_int(const AP_Mission::Mission_Command& cmd, mavlink_mission_item_int_t& packet);
-
- uint32_t last_change_time_ms(void) const { return _last_change_time_ms; }
-
-
-
- uint16_t get_landing_sequence_start();
-
-
-
- bool jump_to_landing_sequence(void);
-
- bool jump_to_abort_landing_sequence(void);
-
-
- HAL_Semaphore_Recursive &get_semaphore(void) {
- return _rsem;
- }
-
- bool contains_item(MAV_CMD command) const;
-
- static const struct AP_Param::GroupInfo var_info[];
- private:
- static AP_Mission *_singleton;
- static StorageAccess _storage;
- static bool stored_in_location(uint16_t id);
- struct Mission_Flags {
- mission_state state;
- uint8_t nav_cmd_loaded : 1;
- uint8_t do_cmd_loaded : 1;
- uint8_t do_cmd_all_done : 1;
- } _flags;
-
-
-
-
- void complete();
- bool verify_command(const Mission_Command& cmd);
- bool start_command(const Mission_Command& cmd);
-
-
-
-
-
- bool advance_current_nav_cmd(uint16_t starting_index = 0);
-
-
-
- void advance_current_do_cmd();
-
-
-
-
- bool get_next_cmd(uint16_t start_index, Mission_Command& cmd, bool increment_jump_num_times_if_found);
-
-
-
-
- bool get_next_do_cmd(uint16_t start_index, Mission_Command& cmd);
-
-
-
-
- void init_jump_tracking();
-
-
- int16_t get_jump_times_run(const Mission_Command& cmd);
-
- void increment_jump_times_run(Mission_Command& cmd);
-
-
- void check_eeprom_version();
-
- static MAV_MISSION_RESULT sanity_check_params(const mavlink_mission_item_int_t& packet);
-
- AP_Int16 _cmd_total;
- AP_Int8 _restart;
- AP_Int16 _options;
-
- mission_cmd_fn_t _cmd_start_fn;
- mission_cmd_fn_t _cmd_verify_fn;
- mission_complete_fn_t _mission_complete_fn;
-
- struct Mission_Command _nav_cmd;
- struct Mission_Command _do_cmd;
- uint16_t _prev_nav_cmd_id;
- uint16_t _prev_nav_cmd_index;
- uint16_t _prev_nav_cmd_wp_index;
-
- struct jump_tracking_struct {
- uint16_t index;
- int16_t num_times_run;
- } _jump_tracking[AP_MISSION_MAX_NUM_DO_JUMP_COMMANDS];
-
- uint32_t _last_change_time_ms;
-
-
- static HAL_Semaphore_Recursive _rsem;
-
- bool start_command_do_gripper(const AP_Mission::Mission_Command& cmd);
- bool start_command_do_servorelayevents(const AP_Mission::Mission_Command& cmd);
- bool start_command_camera(const AP_Mission::Mission_Command& cmd);
- bool start_command_parachute(const AP_Mission::Mission_Command& cmd);
- };
- namespace AP {
- AP_Mission *mission();
- };
|