-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kscalelabs/hexmove
Hexmove imu
- Loading branch information
Showing
9 changed files
with
282 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Example usage of the Hexmove IMU.""" | ||
|
||
import time | ||
|
||
from imu import HexmoveImuReader | ||
|
||
|
||
def main() -> None: | ||
# Initialize the IMU reader for the 'can0' interface and imu with serial number 1 and model 1 | ||
try: | ||
imu_reader = HexmoveImuReader("can0", 1, 1) | ||
except Exception as e: | ||
print(f"Failed to initialize IMU reader: {e}") | ||
return | ||
|
||
try: | ||
while True: | ||
# Get the current IMU data | ||
data = imu_reader.get_data() | ||
print( | ||
f"Angular Position: X={data.x_angle}°, Y={data.y_angle}°, Z={data.z_angle}° | " | ||
f"Angular Velocity: X={data.x_velocity}°/s, Y={data.y_velocity}°/s, Z={data.z_velocity}°/s" | ||
) | ||
|
||
# Sleep for a short duration to avoid spamming the console | ||
time.sleep(0.1) | ||
except KeyboardInterrupt: | ||
print("Stopping IMU reader...") | ||
finally: | ||
imu_reader.stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
"""Defines the top-level API for the IMU package.""" | ||
|
||
__version__ = "0.0.1" | ||
__version__ = "0.0.2" | ||
|
||
# from .bindings import ( | ||
# ) | ||
from .bindings import ( | ||
PyHexmoveImuData as HexmoveImuData, | ||
PyHexmoveImuReader as HexmoveImuReader, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bindings/bindings.pyi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This file is automatically generated by pyo3_stub_gen | ||
# ruff: noqa: E501, F401 | ||
|
||
|
||
class PyHexmoveImuData: | ||
x_angle: float | ||
y_angle: float | ||
z_angle: float | ||
x_velocity: float | ||
y_velocity: float | ||
z_velocity: float | ||
|
||
class PyHexmoveImuReader: | ||
def __new__(cls,interface:str, serial_number:int, model:int): ... | ||
def get_data(self) -> PyHexmoveImuData: | ||
... | ||
|
||
def stop(self) -> None: | ||
... | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use hexmove::ImuReader; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
// Create a new ImuReader for the 'can0' interface | ||
let imu_reader = match ImuReader::new("can0", 1, 1) { | ||
Ok(reader) => reader, | ||
Err(e) => { | ||
eprintln!("Failed to initialize IMU reader: {}", e); | ||
return; | ||
} | ||
}; | ||
|
||
// Continuously read and print IMU data | ||
loop { | ||
let data = imu_reader.get_data(); | ||
println!( | ||
"Angular Position: X={}°, Y={}°, Z={}° | Angular Velocity: X={}°/s, Y={}°/s, Z={}°/s", | ||
data.x_angle, | ||
data.y_angle, | ||
data.z_angle, | ||
data.x_velocity, | ||
data.y_velocity, | ||
data.z_velocity | ||
); | ||
|
||
// Sleep for a short duration to avoid spamming the console | ||
thread::sleep(Duration::from_millis(500)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use log::error; | ||
use socketcan::{CanFrame, CanSocket, EmbeddedFrame, ExtendedId, Id, Socket}; | ||
use std::sync::{Arc, RwLock}; | ||
use std::thread; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct ImuData { | ||
pub x_angle: f32, | ||
pub y_angle: f32, | ||
pub z_angle: f32, | ||
pub x_velocity: f32, | ||
pub y_velocity: f32, | ||
pub z_velocity: f32, | ||
} | ||
|
||
pub struct ImuReader { | ||
socket: Arc<CanSocket>, | ||
data: Arc<RwLock<ImuData>>, | ||
running: Arc<RwLock<bool>>, | ||
} | ||
|
||
impl ImuReader { | ||
pub fn new( | ||
interface: &str, | ||
serial_number: u8, | ||
model: u8, | ||
) -> Result<Self, Box<dyn std::error::Error>> { | ||
let socket = Arc::new(CanSocket::open(interface)?); | ||
let data = Arc::new(RwLock::new(ImuData::default())); | ||
let running = Arc::new(RwLock::new(true)); | ||
|
||
let imu_reader = ImuReader { | ||
socket: socket.clone(), | ||
data: Arc::clone(&data), | ||
running: Arc::clone(&running), | ||
}; | ||
|
||
imu_reader.start_reading_thread(serial_number, model); | ||
|
||
Ok(imu_reader) | ||
} | ||
|
||
fn start_reading_thread(&self, serial_number: u8, model: u8) { | ||
let data = Arc::clone(&self.data); | ||
let running = Arc::clone(&self.running); | ||
let socket = Arc::clone(&self.socket); | ||
|
||
thread::spawn(move || { | ||
while *running.read().unwrap() { | ||
match socket.read_frame() { | ||
Ok(CanFrame::Data(data_frame)) => { | ||
let received_data = data_frame.data(); | ||
let id = data_frame.id(); | ||
|
||
let base_id = | ||
0x0B000000 | (serial_number as u32) << 16 | (model as u32) << 8; | ||
|
||
if id == Id::Extended(ExtendedId::new(base_id | 0xB1).unwrap()) { | ||
let x_angle = i16::from_le_bytes([received_data[0], received_data[1]]) | ||
as f32 | ||
* 0.01; | ||
let y_angle = i16::from_le_bytes([received_data[2], received_data[3]]) | ||
as f32 | ||
* 0.01; | ||
let z_angle = i16::from_le_bytes([received_data[4], received_data[5]]) | ||
as f32 | ||
* 0.01; | ||
|
||
let mut imu_data = data.write().unwrap(); | ||
imu_data.x_angle = x_angle; | ||
imu_data.y_angle = y_angle; | ||
imu_data.z_angle = z_angle; | ||
} | ||
|
||
if id == Id::Extended(ExtendedId::new(base_id | 0xB2).unwrap()) { | ||
let x_velocity = | ||
i16::from_le_bytes([received_data[0], received_data[1]]) as f32 | ||
* 0.01; | ||
let y_velocity = | ||
i16::from_le_bytes([received_data[2], received_data[3]]) as f32 | ||
* 0.01; | ||
let z_velocity = | ||
i16::from_le_bytes([received_data[4], received_data[5]]) as f32 | ||
* 0.01; | ||
|
||
let mut imu_data = data.write().unwrap(); | ||
imu_data.x_velocity = x_velocity; | ||
imu_data.y_velocity = y_velocity; | ||
imu_data.z_velocity = z_velocity; | ||
} | ||
} | ||
Ok(CanFrame::Remote(_)) => { | ||
// Ignore remote frames | ||
} | ||
Ok(CanFrame::Error(_)) => { | ||
// Ignore error frames | ||
} | ||
Err(e) => { | ||
error!("Error reading IMU data: {}", e); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
pub fn get_data(&self) -> ImuData { | ||
self.data.read().unwrap().clone() | ||
} | ||
|
||
pub fn stop(&self) { | ||
let mut running = self.running.write().unwrap(); | ||
*running = false; | ||
} | ||
} | ||
|
||
impl Drop for ImuReader { | ||
fn drop(&mut self) { | ||
self.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters