signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def _check_for_initial_packet_cb(self, data):
self.state = State.CONNECTED<EOL>self.link_established.call(self.link_uri)<EOL>self.packet_received.remove_callback(self._check_for_initial_packet_cb)<EOL>
Called when first packet arrives from Crazyflie. This is used to determine if we are connected to something that is answering.
f1079:c1:m9
def open_link(self, link_uri):
self.connection_requested.call(link_uri)<EOL>self.state = State.INITIALIZED<EOL>self.link_uri = link_uri<EOL>try:<EOL><INDENT>self.link = cflib.crtp.get_link_driver(<EOL>link_uri, self._link_quality_cb, self._link_error_cb)<EOL>if not self.link:<EOL><INDENT>message = '<STR_LIT>'.format(link_uri)<EOL>logger.warning(message)<EOL>self.connection_failed.call(link_uri, message)<EOL><DEDENT>else:<EOL><INDENT>self.packet_received.add_callback(<EOL>self._check_for_initial_packet_cb)<EOL>self._start_connection_setup()<EOL><DEDENT><DEDENT>except Exception as ex: <EOL><INDENT>import traceback<EOL>logger.error("<STR_LIT>",<EOL>ex, traceback.format_exc())<EOL>exception_text = "<STR_LIT>" % (<EOL>ex, traceback.format_exc())<EOL>if self.link:<EOL><INDENT>self.link.close()<EOL>self.link = None<EOL><DEDENT>self.connection_failed.call(link_uri, exception_text)<EOL><DEDENT>
Open the communication link to a copter at the given URI and setup the connection (download log/parameter TOC).
f1079:c1:m10
def close_link(self):
logger.info('<STR_LIT>')<EOL>if (self.link is not None):<EOL><INDENT>self.commander.send_setpoint(<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>)<EOL><DEDENT>if (self.link is not None):<EOL><INDENT>self.link.close()<EOL>self.link = None<EOL><DEDENT>self._answer_patterns = {}<EOL>self.disconnected.call(self.link_uri)<EOL>
Close the communication link.
f1079:c1:m11
def add_port_callback(self, port, cb):
self.incoming.add_port_callback(port, cb)<EOL>
Add a callback to cb on port
f1079:c1:m13
def remove_port_callback(self, port, cb):
self.incoming.remove_port_callback(port, cb)<EOL>
Remove the callback cb on port
f1079:c1:m14
def _no_answer_do_retry(self, pk, pattern):
logger.info('<STR_LIT>', pattern)<EOL>self.send_packet(pk, expected_reply=pattern, resend=True)<EOL>
Resend packets that we have not gotten answers to
f1079:c1:m15
def _check_for_answers(self, pk):
longest_match = ()<EOL>if len(self._answer_patterns) > <NUM_LIT:0>:<EOL><INDENT>data = (pk.header,) + tuple(pk.data)<EOL>for p in list(self._answer_patterns.keys()):<EOL><INDENT>logger.debug('<STR_LIT>', p, data)<EOL>if len(p) <= len(data):<EOL><INDENT>if p == data[<NUM_LIT:0>:len(p)]:<EOL><INDENT>match = data[<NUM_LIT:0>:len(p)]<EOL>if len(match) >= len(longest_match):<EOL><INDENT>logger.debug('<STR_LIT>', match)<EOL>longest_match = match<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if len(longest_match) > <NUM_LIT:0>:<EOL><INDENT>self._answer_patterns[longest_match].cancel()<EOL>del self._answer_patterns[longest_match]<EOL><DEDENT>
Callback called for every packet received to check if we are waiting for an answer on this port. If so, then cancel the retry timer.
f1079:c1:m16
def send_packet(self, pk, expected_reply=(), resend=False, timeout=<NUM_LIT>):
self._send_lock.acquire()<EOL>if self.link is not None:<EOL><INDENT>if len(expected_reply) > <NUM_LIT:0> and not resend andself.link.needs_resending:<EOL><INDENT>pattern = (pk.header,) + expected_reply<EOL>logger.debug(<EOL>'<STR_LIT>',<EOL>pattern)<EOL>new_timer = Timer(timeout,<EOL>lambda: self._no_answer_do_retry(pk,<EOL>pattern))<EOL>self._answer_patterns[pattern] = new_timer<EOL>new_timer.start()<EOL><DEDENT>elif resend:<EOL><INDENT>pattern = expected_reply<EOL>if pattern in self._answer_patterns:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>if self._answer_patterns[pattern]:<EOL><INDENT>new_timer = Timer(timeout,<EOL>lambda:<EOL>self._no_answer_do_retry(<EOL>pk, pattern))<EOL>self._answer_patterns[pattern] = new_timer<EOL>new_timer.start()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logger.debug('<STR_LIT>',<EOL>self._answer_patterns)<EOL><DEDENT><DEDENT>self.link.send_packet(pk)<EOL>self.packet_sent.call(pk)<EOL><DEDENT>self._send_lock.release()<EOL>
Send a packet through the link interface. pk -- Packet to send expect_answer -- True if a packet from the Crazyflie is expected to be sent back, otherwise false
f1079:c1:m17
def add_port_callback(self, port, cb):
logger.debug('<STR_LIT>', port, cb)<EOL>self.add_header_callback(cb, port, <NUM_LIT:0>, <NUM_LIT>, <NUM_LIT>)<EOL>
Add a callback for data that comes on a specific port
f1079:c2:m1
def remove_port_callback(self, port, cb):
logger.debug('<STR_LIT>', port, cb)<EOL>for port_callback in self.cb:<EOL><INDENT>if port_callback.port == port and port_callback.callback == cb:<EOL><INDENT>self.cb.remove(port_callback)<EOL><DEDENT><DEDENT>
Remove a callback for data that comes on a specific port
f1079:c2:m2
def add_header_callback(self, cb, port, channel, port_mask=<NUM_LIT>,<EOL>channel_mask=<NUM_LIT>):
self.cb.append(_CallbackContainer(port, port_mask,<EOL>channel, channel_mask, cb))<EOL>
Add a callback for a specific port/header callback with the possibility to add a mask for channel and port for multiple hits for same callback.
f1079:c2:m3
def __init__(self, crazyflie=None):
self._cf = crazyflie<EOL>
Initialize the object.
f1080:c0:m0
def set_group_mask(self, group_mask=ALL_GROUPS):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_SET_GROUP_MASK,<EOL>group_mask))<EOL>
Set the group mask that the Crazyflie belongs to :param group_mask: mask for which groups this CF belongs to
f1080:c0:m1
def takeoff(self, absolute_height_m, duration_s, group_mask=ALL_GROUPS):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_TAKEOFF,<EOL>group_mask,<EOL>absolute_height_m,<EOL>duration_s))<EOL>
vertical takeoff from current x-y position to given height :param absolute_height_m: absolut (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
f1080:c0:m2
def land(self, absolute_height_m, duration_s, group_mask=ALL_GROUPS):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_LAND,<EOL>group_mask,<EOL>absolute_height_m,<EOL>duration_s))<EOL>
vertical land from current x-y position to given height :param absolute_height_m: absolut (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
f1080:c0:m3
def stop(self, group_mask=ALL_GROUPS):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_STOP,<EOL>group_mask))<EOL>
stops the current trajectory (turns off the motors) :param group_mask: mask for which CFs this should apply to :return:
f1080:c0:m4
def go_to(self, x, y, z, yaw, duration_s, relative=False,<EOL>group_mask=ALL_GROUPS):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_GO_TO,<EOL>group_mask,<EOL>relative,<EOL>x, y, z,<EOL>yaw,<EOL>duration_s))<EOL>
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
f1080:c0:m5
def start_trajectory(self, trajectory_id, time_scale=<NUM_LIT:1.0>, relative=False,<EOL>reversed=False, group_mask=ALL_GROUPS):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_START_TRAJECTORY,<EOL>group_mask,<EOL>relative,<EOL>reversed,<EOL>trajectory_id,<EOL>time_scale))<EOL>
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:
f1080:c0:m6
def define_trajectory(self, trajectory_id, offset, n_pieces):
self._send_packet(struct.pack('<STR_LIT>',<EOL>self.COMMAND_DEFINE_TRAJECTORY,<EOL>trajectory_id,<EOL>self.TRAJECTORY_LOCATION_MEM,<EOL>self.TRAJECTORY_TYPE_POLY4D,<EOL>offset,<EOL>n_pieces))<EOL>
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 :return:
f1080:c0:m7
def clear(self):
self.toc = {}<EOL>
Clear the TOC
f1081:c0:m1
def add_element(self, element):
try:<EOL><INDENT>self.toc[element.group][element.name] = element<EOL><DEDENT>except KeyError:<EOL><INDENT>self.toc[element.group] = {}<EOL>self.toc[element.group][element.name] = element<EOL><DEDENT>
Add a new TocElement to the TOC container.
f1081:c0:m2
def get_element_by_complete_name(self, complete_name):
try:<EOL><INDENT>return self.get_element_by_id(self.get_element_id(complete_name))<EOL><DEDENT>except ValueError:<EOL><INDENT>return None<EOL><DEDENT>
Get a TocElement element identified by complete name from the container.
f1081:c0:m3
def get_element_id(self, complete_name):
[group, name] = complete_name.split('<STR_LIT:.>')<EOL>element = self.get_element(group, name)<EOL>if element:<EOL><INDENT>return element.ident<EOL><DEDENT>else:<EOL><INDENT>logger.warning('<STR_LIT>', complete_name)<EOL>return None<EOL><DEDENT>
Get the TocElement element id-number of the element with the supplied name.
f1081:c0:m4
def get_element(self, group, name):
try:<EOL><INDENT>return self.toc[group][name]<EOL><DEDENT>except KeyError:<EOL><INDENT>return None<EOL><DEDENT>
Get a TocElement element identified by name and group from the container.
f1081:c0:m5
def get_element_by_id(self, ident):
for group in list(self.toc.keys()):<EOL><INDENT>for name in list(self.toc[group].keys()):<EOL><INDENT>if self.toc[group][name].ident == ident:<EOL><INDENT>return self.toc[group][name]<EOL><DEDENT><DEDENT><DEDENT>return None<EOL>
Get a TocElement element identified by index number from the container.
f1081:c0:m6
def start(self):
self._useV2 = self.cf.platform.get_protocol_version() >= <NUM_LIT:4><EOL>logger.debug('<STR_LIT>', self.port, self._useV2)<EOL>logger.debug('<STR_LIT>', self.port)<EOL>self.cf.add_port_callback(self.port, self._new_packet_cb)<EOL>self.state = GET_TOC_INFO<EOL>pk = CRTPPacket()<EOL>pk.set_header(self.port, TOC_CHANNEL)<EOL>if self._useV2:<EOL><INDENT>pk.data = (CMD_TOC_INFO_V2,)<EOL>self.cf.send_packet(pk, expected_reply=(CMD_TOC_INFO_V2,))<EOL><DEDENT>else:<EOL><INDENT>pk.data = (CMD_TOC_INFO,)<EOL>self.cf.send_packet(pk, expected_reply=(CMD_TOC_INFO,))<EOL><DEDENT>
Initiate fetching of the TOC.
f1081:c1:m1
def _toc_fetch_finished(self):
self.cf.remove_port_callback(self.port, self._new_packet_cb)<EOL>logger.debug('<STR_LIT>', self.port)<EOL>self.finished_callback()<EOL>
Callback for when the TOC fetching is finished
f1081:c1:m2
def _new_packet_cb(self, packet):
chan = packet.channel<EOL>if (chan != <NUM_LIT:0>):<EOL><INDENT>return<EOL><DEDENT>payload = packet.data[<NUM_LIT:1>:]<EOL>if (self.state == GET_TOC_INFO):<EOL><INDENT>if self._useV2:<EOL><INDENT>[self.nbr_of_items, self._crc] = struct.unpack(<EOL>'<STR_LIT>', payload[:<NUM_LIT:6>])<EOL><DEDENT>else:<EOL><INDENT>[self.nbr_of_items, self._crc] = struct.unpack(<EOL>'<STR_LIT>', payload[:<NUM_LIT:5>])<EOL><DEDENT>logger.debug('<STR_LIT>',<EOL>self.port, self.nbr_of_items, self._crc)<EOL>cache_data = self._toc_cache.fetch(self._crc)<EOL>if (cache_data):<EOL><INDENT>self.toc.toc = cache_data<EOL>logger.info('<STR_LIT>' % self.port)<EOL>self._toc_fetch_finished()<EOL><DEDENT>else:<EOL><INDENT>self.state = GET_TOC_ELEMENT<EOL>self.requested_index = <NUM_LIT:0><EOL>self._request_toc_element(self.requested_index)<EOL><DEDENT><DEDENT>elif (self.state == GET_TOC_ELEMENT):<EOL><INDENT>if self._useV2:<EOL><INDENT>ident = struct.unpack('<STR_LIT>', payload[:<NUM_LIT:2>])[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>ident = payload[<NUM_LIT:0>]<EOL><DEDENT>if ident != self.requested_index:<EOL><INDENT>return<EOL><DEDENT>if self._useV2:<EOL><INDENT>self.toc.add_element(self.element_class(ident, payload[<NUM_LIT:2>:]))<EOL><DEDENT>else:<EOL><INDENT>self.toc.add_element(self.element_class(ident, payload[<NUM_LIT:1>:]))<EOL><DEDENT>logger.debug('<STR_LIT>', ident)<EOL>if (self.requested_index < (self.nbr_of_items - <NUM_LIT:1>)):<EOL><INDENT>logger.debug('<STR_LIT>',<EOL>self.port, self.requested_index + <NUM_LIT:1>)<EOL>self.requested_index = self.requested_index + <NUM_LIT:1><EOL>self._request_toc_element(self.requested_index)<EOL><DEDENT>else: <EOL><INDENT>self._toc_cache.insert(self._crc, self.toc.toc)<EOL>self._toc_fetch_finished()<EOL><DEDENT><DEDENT>
Handle a newly arrived packet
f1081:c1:m3
def _request_toc_element(self, index):
logger.debug('<STR_LIT>', index, self.port)<EOL>pk = CRTPPacket()<EOL>if self._useV2:<EOL><INDENT>pk.set_header(self.port, TOC_CHANNEL)<EOL>pk.data = (CMD_TOC_ITEM_V2, index & <NUM_LIT>, (index >> <NUM_LIT:8>) & <NUM_LIT>)<EOL>self.cf.send_packet(pk, expected_reply=(<EOL>CMD_TOC_ITEM_V2, index & <NUM_LIT>, (index >> <NUM_LIT:8>) & <NUM_LIT>))<EOL><DEDENT>else:<EOL><INDENT>pk.set_header(self.port, TOC_CHANNEL)<EOL>pk.data = (CMD_TOC_ELEMENT, index)<EOL>self.cf.send_packet(pk, expected_reply=(CMD_TOC_ELEMENT, index))<EOL><DEDENT>
Request information about a specific item in the TOC
f1081:c1:m4
def __init__(self, link_uri, cf=None):
if cf:<EOL><INDENT>self.cf = cf<EOL><DEDENT>else:<EOL><INDENT>self.cf = Crazyflie()<EOL><DEDENT>self._link_uri = link_uri<EOL>self._connect_event = Event()<EOL>self._is_link_open = False<EOL>self._error_message = None<EOL>self.cf.connected.add_callback(self._connected)<EOL>self.cf.connection_failed.add_callback(self._connection_failed)<EOL>self.cf.disconnected.add_callback(self._disconnected)<EOL>
Create a synchronous Crazyflie instance with the specified link_uri
f1082:c0:m0
def _connected(self, link_uri):
print('<STR_LIT>' % link_uri)<EOL>self._is_link_open = True<EOL>self._connect_event.set()<EOL>
This callback is called form the Crazyflie API when a Crazyflie has been connected and the TOCs have been downloaded.
f1082:c0:m6
def _connection_failed(self, link_uri, msg):
print('<STR_LIT>' % (link_uri, msg))<EOL>self._is_link_open = False<EOL>self._error_message = msg<EOL>self._connect_event.set()<EOL>
Callback when connection initial connection fails (i.e no Crazyflie at the specified address)
f1082:c0:m7
def __init__(self, crazyflie=None):
self._cf = crazyflie<EOL>self.receivedLocationPacket = Caller()<EOL>self._cf.add_port_callback(CRTPPort.LOCALIZATION, self._incoming)<EOL>
Initialize the Extpos object.
f1083:c0:m0
def _incoming(self, packet):
if len(packet.data) < <NUM_LIT:1>:<EOL><INDENT>logger.warning('<STR_LIT>' +<EOL>'<STR_LIT>'.format(len(packet.data)))<EOL>return<EOL><DEDENT>pk_type = struct.unpack('<STR_LIT>', packet.data[:<NUM_LIT:1>])[<NUM_LIT:0>]<EOL>data = packet.data[<NUM_LIT:1>:]<EOL>decoded_data = None<EOL>if pk_type == self.RANGE_STREAM_REPORT:<EOL><INDENT>if len(data) % <NUM_LIT:5> != <NUM_LIT:0>:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>return<EOL><DEDENT>decoded_data = {}<EOL>raw_data = data<EOL>for i in range(int(len(data) / <NUM_LIT:5>)):<EOL><INDENT>anchor_id, distance = struct.unpack('<STR_LIT>', raw_data[:<NUM_LIT:5>])<EOL>decoded_data[anchor_id] = distance<EOL>raw_data = raw_data[<NUM_LIT:5>:]<EOL><DEDENT><DEDENT>pk = LocalizationPacket(pk_type, data, decoded_data)<EOL>self.receivedLocationPacket.call(pk)<EOL>
Callback for data received from the copter.
f1083:c0:m1
def send_extpos(self, pos):
pk = CRTPPacket()<EOL>pk.port = CRTPPort.LOCALIZATION<EOL>pk.channel = self.POSITION_CH<EOL>pk.data = struct.pack('<STR_LIT>', pos[<NUM_LIT:0>], pos[<NUM_LIT:1>], pos[<NUM_LIT:2>])<EOL>self._cf.send_packet(pk)<EOL>
Send the current Crazyflie X, Y, Z position. This is going to be forwarded to the Crazyflie's position estimator.
f1083:c0:m2
def send_short_lpp_packet(self, dest_id, data):
pk = CRTPPacket()<EOL>pk.port = CRTPPort.LOCALIZATION<EOL>pk.channel = self.GENERIC_CH<EOL>pk.data = struct.pack('<STR_LIT>', self.LPS_SHORT_LPP_PACKET, dest_id) + data<EOL>self._cf.send_packet(pk)<EOL>
Send ultra-wide-band LPP packet to dest_id
f1083:c0:m3
def __init__(self, crazyflie=None):
self._cf = crazyflie<EOL>
Initialize the Extpos object.
f1084:c0:m0
def send_extpos(self, x, y, z):
self._cf.loc.send_extpos([x, y, z])<EOL>
Send the current Crazyflie X, Y, Z position. This is going to be forwarded to the Crazyflie's position estimator.
f1084:c0:m1
def __init__(self):
self.callbacks = []<EOL>
Create the object
f1086:c0:m0
def add_callback(self, cb):
if ((cb in self.callbacks) is False):<EOL><INDENT>self.callbacks.append(cb)<EOL><DEDENT>
Register cb as a new callback. Will not register duplicates.
f1086:c0:m1
def remove_callback(self, cb):
self.callbacks.remove(cb)<EOL>
Un-register cb from the callbacks
f1086:c0:m2
def call(self, *args):
for cb in self.callbacks:<EOL><INDENT>cb(*args)<EOL><DEDENT>
Call the callbacks registered with the arguments args
f1086:c0:m3
def __init__(self, crazyflie, default_height=<NUM_LIT>):
if isinstance(crazyflie, SyncCrazyflie):<EOL><INDENT>self._cf = crazyflie.cf<EOL><DEDENT>else:<EOL><INDENT>self._cf = crazyflie<EOL><DEDENT>self.default_height = default_height<EOL>self._is_flying = False<EOL>self._thread = None<EOL>
Construct an instance of a MotionCommander :param crazyflie: a Crazyflie or SyncCrazyflie instance :param default_height: the default height to fly at
f1089:c0:m0
def take_off(self, height=None, velocity=VELOCITY):
if self._is_flying:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if not self._cf.is_connected():<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>self._is_flying = True<EOL>self._reset_position_estimator()<EOL>self._thread = _SetPointThread(self._cf)<EOL>self._thread.start()<EOL>if height is None:<EOL><INDENT>height = self.default_height<EOL><DEDENT>self.up(height, velocity)<EOL>
Takes off, that is starts the motors, goes straigt up and hovers. Do not call this function if you use the with keyword. Take off is done automatically when the context is created. :param height: the height (meters) to hover at. None uses the default height set when constructed. :param velocity: the velocity (meters/second) when taking off :return:
f1089:c0:m1
def land(self, velocity=VELOCITY):
if self._is_flying:<EOL><INDENT>self.down(self._thread.get_height(), velocity)<EOL>self._thread.stop()<EOL>self._thread = None<EOL>self._cf.commander.send_stop_setpoint()<EOL>self._is_flying = False<EOL><DEDENT>
Go straight down and turn off the motors. Do not call this function if you use the with keyword. Landing is done automatically when the context goes out of scope. :param velocity: The velocity (meters/second) when going down :return:
f1089:c0:m2
def left(self, distance_m, velocity=VELOCITY):
self.move_distance(<NUM_LIT:0.0>, distance_m, <NUM_LIT:0.0>, velocity)<EOL>
Go left :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m5
def right(self, distance_m, velocity=VELOCITY):
self.move_distance(<NUM_LIT:0.0>, -distance_m, <NUM_LIT:0.0>, velocity)<EOL>
Go right :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m6
def forward(self, distance_m, velocity=VELOCITY):
self.move_distance(distance_m, <NUM_LIT:0.0>, <NUM_LIT:0.0>, velocity)<EOL>
Go forward :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m7
def back(self, distance_m, velocity=VELOCITY):
self.move_distance(-distance_m, <NUM_LIT:0.0>, <NUM_LIT:0.0>, velocity)<EOL>
Go backwards :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m8
def up(self, distance_m, velocity=VELOCITY):
self.move_distance(<NUM_LIT:0.0>, <NUM_LIT:0.0>, distance_m, velocity)<EOL>
Go up :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m9
def down(self, distance_m, velocity=VELOCITY):
self.move_distance(<NUM_LIT:0.0>, <NUM_LIT:0.0>, -distance_m, velocity)<EOL>
Go down :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m10
def turn_left(self, angle_degrees, rate=RATE):
flight_time = angle_degrees / rate<EOL>self.start_turn_left(rate)<EOL>time.sleep(flight_time)<EOL>self.stop()<EOL>
Turn to the left, staying on the spot :param angle_degrees: How far to turn (degrees) :param rate: The trurning speed (degrees/second) :return:
f1089:c0:m11
def turn_right(self, angle_degrees, rate=RATE):
flight_time = angle_degrees / rate<EOL>self.start_turn_right(rate)<EOL>time.sleep(flight_time)<EOL>self.stop()<EOL>
Turn to the right, staying on the spot :param angle_degrees: How far to turn (degrees) :param rate: The trurning speed (degrees/second) :return:
f1089:c0:m12
def circle_left(self, radius_m, velocity=VELOCITY, angle_degrees=<NUM_LIT>):
distance = <NUM_LIT:2> * radius_m * math.pi * angle_degrees / <NUM_LIT><EOL>flight_time = distance / velocity<EOL>self.start_circle_left(radius_m, velocity)<EOL>time.sleep(flight_time)<EOL>self.stop()<EOL>
Go in circle, counter clock wise :param radius_m: The radius of the circle (meters) :param velocity: The velocity along the circle (meters/second) :param angle_degrees: How far to go in the circle (degrees) :return:
f1089:c0:m13
def circle_right(self, radius_m, velocity=VELOCITY, angle_degrees=<NUM_LIT>):
distance = <NUM_LIT:2> * radius_m * math.pi * angle_degrees / <NUM_LIT><EOL>flight_time = distance / velocity<EOL>self.start_circle_right(radius_m, velocity)<EOL>time.sleep(flight_time)<EOL>self.stop()<EOL>
Go in circle, clock wise :param radius_m: The radius of the circle (meters) :param velocity: The velocity along the circle (meters/second) :param angle_degrees: How far to go in the circle (degrees) :return:
f1089:c0:m14
def move_distance(self, distance_x_m, distance_y_m, distance_z_m,<EOL>velocity=VELOCITY):
distance = math.sqrt(distance_x_m * distance_x_m +<EOL>distance_y_m * distance_y_m +<EOL>distance_z_m * distance_z_m)<EOL>flight_time = distance / velocity<EOL>velocity_x = velocity * distance_x_m / distance<EOL>velocity_y = velocity * distance_y_m / distance<EOL>velocity_z = velocity * distance_z_m / distance<EOL>self.start_linear_motion(velocity_x, velocity_y, velocity_z)<EOL>time.sleep(flight_time)<EOL>self.stop()<EOL>
Move in a straight line. positive X is forward positive Y is left positive Z is up :param distance_x_m: The distance to travel along the X-axis (meters) :param distance_y_m: The distance to travel along the Y-axis (meters) :param distance_z_m: The distance to travel along the Z-axis (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1089:c0:m15
def start_left(self, velocity=VELOCITY):
self.start_linear_motion(<NUM_LIT:0.0>, velocity, <NUM_LIT:0.0>)<EOL>
Start moving left. This function returns immediately. :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m16
def start_right(self, velocity=VELOCITY):
self.start_linear_motion(<NUM_LIT:0.0>, -velocity, <NUM_LIT:0.0>)<EOL>
Start moving right. This function returns immediately. :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m17
def start_forward(self, velocity=VELOCITY):
self.start_linear_motion(velocity, <NUM_LIT:0.0>, <NUM_LIT:0.0>)<EOL>
Start moving forward. This function returns immediately. :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m18
def start_back(self, velocity=VELOCITY):
self.start_linear_motion(-velocity, <NUM_LIT:0.0>, <NUM_LIT:0.0>)<EOL>
Start moving backwards. This function returns immediately. :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m19
def start_up(self, velocity=VELOCITY):
self.start_linear_motion(<NUM_LIT:0.0>, <NUM_LIT:0.0>, velocity)<EOL>
Start moving up. This function returns immediately. :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m20
def start_down(self, velocity=VELOCITY):
self.start_linear_motion(<NUM_LIT:0.0>, <NUM_LIT:0.0>, -velocity)<EOL>
Start moving down. This function returns immediately. :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m21
def stop(self):
self._set_vel_setpoint(<NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:0.0>)<EOL>
Stop any motion and hover. :return:
f1089:c0:m22
def start_turn_left(self, rate=RATE):
self._set_vel_setpoint(<NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:0.0>, -rate)<EOL>
Start turning left. This function returns immediately. :param rate: The angular rate (degrees/second) :return:
f1089:c0:m23
def start_turn_right(self, rate=RATE):
self._set_vel_setpoint(<NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:0.0>, rate)<EOL>
Start turning right. This function returns immediately. :param rate: The angular rate (degrees/second) :return:
f1089:c0:m24
def start_circle_left(self, radius_m, velocity=VELOCITY):
circumference = <NUM_LIT:2> * radius_m * math.pi<EOL>rate = <NUM_LIT> * velocity / circumference<EOL>self._set_vel_setpoint(velocity, <NUM_LIT:0.0>, <NUM_LIT:0.0>, -rate)<EOL>
Start a circular motion to the left. This function returns immediately. :param radius_m: The radius of the circle (meters) :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m25
def start_circle_right(self, radius_m, velocity=VELOCITY):
circumference = <NUM_LIT:2> * radius_m * math.pi<EOL>rate = <NUM_LIT> * velocity / circumference<EOL>self._set_vel_setpoint(velocity, <NUM_LIT:0.0>, <NUM_LIT:0.0>, rate)<EOL>
Start a circular motion to the right. This function returns immediately :param radius_m: The radius of the circle (meters) :param velocity: The velocity of the motion (meters/second) :return:
f1089:c0:m26
def start_linear_motion(self, velocity_x_m, velocity_y_m, velocity_z_m):
self._set_vel_setpoint(<EOL>velocity_x_m, velocity_y_m, velocity_z_m, <NUM_LIT:0.0>)<EOL>
Start a linear motion. This function returns immediately. positive X is forward positive Y is left positive Z is up :param velocity_x_m: The velocity along the X-axis (meters/second) :param velocity_y_m: The velocity along the Y-axis (meters/second) :param velocity_z_m: The velocity along the Z-axis (meters/second) :return:
f1089:c0:m27
def stop(self):
self._queue.put(self.TERMINATE_EVENT)<EOL>self.join()<EOL>
Stop the thread and wait for it to terminate :return:
f1089:c1:m1
def set_vel_setpoint(self, velocity_x, velocity_y, velocity_z, rate_yaw):
self._queue.put((velocity_x, velocity_y, velocity_z, rate_yaw))<EOL>
Set the velocity setpoint to use for the future motion
f1089:c1:m2
def get_height(self):
return self._hover_setpoint[self.ABS_Z_INDEX]<EOL>
Get the current height of the Crazyflie. :return: The height (meters)
f1089:c1:m3
def __init__(self, crazyflie,<EOL>x=<NUM_LIT:0.0>, y=<NUM_LIT:0.0>, z=<NUM_LIT:0.0>,<EOL>default_velocity=<NUM_LIT:0.5>,<EOL>default_height=<NUM_LIT:0.5>,<EOL>controller=CONTROLLER_PID):
if isinstance(crazyflie, SyncCrazyflie):<EOL><INDENT>self._cf = crazyflie.cf<EOL><DEDENT>else:<EOL><INDENT>self._cf = crazyflie<EOL><DEDENT>self._default_velocity = default_velocity<EOL>self._default_height = default_height<EOL>self._controller = controller<EOL>self._x = x<EOL>self._y = y<EOL>self._z = z<EOL>self._is_flying = False<EOL>
Construct an instance of a PositionHlCommander :param crazyflie: a Crazyflie or SyncCrazyflie instance :param x: Initial position, x :param y: Initial position, y :param z: Initial position, z :param default_velocity: the default velocity to use :param default_height: the default height to fly at :param controller: Which underlying controller to use
f1091:c0:m0
def take_off(self, height=DEFAULT, velocity=DEFAULT):
if self._is_flying:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if not self._cf.is_connected():<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>self._is_flying = True<EOL>self._reset_position_estimator()<EOL>self._activate_controller()<EOL>self._activate_high_level_commander()<EOL>self._hl_commander = self._cf.high_level_commander<EOL>height = self._height(height)<EOL>duration_s = height / self._velocity(velocity)<EOL>self._hl_commander.takeoff(height, duration_s)<EOL>time.sleep(duration_s)<EOL>self._z = height<EOL>
Takes off, that is starts the motors, goes straight up and hovers. Do not call this function if you use the with keyword. Take off is done automatically when the context is created. :param height: the height (meters) to hover at. None uses the default height set when constructed. :param velocity: the velocity (meters/second) when taking off :return:
f1091:c0:m1
def land(self, velocity=DEFAULT):
if self._is_flying:<EOL><INDENT>duration_s = self._z / self._velocity(velocity)<EOL>self._hl_commander.land(<NUM_LIT:0>, duration_s)<EOL>time.sleep(duration_s)<EOL>self._z = <NUM_LIT:0.0><EOL>self._hl_commander.stop()<EOL>self._is_flying = False<EOL><DEDENT>
Go straight down and turn off the motors. Do not call this function if you use the with keyword. Landing is done automatically when the context goes out of scope. :param velocity: The velocity (meters/second) when going down :return:
f1091:c0:m2
def left(self, distance_m, velocity=DEFAULT):
self.move_distance(<NUM_LIT:0.0>, distance_m, <NUM_LIT:0.0>, velocity)<EOL>
Go left :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m5
def right(self, distance_m, velocity=DEFAULT):
self.move_distance(<NUM_LIT:0.0>, -distance_m, <NUM_LIT:0.0>, velocity)<EOL>
Go right :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m6
def forward(self, distance_m, velocity=DEFAULT):
self.move_distance(distance_m, <NUM_LIT:0.0>, <NUM_LIT:0.0>, velocity)<EOL>
Go forward :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m7
def back(self, distance_m, velocity=DEFAULT):
self.move_distance(-distance_m, <NUM_LIT:0.0>, <NUM_LIT:0.0>, velocity)<EOL>
Go backwards :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m8
def up(self, distance_m, velocity=DEFAULT):
self.move_distance(<NUM_LIT:0.0>, <NUM_LIT:0.0>, distance_m, velocity)<EOL>
Go up :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m9
def down(self, distance_m, velocity=DEFAULT):
self.move_distance(<NUM_LIT:0.0>, <NUM_LIT:0.0>, -distance_m, velocity)<EOL>
Go down :param distance_m: the distance to travel (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m10
def move_distance(self, distance_x_m, distance_y_m, distance_z_m,<EOL>velocity=DEFAULT):
x = self._x + distance_x_m<EOL>y = self._y + distance_y_m<EOL>z = self._z + distance_z_m<EOL>self.go_to(x, y, z, velocity)<EOL>
Move in a straight line. positive X is forward positive Y is left positive Z is up :param distance_x_m: The distance to travel along the X-axis (meters) :param distance_y_m: The distance to travel along the Y-axis (meters) :param distance_z_m: The distance to travel along the Z-axis (meters) :param velocity: the velocity of the motion (meters/second) :return:
f1091:c0:m11
def go_to(self, x, y, z=DEFAULT, velocity=DEFAULT):
z = self._height(z)<EOL>dx = x - self._x<EOL>dy = y - self._y<EOL>dz = z - self._z<EOL>distance = math.sqrt(dx * dx + dy * dy + dz * dz)<EOL>duration_s = distance / self._velocity(velocity)<EOL>self._hl_commander.go_to(x, y, z, <NUM_LIT:0>, duration_s)<EOL>time.sleep(duration_s)<EOL>self._x = x<EOL>self._y = y<EOL>self._z = z<EOL>
Go to a position :param x: X coordinate :param y: Y coordinate :param z: Z coordinate :param velocity: the velocity (meters/second) :return:
f1091:c0:m12
def set_default_velocity(self, velocity):
self._default_velocity = velocity<EOL>
Set the default velocity to use in commands when no velocity is defined :param velocity: The default velocity (meters/s) :return:
f1091:c0:m13
def set_default_height(self, height):
self._default_height = height<EOL>
Set the default height to use in commands when no height is defined :param height: The default height (meters) :return:
f1091:c0:m14
def get_position(self):
return self._x, self._y, self._z<EOL>
Get the current position :return: (x, y, z)
f1091:c0:m16
def __init__(self, clink=None):
self.clink = clink<EOL>self.in_loader = False<EOL>self.page_size = <NUM_LIT:0><EOL>self.buffer_pages = <NUM_LIT:0><EOL>self.flash_pages = <NUM_LIT:0><EOL>self.start_page = <NUM_LIT:0><EOL>self.cpuid = '<STR_LIT>'<EOL>self.error_code = <NUM_LIT:0><EOL>self.protocol_version = <NUM_LIT:0><EOL>self.progress_cb = None<EOL>self.error_cb = None<EOL>self.in_bootloader_cb = None<EOL>self.dev_info_cb = None<EOL>self._boot_plat = None<EOL>self._cload = Cloader(clink,<EOL>info_cb=self.dev_info_cb,<EOL>in_boot_cb=self.in_bootloader_cb)<EOL>
Init the communication class by starting to communicate with the link given. clink is the link address used after resetting to the bootloader. The device is actually considered in firmware mode.
f1093:c0:m0
def read_cf1_config(self):
target = self._cload.targets[<NUM_LIT>]<EOL>config_page = target.flash_pages - <NUM_LIT:1><EOL>return self._cload.read_flash(addr=<NUM_LIT>, page=config_page)<EOL>
Read a flash page from the specified target
f1093:c0:m3
def _get_platform_id(self):
identifier = '<STR_LIT>'<EOL>if (BootVersion.is_cf2(self.protocol_version)):<EOL><INDENT>identifier = '<STR_LIT>'<EOL><DEDENT>return identifier<EOL>
Get platform identifier used in the zip manifest for curr copter
f1093:c0:m12
def __init__(self, link, info_cb=None, in_boot_cb=None):
self.link = None<EOL>self.uri = link<EOL>self.in_loader = False<EOL>self.page_size = <NUM_LIT:0><EOL>self.buffer_pages = <NUM_LIT:0><EOL>self.flash_pages = <NUM_LIT:0><EOL>self.start_page = <NUM_LIT:0><EOL>self.cpuid = '<STR_LIT>'<EOL>self.error_code = <NUM_LIT:0><EOL>self.protocol_version = <NUM_LIT><EOL>self._info_cb = info_cb<EOL>self._in_boot_cb = in_boot_cb<EOL>self.targets = {}<EOL>self.mapping = None<EOL>self._available_boot_uri = ('<STR_LIT>', '<STR_LIT>')<EOL>
Init the communication class by starting to communicate with the link given. clink is the link address used after resetting to the bootloader. The device is actually considered in firmware mode.
f1094:c0:m0
def close(self):
if self.link:<EOL><INDENT>self.link.close()<EOL><DEDENT>
Close the link
f1094:c0:m1
def reset_to_bootloader1(self, cpu_id):
<EOL>pk = CRTPPacket()<EOL>pk.port = CRTPPort.LINKCTRL<EOL>pk.data = (<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>) + cpu_id<EOL>self.link.send_packet(pk)<EOL>pk = None<EOL>while True:<EOL><INDENT>pk = self.link.receive_packet(<NUM_LIT:2>)<EOL>if not pk:<EOL><INDENT>return False<EOL><DEDENT>if pk.port == CRTPPort.LINKCTRL:<EOL><INDENT>break<EOL><DEDENT><DEDENT>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = (<NUM_LIT>, <NUM_LIT>) + cpu_id<EOL>self.link.send_packet(pk)<EOL>pk = None<EOL>while True:<EOL><INDENT>pk = self.link.receive_packet(<NUM_LIT:2>)<EOL>if not pk:<EOL><INDENT>return False<EOL><DEDENT>if pk.port == <NUM_LIT> and tuple(pk.data) == (<NUM_LIT>, <NUM_LIT>) + cpu_id:<EOL><INDENT>pk.data = (<NUM_LIT>, <NUM_LIT>) + cpu_id<EOL>self.link.send_packet(pk)<EOL>break<EOL><DEDENT><DEDENT>time.sleep(<NUM_LIT:0.1>)<EOL>self.link.close()<EOL>self.link = cflib.crtp.get_link_driver(self.clink_address)<EOL>return self._update_info()<EOL>
Reset to the bootloader The parameter cpuid shall correspond to the device to reset. Return true if the reset has been done and the contact with the bootloader is established.
f1094:c0:m4
def reset_to_firmware(self, target_id):
<EOL>fake_cpu_id = (<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:4>, <NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:7>, <NUM_LIT:8>, <NUM_LIT:9>, <NUM_LIT:10>, <NUM_LIT:11>, <NUM_LIT:12>)<EOL>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = (target_id, <NUM_LIT>) + fake_cpu_id<EOL>self.link.send_packet(pk)<EOL>pk = None<EOL>while True:<EOL><INDENT>pk = self.link.receive_packet(<NUM_LIT:2>)<EOL>if not pk:<EOL><INDENT>return False<EOL><DEDENT>if (pk.header == <NUM_LIT> and struct.unpack(<EOL>'<STR_LIT:B>' * len(pk.data), pk.data)[:<NUM_LIT:2>] == (target_id, <NUM_LIT>)):<EOL><INDENT>if target_id == <NUM_LIT>:<EOL><INDENT>pk.data = (target_id, <NUM_LIT>, <NUM_LIT>)<EOL><DEDENT>else:<EOL><INDENT>pk.data = (target_id, <NUM_LIT>) + fake_cpu_id<EOL><DEDENT>self.link.send_packet(pk)<EOL>break<EOL><DEDENT><DEDENT>time.sleep(<NUM_LIT:0.1>)<EOL>
Reset to firmware The parameter cpuid shall correspond to the device to reset. Return true if the reset has been done
f1094:c0:m5
def check_link_and_get_info(self, target_id=<NUM_LIT>):
for _ in range(<NUM_LIT:0>, <NUM_LIT:5>):<EOL><INDENT>if self._update_info(target_id):<EOL><INDENT>if self._in_boot_cb:<EOL><INDENT>self._in_boot_cb.call(True, self.targets[<EOL>target_id].protocol_version)<EOL><DEDENT>if self._info_cb:<EOL><INDENT>self._info_cb.call(self.targets[target_id])<EOL><DEDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>
Try to get a connection with the bootloader by requesting info 5 times. This let roughly 10 seconds to boot the copter ...
f1094:c0:m7
def _update_info(self, target_id):
<EOL>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = (target_id, <NUM_LIT>)<EOL>self.link.send_packet(pk)<EOL>pk = self.link.receive_packet(<NUM_LIT:2>)<EOL>if (pk and pk.header == <NUM_LIT> and struct.unpack('<STR_LIT>', pk.data[<NUM_LIT:0>:<NUM_LIT:2>]) ==<EOL>(target_id, <NUM_LIT>)):<EOL><INDENT>tab = struct.unpack('<STR_LIT>', pk.data[<NUM_LIT:0>:<NUM_LIT:10>])<EOL>cpuid = struct.unpack('<STR_LIT:B>' * <NUM_LIT:12>, pk.data[<NUM_LIT:10>:<NUM_LIT>])<EOL>if target_id not in self.targets:<EOL><INDENT>self.targets[target_id] = Target(target_id)<EOL><DEDENT>self.targets[target_id].addr = target_id<EOL>if len(pk.data) > <NUM_LIT>:<EOL><INDENT>self.targets[target_id].protocol_version = pk.datat[<NUM_LIT>]<EOL>self.protocol_version = pk.datat[<NUM_LIT>]<EOL><DEDENT>self.targets[target_id].page_size = tab[<NUM_LIT:2>]<EOL>self.targets[target_id].buffer_pages = tab[<NUM_LIT:3>]<EOL>self.targets[target_id].flash_pages = tab[<NUM_LIT:4>]<EOL>self.targets[target_id].start_page = tab[<NUM_LIT:5>]<EOL>self.targets[target_id].cpuid = '<STR_LIT>' % cpuid[<NUM_LIT:0>]<EOL>for i in cpuid[<NUM_LIT:1>:]:<EOL><INDENT>self.targets[target_id].cpuid += '<STR_LIT>' % i<EOL><DEDENT>if (self.protocol_version == <NUM_LIT> and<EOL>target_id == TargetTypes.STM32):<EOL><INDENT>self._update_mapping(target_id)<EOL><DEDENT>return True<EOL><DEDENT>return False<EOL>
Call the command getInfo and fill up the information received in the fields of the object
f1094:c0:m9
def upload_buffer(self, target_id, page, address, buff):
<EOL>count = <NUM_LIT:0><EOL>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = struct.pack('<STR_LIT>', target_id, <NUM_LIT>, page, address)<EOL>for i in range(<NUM_LIT:0>, len(buff)):<EOL><INDENT>pk.data.append(buff[i])<EOL>count += <NUM_LIT:1><EOL>if count > <NUM_LIT>:<EOL><INDENT>self.link.send_packet(pk)<EOL>count = <NUM_LIT:0><EOL>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = struct.pack('<STR_LIT>', target_id, <NUM_LIT>, page,<EOL>i + address + <NUM_LIT:1>)<EOL><DEDENT><DEDENT>self.link.send_packet(pk)<EOL>
Upload data into a buffer on the Crazyflie
f1094:c0:m11
def read_flash(self, addr=<NUM_LIT>, page=<NUM_LIT>):
buff = bytearray()<EOL>page_size = self.targets[addr].page_size<EOL>for i in range(<NUM_LIT:0>, int(math.ceil(page_size / <NUM_LIT>))):<EOL><INDENT>pk = None<EOL>retry_counter = <NUM_LIT:5><EOL>while ((not pk or pk.header != <NUM_LIT> or<EOL>struct.unpack('<STR_LIT>', pk.data[<NUM_LIT:0>:<NUM_LIT:2>]) != (addr, <NUM_LIT>)) and<EOL>retry_counter >= <NUM_LIT:0>):<EOL><INDENT>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = struct.pack('<STR_LIT>', addr, <NUM_LIT>, page, (i * <NUM_LIT>))<EOL>self.link.send_packet(pk)<EOL>pk = self.link.receive_packet(<NUM_LIT:1>)<EOL>retry_counter -= <NUM_LIT:1><EOL><DEDENT>if (retry_counter < <NUM_LIT:0>):<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>buff += pk.data[<NUM_LIT:6>:]<EOL><DEDENT><DEDENT>return buff[<NUM_LIT:0>:page_size]<EOL>
Read back a flash page from the Crazyflie and return it
f1094:c0:m12
def write_flash(self, addr, page_buffer, target_page, page_count):
<EOL>pk = None<EOL>pk = self.link.receive_packet(<NUM_LIT:0>)<EOL>while pk is not None:<EOL><INDENT>pk = self.link.receive_packet(<NUM_LIT:0>)<EOL><DEDENT>retry_counter = <NUM_LIT:5><EOL>while ((not pk or pk.header != <NUM_LIT> or<EOL>struct.unpack('<STR_LIT>', pk.data[<NUM_LIT:0>:<NUM_LIT:2>]) != (addr, <NUM_LIT>)) and<EOL>retry_counter >= <NUM_LIT:0>):<EOL><INDENT>pk = CRTPPacket()<EOL>pk.set_header(<NUM_LIT>, <NUM_LIT>)<EOL>pk.data = struct.pack('<STR_LIT>', addr, <NUM_LIT>, page_buffer,<EOL>target_page, page_count)<EOL>self.link.send_packet(pk)<EOL>pk = self.link.receive_packet(<NUM_LIT:1>)<EOL>retry_counter -= <NUM_LIT:1><EOL><DEDENT>if retry_counter < <NUM_LIT:0>:<EOL><INDENT>self.error_code = -<NUM_LIT:1><EOL>return False<EOL><DEDENT>self.error_code = pk.data[<NUM_LIT:3>]<EOL>return pk.data[<NUM_LIT:2>] == <NUM_LIT:1><EOL>
Initiate flashing of data in the buffer to flash.
f1094:c0:m13
def decode_cpu_id(self, cpuid):
ret = ()<EOL>for i in cpuid.split('<STR_LIT::>'):<EOL><INDENT>ret += (eval('<STR_LIT>' + i),)<EOL><DEDENT>return ret<EOL>
Decode the CPU id into a string
f1094:c0:m14
def _find_devices():
ret = []<EOL>logger.info('<STR_LIT>')<EOL>if pyusb1:<EOL><INDENT>for d in usb.core.find(idVendor=USB_VID, idProduct=USB_PID, find_all=<NUM_LIT:1>,<EOL>backend=pyusb_backend):<EOL><INDENT>ret.append(d)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>busses = usb.busses()<EOL>for bus in busses:<EOL><INDENT>for device in bus.devices:<EOL><INDENT>if device.idVendor == USB_VID:<EOL><INDENT>if device.idProduct == USB_PID:<EOL><INDENT>ret += [device, ]<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return ret<EOL>
Returns a list of CrazyRadio devices currently connected to the computer
f1095:m0
def __init__(self, device=None, devid=<NUM_LIT:0>):
self.dev = None<EOL>self.handle = None<EOL>self._last_write = <NUM_LIT:0><EOL>self._last_read = <NUM_LIT:0><EOL>if device is None:<EOL><INDENT>devices = _find_devices()<EOL>try:<EOL><INDENT>self.dev = devices[devid]<EOL><DEDENT>except Exception:<EOL><INDENT>self.dev = None<EOL><DEDENT><DEDENT>if self.dev:<EOL><INDENT>if (pyusb1 is True):<EOL><INDENT>self.dev.set_configuration(<NUM_LIT:1>)<EOL>self.handle = self.dev<EOL>self.version = float(<EOL>'<STR_LIT>'.format(self.dev.bcdDevice >> <NUM_LIT:8>,<EOL>self.dev.bcdDevice & <NUM_LIT>))<EOL><DEDENT>else:<EOL><INDENT>self.handle = self.dev.open()<EOL>self.handle.setConfiguration(<NUM_LIT:1>)<EOL>self.handle.claimInterface(<NUM_LIT:0>)<EOL>self.version = float(self.dev.deviceVersion)<EOL><DEDENT><DEDENT>
Create object and scan for USB dongle if no device is supplied
f1095:c0:m0