Skip to content

Commit 06727b5

Browse files
authored
Merge pull request #44 from pollen-robotics/support-poulpe
Support poulpe
2 parents 317a7ee + 781de4e commit 06727b5

File tree

14 files changed

+1224
-29
lines changed

14 files changed

+1224
-29
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustypot"
3-
version = "0.4.2"
3+
version = "0.5.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
authors = ["Pollen Robotics"]
@@ -10,6 +10,7 @@ repository = "https://github.com/pollen-robotics/rustypot"
1010
readme = "README.md"
1111
keywords = ["robotics", "dynamixel"]
1212
categories = ["science::robotics"]
13+
#autoexamples = false #disable auto discovery of examples
1314

1415

1516
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -19,6 +20,7 @@ log = "0.4.17"
1920
paste = "1.0.10"
2021
serialport = "4.2.0"
2122
clap = { version = "4.0.32", features = ["derive"] }
23+
proc-macro2 = { version = "=1.0.67", features=["default", "proc-macro"] }
2224

2325
[dev-dependencies]
2426
env_logger = "0.10.0"

Changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Version 0.5.0
2+
3+
- Add an post delay option to the read and write method.
4+
- Add dxl XM motor device
5+
16
## Version 0.4.0
27

38
- Add support for orbita-2dof-foc device.
@@ -18,4 +23,4 @@
1823

1924
- Support protocol v1 and v2
2025
- Support read, sync read, write, sync write, ping
21-
- Support mx and xl-320
26+
- Support mx and xl-320

examples/foc.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,67 @@ fn main() -> Result<(), Box<dyn Error>> {
1010
.timeout(Duration::from_millis(100))
1111
.open()?;
1212

13-
let io = DynamixelSerialIO::v1();
13+
let io = DynamixelSerialIO::v1().with_post_delay(Duration::from_millis(1));
1414

1515
let id = 70;
16-
orbita_foc::write_torque_enable(&io, serial_port.as_mut(), id, 0)?;
16+
let mut state = orbita_foc::read_motors_drivers_states(&io, serial_port.as_mut(), id)?;
17+
println!("state {:#010b}", state);
18+
println!("state {:?}", state);
19+
20+
let fw = orbita_foc::read_firmware_version(&io, serial_port.as_mut(), id)?;
21+
println!("Firmware version {:?}", fw);
22+
23+
orbita_foc::write_torque_enable(&io, serial_port.as_mut(), id, 1)?;
24+
thread::sleep(Duration::from_millis(1000));
25+
// orbita_foc::write_torque_enable(&io, serial_port.as_mut(), id, 0)?;
1726

18-
thread::sleep(Duration::from_millis(100));
27+
// thread::sleep(Duration::from_millis(1000));
28+
// orbita_foc::write_torque_enable(&io, serial_port.as_mut(), id, 1)?;
1929

30+
// thread::sleep(Duration::from_millis(1000));
31+
32+
state = orbita_foc::read_motors_drivers_states(&io, serial_port.as_mut(), id)?;
33+
println!("state {:#010b}", state);
2034
let now = SystemTime::now();
2135
// let x = io.ping(serial_port.as_mut(), id);
2236
// println!("{:?}", x);
23-
loop {
37+
let mut total = 0.0;
38+
39+
while total < 10.0 {
2440
// let x = io.ping(serial_port.as_mut(), id);
2541
// println!("{:?}", x);
2642

2743
let pos = orbita_foc::read_present_position(&io, serial_port.as_mut(), id)?;
28-
println!("{:?}", pos);
44+
45+
let tophall = orbita_foc::read_top_present_hall(&io, serial_port.as_mut(), id)?;
46+
let midhall = orbita_foc::read_middle_present_hall(&io, serial_port.as_mut(), id)?;
47+
let bothall = orbita_foc::read_bottom_present_hall(&io, serial_port.as_mut(), id)?;
48+
println!(
49+
"{:?} tophall: {:?} midhal: {:?} bothall: {:?}",
50+
pos, tophall, midhall, bothall
51+
);
2952

3053
let t = now.elapsed().unwrap().as_secs_f32();
31-
let target = 60.0_f32 * (2.0 * PI * 0.5 * t).sin();
32-
orbita_foc::write_top_goal_position(&io, serial_port.as_mut(), id, target)?;
33-
// println!("{}", t);
54+
let target = 10.0_f32 * (2.0 * PI * 0.25 * t).sin();
3455

3556
orbita_foc::write_goal_position(
3657
&io,
3758
serial_port.as_mut(),
3859
id,
3960
DiskValue {
40-
top: target,
41-
middle: target,
42-
bottom: target,
61+
top: target.to_radians() + pos.top,
62+
middle: target.to_radians() + pos.middle,
63+
bottom: target.to_radians() + pos.bottom,
64+
// top: pos.top,
65+
// middle: pos.middle,
66+
// bottom: pos.bottom,
4367
},
4468
)?;
45-
4669
thread::sleep(Duration::from_millis(10));
70+
total += 0.01;
4771
}
72+
orbita_foc::write_torque_enable(&io, serial_port.as_mut(), id, 0)?;
73+
74+
thread::sleep(Duration::from_millis(1000));
75+
Ok(())
4876
}

examples/poulpe2d.rs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
use std::time::SystemTime;
2+
use std::{error::Error, thread, time::Duration, time::Instant};
3+
4+
use rustypot::device::orbita2d_poulpe::{self, MotorValue};
5+
// use rustypot::device::orbita3d_poulpe::{self, MotorValue};
6+
use rustypot::DynamixelSerialIO;
7+
8+
use clap::Parser;
9+
10+
#[derive(Parser, Debug)]
11+
#[command(author, version, about, long_about = None)]
12+
struct Args {
13+
/// tty
14+
#[arg(short, long, default_value = "/dev/ttyUSB0")]
15+
serialport: String,
16+
/// baud
17+
#[arg(short, long, default_value_t = 2_000_000)]
18+
baudrate: u32,
19+
20+
/// id
21+
#[arg(short, long, default_value_t = 43)]
22+
id: u8,
23+
24+
///sinus amplitude (f64)
25+
#[arg(short, long, default_value_t = 1.0)]
26+
amplitude: f32,
27+
28+
///sinus frequency (f64)
29+
#[arg(short, long, default_value_t = 1.0)]
30+
frequency: f32,
31+
}
32+
33+
fn main() -> Result<(), Box<dyn Error>> {
34+
let args = Args::parse();
35+
let serialportname: String = args.serialport;
36+
let baudrate: u32 = args.baudrate;
37+
let id: u8 = args.id;
38+
let amplitude: f32 = args.amplitude;
39+
let frequency: f32 = args.frequency;
40+
41+
//print the standard ids for the arm motors
42+
43+
//print all the argument values
44+
println!("serialport: {}", serialportname);
45+
println!("baudrate: {}", baudrate);
46+
println!("id: {}", id);
47+
println!("amplitude: {}", amplitude);
48+
println!("frequency: {}", frequency);
49+
50+
let mut serial_port = serialport::new(serialportname, baudrate)
51+
.timeout(Duration::from_millis(10))
52+
.open()?;
53+
54+
let now = SystemTime::now();
55+
56+
let io = DynamixelSerialIO::v1();
57+
58+
// Ping
59+
let x = io.ping(serial_port.as_mut(), id);
60+
println!("Ping {:?}: {:?}", id, x);
61+
thread::sleep(Duration::from_millis(100));
62+
63+
let _ = orbita2d_poulpe::write_torque_enable(
64+
&io,
65+
serial_port.as_mut(),
66+
id,
67+
MotorValue::<bool> {
68+
motor_a: true,
69+
motor_b: true,
70+
},
71+
)?;
72+
thread::sleep(Duration::from_millis(1000));
73+
// let torque = orbita3d_poulpe::read_torque_enable(&io, serial_port.as_mut(), id)?;
74+
// println!("torque: {:?}", torque);
75+
// thread::sleep(Duration::from_millis(1000));
76+
77+
let curr_pos = orbita2d_poulpe::read_current_position(&io, serial_port.as_mut(), id)?;
78+
79+
println!("curr_pos: {:?} {:?}", curr_pos.motor_a, curr_pos.motor_b);
80+
81+
// let index_sensor = orbita3d_poulpe::read_index_sensor(&io, serial_port.as_mut(), id)?;
82+
// println!("index_sensor: {:?} {:?} {:?}", index_sensor.top, index_sensor.middle, index_sensor.bottom);
83+
84+
let mut t = now.elapsed().unwrap().as_secs_f32();
85+
let mut nberr = 0;
86+
let mut nbtot = 0;
87+
let mut nbok = 0;
88+
let mut target = 0.0;
89+
loop {
90+
let t0 = Instant::now();
91+
92+
if t > 3.0 {
93+
break;
94+
}
95+
96+
t = now.elapsed().unwrap().as_secs_f32();
97+
// let target = amplitude * 180.0_f32.to_radians() * (2.0 * PI * 0.5 * t).sin();
98+
target += 0.001;
99+
// let feedback = orbita2d_poulpe::write_target_position(&io, serial_port.as_mut(), id, MotorValue::<f32>{motor_a:target+curr_pos.motor_a, motor_b:target+curr_pos.motor_b});
100+
101+
// let feedback = orbita2d_poulpe::write_target_position(
102+
// &io,
103+
// serial_port.as_mut(),
104+
// id,
105+
// MotorValue::<f32> {
106+
// motor_a: target + curr_pos.motor_a,
107+
// motor_b: curr_pos.motor_b,
108+
// },
109+
// );
110+
111+
let feedback = orbita2d_poulpe::write_target_position(
112+
&io,
113+
serial_port.as_mut(),
114+
id,
115+
MotorValue::<f32> {
116+
motor_a: target + curr_pos.motor_a,
117+
motor_b: target + curr_pos.motor_b,
118+
},
119+
);
120+
121+
match feedback {
122+
Ok(feedback) => {
123+
nbok += 1;
124+
println!(
125+
"42 target: {} feedback pos: {} {}",
126+
target, feedback.position.motor_a, feedback.position.motor_b,
127+
);
128+
}
129+
Err(e) => {
130+
nberr += 1;
131+
println!("error: {:?}", e);
132+
}
133+
}
134+
135+
nbtot += 1;
136+
137+
// thread::sleep(Duration::from_micros(1000-t0.elapsed().as_micros() as u64));
138+
// thread::sleep(Duration::from_millis(1));
139+
thread::sleep(Duration::from_micros(500));
140+
// thread::sleep(Duration::from_micros(4500));
141+
println!("ELAPSED: {:?}", t0.elapsed().as_micros());
142+
}
143+
144+
println!(
145+
"nberr: {} nbtot: {} nbok: {} ratio: {:?}",
146+
nberr,
147+
nbtot,
148+
nbok,
149+
nbok as f32 / nbtot as f32
150+
);
151+
152+
println!("STOP");
153+
let _ = orbita2d_poulpe::write_torque_enable(
154+
&io,
155+
serial_port.as_mut(),
156+
id,
157+
MotorValue::<bool> {
158+
motor_a: false,
159+
motor_b: false,
160+
},
161+
)?;
162+
163+
thread::sleep(Duration::from_millis(2000));
164+
165+
Ok(())
166+
}

0 commit comments

Comments
 (0)