11use clap:: { Parser , ValueEnum } ;
2+ use std:: collections:: HashMap ;
23use std:: { error:: Error , time:: Duration } ;
34
5+ use rustypot:: device:: DxlModel ;
46use rustypot:: DynamixelSerialIO ;
5-
67#[ derive( Parser , Debug ) ]
8+ #[ command( author, version, about, long_about = None ) ]
79struct Args {
8- serial_port : String ,
9- #[ arg( value_enum) ]
10+ #[ arg( short, long, default_value = "/dev/ttyUSB0" ) ]
11+ serialport : String ,
12+ /// baud
13+ #[ arg( short, long, default_value_t = 2_000_000 ) ]
14+ baudrate : u32 ,
15+
16+ #[ arg( short, long, value_enum, default_value_t = ProtocolVersion :: V1 ) ]
1017 protocol : ProtocolVersion ,
1118}
1219
@@ -18,21 +25,52 @@ enum ProtocolVersion {
1825
1926fn main ( ) -> Result < ( ) , Box < dyn Error > > {
2027 let args = Args :: parse ( ) ;
28+ let serialport: String = args. serialport ;
29+ let baudrate: u32 = args. baudrate ;
30+ let protocol: ProtocolVersion = args. protocol ;
31+
32+ //print the standard ids for the arm motors
2133
34+ //print all the argument values
35+ println ! ( "serialport: {}" , serialport) ;
36+ println ! ( "baudrate: {}" , baudrate) ;
37+ match protocol {
38+ ProtocolVersion :: V1 => println ! ( "protocol: V1" ) ,
39+ ProtocolVersion :: V2 => println ! ( "protocol: V2" ) ,
40+ }
41+
42+ let mut found = HashMap :: new ( ) ;
2243 println ! ( "Scanning..." ) ;
23- let mut serial_port = serialport:: new ( args . serial_port , 2_000_000 )
44+ let mut serial_port = serialport:: new ( serialport , baudrate )
2445 . timeout ( Duration :: from_millis ( 10 ) )
2546 . open ( ) ?;
2647
27- let io = match args . protocol {
48+ let io = match protocol {
2849 ProtocolVersion :: V1 => DynamixelSerialIO :: v1 ( ) ,
2950 ProtocolVersion :: V2 => DynamixelSerialIO :: v2 ( ) ,
3051 } ;
3152
32- let ids: Vec < u8 > = ( 1 ..253 )
33- . filter ( |id| io. ping ( serial_port. as_mut ( ) , * id) . unwrap ( ) )
34- . collect ( ) ;
35- println ! ( "Ids found: {:?}" , ids) ;
53+ for id in 1 ..253 {
54+ match io. ping ( serial_port. as_mut ( ) , id) {
55+ Ok ( present) => {
56+ if present {
57+ let model = io. read ( serial_port. as_mut ( ) , id, 0 , 2 ) . unwrap ( ) ;
58+
59+ found. insert ( id, u16:: from_le_bytes ( [ model[ 0 ] , model[ 1 ] ] ) ) ;
60+ }
61+ }
62+ Err ( e) => eprintln ! ( "Error: {e}" ) ,
63+ } ;
64+ }
65+
66+ println ! ( "found {} motors" , found. len( ) ) ;
67+ for ( key, value) in found {
68+ println ! (
69+ "id: {} model: {:?}" ,
70+ key,
71+ DxlModel :: try_from( value) . unwrap( )
72+ ) ;
73+ }
3674
3775 Ok ( ( ) )
3876}
0 commit comments