ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

how to move px4 on frames x axis with offboard

asked 2021-05-24 08:48:40 -0500

abdupa gravatar image

Hi! I want to move my drone with Offboard mode to X or Y axis. When I use Twist.linear.x=speed Drone move global X move to my frame's X axis. So for move drone to frame X or Y which mavros messages I have to use? Do you have a example with python?(This is my example script:

!/usr/bin/env python

ROS python API

import rospy import numpy as np

3D point & Stamped Pose msgs

from geometry_msgs.msg import Point, PoseStamped, Twist from sensor_msgs.msg import NavSatFix, Image

import all mavros messages and services

from mavros_msgs.msg import * from mavros_msgs.srv import * from decimal import * from math import radians, cos, sin, asin, sqrt

Message publisher for haversine

velocity_pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel_unstamped', Twist, queue_size=10) msg1 = Twist()

Current Position

latitude = 0 longitude = 0 altitude = 0.0 local_x=0 local_y=0

Position before Move function execute

previous_altitude = 0.0

def globalPositionCallback(globalPositionCallback): global latitude global longitude latitude = globalPositionCallback.latitude longitude = globalPositionCallback.longitude

def localPositionCallback(localPositionCallback): global altitude, local_x,local_y altitude=localPositionCallback.pose.position.z local_x=localPositionCallback.pose.position.x local_y = localPositionCallback.pose.position.y

class fcuModes: def __init__(self): pass

def setTakeoff(self):
global longitude, latitude
    rospy.wait_for_service('mavros/cmd/takeoff')
    try:
        takeoffService = rospy.ServiceProxy(
            '/mavros/cmd/takeoff', mavros_msgs.srv.CommandTOL)
        takeoffService(altitude=5, latitude=latitude,
                       longitude=longitude, min_pitch=0, yaw=0)
    except rospy.ServiceException as e: 
        print ("Service takeoff call failed: %s" %e)

def setArm(self):
    rospy.wait_for_service('mavros/cmd/arming')
    try:
        print("Waiting for arming...")
        armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
        armService(True)
    except rospy.ServiceException as e:
        print("Service arming call failed: %s" % e)

def setDisarm(self):
    rospy.wait_for_service('mavros/cmd/arming')
    try:
        print("Waiting for disarming...")
        armService = rospy.ServiceProxy('mavros/cmd/arming', mavros_msgs.srv.CommandBool)
        armService(False)
    except rospy.ServiceException as e:
        print("Service disarming call failed: %s" % e)

def setStabilizedMode(self):
    rospy.wait_for_service('mavros/set_mode')
    try:
        print("It's stabilazed mode!")
        flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
        flightModeService(custom_mode='STABILIZED')
    except rospy.ServiceException as e:
        print("service set_mode call failed: %s. Stabilized Mode could not be set." % e)

def setOffboardMode(self):
    global sp_pub
    cnt = Controller()
    rate = rospy.Rate(20.0)
    k = 0
    while k < 10:
        sp_pub.publish(cnt.sp)
        rate.sleep()
        k = k + 1
    sp_pub = rospy.Publisher('mavros/setpoint_raw/local', PositionTarget, queue_size=1)
    rospy.wait_for_service('/mavros/set_mode')

    try:
        flightModeService = rospy.ServiceProxy('/mavros/set_mode', mavros_msgs.srv.SetMode)
        response = flightModeService(custom_mode='OFFBOARD')
        return response.mode_sent
    except rospy.ServiceException as e:
        print
        "service set_mode call failed: %s. Offboard Mode could not be set." % e
        return False

def setLoiterMode(self):
    rospy.wait_for_service('/mavros/set_mode')
    try:
        flightModeService = rospy.ServiceProxy('/mavros/set_mode', mavros_msgs.srv.SetMode)
        isModeChanged = flightModeService(custom_mode='AUTO.LOITER')  # return true or false
    except rospy.ServiceException as e:
        print(
            "service set_mode call failed: %s. AUTO.LOITER Mode could not be set. Check that GPS is enabled %s" % e)

def setAltitudeMode(self):
    rospy.wait_for_service('mavros/set_mode')
    try:
        flightModeService = rospy.ServiceProxy('mavros/set_mode', mavros_msgs.srv.SetMode)
        flightModeService(custom_mode='ALTCTL')
    except rospy ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-05-28 17:33:00 -0500

You can check the list of topics available in http://wiki.ros.org/mavros

For setting position setpoints, you can use ~local_position/pose

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2021-05-24 08:48:40 -0500

Seen: 816 times

Last updated: May 24 '21