Skip to content

Commit 013e37a

Browse files
authored
Merge pull request #61 from pollen-robotics/support-poulpe
Small modif to scan tool
2 parents dcee942 + c2199ad commit 013e37a

File tree

4 files changed

+185
-9
lines changed

4 files changed

+185
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ paste = "1.0.10"
2121
serialport = "4.2.0"
2222
clap = { version = "4.0.32", features = ["derive"] }
2323
proc-macro2 = { version = "=1.0.67", features=["default", "proc-macro"] }
24+
signal-hook = "0.3.4"
25+
num_enum = "0.7.3"
2426

2527
[dev-dependencies]
2628
env_logger = "0.10.0"

examples/dxl_sinus.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::f32::consts::PI;
2+
use std::time::SystemTime;
3+
use std::{error::Error, thread, time::Duration};
4+
5+
use rustypot::device::mx;
6+
use rustypot::DynamixelSerialIO;
7+
8+
use clap::Parser;
9+
use rustypot::device::mx::conv::radians_to_dxl_pos;
10+
11+
use signal_hook::flag;
12+
13+
use std::sync::atomic::{AtomicBool, Ordering};
14+
use std::sync::Arc;
15+
16+
#[derive(Parser, Debug)]
17+
#[command(author, version, about, long_about = None)]
18+
struct Args {
19+
/// tty
20+
#[arg(short, long)]
21+
serialport: String,
22+
/// baud
23+
#[arg(short, long, default_value_t = 1_000_000)]
24+
baudrate: u32,
25+
26+
/// id
27+
#[arg(short, long)]
28+
id: u8,
29+
30+
///sinus amplitude (f64)
31+
#[arg(short, long, default_value_t = 10.0)]
32+
amplitude: f32,
33+
34+
///sinus frequency (f64)
35+
#[arg(short, long, default_value_t = 1.0)]
36+
frequency: f32,
37+
}
38+
39+
fn main() -> Result<(), Box<dyn Error>> {
40+
let args = Args::parse();
41+
let serialportname: String = args.serialport;
42+
let baudrate: u32 = args.baudrate;
43+
let id: u8 = args.id;
44+
let amplitude: f32 = args.amplitude;
45+
let frequency: f32 = args.frequency;
46+
47+
//print all the argument values
48+
println!("serialport: {}", serialportname);
49+
println!("baudrate: {}", baudrate);
50+
println!("id: {}", id);
51+
println!("amplitude: {}", amplitude);
52+
println!("frequency: {}", frequency);
53+
let term = Arc::new(AtomicBool::new(false));
54+
55+
flag::register(signal_hook::consts::SIGINT, Arc::clone(&term))?;
56+
57+
let mut serial_port = serialport::new(serialportname, baudrate)
58+
.timeout(Duration::from_millis(10))
59+
.open()?;
60+
println!("serial port opened");
61+
62+
let io = DynamixelSerialIO::v1();
63+
64+
let x: i16 = mx::read_present_position(&io, serial_port.as_mut(), id)?;
65+
println!("present pos: {}", x);
66+
67+
mx::write_torque_enable(&io, serial_port.as_mut(), id, 1)?;
68+
69+
let now = SystemTime::now();
70+
while !term.load(Ordering::Relaxed) {
71+
let t = now.elapsed().unwrap().as_secs_f32();
72+
let target = amplitude * (2.0 * PI * frequency * t).sin().to_radians();
73+
println!("target: {}", target);
74+
mx::write_goal_position(
75+
&io,
76+
serial_port.as_mut(),
77+
id,
78+
radians_to_dxl_pos(target.into()),
79+
)?;
80+
81+
thread::sleep(Duration::from_millis(10));
82+
}
83+
// mx::write_torque_enable(&io, serial_port.as_mut(), id, false)?;
84+
mx::write_torque_enable(&io, serial_port.as_mut(), id, 0)?;
85+
86+
Ok(())
87+
}

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)