测试offboard模式的安全代码

2023-05-16

这段代码用于检验offboard模式的安全启动,执行后飞机只会轻微的旋转,不会起飞。

//
// Created by zm on 19-4-8.
// To enable px4 offboard control.
// input: Target Attitude & thrust
// output: setpoint_raw attitude
//

#include <ros/ros.h>

#include <std_msgs/Float32.h>
#include <geometry_msgs/PoseStamped.h>
#include <geometry_msgs/Quaternion.h>

#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include <mavros_msgs/AttitudeTarget.h>

mavros_msgs::State current_state;
mavros_msgs::AttitudeTarget targetattitude;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}
void thrust_cb(const std_msgs::Float32::ConstPtr& msg){
    targetattitude.thrust = msg->data;
}
void orientation_cb(const geometry_msgs::Quaternion::ConstPtr& msg){
    targetattitude.orientation = *msg;
}

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

    ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
            ("mavros/state", 10, state_cb);
    ros::Subscriber thrust_sub = nh.subscribe<std_msgs::Float32>
            ("/cmd/thrust", 10, thrust_cb);
    ros::Subscriber orientation_sub = nh.subscribe<geometry_msgs::Quaternion>
            ("/cmd/orientation", 10, orientation_cb);

    ros::Publisher attitude_set_pub = nh.advertise<mavros_msgs::AttitudeTarget>
            ("/mavros/setpoint_raw/attitude", 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
    while(ros::ok() && !current_state.connected){
        ros::spinOnce();
        rate.sleep();
        ROS_INFO("Not connected for sim");
    }


    targetattitude.orientation.x = 0;
    targetattitude.orientation.y = 0;
    targetattitude.orientation.z = 0;
    targetattitude.orientation.w = 1;
    targetattitude.thrust = 0.1;
    //send a few setpoints before starting
    for(int i = 10; ros::ok() && i > 0; --i){
        attitude_set_pub.publish(targetattitude);
        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()){
        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.mode_sent){
                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();
            }
        }

        attitude_set_pub.publish(targetattitude);

        ros::spinOnce();
        rate.sleep();
    }

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试offboard模式的安全代码 的相关文章

随机推荐

  • 在history模式下部署vue项目到tomcat服务器的踩坑经历【前端VUE+后端JAVA】

    前言 因为是要将之前用jq写的前端代码重构为基于vue下的项目 xff0c 又是第一次将vue运用起来 xff0c 所以真的可谓是一步一个坑 xff0c 今天先记一波部署的坑 vue项目源码中的修改 router默认的模式是hash xff
  • 页面跳转路径出现;JSESSIONID=XXX的问题【前端VUE+后端JAVA】

    前沿 后台的JAVA框架时在前后端没有分离的时候编写的 xff0c 因为修改起来工程量比较庞大同时也很复杂 xff0c 所以现阶段只是将前端用vue来重写项目框架 xff0c 来减轻项目的运行负担 在与后台结合的时候还是会出现一些运行冲突而
  • express的学习笔记(一)——server.address()

    前言 在跟这教程学习时 xff0c 遇到了一处和教程展示内容不相同的地方 xff0c 后进行修改得以一致 xff0c 遂进行记录 问题 教程中有段代码是这样得 var express 61 require 39 express 39 var
  • Intel RealSense实感深度摄像头自校准(Self-Calibration)步骤详细,D400系列适用

    喜提国庆8天工作乐 xff0c 改代码真的很帅 xff0c 才华皆一切 xff0c 这篇博客的由来是因为我做实验了 xff0c 然后摄像头的有效距离贼差 xff0c 打了技术人员的电话说他们的有效距离4m xff0c 然后边缘相差为百分之2
  • Anaconda导致的GMT安装报错

    这两天在使用gmt6 3 0绘制直方图时 xff0c 发现一旦使用其 L选项 xff0c 数据统计就是错误的 让同学帮忙用gmt6 2 0绘制时就没有这个错误 xff0c 于是决定卸载gmt6 3 0重装gmt6 2 0 在完成所有准备工作
  • 服务器能ping通,ssh却连不上

    问题现象 xff1a 1 原来用Xshell连着的窗口能正常操作 2 重新打开新的窗口却提示 xff1a Connecting to 192 168 36 61 22 Connection established To escape to
  • ubuntu-18.04 root登录图形界面失败问题解决方案

    1 设置ROOT密码 先输入当前所在用户密码 xff0c 然后输入你要设置的root密码 xff0c 输入两次即可 xff01 2 进入 etc pam d目录 主要修改两个文件 圈了红色框框 xff0c 记得命令行下切换root账户 su
  • 树莓派做飞控的四轴无人机

    为什么拿树莓派做飞控 市面上有很多机遇stm32的现成飞控和教程 我碰巧有做一个无人机的想法 xff0c 而且手上刚好有树莓派4b一个 树莓派支持pyrhon编程和实时调控 需要准备的材料 电流电压匹配的电机 电调和电池 我选择的是闲鱼买的
  • linux系统之根文件系统树制作

    前言 xff1a 很早就在linux下做过了uboot移植 xff0c linux内核移植以及文件系统的制作 xff0c 一直没有来得及总结 xff0c 现在好好把之前做过的东西整理一下 xff0c 主要是为了备忘 现在总结一下根文件系统树
  • 二级必会词汇

    形容词 xff08 xff11 xff09 xff11 惜 xff12 xff1a 怪 xff13 xff1a 嬉 xff14 xff1a 悲 xff15 xff1a 厳 xff16 xff1a 悔 xff17 xff1a 苦 xff18
  • python学习-isinstance()

    isinstance 是一个内置的函数 xff08 BIF xff09 它允许某个特定标识符是否包含某个特定类型的数据 如判断某个对象是不是列表 字典 整型等 gt gt gt a 61 1 2 3 gt gt gt b 61 123 gt
  • Ubuntu上安装PX4

    文字教程 视频教程 在Win10上安装虚拟机 VMware Workstation 16 Pro xff0c 在虚拟机安装18 04版ubuntu xff0c 其他版本可能安装不成功 xff0c 之前我20版本就安装失败 下列步骤开始前 x
  • stm32 红外遥控实现

    一 概述 红外遥控采用NEC协议 定时中断 预分频器采用72 xff0c 72M 72 61 1M xff0c 每秒1千次 xff0c 一次1us xff0c 即第1us计数器加1 溢出值设为10000 xff0c 即10ms xff0c
  • 关于simulink中的函数function模块

    前言 xff1a 我们前面提到过当遇到库中没有我们需要的模块时 xff0c 我们可以自己书写s函数 xff0c 其实s函数是一个比较高端的工具 xff0c 是用来书写一些比较复杂的模块 xff0c 而遇到一些简单的模块 xff0c 我们可以
  • px4飞行数据.ulg文件的分析

    参考 xff1a https blog csdn net walk2011 article details 83757139 以及 xff1a https blog csdn net qq 42570955 article details
  • offboard代码超详细注释

    这一部分主要对服务做了特别详细的讲解 span class token macro property span class token directive keyword include span span class token stri
  • offboard模式的全球位置发布

    例子中的代码控制的是飞机的本地位置 xff0c 也就是相对起飞位置的位置 xff0c 而如果我们采用全球位置 xff0c 也就是经纬海拔坐标 xff0c 如何去书写呢 xff1f 发布全球位置 xff08 经纬海拔坐标 xff09 头文件
  • mavros的常用服务介绍

    在mavros中 xff0c 最常用的服务就两个 xff0c 一个是解锁 xff0c 还有一个就是模式切换 当然还有其他的服务 xff0c 比如通过mavros修改航点信息 xff0c 但是不常用 xff0c 所以下面只介绍解锁和模式切换
  • offboard模式实现简单四边航线并自动降落

    前言 xff1a 这一部分不想在重复写了 xff0c 已放在作者的github上 xff0c 偷个懒0 0
  • 测试offboard模式的安全代码

    这段代码用于检验offboard模式的安全启动 xff0c 执行后飞机只会轻微的旋转 xff0c 不会起飞 span class token comment span span class token comment Created by