-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroneRotate.py
109 lines (90 loc) · 3.49 KB
/
droneRotate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from dronekit import connect, VehicleMode, LocationGlobalRelative, APIException, Command
from pymavlink import mavutil
import time
import socket
import math
import argparse
def copterConnection():
parser = argparse.ArgumentParser(description = 'Connect to Copter')
parser.add_argument('--connect')
args = parser.parse_args()
connection_string = args.connect
# Start SITL if no connection string specified
if not connection_string:
import dronekit_sitl
sitl = dronekit_sitl.start_default()
connection_string = sitl.connection_string()
print("Connected at : %s" %connection_string)
# Connect to the Vehicle
vehicle = connect(connection_string, wait_ready=True)
return vehicle
def armTakeoff(targetAlt):
# Pre-flight check & drone arm
print("Basic pre-arm checks")
# Don't try to arm until autopilot is ready
while not vehicle.is_armable:
print(" Waiting for vehicle to initialise...")
time.sleep(1)
print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
# Confirm vehicle armed before attempting to take off
while not vehicle.armed:
print(" Waiting for arming...")
time.sleep(1)
print("Ready to takeoff")
vehicle.simple_takeoff(targetAlt)
while True:
# Break and return from function just below target altitude.
if vehicle.location.global_relative_frame.alt >= targetAlt * 0.97:
print("Reached target altitude")
break
time.sleep(1)
return None
def yawControl(degrees, relativeFrameID):
if relativeFrameID:
is_relative = 1 #yaw relative to the drove heading
else:
is_relative = 0 #yaw is absolute angle
msg = vehicle.message_factory.command_long_encode(
0, 0, # target system , target component
mavutil.mavlink.MAV_CMD_CONDITION_YAW, # command type
0, # confirmation
degrees, # yaw degrees
0, # yaw speed
1, # direction -1:CCW 1:CW (CW: ClockWise)
is_relative, # 1:relative offset 0:absolute value
0, 0, 0) # params not used in this msg
vehicle.send_mavlink(msg)
vehicle.flush()
def dummyYawInitialiser():
lat = vehicle.location.global_relative_frame.lat
lon = vehicle.location.global_relative_frame.lon
alt = vehicle.location.global_relative_frame.alt
droneLocation = LocationGlobalRelative(lat, lon, alt)
msg = vehicle.message_factory.set_position_target_global_int_encode( 0, 0, 0,
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
0b0000111111111000, # type mask
droneLocation.lat*1e7,
droneLocation.lon*1e7,
droneLocation.alt,
0, 0, 0, # x,y,z velocity in NED frame
0, 0, 0,
0, 0)
vehicle.send_mavlink(msg)
vehicle.flush()
vehicle = copterConnection()
vehicle.parameters['BATT_CAPACITY']=99999999
dummyYawInitialiser()
vehicle.airspeed = 7
print(" Home Location: %s" % vehicle.location.global_frame)
armTakeoff(10)
time.sleep(2)
yawControl(30,1)
time.sleep(10)
yawControl(0,0)
time.sleep(10)
yawControl(270,1)
time.sleep(10)
print("End of Rotations")