Skip to content

Commit

Permalink
Closes #178: Add LH angle streaming support
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Nov 19, 2020
1 parent 63809da commit 0ede756
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cflib/crazyflie/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from cflib.crtp.crtpstack import CRTPPort
from cflib.utils.callbacks import Caller

from cflib.utils.fp16 import fp16_to_float

__author__ = 'Bitcraze AB'
__all__ = ['Localization', 'LocalizationPacket']

Expand Down Expand Up @@ -65,6 +67,7 @@ class Localization():
COMM_GNSS_PROPRIETARY = 7
EXT_POSE = 8
EXT_POSE_PACKED = 9
LH_ANGLE_STREAM = 10

def __init__(self, crazyflie=None):
"""
Expand Down Expand Up @@ -100,10 +103,31 @@ def _incoming(self, packet):
anchor_id, distance = struct.unpack('<Bf', raw_data[:5])
decoded_data[anchor_id] = distance
raw_data = raw_data[5:]
elif pk_type == self.LH_ANGLE_STREAM:
decoded_data = self._decode_lh_angle(data)

pk = LocalizationPacket(pk_type, data, decoded_data)
self.receivedLocationPacket.call(pk)

def _decode_lh_angle(self, data):
decoded_data = {}

raw_data = struct.unpack("<Bfhhhfhhh", data)

decoded_data['basestation'] = raw_data[0]
decoded_data['x'] = [0, 0, 0, 0]
decoded_data['x'][0] = raw_data[1]
decoded_data['x'][1] = raw_data[1] - fp16_to_float(raw_data[2])
decoded_data['x'][2] = raw_data[1] - fp16_to_float(raw_data[3])
decoded_data['x'][3] = raw_data[1] - fp16_to_float(raw_data[4])
decoded_data['y'] = [0, 0, 0, 0]
decoded_data['y'][0] = raw_data[5]
decoded_data['y'][1] = raw_data[5] - fp16_to_float(raw_data[6])
decoded_data['y'][2] = raw_data[5] - fp16_to_float(raw_data[7])
decoded_data['y'][3] = raw_data[5] - fp16_to_float(raw_data[8])

return decoded_data

def send_extpos(self, pos):
"""
Send the current Crazyflie X, Y, Z position. This is going to be
Expand Down
56 changes: 56 additions & 0 deletions cflib/utils/fp16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

import struct


# Code from davidejones at https://gamedev.stackexchange.com/a/28756
def fp16_to_float(float16):
s = int((float16 >> 15) & 0x00000001) # sign
e = int((float16 >> 10) & 0x0000001f) # exponent
f = int(float16 & 0x000003ff) # fraction

if e == 0:
if f == 0:
return int(s << 31)
else:
while not (f & 0x00000400):
f = f << 1
e -= 1
e += 1
f &= ~0x00000400
# print(s,e,f)
elif e == 31:
if f == 0:
return int((s << 31) | 0x7f800000)
else:
return int((s << 31) | 0x7f800000 | (f << 13))

e = e + (127 - 15)
f = f << 13
result = int((s << 31) | (e << 23) | f)
return struct.unpack("f", struct.pack("I", result))[0]

0 comments on commit 0ede756

Please sign in to comment.