mavlink / mavros

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

Error z position offboard #1490

Open bobolodrone opened 4 years ago

bobolodrone commented 4 years ago

Hi everyone, I am new here and I hope that I didn’t skip any step in order to public a topic :slight_smile: I built a drone and a would like to use the offboard mode in order to program my own code. Yesterday i used this code and when the drone took off from the ground didn’t reach 0.30 m but continued to rise until it hit the ceiling (2.80m). i don’t understand why this error. i attached my code here.

include <ros/ros.h>

include <geometry_msgs/PoseStamped.h>

include <mavros_msgs/CommandBool.h>

include <mavros_msgs/CommandTOL.h>

include <mavros_msgs/SetMode.h>

include <mavros_msgs/State.h>

define FLIGHT_ALTITUDE 0.3f

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

int main(int argc, char **argv) { ros::init(argc, argv, "off_square"); 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 land_client = nh.serviceClient<mavros_msgs::CommandTOL>
  ("mavros/cmd/land");
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
        ("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();
    ROS_INFO("connecting to FCT...");
}

geometry_msgs::PoseStamped pose;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;

//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;

mavros_msgs::CommandTOL land_cmd;
land_cmd.request.yaw = 0;
land_cmd.request.latitude = 0;
land_cmd.request.longitude = 0;
land_cmd.request.altitude = 0;

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

// change to offboard mode and arm
while(ros::ok() && !current_state.armed){
    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();
        }
    }
    local_pos_pub.publish(pose);
    ros::spinOnce();
    rate.sleep();
}

// go to the first waypoint
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;

ROS_INFO("going to the first way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){
  local_pos_pub.publish(pose);
  ros::spinOnce();
  rate.sleep();
}
ROS_INFO("first way point finished!");

// go to the second waypoint
pose.pose.position.x = 0;
pose.pose.position.y = 0.3;
pose.pose.position.z = FLIGHT_ALTITUDE;

//send setpoints for 10 seconds
ROS_INFO("going to second way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){

  local_pos_pub.publish(pose);
  ros::spinOnce();
  rate.sleep();
}
ROS_INFO("second way point finished!");

// go to the third waypoint
pose.pose.position.x = 0.3;
pose.pose.position.y = 0.3;
pose.pose.position.z = FLIGHT_ALTITUDE;
//send setpoints for 10 seconds
ROS_INFO("going to third way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){

  local_pos_pub.publish(pose);
  ros::spinOnce();
  rate.sleep();
}
ROS_INFO("third way point finished!");

// go to the forth waypoint
pose.pose.position.x = 0.3;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;
//send setpoints for 10 seconds
ROS_INFO("going to forth way point");
for(int i = 0; ros::ok() && i < 10*20; ++i){

  local_pos_pub.publish(pose);
  ros::spinOnce();
  rate.sleep();
}
ROS_INFO("forth way point finished!");

pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = FLIGHT_ALTITUDE;
ROS_INFO("going back to the first point!");
//send setpoints for 10 seconds
for(int i = 0; ros::ok() && i < 10*20; ++i){

  local_pos_pub.publish(pose);
  ros::spinOnce();
  rate.sleep();
}

ROS_INFO("tring to land");
while (!(land_client.call(land_cmd) &&
        land_cmd.response.success)){
  //local_pos_pub.publish(pose);
  ROS_INFO("tring to land");
  ros::spinOnce();
  rate.sleep();
}
return 0;

}

staycoolish commented 4 years ago

Look at this topic: https://github.com/mavlink/mavros/issues/1204

This person changed the baud rate to get better responses. Maybe works for you as well.

bobolodrone commented 3 years ago

Thank for your answer, I tried it but nothing. I saw that local pose give me negative number-5m)maybe this the problem so The drone want To fly up for 5,30 m. What do you think?

bobolodrone commented 3 years ago

Thanks for the answer. I tried it but nothing. I saw That local pose give me negative number (-5m) so, maybe, the drone want to flight up for 5.30 m. What do you think about it?

mazmazbiasa commented 2 months ago

@bobolodrone have you solve the problem?