79

阿木币

0

精华

36 小时

在线时间

老司机

Rank: 2

发表于 2019-7-12 14:01:37 6937 浏览 3 回复

[控制算法] mavros offboard 位置控制

2阿木币
我根据官网教程,修改demo的代码,想让无人机在gazebo里飞一个矩形后在降落,代码如下:
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
/*
#include <cstdio>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
*/
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}

bool begin_hhh = false;

int main(int argc, char **argv)
{
    ros::init(argc, argv, "offboard_node");
    ros::NodeHandle nh;

    ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
            ("mavros/state", 10, state_cb);
    ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
            ("mavros/setpoint_position/local", 10);
    ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
            ("mavros/cmd/arming");
    ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
            ("mavros/set_mode");

    //the setpoint publishing rate MUST be faster than 2Hz
    ros::Rate rate(20.0);

    // wait for FCU connection


    geometry_msgs::PoseStamped pose;
    pose.pose.position.x = 0;
    pose.pose.position.y = 0;
    pose.pose.position.z = 2;

    geometry_msgs::PoseStamped pose1;
    pose1.pose.position.x = 2;
    pose1.pose.position.y = 0;
    pose1.pose.position.z = 2;

    geometry_msgs::PoseStamped pose2;
    pose2.pose.position.x = 2;
    pose2.pose.position.y = 2;
    pose2.pose.position.z = 2;

    geometry_msgs::PoseStamped pose3;
    pose3.pose.position.x = 0;
    pose3.pose.position.y = 2;
    pose3.pose.position.z = 2;
    geometry_msgs::PoseStamped pose4;
    pose4.pose.position.x = 0;
    pose4.pose.position.y = 0;
    pose4.pose.position.z = 2;

    geometry_msgs::PoseStamped pose5;
    pose5.pose.position.x = 0;
    pose5.pose.position.y = 0;
    pose5.pose.position.z = 0;

    //send a few setpoints before starting
    for(int i = 100; ros::ok() && i > 0; --i){
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }

    mavros_msgs::SetMode offb_set_mode;
    offb_set_mode.request.custom_mode = "OFFBOARD";

    mavros_msgs::CommandBool arm_cmd;
    arm_cmd.request.value = true;

    ros::Time last_request = ros::Time::now();

    while(ros::ok()){
        ros::spinOnce();

        if( current_state.mode != "OFFBOARD" &&
            (ros::Time::now() - last_request > ros::Duration(5.0))){
            if( set_mode_client.call(offb_set_mode) &&
                offb_set_mode.response.success){
                ROS_INFO("Offboard enabled");
            }
            last_request = ros::Time::now();
        } else {
            if( !current_state.armed &&
                (ros::Time::now() - last_request > ros::Duration(5.0))){
                if( arming_client.call(arm_cmd) &&
                    arm_cmd.response.success){
                    ROS_INFO("Vehicle armed");
                }
                last_request = ros::Time::now();
            }
        }
        
        ROS_INFO("yo yooooooooooooooooooooooo");
        begin_hhh = true;
        ros::spinOnce();
        local_pos_pub.publish(pose);
        ros::Time time_1 = ros::Time::now();
        while(time_1.sec ==0){
            time_1 = ros::Time::now();
        }
        time_1 = ros::Time::now();

        
        while(1){
            local_pos_pub.publish(pose);
            rate.sleep();
            ROS_INFO("Get pose !!!!!!!!!!");
            ros::Time time_2 = ros::Time::now();
            while(time_2.sec - time_1.sec >10){
                local_pos_pub.publish(pose2);
                ROS_INFO("hhhhhhhhhhhhhhhhhhhhh ??????????????");
                rate.sleep();
            }
        }   
        ros::spinOnce();
    }
   

    return 0;
}



但是运行后,gazebo里无人机依然只是起飞到离地2米处,无其他动作,代码哪里出错了?(甚至后面添加的打印语句如“yo ooooooooo”都打印不出了),
绞尽脑汁,始终无果,实属无奈,寻求大佬帮助!!!



最佳答案

查看完整内容

这个代码这样写是有问题的,正确的做法是读取当前无人机的位置,判断无人机到达矩形的一个点过后然后发送下一个点

扫一扫浏览分享
回复

使用道具 举报

9

阿木币

0

精华

2 小时

在线时间

应届白菜

Rank: 1

发表于 2019-7-12 14:01:38
这个代码这样写是有问题的,正确的做法是读取当前无人机的位置,判断无人机到达矩形的一个点过后然后发送下一个点
回复

使用道具 举报

176

阿木币

0

精华

349 小时

在线时间

版主

Rank: 7Rank: 7Rank: 7

发表于 2019-7-13 09:53:19
这种只能看AmovlabQYP他们做视觉无人机的对这一块比较熟,是不是你这个代码都没正常运行哦。Offboard enabled那些消息打印出来了吗
回复

使用道具 举报

185

阿木币

3

精华

71 小时

在线时间

技术大V

Rank: 4

发表于 2019-7-15 11:28:05
每个地方都加上打印 不就能简单的定位出错位在的地方了?

代码太长。。不想看。。
回复

使用道具 举报

返回列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表