dariush
dariush

Reputation: 3341

How to do «go to left/right/forward/backward» with python dronekit?

I am using APM for autopiloting my hexacopter and following this tutorial. by looking at this available commands, I cannot see how one can command the drone to go to left/right/forward/backward?

Can anybody help me on this?

Upvotes: 0

Views: 4430

Answers (3)

BigZee
BigZee

Reputation: 496

I had the same problem, I hope this link helps look for the "controlling by specifying the vehicle’s velocity components" section of the article here

Upvotes: 0

dronacharya
dronacharya

Reputation: 1

Dronekit-python has non trivial APIs to command the drone in the local frame. From my personal experience, it was hard to get my head around on using these commands make my drone follow a shape locally, e.g a square or a circle. An alternative is using FlytOS drone APIs. If you see this sample python code on github you can see how easy it is to do command the drone to go left x meters then forward y meters etc. Jon's answer does show correctly how dronekit could be used to achieve what you are trying to do, but another beginner who might be intimidated by the complex code.

Upvotes: 0

jon
jon

Reputation: 36

You need to create a vehicle.message_factory.set_position_target_local_ned_encode. It will require a frame of mavutil.mavlink.MAV_FRAME_BODY_NED. You the add the required x,y and/or z velocities (in m/s) to the message.

from pymavlink import mavutil
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time

def send_body_ned_velocity(velocity_x, velocity_y, velocity_z, duration=0):
    msg = vehicle.message_factory.set_position_target_local_ned_encode(
        0,       # time_boot_ms (not used)
        0, 0,    # target system, target component
        mavutil.mavlink.MAV_FRAME_BODY_NED, # frame Needs to be MAV_FRAME_BODY_NED for forward/back left/right control.
        0b0000111111000111, # type_mask
        0, 0, 0, # x, y, z positions (not used)
        velocity_x, velocity_y, velocity_z, # m/s
        0, 0, 0, # x, y, z acceleration
        0, 0)
    for x in range(0,duration):
        vehicle.send_mavlink(msg)
        time.sleep(1)

connection_string = 'tcp:192.168.1.2:5760' # Edit to suit your needs.
takeoff_alt = 10
vehicle = connect(connection_string, wait_ready=True)
while not vehicle.is_armable:
    time.sleep(1)
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
while not vehicle.armed:
    print('Waiting for arming...')
    time.sleep(1)
vehicle.simple_takeoff(takeoff_alt) # Take off to target altitude
while True:
    print('Altitude: %d' %  self.vehicle.location.global_relative_frame.alt)
    if vehicle.location.global_relative_frame.alt >= takeoff_alt * 0.95:
        print('REACHED TARGET ALTITUDE')
        break
    time.sleep(1)

# This is the command to move the copter 5 m/s forward for 10 sec.
velocity_x = 0
velocity_y = 5
velocity_z = 0
duration = 10
send_body_ned_velocity(velocity_x, velocity_y, velocity_z, duration)

# backwards at 5 m/s for 10 sec.
velocity_x = 0
velocity_y = -5
velocity_z = 0
duration = 10
send_body_ned_velocity(velocity_x, velocity_y, velocity_z, duration)

vehicle.mode = VehicleMode("LAND")

Have fun, and of course, observe rigorous safe guards when programming and flying UAVs. A manual mode override is a must!

Upvotes: 2

Related Questions