mavlink / mavros

MAVLink to ROS gateway with proxy for Ground Control Station
Other
879 stars 990 forks source link

Unable to arm APM with with ROS #1153

Closed Alvinosaur closed 5 years ago

Alvinosaur commented 5 years ago

This is only bug and feature tracker, please use it to report bugs or request features.


Issue details

I have a couple of questions regarding the proper way to connect to my pixhawk (running on APM) correctly and also how to arm and send commands through an offboard computer. I am also unable to arm the pixhawk through mavros.

First, I am only able to occasionally connect with the pixhawk with mavros and get actual data on topics. My procedure for connecting is as follows:

  1. Connect battery to drone and usb serial to my laptop (just temporary for testing)
  2. run roscore in one tab
  3. run mavros: roslaunch mavros apm.launch

After the above steps, I feel that I should be able to connect, but the various topics I can use (ex: /mavros/battery) do not output anything. When I see this, I try random things like using the RC to try to connect and arm the drone or starting up QGC, which successfully connects with the drone. After several of these random attempts and head-banging, I can connect.

Second, I am unable to arm the drone. I try running the following: rosrun mavros mavsafety arm, but this produces the error: Request failed. Check mavros logs. Of course then, I try to check the logs by first running roslaunch-logs, which gives me the path to a file that lists various log files. I vim into these, but do not see anything meaningful. I then run rqt_console to see real-time error messages, which is pretty helpful, but doesn't help me with arming the drone. Where can I check the mavros logs?

Also, when I am trying to arm the drone through a .cpp file rather than through terminal, I am following the offboard example. Here is my code below. Before taking a look, my understanding is that I publish desired values (ex: global positions) that I want the drone to move to. I also should publish some arm command to the arming topic, mavros/cmd/arming.

Please, any assistance is greatly appreciated. Please let me know if my post was bad as this is my first github issue post. This project is to have an Iris 3DR drone fly to some goal location and detect crop anomalies to send to mobile ground robots for further inspection :) Thank you!

#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <geometry_msgs/TwistStamped.h>
#include <geometry_msgs/Vector3.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include <mavros_msgs/Thrust.h>

#include <sstream>

mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}

int main(int argc, char **argv)
{
    char *target_mode = "GUIDED";
    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::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::Publisher pub_att = nh.advertise<geometry_msgs::PoseStamped>("/mavros/setpoint_attitude/attitude", 100);
    ros::Publisher pub_vel = nh.advertise<geometry_msgs::TwistStamped>("/mavros/setpoint_attitude/cmd_vel", 100);
    ros::Publisher pub_thr = nh.advertise<mavros_msgs::Thrust>("/mavros/setpoint_attitude/thrust", 100);

    //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();
        // std::stringstream ss;
        // ss << "[Not Connected]" << ", armed: " << armed << mode;
        ROS_INFO("Not Connected!");
    }

    // Set goals
    geometry_msgs::PoseStamped cmd_pose;
    cmd_pose.header.frame_id = 1;
    cmd_pose.pose.position.x = 0;
    cmd_pose.pose.position.y = 0;
    cmd_pose.pose.position.z = 2;

    geometry_msgs::TwistStamped cmd_vel;
    cmd_vel.header.frame_id = 1;
    cmd_vel.twist.linear.x = 0.0;
    cmd_vel.twist.linear.y = 0.0;
    cmd_vel.twist.linear.z = 1.0;

    mavros_msgs::Thrust cmd_thr;
    cmd_thr.header.frame_id = 1;

    // send a few setpoints before starting to make sure drone
    // has an initial pose when offboard mode starts
    ROS_INFO("Setting initial setpoints");
    for(int i = 0; ros::ok() && i < 100; i++){
        cmd_pose.header.stamp = ros::Time::now();
        cmd_pose.header.seq = i;

        local_pos_pub.publish(cmd_pose);
        pub_att.publish(cmd_pose);
        pub_vel.publish(cmd_vel);
        pub_thr.publish(cmd_thr);
        ros::spinOnce();
        rate.sleep();
    }

    // now start offboard mode now that some initial pose has been set
    mavros_msgs::SetMode offb_set_mode;
    offb_set_mode.request.custom_mode = target_mode;

    mavros_msgs::CommandBool arm_cmd;
    arm_cmd.request.value = true;

    ros::Time last_request = ros::Time::now();

    // switch to offboard mode, and after 5 seconds arm the quad to allow
    // it to fly
    ROS_INFO("Starting Loop");
    while(!ros::ok()){
        ROS_INFO("Not OK!");
    }
    while(ros::ok()){
        /*
        if( current_state.mode == target_mode &&
            (ros::Time::now() - last_request > ros::Duration(5.0))){
            if( set_mode_client.call(offb_set_mode) &&
                offb_set_mode.response.mode_sent){
                if (current_state.mode != target_mode) ROS_INFO("WRONG MODE");
                ROS_INFO("Offboard enabled");
            }
            last_request = ros::Time::now();
        } else {
        */
            if((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();
            }

        local_pos_pub.publish(cmd_pose);

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

MAVROS version and platform

Mavros: 0.28.0 ROS: Kinetic Ubuntu: 16.04

Autopilot type and version

[ ArduPilotMega ] ArduPilot

Version: not too sure at the moment, don't want to close down mavros connection to check QGC

Node logs

how do I get this? is this the /mavlink/from topic?

Diagnostics

header: 
  seq: 2786
  stamp: 
    secs: 1546585718
    nsecs:  49762905
  frame_id: ''
status: 
  - 
    level: 0
    name: "mavros: FCU connection"
    message: "connected"
    hardware_id: "/dev/ttyACM0:57600"
    values: 
      - 
        key: "Received packets:"
        value: "16002"
      - 
        key: "Dropped packets:"
        value: "0"
      - 
        key: "Buffer overruns:"
        value: "0"
      - 
        key: "Parse errors:"
        value: "0"
      - 
        key: "Rx sequence number:"
        value: "67"
      - 
        key: "Tx sequence number:"
        value: "183"
      - 
        key: "Rx total bytes:"
        value: "6011092"
      - 
        key: "Tx total bytes:"
        value: "93002"
      - 
        key: "Rx speed:"
        value: "1540.000000"
      - 
        key: "Tx speed:"
        value: "21.000000"
  - 
    level: 0
    name: "mavros: GPS"
    message: "3D fix"
    hardware_id: "/dev/ttyACM0:57600"
    values: 
      - 
        key: "Satellites visible"
        value: "7"
      - 
        key: "Fix type"
        value: "3"
      - 
        key: "EPH (m)"
        value: "2.48"
      - 
        key: "EPV (m)"
        value: "Unknown"
  - 
    level: 0
    name: "mavros: Heartbeat"
    message: "Normal"
    hardware_id: "/dev/ttyACM0:57600"
    values: 
      - 
        key: "Heartbeats since startup"
        value: "3482"
      - 
        key: "Frequency (Hz)"
        value: "0.962970"
      - 
        key: "Vehicle type"
        value: "Quadrotor"
      - 
        key: "Autopilot type"
        value: "ArduPilotMega / ArduCopter"
      - 
        key: "Mode"
        value: "LOITER"
      - 
        key: "System status"
        value: "Standby"
  - 
    level: 0
    name: "mavros: System"
    message: "Normal"
    hardware_id: "/dev/ttyACM0:57600"
    values: 
      - 
        key: "Sensor present"
        value: "0x0061FC2F"
      - 
        key: "Sensor enabled"
        value: "0x0061FC2F"
      - 
        key: "Sensor helth"
        value: "0x0061FC2F"
      - 
        key: "3D gyro"
        value: "Ok"
      - 
        key: "3D accelerometer"
        value: "Ok"
      - 
        key: "3D magnetometer"
        value: "Ok"
      - 
        key: "absolute pressure"
        value: "Ok"
      - 
        key: "GPS"
        value: "Ok"
      - 
        key: "3D angular rate control"
        value: "Ok"
      - 
        key: "attitude stabilization"
        value: "Ok"
      - 
        key: "yaw position"
        value: "Ok"
      - 
        key: "z/altitude control"
        value: "Ok"
      - 
        key: "x/y position control"
        value: "Ok"
      - 
        key: "motor outputs / control"
        value: "Ok"
      - 
        key: "rc receiver"
        value: "Ok"
      - 
        key: "AHRS subsystem health"
        value: "Ok"
      - 
        key: "Terrain subsystem health"
        value: "Ok"
      - 
        key: "CPU Load (%)"
        value: "62.8"
      - 
        key: "Drop rate (%)"
        value: "0.0"
      - 
        key: "Errors comm"
        value: "0"
      - 
        key: "Errors count #1"
        value: "0"
      - 
        key: "Errors count #2"
        value: "0"
      - 
        key: "Errors count #3"
        value: "0"
      - 
        key: "Errors count #4"
        value: "0"
  - 
    level: 0
    name: "mavros: Battery"
    message: "Normal"
    hardware_id: "/dev/ttyACM0:57600"
    values: 
      - 
        key: "Voltage"
        value: "11.74"
      - 
        key: "Current"
        value: "0.3"
      - 
        key: "Remaining"
        value: "91.0"
---

Check ID

OK. I got messages from 1:1.

---
Received 878 messages, from 1 addresses
sys:comp   list of messages
  1:1     0, 1, 2, 133, 136, 152, 150, 24, 27, 29, 30, 33, 162, 163, 36, 165, 42, 178, 62, 74, 35, 116, 125
Alvinosaur commented 5 years ago

I've solved this issue, but am having others. I apologize for opening the issue so soon.

Jawad-RoboLearn commented 2 years ago

Hi, how did you solve this issue? I have faced the same problem. As I have used the same type of code.

  1. First I have used vicon for external pose. In launch file, i remap from /vicon/myquad/myquad to /mavros/mocap/tf. It works well.
  2. Then, i use /mavros/setpoint_position/local to send the message.
  3. The issue is offboard is enable after every 5 seconds but no mavros did not arm the drone.
  4. Whats wrong?