Function Pushbutton1
Function Pushbutton1
"""
This example shows how to move/direct Copter and send commands in GUIDED mode using
DroneKit Python.
"""
import time
import math
import argparse
parser.add_argument('--connect',
help="Vehicle connection target string. If not specified, SITL automatically started and
)".used
args = parser.parse_args()
connection_string = [Link]
sitl = None
if not connection_string:
import dronekit_sitl
)(sitl = dronekit_sitl.start_default
)(connection_string = sitl.connection_string
def arm_and_takeoff(aTargetAltitude):
"""
"""
[Link](1)
print("Arming motors")
[Link] = VehicleMode("GUIDED")
[Link] = True
[Link](1)
print("Taking off!")
Wait until the vehicle reaches a safe height before processing the goto (otherwise the #
command
:while True
break
[Link](1)
arm_and_takeoff(5)
"""
Convenience functions for sending immediate/guided mode commands to control the Copter.
MAV_CMD_DO_SET_ROI - set direction where the camera gimbal is aimed (latitude, longitude, *
altitude)
/[Link]
"""
"""
This method sets an absolute heading by default, but you can set the `relative` parameter
By default the yaw of the vehicle will follow the direction of travel. After setting
the yaw using this function there is no way to return to the default yaw "follow direction
"""
:if relative
:else
(msg = vehicle.message_factory.command_long_encode
[Link].MAV_CMD_CONDITION_YAW, #command
confirmation# ,0
vehicle.send_mavlink(msg)
def set_roi(location):
"""
"""
(msg = vehicle.message_factory.command_long_encode
[Link].MAV_CMD_DO_SET_ROI, #command
confirmation# ,0
params 1-4# ,0 ,0 ,0 ,0
,[Link]
,[Link]
[Link]
vehicle.send_mavlink(msg)
"""
Functions to make it easy to convert between the different frames-of-reference. In particular these
make it easy to navigate in terms of "metres from the current position" when using commands that
take
The methods are approximations only, and may be less accurate over longer distances, and when
close
Specifically, it provides:
get_location_metres - Get LocationGlobal (decimal degrees) at distance (m) North & East of a given *
.LocationGlobal
"""
"""
Returns a LocationGlobal object containing the latitude/longitude `dNorth` and `dEast` metres
from the
specified `original_location`. The returned LocationGlobal has the same `alt` value
.`as `original_location
The function is useful when you want to move the vehicle around specifying locations relative to
The algorithm is relatively accurate over small distances (10m within 1km) except close to the
.poles
[Link]
some-amount-of-meters
"""
dLat = dNorth/earth_radius
dLon = dEast/(earth_radius*[Link]([Link]*original_location.lat/180))
targetlocation=LocationGlobal(newlat, newlon,original_location.alt)
targetlocation=LocationGlobalRelative(newlat, newlon,original_location.alt)
:else
;return targetlocation
"""
This method is an approximation, and will not be accurate over large distances and close to the
[Link]
"""
"""
.Returns the bearing between the two LocationGlobal objects passed as parameters
This method is an approximation, and may not be accurate over large distances and close to the
[Link]
"""
bearing += 360.00
;return bearing
"""
Functions to move the vehicle to a specified position (as opposed to controlling movement by setting
velocity components).
MAV_FRAME_GLOBAL_RELATIVE_ALT_INT frame
MAV_FRAME_BODY_NED frame
def goto_position_target_global_int(aLocation):
"""
.See the above link for information on the type_mask (0=enable, 1=ignore)
"""
(msg = vehicle.message_factory.set_position_target_global_int_encode
[Link].MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
[Link], # alt - Altitude in meters in AMSL altitude, not WGS84 if absolute or relative,
above terrain if GLOBAL_TERRAIN_ALT_INT
vehicle.send_mavlink(msg)
def goto_position_target_local_ned(north, east, down):
"""
It is important to remember that in this frame, positive altitudes are entered as negative
.Down" values. So if down is "10", this will be 10 metres below the home altitude "
Starting from AC3.3 the method respects the frame setting. Prior to that the frame was
[Link]
.See the above link for information on the type_mask (0=enable, 1=ignore)
"""
(msg = vehicle.message_factory.set_position_target_local_ned_encode
[Link].MAV_FRAME_LOCAL_NED, # frame
north, east, down, # x, y, z positions (or North, East, Down in the MAV_FRAME_BODY_NED
frame
vehicle.send_mavlink(msg)
"""
Moves the vehicle to a position dNorth metres North and dEast metres East of the current
.position
.the target position. This allows it to be called with different position-setting commands
"""
currentLocation = [Link].global_relative_frame
gotoFunction(targetLocation)
remainingDistance=get_distance_metres([Link].global_relative_frame,
targetLocation)
print("Reached target")
;break
[Link](2)
"""
Functions that move the vehicle by specifying the velocity components in each direction.
The two functions use different MAVLink commands. The main difference is
that depending on the frame used, the NED velocity can be relative to the vehicle
.orientation
"""
"""
velocity components
(
.)[Link]
Note that from AC3.3 the message should be re-sent every second (after about 3 seconds
with no message the velocity will drop back to zero). In AC3.2.1 and earlier the specified
velocity persists until it is canceled. The code below should work on either version
.See the above link for information on the type_mask (0=enable, 1=ignore)
"""
(msg = vehicle.message_factory.set_position_target_local_ned_encode
[Link].MAV_FRAME_LOCAL_NED, # frame
:for x in range(0,duration)
vehicle.send_mavlink(msg)
[Link](1)
def send_global_velocity(velocity_x, velocity_y, velocity_z, duration):
"""
This uses the SET_POSITION_TARGET_GLOBAL_INT command with type mask enabling only
velocity components
(
.)[Link]
Note that from AC3.3 the message should be re-sent every second (after about 3 seconds
with no message the velocity will drop back to zero). In AC3.2.1 and earlier the specified
velocity persists until it is canceled. The code below should work on either version
.See the above link for information on the type_mask (0=enable, 1=ignore)
"""
(msg = vehicle.message_factory.set_position_target_global_int_encode
[Link].MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
:for x in range(0,duration)
vehicle.send_mavlink(msg)
[Link](1)
"""
The method is called indirectly via a custom "goto" that allows the target position to be
specified as a distance in metres (North/East) from the current position, and which reports
the distance-to-target.
"""
[Link]=5
goto(0, 100)
goto(-80, -50)
"""
a target position (rather than controlling movement using velocity vectors). The command is
The goto_position_target_global_int method is called indirectly from a custom "goto" that allows
,the target position to be specified as a distance in metres (North/East) from the current position
The code also sets the speed (MAV_CMD_DO_CHANGE_SPEED). In AC3.2.1 Copter will accelerate to
this speed
.near the centre of its journey and then decelerate as it reaches the target
"""
[Link] = 5
[Link] = 15
[Link] = 10
"""
Fly the vehicle in a 50m square path, using the SET_POSITION_TARGET_LOCAL_NED command
.and specifying a target position (rather than controlling movement using velocity vectors)
The position is specified in terms of the NED (North East Down) relative to the Home location.
WARNING: The "D" in NED means "Down". Using a positive D value will drive the vehicle into the
ground!
The code sleeps for a time (DURATION) to give the vehicle time to reach each position (rather than
The code also sets the region of interest (MAV_CMD_DO_SET_ROI) via the `set_roi()` method. This
points the
camera gimbal at the the selected location (in this case it aligns the whole vehicle to point at the
.ROI)
"""
goto_position_target_local_ned(50,0,-10)
NOTE that this has to be called after the goto command as first "move" command of a particular #
type
set_roi([Link].global_relative_frame)
[Link](DURATION)
goto_position_target_local_ned(50,50,-10)
[Link](DURATION)
set_roi([Link].global_relative_frame)
[Link](DURATION)
goto_position_target_local_ned(0,0,-10)
[Link](DURATION)
"""
Fly the vehicle in a SQUARE path using velocity vectors (the underlying code calls the
The thread sleeps for a time (DURATION) which defines the distance that will be travelled.
The code also sets the yaw (MAV_CMD_CONDITION_YAW) using the `set_yaw()` method in each
segment
"""
NORTH = 2
SOUTH = -2
:Note for vy #
vy > 0 => fly East #
EAST = 2
WEST = -2
:Note for vz #
UP = -0.5
DOWN = 0.5
condition_yaw(180)
send_ned_velocity(SOUTH,0,UP,DURATION)
send_ned_velocity(0,0,0,1)
condition_yaw(270)
send_ned_velocity(0,0,0,1)
condition_yaw(0)
print("Velocity North")
send_ned_velocity(NORTH,0,0,DURATION)
send_ned_velocity(0,0,0,1)
condition_yaw(90)
print("Velocity East")
send_ned_velocity(0,EAST,0,DURATION)
send_ned_velocity(0,0,0,1)
"""
Fly the vehicle in a DIAMOND path using velocity vectors (the underlying code calls the
The thread sleeps for a time (DURATION) which defines the distance that will be travelled.
The code sets the yaw (MAV_CMD_CONDITION_YAW) using the `set_yaw()` method using relative
headings
.so that the front of the vehicle points in the direction of travel
At the end of the second segment the code sets a new home location to the current point.
"""
vx, vy are parallel to North and East (independent of the vehicle orientation) #
condition_yaw(225)
send_global_velocity(SOUTH,WEST,UP,DURATION)
send_global_velocity(0,0,0,1)
condition_yaw(90,relative=True)
send_global_velocity(NORTH,WEST,DOWN,DURATION)
send_global_velocity(0,0,0,1)
vehicle.home_location=[Link].global_frame
)([Link]
cmds.wait_ready()
condition_yaw(90,relative=True)
send_global_velocity(NORTH,EAST,0,DURATION)
send_global_velocity(0,0,0,1)
condition_yaw(90,relative=True)
send_global_velocity(SOUTH,EAST,0,DURATION)
send_global_velocity(0,0,0,1)
"""
"""
)([Link]
)([Link]
print("Completed")