2022-03-01 14:09:13 -04:00
|
|
|
/**
|
|
|
|
* @file offb_node.cpp
|
|
|
|
* @brief Offboard control example node, written with MAVROS version 0.19.x, PX4 Pro Flight
|
|
|
|
* Stack and tested in Gazebo SITL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ros/ros.h>
|
|
|
|
#include <sensor_msgs/NavSatFix.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/CommandTOL.h>
|
|
|
|
#include <mavros_msgs/AttitudeTarget.h>
|
|
|
|
#include <mavros_msgs/Thrust.h>
|
|
|
|
#include <mavros_msgs/VFR_HUD.h>
|
2022-04-25 11:18:49 -03:00
|
|
|
#include <oscillation_ctrl/WaypointTrack.h>
|
2022-03-01 14:09:13 -04:00
|
|
|
#include <tf2/LinearMath/Quaternion.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
/********* CALLBACK FUNCTIONS **********************/
|
|
|
|
// Initiate variables
|
|
|
|
mavros_msgs::State current_state;
|
|
|
|
geometry_msgs::PoseStamped desPose;
|
|
|
|
|
|
|
|
// Callback function which will save the current state of the autopilot.
|
|
|
|
// Allows to check connection, arming, and Offboard tags*/
|
|
|
|
void state_cb(const mavros_msgs::State::ConstPtr& msg){
|
|
|
|
current_state = *msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cb to recieve pose information
|
|
|
|
// Initiate variables
|
|
|
|
geometry_msgs::PoseStamped pose;
|
|
|
|
geometry_msgs::Quaternion q_init;
|
|
|
|
geometry_msgs::PoseStamped mavPose;
|
|
|
|
bool pose_read = false;
|
|
|
|
double current_altitude;
|
|
|
|
|
|
|
|
void mavPose_cb(const geometry_msgs::PoseStamped::ConstPtr& msg){
|
|
|
|
mavPose = *msg;
|
|
|
|
current_altitude = mavPose.pose.position.z;
|
|
|
|
while(pose_read == false){
|
|
|
|
q_init = mavPose.pose.orientation;
|
|
|
|
if(q_init.x == 0.0 && q_init.w == 0.0){
|
|
|
|
ROS_INFO("Waiting...");
|
|
|
|
} else {
|
|
|
|
pose_read = true;
|
2022-08-18 15:29:26 -03:00
|
|
|
// pose.pose.orientation.x = q_init.x;
|
|
|
|
// pose.pose.orientation.y = q_init.y;
|
|
|
|
// pose.pose.orientation.z = q_init.z;
|
|
|
|
// pose.pose.orientation.w = q_init.w;
|
2022-03-01 14:09:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
ros::init(argc, argv, "offb_node");
|
|
|
|
ros::NodeHandle nh;
|
|
|
|
|
|
|
|
/********************** SUBSCRIBERS **********************/
|
|
|
|
// Get current state
|
|
|
|
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
|
2022-08-18 15:29:26 -03:00
|
|
|
("mavros/state", 10, state_cb);
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
// Pose subscriber
|
|
|
|
ros::Subscriber mavPose_sub = nh.subscribe<geometry_msgs::PoseStamped>
|
|
|
|
("mavros/local_position/pose",10,mavPose_cb);
|
|
|
|
|
|
|
|
// Waypoint Subscriber
|
2022-04-25 11:18:49 -03:00
|
|
|
/*
|
2022-03-01 14:09:13 -04:00
|
|
|
ros::Subscriber waypoint_sub = nh.subscribe<geometry_msgs::PoseStamped>
|
|
|
|
("/reference/waypoints",10,waypoints_cb);
|
2022-04-25 11:18:49 -03:00
|
|
|
*/
|
|
|
|
ros::ServiceClient waypoint_cl = nh.serviceClient<oscillation_ctrl::WaypointTrack>
|
|
|
|
("status/waypoint_tracker");
|
|
|
|
|
2022-03-01 14:09:13 -04:00
|
|
|
/********************** PUBLISHERS **********************/
|
|
|
|
// Initiate publisher to publish commanded local position
|
|
|
|
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
|
2022-04-25 11:18:49 -03:00
|
|
|
("mavros/setpoint_position/local", 10);
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
// Publish desired attitude
|
|
|
|
ros::Publisher thrust_pub = nh.advertise<mavros_msgs::Thrust>
|
2022-04-25 11:18:49 -03:00
|
|
|
("mavros/setpoint_attitude/thrust", 10);
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
// Publish attitude commands
|
|
|
|
ros::Publisher att_pub = nh.advertise<geometry_msgs::PoseStamped>
|
2022-08-18 15:29:26 -03:00
|
|
|
("/mavros/setpoint_attitude/attitude",10);
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
// Service Clients
|
|
|
|
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
|
2022-04-25 11:18:49 -03:00
|
|
|
("mavros/cmd/arming");
|
2022-03-01 14:09:13 -04:00
|
|
|
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
|
2022-04-25 11:18:49 -03:00
|
|
|
("mavros/set_mode");
|
2022-03-01 14:09:13 -04:00
|
|
|
ros::ServiceClient takeoff_cl = nh.serviceClient<mavros_msgs::CommandTOL>
|
2022-08-18 15:29:26 -03:00
|
|
|
("mavros/cmd/takeoff");
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
//the setpoint publishing rate MUST be faster than 2Hz... PX4 timeout = 500 ms
|
|
|
|
ros::Rate rate(20.0);
|
|
|
|
|
|
|
|
// wait for FCU connection
|
|
|
|
while(ros::ok() && !current_state.connected){
|
|
|
|
ros::spinOnce();
|
|
|
|
rate.sleep();
|
|
|
|
}
|
2022-04-25 11:18:49 -03:00
|
|
|
|
|
|
|
// Retrieve desired waypoints
|
|
|
|
oscillation_ctrl::WaypointTrack wpoint_srv;
|
|
|
|
wpoint_srv.request.query = false;
|
|
|
|
if (waypoint_cl.call(wpoint_srv))
|
|
|
|
{
|
|
|
|
ROS_INFO("Waypoints received");
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate desired waypoints
|
|
|
|
pose.pose.position.x = wpoint_srv.response.xd.x;
|
|
|
|
pose.pose.position.y = wpoint_srv.response.xd.y;
|
|
|
|
pose.pose.position.z = wpoint_srv.response.xd.z;
|
|
|
|
|
|
|
|
/*/ Populate pose msg
|
2022-03-01 14:09:13 -04:00
|
|
|
pose.pose.orientation.x = q_init.x;
|
|
|
|
pose.pose.orientation.y = q_init.y;
|
|
|
|
pose.pose.orientation.z = q_init.z;
|
|
|
|
pose.pose.orientation.w = q_init.w;
|
2022-04-25 11:18:49 -03:00
|
|
|
*/
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
//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()){
|
|
|
|
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(3.0))){
|
|
|
|
if(arming_client.call(arm_cmd) &&
|
|
|
|
arm_cmd.response.success){
|
|
|
|
ROS_INFO("Vehicle armed");
|
|
|
|
}
|
|
|
|
last_request = ros::Time::now();
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 15:29:26 -03:00
|
|
|
// check if waypoints have changed desired waypoints
|
2022-04-25 11:18:49 -03:00
|
|
|
waypoint_cl.call(wpoint_srv);
|
|
|
|
pose.pose.position.x = wpoint_srv.response.xd.x;
|
|
|
|
pose.pose.position.y = wpoint_srv.response.xd.y;
|
|
|
|
pose.pose.position.z = wpoint_srv.response.xd.z;
|
2022-08-18 15:29:26 -03:00
|
|
|
|
|
|
|
// User info
|
|
|
|
ROS_INFO("Current Altitude: %.2f",mavPose.pose.position.z);
|
|
|
|
ROS_INFO("Desired Altitude: %.2f",pose.pose.position.z);
|
|
|
|
ROS_INFO("---------------------------");
|
2022-03-01 14:09:13 -04:00
|
|
|
|
|
|
|
local_pos_pub.publish(pose);
|
|
|
|
|
|
|
|
ros::spinOnce();
|
|
|
|
rate.sleep();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|