1
1
use std:: {
2
- io:: { Read , Write } ,
3
- mem:: size_of,
2
+ io:: { self , Write } ,
3
+ mem:: { size_of, MaybeUninit } ,
4
4
os:: unix:: net:: UnixStream ,
5
- ptr:: read ,
5
+ ptr:: null_mut ,
6
6
slice:: from_raw_parts,
7
7
} ;
8
8
9
- use color_eyre:: Result ;
9
+ use color_eyre:: { eyre:: Context , Result } ;
10
+ use libc:: { fd_set, select, FD_SET , FD_ZERO } ;
10
11
use nalgebra:: { vector, Vector2 , Vector3 } ;
11
12
use types:: { self , ArmJoints , HeadJoints , Joints , LegJoints } ;
12
13
13
- #[ derive( Clone , Copy , Debug , Default ) ]
14
+ use super :: double_buffered_reader:: DoubleBufferedReader ;
15
+
16
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
14
17
#[ repr( C ) ]
15
18
pub struct RobotConfiguration {
16
19
pub body_id : [ u8 ; 20 ] ,
@@ -19,7 +22,7 @@ pub struct RobotConfiguration {
19
22
pub head_version : u8 ,
20
23
}
21
24
22
- #[ derive( Clone , Copy , Debug , Default ) ]
25
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
23
26
#[ repr( C ) ]
24
27
pub struct Battery {
25
28
pub charge : f32 ,
@@ -28,7 +31,7 @@ pub struct Battery {
28
31
pub temperature : f32 ,
29
32
}
30
33
31
- #[ derive( Clone , Copy , Debug , Default ) ]
34
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
32
35
#[ repr( C ) ]
33
36
pub struct Vertex2 {
34
37
x : f32 ,
@@ -41,7 +44,7 @@ impl From<Vertex2> for Vector2<f32> {
41
44
}
42
45
}
43
46
44
- #[ derive( Clone , Copy , Debug , Default ) ]
47
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
45
48
#[ repr( C ) ]
46
49
pub struct Vertex3 {
47
50
x : f32 ,
@@ -55,7 +58,7 @@ impl From<Vertex3> for Vector3<f32> {
55
58
}
56
59
}
57
60
58
- #[ derive( Clone , Copy , Debug , Default ) ]
61
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
59
62
#[ repr( C ) ]
60
63
pub struct InertialMeasurementUnit {
61
64
pub accelerometer : Vertex3 ,
@@ -73,7 +76,7 @@ impl From<InertialMeasurementUnit> for types::InertialMeasurementUnitData {
73
76
}
74
77
}
75
78
76
- #[ derive( Clone , Copy , Debug , Default ) ]
79
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
77
80
#[ repr( C ) ]
78
81
pub struct ForceSensitiveResistors {
79
82
left_foot_front_left : f32 ,
@@ -105,7 +108,7 @@ impl From<ForceSensitiveResistors> for types::ForceSensitiveResistors {
105
108
}
106
109
}
107
110
108
- #[ derive( Clone , Copy , Debug , Default ) ]
111
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
109
112
#[ repr( C ) ]
110
113
pub struct TouchSensors {
111
114
chest_button : bool ,
@@ -145,7 +148,7 @@ impl From<TouchSensors> for types::TouchSensors {
145
148
}
146
149
}
147
150
148
- #[ derive( Clone , Copy , Debug , Default ) ]
151
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
149
152
#[ repr( C ) ]
150
153
pub struct SonarSensors {
151
154
pub left : f32 ,
@@ -161,7 +164,7 @@ impl From<SonarSensors> for types::SonarSensors {
161
164
}
162
165
}
163
166
164
- #[ derive( Clone , Copy , Debug , Default ) ]
167
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
165
168
#[ repr( C ) ]
166
169
pub struct JointsArray {
167
170
pub head_yaw : f32 ,
@@ -267,7 +270,7 @@ impl From<JointsArray> for Joints {
267
270
}
268
271
}
269
272
270
- #[ derive( Clone , Copy , Debug , Default ) ]
273
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
271
274
#[ repr( C ) ]
272
275
pub struct StateStorage {
273
276
/// Seconds since proxy start
@@ -285,7 +288,7 @@ pub struct StateStorage {
285
288
pub status : JointsArray ,
286
289
}
287
290
288
- #[ derive( Clone , Copy , Debug , Default ) ]
291
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
289
292
#[ repr( C ) ]
290
293
pub struct Color {
291
294
pub red : f32 ,
@@ -303,7 +306,7 @@ impl From<types::Rgb> for Color {
303
306
}
304
307
}
305
308
306
- #[ derive( Clone , Copy , Debug , Default ) ]
309
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
307
310
#[ repr( C ) ]
308
311
pub struct Eye {
309
312
pub color_at_0 : Color ,
@@ -331,7 +334,7 @@ impl From<types::Eye> for Eye {
331
334
}
332
335
}
333
336
334
- #[ derive( Clone , Copy , Debug , Default ) ]
337
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
335
338
#[ repr( C ) ]
336
339
pub struct Ear {
337
340
pub intensity_at_0 : f32 ,
@@ -363,7 +366,7 @@ impl From<types::Ear> for Ear {
363
366
}
364
367
}
365
368
366
- #[ derive( Clone , Copy , Debug , Default ) ]
369
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
367
370
#[ repr( C ) ]
368
371
pub struct ControlStorage {
369
372
pub left_eye : Eye ,
@@ -377,10 +380,30 @@ pub struct ControlStorage {
377
380
pub stiffness : JointsArray ,
378
381
}
379
382
380
- pub fn read_from_hula ( stream : & mut UnixStream ) -> Result < StateStorage > {
381
- let mut read_buffer = [ 0 ; size_of :: < StateStorage > ( ) ] ;
382
- stream. read_exact ( & mut read_buffer) ?;
383
- Ok ( unsafe { read ( read_buffer. as_ptr ( ) as * const StateStorage ) } )
383
+ pub fn read_from_hula (
384
+ stream : & mut UnixStream ,
385
+ reader : & mut DoubleBufferedReader ,
386
+ ) -> Result < StateStorage > {
387
+ reader
388
+ . read ( stream, |file_descriptor| unsafe {
389
+ let mut set = MaybeUninit :: < fd_set > :: uninit ( ) ;
390
+ FD_ZERO ( set. as_mut_ptr ( ) ) ;
391
+ let mut set = set. assume_init ( ) ;
392
+ FD_SET ( file_descriptor, & mut set) ;
393
+ if select (
394
+ file_descriptor + 1 ,
395
+ & mut set,
396
+ null_mut ( ) ,
397
+ null_mut ( ) ,
398
+ null_mut ( ) ,
399
+ ) < 0
400
+ {
401
+ return Err ( io:: Error :: last_os_error ( ) ) ;
402
+ }
403
+ Ok ( ( ) )
404
+ } )
405
+ . wrap_err ( "failed to read from stream" ) ?;
406
+ Ok ( * reader. get_last ( ) )
384
407
}
385
408
386
409
pub fn write_to_hula ( stream : & mut UnixStream , control_storage : ControlStorage ) -> Result < ( ) > {
0 commit comments