|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# || ____ _ __ |
| 5 | +# +------+ / __ )(_) /_______________ _____ ___ |
| 6 | +# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ |
| 7 | +# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ |
| 8 | +# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ |
| 9 | +# |
| 10 | +# Copyright (C) 2018 Bitcraze AB |
| 11 | +# |
| 12 | +# Crazyflie Nano Quadcopter Client |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# You should have received a copy of the GNU General Public License |
| 24 | +# along with this program; if not, write to the Free Software |
| 25 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 26 | +# MA 02110-1301, USA. |
| 27 | +""" |
| 28 | +Used for sending high level setpoints to the Crazyflie |
| 29 | +""" |
| 30 | +import struct |
| 31 | + |
| 32 | +from cflib.crtp.crtpstack import CRTPPacket |
| 33 | +from cflib.crtp.crtpstack import CRTPPort |
| 34 | + |
| 35 | +__author__ = 'Bitcraze AB' |
| 36 | +__all__ = ['HighLevelCommander'] |
| 37 | + |
| 38 | + |
| 39 | +class HighLevelCommander(): |
| 40 | + """ |
| 41 | + Used for sending high level setpoints to the Crazyflie |
| 42 | + """ |
| 43 | + |
| 44 | + COMMAND_SET_GROUP_MASK = 0 |
| 45 | + COMMAND_TAKEOFF = 1 |
| 46 | + COMMAND_LAND = 2 |
| 47 | + COMMAND_STOP = 3 |
| 48 | + COMMAND_GO_TO = 4 |
| 49 | + COMMAND_START_TRAJECTORY = 5 |
| 50 | + COMMAND_DEFINE_TRAJECTORY = 6 |
| 51 | + |
| 52 | + ALL_GROUPS = 0 |
| 53 | + |
| 54 | + TRAJECTORY_LOCATION_MEM = 1 |
| 55 | + TRAJECTORY_TYPE_POLY4D = 0 |
| 56 | + |
| 57 | + def __init__(self, crazyflie=None): |
| 58 | + """ |
| 59 | + Initialize the object. |
| 60 | + """ |
| 61 | + self._cf = crazyflie |
| 62 | + |
| 63 | + def set_group_mask(self, group_mask=ALL_GROUPS): |
| 64 | + """ |
| 65 | + Set the group mask that the Crazyflie belongs to |
| 66 | +
|
| 67 | + :param group_mask: mask for which groups this CF belongs to |
| 68 | + """ |
| 69 | + self._send_packet(struct.pack('<BB', |
| 70 | + self.COMMAND_SET_GROUP_MASK, |
| 71 | + group_mask)) |
| 72 | + |
| 73 | + def takeoff(self, absolute_height_m, duration_s, group_mask=ALL_GROUPS): |
| 74 | + """ |
| 75 | + vertical takeoff from current x-y position to given height |
| 76 | +
|
| 77 | + :param absolute_height_m: absolut (m) |
| 78 | + :param duration_s: time it should take until target height is |
| 79 | + reached (s) |
| 80 | + :param group_mask: mask for which CFs this should apply to |
| 81 | + """ |
| 82 | + self._send_packet(struct.pack('<BBff', |
| 83 | + self.COMMAND_TAKEOFF, |
| 84 | + group_mask, |
| 85 | + absolute_height_m, |
| 86 | + duration_s)) |
| 87 | + |
| 88 | + def land(self, absolute_height_m, duration_s, group_mask=ALL_GROUPS): |
| 89 | + """ |
| 90 | + vertical land from current x-y position to given height |
| 91 | +
|
| 92 | + :param absolute_height_m: absolut (m) |
| 93 | + :param duration_s: time it should take until target height is |
| 94 | + reached (s) |
| 95 | + :param group_mask: mask for which CFs this should apply to |
| 96 | + """ |
| 97 | + self._send_packet(struct.pack('<BBff', |
| 98 | + self.COMMAND_LAND, |
| 99 | + group_mask, |
| 100 | + absolute_height_m, |
| 101 | + duration_s)) |
| 102 | + |
| 103 | + def stop(self, group_mask=ALL_GROUPS): |
| 104 | + """ |
| 105 | + stops the current trajectory (turns off the motors) |
| 106 | +
|
| 107 | + :param group_mask: mask for which CFs this should apply to |
| 108 | + :return: |
| 109 | + """ |
| 110 | + self._send_packet(struct.pack('<BB', |
| 111 | + self.COMMAND_STOP, |
| 112 | + group_mask)) |
| 113 | + |
| 114 | + def go_to(self, x, y, z, yaw, duration_s, relative=False, |
| 115 | + group_mask=ALL_GROUPS): |
| 116 | + """ |
| 117 | + Go to an absolute or relative position |
| 118 | +
|
| 119 | + :param x: x (m) |
| 120 | + :param y: y (m) |
| 121 | + :param z: z (m) |
| 122 | + :param yaw: yaw (radians) |
| 123 | + :param duration_s: time it should take to reach the position (s) |
| 124 | + :param relative: True if x, y, z is relative to the current position |
| 125 | + :param group_mask: mask for which CFs this should apply to |
| 126 | + """ |
| 127 | + self._send_packet(struct.pack('<BBBfffff', |
| 128 | + self.COMMAND_GO_TO, |
| 129 | + group_mask, |
| 130 | + relative, |
| 131 | + x, y, z, |
| 132 | + yaw, |
| 133 | + duration_s)) |
| 134 | + |
| 135 | + def start_trajectory(self, trajectory_id, time_scale=1.0, relative=False, |
| 136 | + reversed=False, group_mask=ALL_GROUPS): |
| 137 | + """ |
| 138 | + starts executing a specified trajectory |
| 139 | +
|
| 140 | + :param trajectory_id: id of the trajectory (previously defined by |
| 141 | + define_trajectory) |
| 142 | + :param time_scale: time factor; 1.0 = original speed; |
| 143 | + >1.0: slower; |
| 144 | + <1.0: faster |
| 145 | + :param relative: set to True, if trajectory should be shifted to |
| 146 | + current setpoint |
| 147 | + :param reversed: set to True, if trajectory should be executed in |
| 148 | + reverse |
| 149 | + :param group_mask: mask for which CFs this should apply to |
| 150 | + :return: |
| 151 | + """ |
| 152 | + self._send_packet(struct.pack('<BBBBBf', |
| 153 | + self.COMMAND_START_TRAJECTORY, |
| 154 | + group_mask, |
| 155 | + relative, |
| 156 | + reversed, |
| 157 | + trajectory_id, |
| 158 | + time_scale)) |
| 159 | + |
| 160 | + def define_trajectory(self, trajectory_id, offset, n_pieces): |
| 161 | + """ |
| 162 | + Define a trajectory that has previously been uploaded to memory. |
| 163 | +
|
| 164 | + :param trajectory_id: The id of the trajectory |
| 165 | + :param offset: offset in uploaded memory |
| 166 | + :param n_pieces: Nr of pieces in the trajectory |
| 167 | + :return: |
| 168 | + """ |
| 169 | + self._send_packet(struct.pack('<BBBBIB', |
| 170 | + self.COMMAND_DEFINE_TRAJECTORY, |
| 171 | + trajectory_id, |
| 172 | + self.TRAJECTORY_LOCATION_MEM, |
| 173 | + self.TRAJECTORY_TYPE_POLY4D, |
| 174 | + offset, |
| 175 | + n_pieces)) |
| 176 | + |
| 177 | + def _send_packet(self, data): |
| 178 | + pk = CRTPPacket() |
| 179 | + pk.port = CRTPPort.SETPOINT_HL |
| 180 | + pk.data = data |
| 181 | + self._cf.send_packet(pk) |
0 commit comments