4.ROS&PX4--offboard模式多航点代码编写
offboard模式多航点代码编写
等待更新
#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 <mavros_msgs/PositionTarget.h>
int flag1, flag2;
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
int main(int argc, char **argv)
{
flag1 = 1;
flag2 = 0;
ros::init(argc, argv, "offb_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::Publisher raw_local_pub = nh.advertise<mavros_msgs::PositionTarget>
("mavros/setpoint_raw/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");
ros::Rate rate(20.0);
while(ros::ok() && !current_state.connected)
{
ros::spinOnce();
rate.sleep();
}
geometry_msgs::PoseStamped pose;
mavros_msgs::PositionTarget raw_data;
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())
{
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();
}
}
if((ros::Time::now() - last_request > ros::Duration(5.0)) && flag1 == 1)
{
ROS_INFO("POSE1");
flag1 = 0;
flag2 = 1;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 5;
last_request = ros::Time::now();
}
if((ros::Time::now() - last_request > ros::Duration(5.0)) && flag2 == 1)
{
ROS_INFO("Circle");
raw_data.coordinate_frame = 8;
raw_data.type_mask = 1 + 2 + 512 + 1024 ;
raw_data.velocity.x = 2;
raw_data.position.z = 5;
raw_data.yaw_rate = 1;
last_request = ros::Time::now();
flag2 = 3;
}
if(flag2 == 3 )
{
raw_local_pub.publish(raw_data);
}
else
{
local_pos_pub.publish(pose);
}
ros::spinOnce();
rate.sleep();
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)