mc_pos_control.cpp 之 control_offboard(dt)

  • control_offboard(float dt)
void
MulticopterPositionControl::control_offboard(float dt)
{
    if (_pos_sp_triplet.current.valid) {

        if (_control_mode.flag_control_position_enabled && _pos_sp_triplet.current.position_valid) {
            /* control position
             * 控制位置 */
            _pos_sp(0) = _pos_sp_triplet.current.x;
            _pos_sp(1) = _pos_sp_triplet.current.y;
            _run_pos_control = true;

            _hold_offboard_xy = false;

        } else if (_control_mode.flag_control_velocity_enabled && _pos_sp_triplet.current.velocity_valid) {
            /* control velocity
             * 控制速度 */

            /* reset position setpoint to current position if needed
             * 如果需要,将位置设定值重置为当前位置 */
            reset_pos_sp();

            if (fabsf(_pos_sp_triplet.current.vx) <= FLT_EPSILON &&
                fabsf(_pos_sp_triplet.current.vy) <= FLT_EPSILON &&
                _local_pos.xy_valid) {

                if (!_hold_offboard_xy) {
                    _pos_sp(0) = _pos(0);
                    _pos_sp(1) = _pos(1);
                    _hold_offboard_xy = true;
                }

                _run_pos_control = true;

            } else {

                if (_pos_sp_triplet.current.velocity_frame == position_setpoint_s::VELOCITY_FRAME_LOCAL_NED) {
                    /* set position setpoint move rate
                     * 设定位置设定值的变化速率 */
                    _vel_sp(0) = _pos_sp_triplet.current.vx;
                    _vel_sp(1) = _pos_sp_triplet.current.vy;

                } else if (_pos_sp_triplet.current.velocity_frame == position_setpoint_s::VELOCITY_FRAME_BODY_NED) {
                    /* Transform velocity command from body frame to NED frame
                     * 速度转换指令:将机体坐标系下的速度转换到北东地大地坐标系 */
                    _vel_sp(0) = cosf(_yaw) * _pos_sp_triplet.current.vx - sinf(_yaw) * _pos_sp_triplet.current.vy;
                    _vel_sp(1) = sinf(_yaw) * _pos_sp_triplet.current.vx + cosf(_yaw) * _pos_sp_triplet.current.vy;

                } else {
                    PX4_WARN("Unknown velocity offboard coordinate frame");
                }

                _run_pos_control = false;

                _hold_offboard_xy = false;
            }

        }

        if (_control_mode.flag_control_altitude_enabled && _pos_sp_triplet.current.alt_valid) {
            /* control altitude as it is enabled
             * 入股使能,则控制高度 */
            _pos_sp(2) = _pos_sp_triplet.current.z;
            _run_alt_control = true;

            _hold_offboard_z = false;

        } else if (_control_mode.flag_control_climb_rate_enabled && _pos_sp_triplet.current.velocity_valid) {

            /* reset alt setpoint to current altitude if needed
             * 如果需要,将高度设定值重置为当前高度 */
            reset_alt_sp();

            if (fabsf(_pos_sp_triplet.current.vz) <= FLT_EPSILON &&
                _local_pos.z_valid) {

                if (!_hold_offboard_z) {
                    _pos_sp(2) = _pos(2);
                    _hold_offboard_z = true;
                }

                _run_alt_control = true;

            } else {
                /* set position setpoint move rate
                 * 设定位置设定值变化速率 */
                _vel_sp(2) = _pos_sp_triplet.current.vz;
                _run_alt_control = false;

                _hold_offboard_z = false;
            }
        }

        if (_pos_sp_triplet.current.yaw_valid) {
            _att_sp.yaw_body = _pos_sp_triplet.current.yaw;

        } else if (_pos_sp_triplet.current.yawspeed_valid) {
            _att_sp.yaw_body = _att_sp.yaw_body + _pos_sp_triplet.current.yawspeed * dt;
        }

    } else {
        _hold_offboard_xy = false;
        _hold_offboard_z = false;
        reset_pos_sp();
        reset_alt_sp();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
更正这个Python代码import rospy from mavros_msgs.msg import State from mavros_msgs.srv import CommandBool, SetMode from geometry_msgs.msg import PoseStamped import time current_state = State() def state_cb(msg): global current_state current_state = msg rospy.init_node('position') rate = rospy.Rate(20.0) state_sub = rospy.Subscriber("mavros/state", State, state_cb) local_pos_pub = rospy.Publisher("mavros/setpoint_position/local", PoseStamped, queue_size=10) arming_client = rospy.ServiceProxy("mavros/cmd/arming", CommandBool) set_mode_client = rospy.ServiceProxy("mavros/set_mode", SetMode) wait for FCU connection while not rospy.is_shutdown() and not current_state.connected: rate.sleep() pose = PoseStamped() pose.pose.position.x = 0 pose.pose.position.y = 0 pose.pose.position.z = 1.5 offb_set_mode = SetMode() offb_set_mode.custom_mode = "OFFBOARD" arm_cmd = CommandBool() arm_cmd.value = True state = 3 last_request = rospy.Time.now() while not rospy.is_shutdown() and (rospy.Time.now() - last_request < rospy.Duration(5.0)): if not current_state.armed: if arming_client(arm_cmd) and arm_cmd.response.success: rospy.loginfo("Vehicle armed") if current_state.mode != "OFFBOARD": if set_mode_client(offb_set_mode) and offb_set_mode.response.mode_sent: rospy.loginfo("Offboard enabled") rate.sleep() while state > 0: last_request = rospy.Time.now() while not rospy.is_shutdown() and (rospy.Time.now() - last_request < rospy.Duration(5.0)): pose.pose.position.x = 0 pose.pose.position.y = 0 local_pos_pub.publish(pose) rospy.loginfo("SUCCESS0") rate.sleep() last_request = rospy.Time.now() while not rospy.is_shutdown() and (rospy.Time.now() - last_request < rospy.Duration(5.0)): pose.pose.position.x = 2 pose.pose.position.y = 2 local_pos_pub.publish(pose) rospy.loginfo("SUCCESS1") rate.sleep() state -= 1 rospy.loginfo("state=" + str(state)) offb_set_mode.custom_mode = "AUTO.LAND" if set_mode_client(offb_set_mode) and offb_set_mode.response.mode_sent: rospy.loginfo("AUTO.LAND enabled") last_request = rospy.Time.now() rospy.spin()
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值