-
Notifications
You must be signed in to change notification settings - Fork 929
Expand file tree
/
Copy pathhigh_level_commander.py
More file actions
202 lines (176 loc) · 7.34 KB
/
Copy pathhigh_level_commander.py
File metadata and controls
202 lines (176 loc) · 7.34 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2018-2020 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/>.
"""
Used for sending high level setpoints to the Crazyflie
"""
import struct
from cflib.crtp.crtpstack import CRTPPacket
from cflib.crtp.crtpstack import CRTPPort
__author__ = 'Bitcraze AB'
__all__ = ['HighLevelCommander']
class HighLevelCommander():
"""
Used for sending high level setpoints to the Crazyflie
"""
COMMAND_SET_GROUP_MASK = 0
COMMAND_STOP = 3
COMMAND_GO_TO = 4
COMMAND_START_TRAJECTORY = 5
COMMAND_DEFINE_TRAJECTORY = 6
COMMAND_TAKEOFF_2 = 7
COMMAND_LAND_2 = 8
ALL_GROUPS = 0
TRAJECTORY_LOCATION_MEM = 1
TRAJECTORY_TYPE_POLY4D = 0
TRAJECTORY_TYPE_POLY4D_COMPRESSED = 1
def __init__(self, crazyflie=None):
"""
Initialize the object.
"""
self._cf = crazyflie
def set_group_mask(self, group_mask=ALL_GROUPS):
"""
Set the group mask that the Crazyflie belongs to
:param group_mask: Mask for which groups this CF belongs to
"""
self._send_packet(struct.pack('<BB',
self.COMMAND_SET_GROUP_MASK,
group_mask))
def takeoff(self, absolute_height_m, duration_s, group_mask=ALL_GROUPS,
yaw=0.0):
"""
vertical takeoff from current x-y position to given height
:param absolute_height_m: Absolute (m)
:param duration_s: Time it should take until target height is
reached (s)
:param group_mask: Mask for which CFs this should apply to
:param yaw: Yaw (rad). Use current yaw if set to None.
"""
target_yaw = yaw
useCurrentYaw = False
if yaw is None:
target_yaw = 0.0
useCurrentYaw = True
self._send_packet(struct.pack('<BBff?f',
self.COMMAND_TAKEOFF_2,
group_mask,
absolute_height_m,
target_yaw,
useCurrentYaw,
duration_s))
def land(self, absolute_height_m, duration_s, group_mask=ALL_GROUPS,
yaw=0.0):
"""
vertical land from current x-y position to given height
:param absolute_height_m: Absolute (m)
:param duration_s: Time it should take until target height is
reached (s)
:param group_mask: Mask for which CFs this should apply to
:param yaw: Yaw (rad). Use current yaw if set to None.
"""
target_yaw = yaw
useCurrentYaw = False
if yaw is None:
target_yaw = 0.0
useCurrentYaw = True
self._send_packet(struct.pack('<BBff?f',
self.COMMAND_LAND_2,
group_mask,
absolute_height_m,
target_yaw,
useCurrentYaw,
duration_s))
def stop(self, group_mask=ALL_GROUPS):
"""
stops the current trajectory (turns off the motors)
:param group_mask: Mask for which CFs this should apply to
:return:
"""
self._send_packet(struct.pack('<BB',
self.COMMAND_STOP,
group_mask))
def go_to(self, x, y, z, yaw, duration_s, relative=False,
group_mask=ALL_GROUPS):
"""
Go to an absolute or relative position
:param x: X (m)
:param y: Y (m)
:param z: Z (m)
:param yaw: Yaw (radians)
:param duration_s: Time it should take to reach the position (s)
:param relative: True if x, y, z is relative to the current position
:param group_mask: Mask for which CFs this should apply to
"""
self._send_packet(struct.pack('<BBBfffff',
self.COMMAND_GO_TO,
group_mask,
relative,
x, y, z,
yaw,
duration_s))
def start_trajectory(self, trajectory_id, time_scale=1.0, relative=False,
reversed=False, group_mask=ALL_GROUPS):
"""
starts executing a specified trajectory
:param trajectory_id: Id of the trajectory (previously defined by
define_trajectory)
:param time_scale: Time factor; 1.0 = original speed;
>1.0: slower;
<1.0: faster
:param relative: Set to True, if trajectory should be shifted to
current setpoint
:param reversed: Set to True, if trajectory should be executed in
reverse
:param group_mask: Mask for which CFs this should apply to
:return:
"""
self._send_packet(struct.pack('<BBBBBf',
self.COMMAND_START_TRAJECTORY,
group_mask,
relative,
reversed,
trajectory_id,
time_scale))
def define_trajectory(self, trajectory_id, offset, n_pieces, type=TRAJECTORY_TYPE_POLY4D):
"""
Define a trajectory that has previously been uploaded to memory.
:param trajectory_id: The id of the trajectory
:param offset: Offset in uploaded memory
:param n_pieces: Nr of pieces in the trajectory
:param type: The type of trajectory data; TRAJECTORY_TYPE_POLY4D or TRAJECTORY_TYPE_POLY4D_COMPRESSED
:return:
"""
self._send_packet(struct.pack('<BBBBIB',
self.COMMAND_DEFINE_TRAJECTORY,
trajectory_id,
self.TRAJECTORY_LOCATION_MEM,
type,
offset,
n_pieces))
def _send_packet(self, data):
pk = CRTPPacket()
pk.port = CRTPPort.SETPOINT_HL
pk.data = data
self._cf.send_packet(pk)