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 - I2C ToF #26

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a76e007
initial commit
Aux1r Oct 14, 2024
6a1c274
initial ToF code
Aux1r Oct 17, 2024
f6d2c11
Merge branch 'main' into kacper/hype-27-implement-time-of-flight
Aux1r Oct 17, 2024
97bc9ae
updates to tof file
Aux1r Oct 24, 2024
ac06130
tof.rs updates, i2c.rs updates
Aux1r Oct 31, 2024
5a0f491
need to start writing tests now
Aux1r Nov 7, 2024
c5b556c
Merge branch 'main' into kacper/hype-27-implement-time-of-flight
Aux1r Nov 7, 2024
e2e0c7f
need to write tests, made some changes to i2c
Aux1r Nov 7, 2024
801c9fa
nearly done with tests
Aux1r Nov 14, 2024
15163d8
Tests & Task done
Aux1r Nov 14, 2024
c61d213
Merge branch 'main' into kacper/hype-27-implement-time-of-flight
Aux1r Nov 14, 2024
2a6465e
tof_test added + 16-bit changes
Aux1r Nov 14, 2024
d3f4eec
add semicolons
Aux1r Nov 14, 2024
c15a348
hopefully fixed clippy
Aux1r Nov 14, 2024
c3ea4cc
clippy please
Aux1r Nov 14, 2024
5706f91
check if range read test works
Aux1r Nov 16, 2024
344b048
see if board build works
Aux1r Nov 16, 2024
94051df
testing
Aux1r Nov 16, 2024
d8cc8a9
fixing i2c.rs
Aux1r Nov 16, 2024
f48b519
please
Aux1r Nov 16, 2024
3ac350d
getting somewhere
Aux1r Nov 16, 2024
0e282c8
seeing where new() is failing
Aux1r Nov 16, 2024
e04407d
seeing where new() is failing part 2
Aux1r Nov 16, 2024
5173bbd
seeing where new() is failing part 3
Aux1r Nov 16, 2024
99cc6d6
seeing where new() is failing part 4
Aux1r Nov 16, 2024
a7732f6
seeing where new() is failing part 4
Aux1r Nov 16, 2024
f8a0be0
seeing where new() is failing part 5
Aux1r Nov 16, 2024
a846302
seeing where new() is failing part 6 PROGRESS
Aux1r Nov 16, 2024
a27d3e7
seeing where new() is failing part 7 PROGRESS
Aux1r Nov 16, 2024
db47381
finding the bad u8
Aux1r Nov 16, 2024
4c62b4d
finding the bad u8 part 2
Aux1r Nov 16, 2024
af4d1e0
try removing the bad u8
Aux1r Nov 16, 2024
98d4f0c
try removing the 2nd bad u8
Aux1r Nov 16, 2024
b17a295
try removing the 3rd bad u8
Aux1r Nov 16, 2024
e2a05e7
try removing the 4th bad u8, final one?
Aux1r Nov 16, 2024
66f4525
what
Aux1r Nov 16, 2024
dc0080c
what 2
Aux1r Nov 16, 2024
ae52a9b
what 3
Aux1r Nov 16, 2024
26a6ba1
what
Aux1r Nov 16, 2024
b5fd966
what
Aux1r Nov 16, 2024
123dc1c
please
Aux1r Nov 16, 2024
eb848b6
Hopefully tests work now
Aux1r Nov 16, 2024
345505d
run fmt and fix clippy
Aux1r Nov 16, 2024
24f29f5
run fmt and fix clippy 2
Aux1r Nov 16, 2024
c56810d
run fmt and fix clippy 3
Aux1r Nov 16, 2024
822a3c5
ToF sensor working!
davidbeechey Nov 20, 2024
f59f5f6
Check tests
Aux1r Nov 21, 2024
c246276
Fixing ToF Tests
Aux1r Nov 21, 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
7 changes: 7 additions & 0 deletions lib/io/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ pub trait HypedI2c {
register_address: u8,
data: u8,
) -> Result<(), I2cError>;
fn read_byte_16(&mut self, device_address: u8, register_address: u16) -> Option<u8>;
Aux1r marked this conversation as resolved.
Show resolved Hide resolved
fn write_byte_to_register_16(
&mut self,
device_address: u8,
register_address: u16,
data: u8,
) -> Result<(), I2cError>;
}
1 change: 1 addition & 0 deletions lib/sensors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]

pub mod tof;
pub mod keyence;
pub mod temperature;
205 changes: 205 additions & 0 deletions lib/sensors/src/tof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
use hyped_io::i2c::{HypedI2c, I2cError};

///ToF implements the logic to read Time of Flight data from the VL6180V1 Time of Flight
///sensor using I2C peripheral provided by the Hyped I2c trait.
///
/// finish this write up later.
///
/// Data Sheet: https://www.st.com/en/imaging-and-photonics-solutions/vl6180.html#overview
///
/// Application Sheet: https://www.st.com/resource/en/application_note/an4545-vl6180x-basic-ranging-application-note-stmicroelectronics.pdf

pub struct TimeOfFlight<T: HypedI2c> {
i2c: T,
device_address: u8,
}

impl<T: HypedI2c> TimeOfFlight<T> {
// Create a new instance of time of flight sensor, configure
pub fn new(mut i2c: T, device_address: ToFAddresses) -> Result<Self, ToFError> { // SR03 Settings as seen in Application Sheet
let device_address = device_address as u8;
for i in 0..30 { // writing to private registers
match i2c.write_byte_to_register_16(device_address, PRIVATE_REGISTERS[i], PRIVATE_REGISTER_DATA[i]) {
Ok(p) => p, // unused
Err(e) => return Err(ToFError::I2cError(e)),
}
}
// Recommended Public Registers now (see Application Sheet)
match i2c.write_byte_to_register(device_address, SYS_MODE_GPIO1, SYS_MODE_GPIO_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}
match i2c.write_byte_to_register_16(device_address, AVG_SAMPLE_PERIOD, AVG_SAMPLE_PERIOD_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}
match i2c.write_byte_to_register(device_address, SYSRANGE_VHV_REPEAT_RATE, SYSRANGE_VHV_REPEAT_RATE_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}
match i2c.write_byte_to_register(device_address, SYSRANGE_VHV_RECALIBRATE, SYSRANGE_VHV_RECALIBRATE_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}
match i2c.write_byte_to_register(device_address, SYSRANGE_INTERMEASURE_PERIOD, SYSRANGE_INTERMEASURE_PERIOD_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}
match i2c.write_byte_to_register(device_address, SYS_INTERRUPT_CONFIG_GPIO, SYS_INTERRUPT_CONFIG_GPIO_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}

// not sure if this works? effectively if no errors returned from previous match arms, return Ok(Self)
Ok(Self {
i2c,
device_address,
})
}

pub fn start_ss_measure(mut i2c: T, device_address: ToFAddresses) -> Result<Self, ToFError> { // start single shot measurement
let device_address = device_address as u8;
match i2c.write_byte_to_register(device_address, SYSRANGE_START, SYSRANGE_START_SS_VAL) {
Ok(p) => p,
Err(e) => return Err(ToFError::I2cError(e)),
}

Ok (Self {
i2c,
device_address
})
}

pub fn poll_range(&mut self) {
let status_byte =
match self.i2c.read_byte(self.device_address, RESULT_INTERR_STATUS_GPIO) {
Some(byte) => byte,
None => 0, // not sure about returning 0 for None - will this somehow break stuff?
};
let range_status = status_byte & 0x07;
while (range_status != 0x04) {
Aux1r marked this conversation as resolved.
Show resolved Hide resolved
let status_byte = match self.i2c.read_byte(self.device_address, RESULT_INTERR_STATUS_GPIO) {
Some(byte) => byte,
None => 0,
};
let range_status = status_byte & 0x07;
}

}

pub fn read_range(&mut self) -> Option<f32> {
let range_byte =
match self.i2c.read_byte(self.device_address, RESULT_RANGE_VAL) {
Some(byte) => byte,
None => {
return None;
}
}; // implement logic to convert byte to f32 (mm)
let range = range_byte as f32; // hopefully this works, not sure how it's encoded..
Some(range)
}
}



pub enum ToFAddresses {
Address29 = 0x29,
}

pub enum ToFError {
I2cError(I2cError)
}

// public register addresses
const SYS_MODE_GPIO1: u8 = 0x0011;
const AVG_SAMPLE_PERIOD: u16 = 0x010a;
// can't find reference to 0x003f address for light and dark gain in datasheet from the application note
const SYSRANGE_VHV_REPEAT_RATE: u8 = 0x031;
// can't find reference to 0x0041, see above.
const SYSRANGE_VHV_RECALIBRATE: u8 = 0x002e;
const SYSRANGE_INTERMEASURE_PERIOD: u8 = 0x01b;
// same story with 0x003e
const SYS_INTERRUPT_CONFIG_GPIO: u8 = 0x014;
const SYSRANGE_START: u8 = 0x018;
const RESULT_INTERR_STATUS_GPIO: u8 = 0x04f;
// this one has VAL because that's what its' called in the docs, not actually a VALUE.
const RESULT_RANGE_VAL: u8 = 0x062;

// init values for public registers
const SYS_MODE_GPIO_VAL: u8 = 0x01;
const AVG_SAMPLE_PERIOD_VAL: u8 = 0x30;
const SYSRANGE_VHV_REPEAT_RATE_VAL: u8 = 0xFF;
const SYSRANGE_VHV_RECALIBRATE_VAL: u8 = 0x01;
const SYSRANGE_INTERMEASURE_PERIOD_VAL: u8 = 0x09;
const SYS_INTERRUPT_CONFIG_GPIO_VAL: u8 = 0x24;
const SYSRANGE_START_SS_VAL: u8 = 0x01;

// private registers array
const PRIVATE_REGISTERS: [u16; 30] = [
0x0207,
0x0208,
0x0096,
0x0097,
0x00e3,
0x00e4,
0x00e5,
0x00e6,
0x00e7,
0x00f5,
0x00d9,
0x00db,
0x00dc,
0x00dd,
0x009f,
0x00a3,
0x00b7,
0x00bb,
0x00b2,
0x00ca,
0x0198,
0x01b0,
0x01ad,
0x00ff,
0x0100,
0x0199,
0x01a6,
0x01ac,
0x01a7,
0x0030
];
// init values for private registers array
const PRIVATE_REGISTER_DATA: [u8; 30] = [
0x01,
0x01,
0x00,
0xfd,
0x01,
0x03,
0x02,
0x01,
0x03,
0x02,
0x05,
0xce,
0x03,
0xf8,
0x00,
0x3c,
0x00,
0x3c,
0x09,
0x09,
0x01,
0x17,
0x00,
0x05,
0x05,
0x05,
0x1b,
0x3e,
0x1f,
0x00
];



Loading