Skip to content

Commit c2199ad

Browse files
author
Steve Nguyen
committed
clean scan
1 parent d3c32b0 commit c2199ad

File tree

4 files changed

+97
-145
lines changed

4 files changed

+97
-145
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ serialport = "4.2.0"
2222
clap = { version = "4.0.32", features = ["derive"] }
2323
proc-macro2 = { version = "=1.0.67", features=["default", "proc-macro"] }
2424
signal-hook = "0.3.4"
25-
25+
num_enum = "0.7.3"
2626

2727
[dev-dependencies]
2828
env_logger = "0.10.0"

examples/ping.rs

Lines changed: 0 additions & 135 deletions
This file was deleted.

src/bin/dxl_scan.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
use clap::{Parser, ValueEnum};
2+
use std::collections::HashMap;
23
use std::{error::Error, time::Duration};
34

5+
use rustypot::device::DxlModel;
46
use rustypot::DynamixelSerialIO;
5-
67
#[derive(Parser, Debug)]
8+
#[command(author, version, about, long_about = None)]
79
struct 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

1926
fn 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
}

src/device/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
//! High-level register access functions for a specific dynamixel device
22
3+
use num_enum::IntoPrimitive;
4+
use num_enum::TryFromPrimitive;
5+
36
use paste::paste;
47
use std::mem::size_of;
58

69
use crate::{reg_read_only, reg_read_write, DynamixelSerialIO, Result};
710

11+
#[derive(Debug, IntoPrimitive, TryFromPrimitive)]
12+
#[repr(u16)]
13+
pub enum DxlModel {
14+
AX12A = 12,
15+
AX12W = 300,
16+
AX18A = 18,
17+
RX10 = 10,
18+
RX24F = 24,
19+
RX28 = 28,
20+
RX64 = 64,
21+
EX106 = 107,
22+
MX12W = 360,
23+
MX28 = 29,
24+
MX282 = 30,
25+
MX64 = 310,
26+
MX642 = 311,
27+
MX106 = 320,
28+
MX1062 = 321,
29+
XL320 = 350,
30+
XL330M077 = 1190,
31+
XL330M288 = 1200,
32+
XC330M181 = 1230,
33+
XC330M288 = 1240,
34+
XC330T181 = 1210,
35+
XC330T288 = 1220,
36+
XL430W250 = 1060,
37+
XL430W2502 = 1090,
38+
XC430W2502 = 1160,
39+
XC430W150 = 1070,
40+
XC430W240 = 1080,
41+
XM430W210 = 1030,
42+
XM430W350 = 1020,
43+
XM540W150 = 1130,
44+
XM540W270 = 1120,
45+
XH430W210 = 1010,
46+
XH430W350 = 1000,
47+
XH430V210 = 1050,
48+
XH430V350 = 1040,
49+
XH540W150 = 1110,
50+
XH540W270 = 1100,
51+
XH540V150 = 1150,
52+
XH540V270 = 1140,
53+
XW540T260 = 1170,
54+
XW540T140 = 1180,
55+
}
56+
857
/// Generates read and sync_read functions for given register
958
#[macro_export]
1059
macro_rules! reg_read_only {

0 commit comments

Comments
 (0)