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

SNS - Optical Flow Sensor #60

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
d2c4bcf
new branch with everything
licornes-fluos Jan 27, 2024
cf4eb63
ToDoLater
licornes-fluos Feb 26, 2024
4d38dad
Merge branch 'master' into newOpticalFlowSensors
TomLonergan03 May 6, 2024
213db5a
start code for testing
licornes-fluos May 7, 2024
3f0c866
Merge remote-tracking branch 'refs/remotes/origin/newOpticalFlowSenso…
licornes-fluos May 7, 2024
3f8288c
part test code for optical flow
licornes-fluos May 13, 2024
939cac3
Merge branch 'master' into newOpticalFlowSensors
TomLonergan03 May 13, 2024
95d1e8f
god bless static analysis
TomLonergan03 May 13, 2024
e2f4b79
fix build
TomLonergan03 May 14, 2024
b29669b
fix repl segfault on failed toml parse
TomLonergan03 May 14, 2024
9b4269e
add 2MHz speed
TomLonergan03 May 14, 2024
4142001
read optical flow from config
TomLonergan03 May 14, 2024
74b916f
read optical flow from config pt 2
TomLonergan03 May 14, 2024
466100b
correct spi reads?
TomLonergan03 May 20, 2024
d442fac
proper initialiser
TomLonergan03 May 20, 2024
7c51edf
close terminal on destruct
TomLonergan03 May 20, 2024
b06422a
constructs
TomLonergan03 May 20, 2024
bcd0145
actual read command
TomLonergan03 May 20, 2024
1813769
change casts
TomLonergan03 May 20, 2024
f7502d1
fix types
TomLonergan03 May 20, 2024
b830e69
arrays lmao
TomLonergan03 May 20, 2024
7633a43
not sure on the conversion
TomLonergan03 May 20, 2024
9c86777
refactor spi to not use nasty arrays
TomLonergan03 May 23, 2024
a6be61b
fix includes
TomLonergan03 May 23, 2024
4b689e1
magic numbers
TomLonergan03 May 23, 2024
88c7bd4
power up reset
TomLonergan03 May 23, 2024
0a5d183
fix build
TomLonergan03 May 23, 2024
f931fae
stolen from python lib
TomLonergan03 May 23, 2024
1462e84
correct read?
TomLonergan03 May 23, 2024
0ced922
magic numbers pt 2
TomLonergan03 May 27, 2024
712125a
marginally better names
TomLonergan03 May 27, 2024
56299e4
magic read
TomLonergan03 May 27, 2024
2c666b4
wait for ready
TomLonergan03 May 27, 2024
9dd53b9
wait for good read
TomLonergan03 May 27, 2024
c1898a6
400khz spi
TomLonergan03 May 27, 2024
9213246
different register
TomLonergan03 May 27, 2024
a39933e
remove read success logs, they spammy
TomLonergan03 May 27, 2024
d0bb69f
another one
TomLonergan03 May 27, 2024
4549391
sleeps wtf????
TomLonergan03 May 27, 2024
dd659a2
lints and more sleeps
TomLonergan03 May 27, 2024
86afb1a
repeat reads
TomLonergan03 May 27, 2024
9b54357
actually deref optional
TomLonergan03 May 27, 2024
91e0ec7
change a couple of magic numbers
TomLonergan03 May 28, 2024
c971946
correct ready bit
TomLonergan03 May 28, 2024
39234e9
Revert "change a couple of magic numbers"
TomLonergan03 May 28, 2024
0e07f94
swap bytes??
TomLonergan03 May 28, 2024
196a13e
actually revert ready bit
TomLonergan03 May 28, 2024
7097968
try the other read
TomLonergan03 May 28, 2024
144168d
grasping at straws
TomLonergan03 May 28, 2024
23276ed
Revert "grasping at straws"
TomLonergan03 May 28, 2024
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
58 changes: 58 additions & 0 deletions lib/sensors/optical_flow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "optical_flow.hpp"

namespace hyped::sensors {

std::optional<OpticalFlow> OpticalFlow::create(core::ILogger &logger,
std::shared_ptr<io::ISpi> spi,
const std::uint8_t channel)
{
// check we are communicating with the correct sensor
const std::uint8_t *device_id;
spi->read(kDeviceIdAddress, device_id, 1);
if (!device_id) {
logger.log(core::LogLevel::kFatal, "Failed to read the optical flow device");
return std::nullopt;
}
if (*device_id != kExpectedDeviceIdValue) {
logger.log(core::LogLevel::kFatal, "Failure, mismatched device ID for optical flow sensor");
return std::nullopt;
}
return OpticalFlow(logger, spi, channel);
}

OpticalFlow::OpticalFlow(core::ILogger &logger,
std::shared_ptr<io::ISpi> spi,
const std::uint8_t channel)
: logger_(logger),
spi_(spi),
channel_(channel)
{
}

OpticalFlow::~OpticalFlow()
{
}

// inspired by Bitcraze_PMW3901 on github
// (https://github.com/bitcraze/Bitcraze_PMW3901/blob/master/src/Bitcraze_PMW3901.cpp)
std::uint8_t OpticalFlow::getDeltaX(std::shared_ptr<io::ISpi> spi)
{
std::uint8_t *x_low;
std::uint8_t *x_high;

spi->read(kXLowAddress, x_low, 1);
spi->read(kXHighAddress, x_high, 1);

return static_cast<std::int16_t>((*x_high << 8) | *x_low);
licornes-fluos marked this conversation as resolved.
Show resolved Hide resolved
}
std::uint8_t OpticalFlow::getDeltaY(std::shared_ptr<io::ISpi> spi)
{
std::uint8_t *y_low;
std::uint8_t *y_high;

spi->read(kYLowAddress, y_low, 1);
spi->read(kYHighAddress, y_high, 1);

return static_cast<std::int16_t>((*y_high << 8) | *y_low);
}
} // namespace hyped::sensors
43 changes: 43 additions & 0 deletions lib/sensors/optical_flow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <cstdint>
#include <cstring>
#include <memory>
#include <optional>

#include <core/logger.hpp>
#include <core/types.hpp>
#include <io/spi.hpp>

namespace hyped::sensors {

class OpticalFlow {
// include "magic sauce" optimisation?
licornes-fluos marked this conversation as resolved.
Show resolved Hide resolved
public:
static std::optional<OpticalFlow> create(core::ILogger &logger,
std::shared_ptr<io::ISpi> spi,
const std::uint8_t channel);
~OpticalFlow();

private:
OpticalFlow(core::ILogger &logger, std::shared_ptr<io::ISpi> spi, const std::uint8_t channel);
std::uint8_t getDeltaX(std::shared_ptr<io::ISpi> spi);
std::uint8_t getDeltaY(std::shared_ptr<io::ISpi> spi);

private:
core::ILogger &logger_;
std::shared_ptr<io::ISpi> spi_;
const std::uint8_t channel_;

private:
// Register addresses for x and y
static constexpr std::uint8_t kXLowAddress = 0x03;
static constexpr std::uint8_t kXHighAddress = 0x04;
static constexpr std::uint8_t kYLowAddress = 0x05;
static constexpr std::uint8_t kYHighAddress = 0x06;

static constexpr std::uint8_t kDeviceIdAddress = 0x00;
static constexpr std::uint8_t kExpectedDeviceIdValue = 0x49; // unsure that's the value
};

} // namespace hyped::sensors
Loading