Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ranging data notification #104

Merged
merged 17 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/uwb/UwbDevice.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ UwbDevice::OnSessionRangingData(UwbRangingData rangingData)
return;
}

// TODO: implement this
PLOG_VERBOSE << "Session with id " << rangingData.SessionId << " processing new ranging data";
std::vector<UwbPeer> peersData;
peersData.reserve(rangingData.RangingMeasurements.size());
for (const auto& peerData : rangingData.RangingMeasurements) {
UwbPeer data{ peerData };
PLOG_VERBOSE << "Peer data: " << data.ToString();
peersData.push_back(std::move(data));
}
session->ProcessRangingData(peersData);
}

void
Expand Down
44 changes: 44 additions & 0 deletions lib/uwb/UwbPeer.cxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <cmath>
#include <tuple>

#include <notstd/tostring.hxx>
Expand Down Expand Up @@ -32,6 +33,49 @@ UwbPeer::UwbPeer(UwbMacAddress address) :
m_address(std::move(address))
{}

/**
* @brief Converts a Q9.7-formatted value to an IEEE 754 double precision floating point formatted value.
*
* Assuming the Arm definition of Qm.n formatting, the most significant bit is the sign, the next
* (m-1) bits are an integer, and the next n bits is the number to be multiplied by pow(2,n)
* The double equivalent will be the sum of those two results
* TODO double check this conversion, write tests for it
*
* @param q97 a number in Q9.7 format
* @return double
*/
double
ConvertQ97FormatToIEEE(uint16_t q97)
{
static const double pow2 = std::pow(2, -7);
static const uint16_t signMask = 0b1000'0000'0000'0000U;
static const uint16_t unsignedIntegerMask = 0b0111'1111'1000'0000U;
static const uint16_t fractionMask = ~(signMask | unsignedIntegerMask);

bool sign = q97 & signMask;
int unsignedIntegerPart = (q97 & unsignedIntegerMask) >> 7U;
int fractionPart = q97 & fractionMask;

double unsignedNumber = ((double)unsignedIntegerPart) + (((double)fractionPart) * pow2);

return (sign ? -1 : 1) * unsignedNumber;
}

UwbPeer::UwbPeer(const uwb::protocol::fira::UwbRangingMeasurement& data) :
m_address{ data.PeerMacAddress },
m_spatialProperties{
.Distance{ data.Distance }, // TODO is this also q97
.AngleAzimuth{ ConvertQ97FormatToIEEE(data.AoAAzimuth.Result) },
.AngleElevation{ ConvertQ97FormatToIEEE(data.AoAElevation.Result) },
.Elevation{ ConvertQ97FormatToIEEE(data.AoaDestinationElevation.Result) }, // TODO is this right?

.AngleAzimuthFom{ data.AoAAzimuth.FigureOfMerit },
.AngleElevationFom{ data.AoAElevation.FigureOfMerit },
.ElevationFom{ data.AoaDestinationElevation.FigureOfMerit }
}
{
}

UwbPeer::UwbPeer(const UwbPeer& other) :
m_address(other.m_address),
m_spatialProperties(other.m_spatialProperties)
Expand Down
10 changes: 10 additions & 0 deletions lib/uwb/UwbSession.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ UwbSession::InsertPeerImpl(const uwb::UwbMacAddress& peerAddress)
{
m_peers.insert(peerAddress);
PLOG_VERBOSE << "Session with id " << m_sessionId << " added peer via DDI with mac address " << peerAddress.ToString();
}

void
UwbSession::ProcessRangingData(const std::vector<uwb::UwbPeer>& peerRangingData)
{
auto callbacks = m_callbacks.lock();
if (callbacks) {
PLOG_VERBOSE << "Session with id " << m_sessionId << " processing peer ranging data";
callbacks->OnPeerPropertiesChanged(this, peerRangingData);
}
}
12 changes: 12 additions & 0 deletions lib/uwb/include/uwb/UwbPeer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <optional>

#include <uwb/UwbMacAddress.hxx>
#include <uwb/protocols/fira/FiraDevice.hxx>

namespace uwb
{
Expand All @@ -19,6 +20,10 @@ struct UwbPeerSpatialProperties
std::optional<double> AngleElevation;
std::optional<double> Elevation;

std::optional<uint8_t> AngleAzimuthFom;
std::optional<uint8_t> AngleElevationFom;
std::optional<uint8_t> ElevationFom;

std::string
ToString() const;

Expand All @@ -39,6 +44,13 @@ public:
*/
explicit UwbPeer(UwbMacAddress address);

/**
* @brief Construct a new Uwb Peer object from UwbRangingMeasurement data
*
* @param data
*/
explicit UwbPeer(const uwb::protocol::fira::UwbRangingMeasurement& data);
forwardpointer marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Construct a new UwbPeer object from another.
*
Expand Down
8 changes: 8 additions & 0 deletions lib/uwb/include/uwb/UwbSession.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ public:
}
}

/**
* @brief Temporarily public function to call the UwbSessionEventCallbacks callback for new ranging data
*
* @param peerRangingData
*/
void
ProcessRangingData(const std::vector<uwb::UwbPeer>& peerRangingData);

private:
/**
* @brief Internal function to insert a peer address to this session
Expand Down