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 - Time-of-flight Sensor #25

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft

SNS - Time-of-flight Sensor #25

wants to merge 19 commits into from

Conversation

RheaBose
Copy link

@RheaBose RheaBose commented Nov 6, 2023

Implemented with reference to STMicroelectronics application note AN4545

  • Rebase with master after SNS - 16-Bit I2C #58 has been merged
  • Confirm requirements for what this sensor should do (Is ambient light reading required? Continuous or single-shot?)
  • Implement initialisation of ToF, including registers without names
  • Implement reading continuous range
  • Implement reading single-shot range
  • Implement II2cMuxSensor
  • Test on hardware

Copy link
Contributor

@DylanCavers DylanCavers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few changes, mostly to do with formatting and code style, make sure to change the tof file to tof.cpp

Copy link
Contributor

@TomLonergan03 TomLonergan03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, run clang-format over this

Copy link
Contributor

@TomLonergan03 TomLonergan03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this PR also

@licornes-fluos licornes-fluos changed the title Add files via upload Time-of-flight Sensor Nov 9, 2023
@davidbeechey davidbeechey changed the title Time-of-flight Sensor SNS - Time-of-flight Sensor Nov 9, 2023
@licornes-fluos licornes-fluos marked this pull request as draft November 25, 2023 10:31
@sa-fx
Copy link
Contributor

sa-fx commented Jan 28, 2024

Implemented with reference to STMicroelectronics application note AN4545

Task list moved to top of PR


private:
// TODOLater - Confirm these addresses are correct
// Register addresses/values taken from the VL6180X datasheet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where exactly they've been taken from, or what they all mean - Are these needed?

static constexpr std::uint16_t kReadoutSamplingPeriod = 0x010a;
static constexpr std::uint8_t kSysAlsAnalogueGain = 0x03f;
static constexpr std::uint8_t kSysRangeVhvRepeatRate = 0x031;
// TODOLater - Confirm SYSALS__INTEGRATION_PERIOD (application note vs datasheet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The datasheet gives this register an address of 0x040, but the application note AN4545 gives this register an address of 0x0041 on page 24

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lmfao of course it does

engineers write a useful datasheet challenge (impossible)

Copy link
Contributor

@TomLonergan03 TomLonergan03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ik it's a wip, but here's critique anyway lol

Comment on lines +97 to +107
std::optional<std::uint8_t> TimeOfFlight::getRangeSingleShot()
{
if (checkSensorMode(kModeSingleShot) == core::Result::kFailure) { return std::nullopt; }
return getRange();
};

std::optional<std::uint8_t> TimeOfFlight::getRangeContinuous()
{
if (checkSensorMode(kModeContinuous) == core::Result::kFailure) { return std::nullopt; }
return getRange();
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are these for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sensor has two modes of operation: Single shot, and continuous. Single shot is a single measurement, while continuous will repeatedly measure the range with a time interval based on the value of kSysRangeIntermeasurementPeriod. Before reading in a given mode, register kSysRangeStart has to be set to either 1 or 3
Because the code to check that kSysRangeStart has the correct value is the same whether its checking for continuous or single-shot, and because the register to read the range from is the same in both cases if kSysRangeStart has the correct value (kResultRangeVal), getRangeSingleShot() and getRangeContinuous() call these separate functions to retrieve the range

As for which value will be wanted, single shot or continous, that's yet to be decided

auto range_status = *status & 0b111;

// Wait for new measurement ready status
while (range_status != 0x04) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is scary, we need to be able to guarantee that it can't get stuck in this loop

@licornes-fluos licornes-fluos marked this pull request as ready for review February 1, 2024 18:08
@sa-fx sa-fx marked this pull request as draft May 2, 2024 08:19
i2c_->writeByteToRegister(device_address_, array_return_addresses[15], 0x3c);
i2c_->writeByteToRegister(device_address_, array_return_addresses[16], 0x09);
i2c_->writeByteToRegister(device_address_, array_return_addresses[17], 0x09);
// i2c_->writeByteToRegister(device_address_, 0x0198, 0x01);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These registers were not commented out because they should be removed. They were commented out because at the time this code was written, #58 hadn't been merged yet. As such, the 16-bit registers being written to would cause build failure.
These registers should be added back in. Refer to the application note in the PR's header

while (range_status != 0x04) {
status = i2c_->readByte(device_address_, kResultInterruptStatusGpio);
if (!status) {
const auto statuss = i2c_->readByte(device_address_, kResultInterruptStatusGpio);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statuss is quite the variable name.

if (!status) {
logger_.log(core::LogLevel::kFatal, "Failed to get time of flight interrupt status");
return std::nullopt;
}
auto range_status = *status & 0b111;

// Wait for new measurement ready status
uint8_t timeout = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::uint8_t

range_status = *status & 0b111;
range_status = *statuss & 0b111;
timeout++;
if (timeout > 1000000) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing a uint8_t variable to 1,000,000 will always return false

range_status = *status & 0b111;
range_status = *statuss & 0b111;
timeout++;
if (timeout > 1000000) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 1,000,000 sensible? It seems quite large.

i2c_->writeByteToRegister(device_address_, kSystemFreshOutOfReset, 0);

// Recommended : Public registers
i2c_->writeByteToRegister(device_address_, kSystemModeGpioOne, 0x10);
// i2c_->writeByteToRegister(device_address_, kReadoutSamplingPeriod, 0x30);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this was a 16-bit register which would cause build failure, not a line which needed to be removed. It should be added back in

// i2c_->writeByteToRegister(device_address_, 0x01a7, 0x1f);
i2c_->writeByteToRegister(device_address_, array_return_addresses[19], 0x00);

constexpr std::uint8_t data[] = {0xfd,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add entries to this for the values to be written to the 16-bit registers

@@ -1,4 +1,4 @@
#include "time_of_flight.hpp"
#include "tof.hpp"
Copy link
Contributor

@sa-fx sa-fx May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make array_return_addresses a std::uint16_t array, and add the 16-bit registers. Then you can itterate through all the registers
(AN4545, p20)

@sa-fx sa-fx assigned H-Allen and unassigned RheaBose May 2, 2024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should delete these tof.cpp/hpp files.


// Wait for new measurement ready status
std::uint8_t timeout = 0;
std::uint8_t threshold = 10000;
Copy link
Contributor

@sa-fx sa-fx May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::unit8_t has a max value of 255. If you want to use 10,000 here, you'll need std::uint16_t, and you'll also need timeout to be std::uint16_t for the comparison to work


// Wait for new measurement ready status
std::uint8_t timeout = 0;
std::uint8_t threshold = 10000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where has the value of 10,000 come from? What's the rationale for choosing that value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants