setHeight.py
1.18 KB
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
# 드론 높이조정 테스트 코드 (드론 1대용)
import CoDrone
from CoDrone.system import Direction
from time import sleep
errorRange = 50 # 오차범위
def getPosition(drone):
position = drone.get_opt_flow_position() # 상대좌표 (시작 0,0)
height = drone.get_height() # 고
print("x={} y={} z={}".format(position.X, position.Y, height)) # 단위(mm)
def setHeight(_mHeight, _slave):
while True:
_sHeight = _slave.get_height()
if _mHeight - errorRange <= _sHeight <= _mHeight + errorRange:
print('hit')
return
elif _sHeight < _mHeight - errorRange:
_slave.go(Direction.UP)
getPosition(_slave)
sleep(0.2)
print('up')
elif _sHeight > _mHeight + errorRange:
_slave.go(Direction.DOWN)
getPosition(_slave)
sleep(0.2)
print('down')
def main():
drone = CoDrone.CoDrone()
drone.connect()
oHeight = 200 # 목적 높이
drone.takeoff()
getPosition(drone)
setHeight(oHeight, drone)
getPosition(drone)
drone.hover(3)
print('Land')
drone.land()
if __name__ == '__main__':
main()