2016-10-10 21:22:17 -03:00
|
|
|
#pragma once
|
2017-02-10 15:18:44 -04:00
|
|
|
#include <ros/ros.h>
|
2017-12-21 13:20:26 -04:00
|
|
|
#include <tf/tf.h>
|
2017-02-10 15:18:44 -04:00
|
|
|
#include <sensor_msgs/NavSatFix.h>
|
2017-12-20 13:20:43 -04:00
|
|
|
#include <nav_msgs/OccupancyGrid.h>
|
2017-02-20 00:30:45 -04:00
|
|
|
#include <std_msgs/UInt8.h>
|
2016-10-10 21:22:17 -03:00
|
|
|
#include "mavros_msgs/GlobalPositionTarget.h"
|
|
|
|
#include "mavros_msgs/CommandCode.h"
|
2017-02-16 21:58:21 -04:00
|
|
|
#include "mavros_msgs/CommandLong.h"
|
2017-02-17 17:35:08 -04:00
|
|
|
#include "mavros_msgs/CommandBool.h"
|
2016-11-18 15:05:31 -04:00
|
|
|
#include "mavros_msgs/ExtendedState.h"
|
2017-02-17 17:35:08 -04:00
|
|
|
#include "mavros_msgs/SetMode.h"
|
2016-10-10 21:22:17 -03:00
|
|
|
#include "mavros_msgs/State.h"
|
|
|
|
#include "mavros_msgs/BatteryStatus.h"
|
2018-09-06 13:47:38 -03:00
|
|
|
#include "sensor_msgs/BatteryState.h"
|
2016-10-10 21:22:17 -03:00
|
|
|
#include "mavros_msgs/Mavlink.h"
|
2017-03-13 19:11:01 -03:00
|
|
|
#include "mavros_msgs/PositionTarget.h"
|
2017-02-23 22:10:49 -04:00
|
|
|
#include "sensor_msgs/NavSatStatus.h"
|
2017-02-21 17:09:16 -04:00
|
|
|
#include "mavros_msgs/WaypointPush.h"
|
|
|
|
#include "mavros_msgs/Waypoint.h"
|
2017-03-14 12:09:01 -03:00
|
|
|
#include "mavros_msgs/PositionTarget.h"
|
2017-03-29 15:57:31 -03:00
|
|
|
#include "mavros_msgs/StreamRate.h"
|
2017-04-03 16:41:03 -03:00
|
|
|
#include "mavros_msgs/ParamGet.h"
|
2017-04-10 21:23:04 -03:00
|
|
|
#include "geometry_msgs/PoseStamped.h"
|
2017-04-03 20:50:09 -03:00
|
|
|
#include "std_msgs/Float64.h"
|
2017-12-08 20:44:24 -04:00
|
|
|
#include "std_msgs/String.h"
|
2017-02-10 15:18:44 -04:00
|
|
|
#include <sensor_msgs/LaserScan.h>
|
2017-01-09 18:15:33 -04:00
|
|
|
#include <rosbuzz/neigh_pos.h>
|
2016-10-10 21:22:17 -03:00
|
|
|
#include <sstream>
|
|
|
|
#include <buzz/buzzasm.h>
|
|
|
|
#include "buzz_utility.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <ostream>
|
|
|
|
#include <map>
|
2016-12-24 21:59:00 -04:00
|
|
|
#include "buzzuav_closures.h"
|
2018-09-06 17:19:58 -03:00
|
|
|
#include "rosbuzz/mavrosCC.h"
|
2016-10-10 21:22:17 -03:00
|
|
|
|
2018-09-06 13:47:38 -03:00
|
|
|
/*
|
|
|
|
* ROSBuzz message types
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
ROS_BUZZ_MSG_NIL = 0, // dummy msg
|
|
|
|
UPDATER_MESSAGE, // Update msg
|
2018-09-07 01:05:19 -03:00
|
|
|
BUZZ_MESSAGE_NO_TIME, // Broadcast message wihout time info
|
2018-09-06 13:47:38 -03:00
|
|
|
BUZZ_MESSAGE_TIME, // Broadcast message with time info
|
|
|
|
} rosbuzz_msgtype;
|
|
|
|
|
|
|
|
// Time sync algo. constants
|
|
|
|
#define COM_DELAY 100000000 // in nano seconds i.e 100 ms
|
2018-09-07 01:05:19 -03:00
|
|
|
#define TIME_SYNC_JUMP_THR 500000000
|
2018-09-06 13:47:38 -03:00
|
|
|
#define MOVING_AVERAGE_ALPHA 0.1
|
|
|
|
#define MAX_NUMBER_OF_ROBOTS 10
|
|
|
|
|
2017-12-08 19:53:32 -04:00
|
|
|
#define TIMEOUT 60
|
2017-05-18 00:10:10 -03:00
|
|
|
#define BUZZRATE 10
|
2017-01-27 23:00:11 -04:00
|
|
|
|
2016-10-10 21:22:17 -03:00
|
|
|
using namespace std;
|
|
|
|
|
2018-09-07 01:05:19 -03:00
|
|
|
namespace rosbuzz_node
|
2017-06-23 11:39:14 -03:00
|
|
|
{
|
|
|
|
class roscontroller
|
|
|
|
{
|
2016-10-10 21:22:17 -03:00
|
|
|
public:
|
2017-06-23 11:39:14 -03:00
|
|
|
roscontroller(ros::NodeHandle& n_c, ros::NodeHandle& n_c_priv);
|
|
|
|
~roscontroller();
|
|
|
|
void RosControllerRun();
|
|
|
|
|
2017-09-05 16:39:03 -03:00
|
|
|
static const string CAPTURE_SRV_DEFAULT_NAME;
|
|
|
|
|
2016-10-10 21:22:17 -03:00
|
|
|
private:
|
2017-06-23 11:39:14 -03:00
|
|
|
struct num_robot_count
|
|
|
|
{
|
|
|
|
uint8_t history[10];
|
|
|
|
uint8_t index = 0;
|
|
|
|
uint8_t current = 0;
|
2017-12-08 19:53:32 -04:00
|
|
|
num_robot_count()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2017-12-08 20:44:24 -04:00
|
|
|
typedef struct num_robot_count Num_robot_count;
|
|
|
|
|
|
|
|
Num_robot_count count_robots;
|
2017-06-23 11:39:14 -03:00
|
|
|
|
2017-12-21 13:20:26 -04:00
|
|
|
struct POSE
|
2017-06-23 11:39:14 -03:00
|
|
|
{
|
2017-12-08 19:53:32 -04:00
|
|
|
double longitude = 0.0;
|
|
|
|
double latitude = 0.0;
|
|
|
|
float altitude = 0.0;
|
2017-12-21 13:20:26 -04:00
|
|
|
// NED coordinates
|
|
|
|
float x = 0.0;
|
|
|
|
float y = 0.0;
|
|
|
|
float z = 0.0;
|
|
|
|
float yaw = 0.0;
|
2017-12-08 19:53:32 -04:00
|
|
|
};
|
2017-12-21 13:20:26 -04:00
|
|
|
typedef struct POSE ros_pose;
|
2017-09-05 16:39:03 -03:00
|
|
|
|
2018-09-07 01:05:19 -03:00
|
|
|
ros_pose target, home, cur_pos;
|
2018-09-06 13:47:38 -03:00
|
|
|
|
|
|
|
struct MsgData
|
|
|
|
{
|
|
|
|
int msgid;
|
|
|
|
uint16_t nid;
|
|
|
|
uint16_t size;
|
|
|
|
double sent_time;
|
|
|
|
uint64_t received_time;
|
2018-09-07 01:05:19 -03:00
|
|
|
MsgData(int mi, uint16_t ni, uint16_t s, double st, uint64_t rt):
|
2018-09-06 13:47:38 -03:00
|
|
|
msgid(mi), nid(ni), size(s),sent_time(st), received_time(rt){};
|
|
|
|
MsgData(){};
|
|
|
|
};
|
|
|
|
typedef struct MsgData msg_data;
|
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
uint64_t payload;
|
2017-12-08 19:53:32 -04:00
|
|
|
std::map<int, buzz_utility::Pos_struct> neighbours_pos_map;
|
|
|
|
std::map<int, buzz_utility::Pos_struct> raw_neighbours_pos_map;
|
2018-09-06 13:47:38 -03:00
|
|
|
std::map<int, buzz_utility::neighbor_time> neighbours_time_map;
|
2017-12-08 19:53:32 -04:00
|
|
|
int timer_step = 0;
|
|
|
|
int robot_id = 0;
|
2018-09-06 13:47:38 -03:00
|
|
|
ros::Time logical_clock;
|
|
|
|
ros::Time previous_step_time;
|
2018-09-07 01:05:19 -03:00
|
|
|
std::vector<msg_data> inmsgdata;
|
|
|
|
uint64_t out_msg_time;
|
2018-09-06 13:47:38 -03:00
|
|
|
double logical_time_rate;
|
|
|
|
bool time_sync_jumped;
|
2017-06-23 11:39:14 -03:00
|
|
|
std::string robot_name = "";
|
2017-06-26 15:35:15 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
int rc_cmd;
|
|
|
|
float fcu_timeout;
|
|
|
|
int armstate;
|
|
|
|
int barrier;
|
2018-09-06 13:47:38 -03:00
|
|
|
int update;
|
2017-12-08 19:53:32 -04:00
|
|
|
int message_number = 0;
|
|
|
|
uint8_t no_of_robots = 0;
|
2017-12-21 13:20:26 -04:00
|
|
|
bool rcclient;
|
|
|
|
bool xbeeplugged = false;
|
|
|
|
bool multi_msg;
|
2017-12-08 19:53:32 -04:00
|
|
|
uint8_t no_cnt = 0;
|
|
|
|
uint8_t old_val = 0;
|
2018-09-07 01:05:19 -03:00
|
|
|
bool debug = false;
|
2017-09-05 16:39:03 -03:00
|
|
|
std::string bzzfile_name;
|
|
|
|
std::string fcclient_name;
|
|
|
|
std::string armclient;
|
|
|
|
std::string modeclient;
|
|
|
|
std::string rcservice_name;
|
2017-12-08 19:53:32 -04:00
|
|
|
std::string bcfname, dbgfname;
|
2017-09-05 16:39:03 -03:00
|
|
|
std::string out_payload;
|
|
|
|
std::string in_payload;
|
|
|
|
std::string stand_by;
|
|
|
|
std::string xbeesrv_name;
|
|
|
|
std::string capture_srv_name;
|
|
|
|
std::string setpoint_name;
|
2017-06-23 11:39:14 -03:00
|
|
|
std::string stream_client_name;
|
|
|
|
std::string setpoint_nonraw;
|
2017-12-21 13:20:26 -04:00
|
|
|
|
|
|
|
// ROS service, publishers and subscribers
|
2017-06-23 11:39:14 -03:00
|
|
|
ros::ServiceClient mav_client;
|
|
|
|
ros::ServiceClient xbeestatus_srv;
|
2017-09-05 16:39:03 -03:00
|
|
|
ros::ServiceClient capture_srv;
|
2017-12-21 13:20:26 -04:00
|
|
|
ros::ServiceClient stream_client;
|
2017-06-23 11:39:14 -03:00
|
|
|
ros::Publisher payload_pub;
|
2017-08-09 20:23:42 -03:00
|
|
|
ros::Publisher MPpayload_pub;
|
2017-06-23 11:39:14 -03:00
|
|
|
ros::Publisher neigh_pos_pub;
|
2018-09-07 01:05:19 -03:00
|
|
|
ros::Publisher bvmstate_pub;
|
2017-12-20 13:20:43 -04:00
|
|
|
ros::Publisher grid_pub;
|
2017-06-23 11:39:14 -03:00
|
|
|
ros::Publisher localsetpoint_nonraw_pub;
|
|
|
|
ros::ServiceServer service;
|
|
|
|
ros::Subscriber current_position_sub;
|
|
|
|
ros::Subscriber users_sub;
|
|
|
|
ros::Subscriber battery_sub;
|
|
|
|
ros::Subscriber payload_sub;
|
2017-08-30 15:58:44 -03:00
|
|
|
ros::Subscriber flight_estatus_sub;
|
2017-06-23 11:39:14 -03:00
|
|
|
ros::Subscriber flight_status_sub;
|
|
|
|
ros::Subscriber obstacle_sub;
|
|
|
|
ros::Subscriber Robot_id_sub;
|
|
|
|
ros::Subscriber relative_altitude_sub;
|
|
|
|
ros::Subscriber local_pos_sub;
|
|
|
|
|
2017-12-21 13:20:26 -04:00
|
|
|
std::map<std::string, std::string> m_smTopic_infos;
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
int setpoint_counter;
|
|
|
|
|
|
|
|
std::ofstream log;
|
|
|
|
|
2017-12-21 13:20:26 -04:00
|
|
|
// Commands for flight controller
|
2017-06-23 11:39:14 -03:00
|
|
|
mavros_msgs::CommandLong cmd_srv;
|
|
|
|
|
|
|
|
mavros_msgs::CommandBool m_cmdBool;
|
|
|
|
ros::ServiceClient arm_client;
|
|
|
|
|
|
|
|
mavros_msgs::SetMode m_cmdSetMode;
|
|
|
|
ros::ServiceClient mode_client;
|
|
|
|
|
2017-12-21 13:20:26 -04:00
|
|
|
// Initialize publisher and subscriber, done in the constructor
|
2017-06-23 11:39:14 -03:00
|
|
|
void Initialize_pub_sub(ros::NodeHandle& n_c);
|
|
|
|
|
2017-12-08 20:44:24 -04:00
|
|
|
std::string current_mode;
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*Obtain data from ros parameter server*/
|
|
|
|
void Rosparameters_get(ros::NodeHandle& n_c_priv);
|
|
|
|
|
|
|
|
/*compiles buzz script from the specified .bzz file*/
|
|
|
|
std::string Compile_bzz(std::string bzzfile_name);
|
|
|
|
|
|
|
|
/*Flight controller service call*/
|
|
|
|
void flight_controller_service_call();
|
|
|
|
|
|
|
|
/*Neighbours pos publisher*/
|
|
|
|
void neighbours_pos_publisher();
|
|
|
|
|
2017-12-08 20:44:24 -04:00
|
|
|
/*UAVState publisher*/
|
2018-09-07 01:05:19 -03:00
|
|
|
void state_publisher();
|
2017-12-08 20:44:24 -04:00
|
|
|
|
2017-12-20 13:20:43 -04:00
|
|
|
/*Grid publisher*/
|
|
|
|
void grid_publisher();
|
|
|
|
|
2017-12-08 20:44:24 -04:00
|
|
|
/*BVM message payload publisher*/
|
2017-08-09 20:23:42 -03:00
|
|
|
void send_MPpayload();
|
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*Prepare messages and publish*/
|
|
|
|
void prepare_msg_and_publish();
|
|
|
|
|
|
|
|
/*Refresh neighbours Position for every ten step*/
|
|
|
|
void maintain_pos(int tim_step);
|
|
|
|
|
|
|
|
/*Puts neighbours position inside neigbours_pos_map*/
|
2017-12-08 19:53:32 -04:00
|
|
|
void neighbours_pos_put(int id, buzz_utility::Pos_struct pos_arr);
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*Puts raw neighbours position in lat.,long.,alt. inside raw_neigbours_pos_map*/
|
2017-12-08 19:53:32 -04:00
|
|
|
void raw_neighbours_pos_put(int id, buzz_utility::Pos_struct pos_arr);
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*Set the current position of the robot callback*/
|
2017-12-08 19:53:32 -04:00
|
|
|
void set_cur_pos(double latitude, double longitude, double altitude);
|
2017-12-08 20:44:24 -04:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*convert from spherical to cartesian coordinate system callback */
|
2017-12-08 19:53:32 -04:00
|
|
|
float constrainAngle(float x);
|
2017-12-21 13:20:26 -04:00
|
|
|
void gps_rb(POSE nei_pos, double out[]);
|
|
|
|
void gps_ned_cur(float& ned_x, float& ned_y, POSE t);
|
2017-12-08 19:53:32 -04:00
|
|
|
void gps_convert_ned(float& ned_x, float& ned_y, double gps_t_lon, double gps_t_lat, double gps_r_lon,
|
|
|
|
double gps_r_lat);
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*battery status callback */
|
2018-09-06 13:47:38 -03:00
|
|
|
void battery(const sensor_msgs::BatteryState::ConstPtr& msg);
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*flight extended status callback*/
|
|
|
|
void flight_extended_status_update(const mavros_msgs::ExtendedState::ConstPtr& msg);
|
|
|
|
|
|
|
|
/*flight status callback*/
|
|
|
|
void flight_status_update(const mavros_msgs::State::ConstPtr& msg);
|
|
|
|
|
|
|
|
/*current position callback*/
|
2017-12-21 13:20:26 -04:00
|
|
|
void global_gps_callback(const sensor_msgs::NavSatFix::ConstPtr& msg);
|
2016-10-10 21:22:17 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*current relative altitude callback*/
|
2017-12-21 13:20:26 -04:00
|
|
|
void rel_alt_callback(const std_msgs::Float64::ConstPtr& msg);
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*payload callback callback*/
|
|
|
|
void payload_obt(const mavros_msgs::Mavlink::ConstPtr& msg);
|
|
|
|
|
|
|
|
/* RC commands service */
|
2017-12-08 19:53:32 -04:00
|
|
|
bool rc_callback(mavros_msgs::CommandLong::Request& req, mavros_msgs::CommandLong::Response& res);
|
2017-06-23 11:39:14 -03:00
|
|
|
|
|
|
|
/*robot id sub callback*/
|
|
|
|
void set_robot_id(const std_msgs::UInt8::ConstPtr& msg);
|
2017-04-03 20:50:09 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*Obstacle distance table callback*/
|
2017-12-21 13:20:26 -04:00
|
|
|
void obstacle_dist_callback(const sensor_msgs::LaserScan::ConstPtr& msg);
|
2016-10-10 21:22:17 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*Get publisher and subscriber from YML file*/
|
|
|
|
void GetSubscriptionParameters(ros::NodeHandle& node_handle);
|
2016-10-10 21:22:17 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*Arm/disarm method that can be called from buzz*/
|
|
|
|
void Arm();
|
2017-02-20 00:30:45 -04:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*set mode like guided for solo*/
|
|
|
|
void SetMode(std::string mode, int delay_miliseconds);
|
2016-10-10 21:22:17 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
/*Robot independent subscribers*/
|
|
|
|
void Subscribe(ros::NodeHandle& n_c);
|
2017-02-15 19:08:41 -04:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
void local_pos_callback(const geometry_msgs::PoseStamped::ConstPtr& pose);
|
2017-02-17 17:35:08 -04:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
void fc_command_setup();
|
2017-02-20 18:59:35 -04:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
void SetLocalPosition(float x, float y, float z, float yaw);
|
2017-05-12 11:46:39 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
void SetLocalPositionNonRaw(float x, float y, float z, float yaw);
|
2017-02-21 17:09:16 -04:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
void SetStreamRate(int id, int rate, int on_off);
|
2017-03-14 12:09:01 -03:00
|
|
|
|
2017-06-23 11:39:14 -03:00
|
|
|
void get_number_of_robots();
|
2017-03-29 15:57:31 -03:00
|
|
|
|
2017-12-21 13:20:26 -04:00
|
|
|
// functions related to Xbee modules information
|
2017-06-23 11:39:14 -03:00
|
|
|
void GetRobotId();
|
2017-12-08 19:53:32 -04:00
|
|
|
bool GetDequeFull(bool& result);
|
|
|
|
bool GetRssi(float& result);
|
2017-06-23 11:39:14 -03:00
|
|
|
bool TriggerAPIRssi(const uint8_t short_id);
|
2017-12-08 19:53:32 -04:00
|
|
|
bool GetAPIRssi(const uint8_t short_id, float& result);
|
|
|
|
bool GetRawPacketLoss(const uint8_t short_id, float& result);
|
|
|
|
bool GetFilteredPacketLoss(const uint8_t short_id, float& result);
|
2017-07-05 10:37:35 -03:00
|
|
|
void get_xbee_status();
|
2017-12-21 13:20:26 -04:00
|
|
|
|
2018-09-06 13:47:38 -03:00
|
|
|
void time_sync_step();
|
|
|
|
void push_timesync_nei_msg(int nid, uint64_t nh, uint64_t nl, double nr);
|
|
|
|
uint64_t get_logical_time();
|
|
|
|
void set_logical_time_correction(uint64_t cor);
|
2016-10-10 21:22:17 -03:00
|
|
|
};
|
|
|
|
}
|