/
motion_commander_demo.py
99 lines (80 loc) · 3.25 KB
/
motion_commander_demo.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
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2017 Bitcraze AB
#
# Crazyflie Nano Quadcopter Client
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
This script shows the basic use of the MotionCommander class.
Simple example that connects to the crazyflie at `URI` and runs a
sequence. This script requires some kind of location system, it has been
tested with (and designed for) the flow deck.
The MotionCommander uses velocity setpoints.
Change the URI variable to your Crazyflie configuration.
"""
import logging
import time
import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.positioning.motion_commander import MotionCommander
from cflib.utils import uri_helper
URI = uri_helper.uri_from_env(default='radio://0/80/2M/E7E7E7E7E7')
# Only output errors from the logging framework
logging.basicConfig(level=logging.ERROR)
if __name__ == '__main__':
# Initialize the low-level drivers
cflib.crtp.init_drivers()
with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:
# We take off when the commander is created
with MotionCommander(scf) as mc:
time.sleep(1)
# There is a set of functions that move a specific distance
# We can move in all directions
mc.forward(0.8)
mc.back(0.8)
time.sleep(1)
mc.up(0.5)
mc.down(0.5)
time.sleep(1)
# We can also set the velocity
mc.right(0.5, velocity=0.8)
time.sleep(1)
mc.left(0.5, velocity=0.4)
time.sleep(1)
# We can do circles or parts of circles
mc.circle_right(0.5, velocity=0.5, angle_degrees=180)
# Or turn
mc.turn_left(90)
time.sleep(1)
# We can move along a line in 3D space
mc.move_distance(-1, 0.0, 0.5, velocity=0.6)
time.sleep(1)
# There is also a set of functions that start a motion. The
# Crazyflie will keep on going until it gets a new command.
mc.start_left(velocity=0.5)
# The motion is started and we can do other stuff, printing for
# instance
for _ in range(5):
print('Doing other work')
time.sleep(0.2)
# And we can stop
mc.stop()
# We land when the MotionCommander goes out of scope