|
| 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