I am trying to set up a multiple vehicle controller with ROS noetic using Gazebo, but I have run into a roadblock with initializing multiple drones in simulation. I am using c++ to create the code.
I have modified the the mavros offboard example (MAVROS Offboard control example | PX4 User Guide (v1.12)) to suit my needs up until this point, but I am unable to have multiple drones arm and fly in the same simulation instance.
For starters, the main function looks like this:
int main(int argc, char **argv){
waypoint way_point;
ros::init(argc, argv, "five_move_sim_node");
ros::NodeHandle nh;
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
("uav1/mavros/state", 10, state_cb);
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
("uav1/mavros/cmd/arming");
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
("uav1/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();
}
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(6.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(6.0))){
if( arming_client.call(arm_cmd) && arm_cmd.response.success){
ROS_INFO("Vehicle armed");
}
last_request = ros::Time::now();
}
}
way_point.go_to_skyhome();
ros::spinOnce();
rate.sleep();
}
return 0;
}
I can swap the ROS topics “uav1/mavros/state” etc with “uav0/mavros/state” but this only arms and flies one drone. I have tried to mimic this code in a function that is called for in main(), but it is never able to arm or offboard the vehicles. This code I have shared works on individual drones, so it is not an issue with the way_point.go_to_skyghome() function that sends setpoint commands. I have also tried to search for the specific instances of “mavros_msgs::SetMode” and “mavros_msgs::CommandBool” but I cannot find any documentation on how to extend these to multiple drones.
I found on the forums a post by user Jaeyoung-Lim GitHub - Jaeyoung-Lim/mavros_swarm: ROS based mav fleet control for swarming but I cannot see any commands that arm the drones.
Is there a better method in c++ to arming and offboarding multiple drones for flight in a single simulation environment? What code should I use instead to arm and offboard the drones?