99//! a C wrapper of the `RTIMULib` C++ API. We then call that unsafe C wrapper
1010//! here, ensuring that any memory allocations were undone on drop.
1111
12- use super :: { Angle , Orientation } ;
12+ use super :: { Angle , Orientation , Vector3D , ImuData } ;
1313use libc;
1414
1515enum RTIMULibContext { }
@@ -26,12 +26,33 @@ extern "C" {
2626 fn rtimulib_wrapper_imu_read ( p_context : * mut RTIMULibContext ) -> libc:: c_int ;
2727 fn rtimulib_wrapper_get_imu_data (
2828 p_context : * mut RTIMULibContext ,
29- orientation : * mut COrientation ,
29+ orientation : * mut CAllData ,
3030 ) -> libc:: c_int ;
3131}
3232
3333#[ repr( C ) ]
34- struct COrientation {
34+ #[ derive( Default ) ]
35+ struct CAllData {
36+ timestamp : libc:: uint64_t ,
37+ fusion_pose_valid : libc:: c_int ,
38+ fusion_pose : CVector3D ,
39+ gyro_valid : libc:: c_int ,
40+ gyro : CVector3D ,
41+ accel_valid : libc:: c_int ,
42+ accel : CVector3D ,
43+ compass_valid : libc:: c_int ,
44+ compass : CVector3D ,
45+ pressure_valid : libc:: c_int ,
46+ pressure : libc:: c_double ,
47+ temperature_valid : libc:: c_int ,
48+ temperature : libc:: c_double ,
49+ humidity_valid : libc:: c_int ,
50+ humidity : libc:: c_double ,
51+ }
52+
53+ #[ repr( C ) ]
54+ #[ derive( Default ) ]
55+ struct CVector3D {
3556 x : libc:: c_double ,
3657 y : libc:: c_double ,
3758 z : libc:: c_double ,
@@ -42,13 +63,13 @@ pub enum Error {
4263 RTIMULibError ,
4364}
4465
45- pub struct Lsm9ds1 < ' a > {
66+ pub ( crate ) struct Lsm9ds1 < ' a > {
4667 rtimulib_ref : & ' a mut RTIMULibContext ,
4768}
4869
4970impl < ' a > Lsm9ds1 < ' a > {
5071 /// Uses the `RTIMULib` library.
51- pub fn new ( ) -> Result < Lsm9ds1 < ' a > , Error > {
72+ pub ( crate ) fn new ( ) -> Result < Lsm9ds1 < ' a > , Error > {
5273 let ctx_ref = unsafe {
5374 let ctx_p = rtimulib_wrapper_create ( ) ;
5475 if ctx_p. is_null ( ) {
@@ -64,47 +85,92 @@ impl<'a> Lsm9ds1<'a> {
6485
6586 /// Make the IMU do some work. When this function returns true, the IMU
6687 /// has data we can fetch with `get_imu_data()`.
67- pub fn imu_read ( & mut self ) -> bool {
88+ pub ( crate ) fn imu_read ( & mut self ) -> bool {
6889 let result = unsafe { rtimulib_wrapper_imu_read ( self . rtimulib_ref ) } ;
6990 result != 0
7091 }
7192
72- pub fn set_fusion ( & mut self ) {
93+ pub ( crate ) fn set_fusion ( & mut self ) {
7394 unsafe {
7495 rtimulib_set_sensors ( self . rtimulib_ref , 1 , 1 , 1 ) ;
7596 }
7697 }
7798
78- pub fn set_compass_only ( & mut self ) {
99+ pub ( crate ) fn set_compass_only ( & mut self ) {
79100 unsafe {
80101 rtimulib_set_sensors ( self . rtimulib_ref , 0 , 0 , 1 ) ;
81102 }
82103 }
83104
84- pub fn set_gyro_only ( & mut self ) {
105+ pub ( crate ) fn set_gyro_only ( & mut self ) {
85106 unsafe {
86107 rtimulib_set_sensors ( self . rtimulib_ref , 1 , 0 , 0 ) ;
87108 }
88109 }
89110
90- pub fn set_accel_only ( & mut self ) {
111+ pub ( crate ) fn set_accel_only ( & mut self ) {
91112 unsafe {
92113 rtimulib_set_sensors ( self . rtimulib_ref , 0 , 1 , 0 ) ;
93114 }
94115 }
95116
96- pub fn get_imu_data ( & mut self ) -> Result < Orientation , Error > {
97- let mut temp = COrientation {
98- x : 0.0 ,
99- y : 0.0 ,
100- z : 0.0 ,
101- } ;
117+ pub ( crate ) fn get_imu_data ( & mut self ) -> Result < ImuData , Error > {
118+ let mut temp = CAllData :: default ( ) ;
102119 let result = unsafe { rtimulib_wrapper_get_imu_data ( self . rtimulib_ref , & mut temp) } ;
103120 if result != 0 {
104- Ok ( Orientation {
105- roll : Angle :: from_radians ( temp. x ) ,
106- pitch : Angle :: from_radians ( temp. y ) ,
107- yaw : Angle :: from_radians ( temp. z ) ,
121+ Ok ( ImuData {
122+ timestamp : temp. timestamp ,
123+ fusion_pose : if temp. fusion_pose_valid != 0 {
124+ Some ( Orientation {
125+ roll : Angle :: from_radians ( temp. fusion_pose . x ) ,
126+ pitch : Angle :: from_radians ( temp. fusion_pose . y ) ,
127+ yaw : Angle :: from_radians ( temp. fusion_pose . z ) ,
128+ } )
129+ } else {
130+ None
131+ } ,
132+ gyro : if temp. gyro_valid != 0 {
133+ Some ( Vector3D {
134+ x : temp. gyro . x ,
135+ y : temp. gyro . y ,
136+ z : temp. gyro . z ,
137+ } )
138+ } else {
139+ None
140+ } ,
141+ accel : if temp. accel_valid != 0 {
142+ Some ( Vector3D {
143+ x : temp. accel . x ,
144+ y : temp. accel . y ,
145+ z : temp. accel . z ,
146+ } )
147+ } else {
148+ None
149+ } ,
150+ compass : if temp. compass_valid != 0 {
151+ Some ( Vector3D {
152+ x : temp. compass . x ,
153+ y : temp. compass . y ,
154+ z : temp. compass . z ,
155+ } )
156+ } else {
157+ None
158+ } ,
159+ pressure : if temp. pressure_valid != 0 {
160+ Some ( temp. pressure )
161+ } else {
162+ None
163+ } ,
164+ temperature : if temp. temperature_valid != 0 {
165+ Some ( temp. temperature )
166+ } else {
167+ None
168+ } ,
169+ humidity : if temp. humidity_valid != 0 {
170+ Some ( temp. humidity )
171+ } else {
172+ None
173+ } ,
108174 } )
109175 } else {
110176 Err ( Error :: RTIMULibError )
0 commit comments