1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
use super::control_node::ControlNode; use arci::gamepad::{Axis, Button, GamepadEvent}; use arci::{JointTrajectoryClient, Speaker}; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use std::time::Duration; const AXIS_GAIN: f64 = 2.0; const JOINT_POSITION_TURBO_GAIN: f64 = 2.0; pub struct JoyJointTeleopNode<J, S> where J: JointTrajectoryClient, S: Speaker, { joint_trajectory_client: J, speaker: S, mode: String, submode: String, joint_step: f64, velocity: f64, dof: usize, step_duration: Duration, joint_index: usize, is_turbo: bool, is_sending: bool, } impl<J, S> JoyJointTeleopNode<J, S> where J: JointTrajectoryClient, S: Speaker, { pub fn new( mode: String, joint_trajectory_client: J, joint_step: f64, step_duration: Duration, speaker: S, ) -> Self { let dof = joint_trajectory_client.joint_names().len(); Self { joint_trajectory_client, speaker, mode, dof, submode: "0".to_string(), joint_step, velocity: 0.0, step_duration, joint_index: 0, is_turbo: false, is_sending: false, } } pub fn new_from_config( config: JoyJointTeleopNodeConfig, joint_trajectory_client: J, speaker: S, ) -> Self { Self::new( config.mode, joint_trajectory_client, config.joint_step, Duration::from_secs_f64(config.step_duration_secs), speaker, ) } } #[async_trait] impl<N, S> ControlNode for JoyJointTeleopNode<N, S> where N: JointTrajectoryClient, S: Speaker, { fn set_event(&mut self, event: GamepadEvent) { match event { GamepadEvent::ButtonPressed(Button::East) => { self.joint_index = (self.joint_index + 1) % self.dof; self.submode = format!("{}", self.joint_index); self.speaker .speak(&format!("{}{}", self.mode, self.submode())); } GamepadEvent::ButtonPressed(Button::LeftTrigger2) => { self.is_turbo = true; } GamepadEvent::ButtonReleased(Button::LeftTrigger2) => { self.is_turbo = false; } GamepadEvent::ButtonPressed(Button::RightTrigger2) => { self.is_sending = true; } GamepadEvent::ButtonReleased(Button::RightTrigger2) => { self.is_sending = false; self.velocity = 0.0; } GamepadEvent::ButtonPressed(Button::West) => { self.velocity = self.joint_step; } GamepadEvent::ButtonReleased(Button::West) => { self.velocity = 0.0; } GamepadEvent::ButtonPressed(Button::South) => { self.velocity = -self.joint_step; } GamepadEvent::ButtonReleased(Button::South) => { self.velocity = 0.0; } GamepadEvent::AxisChanged(Axis::RightStickY, v) => { self.velocity = self.joint_step * v * AXIS_GAIN; } _ => {} } } async fn proc(&self) { if self.is_sending { let mut pos = self .joint_trajectory_client .current_joint_positions() .unwrap(); pos[self.joint_index] += self.velocity * if self.is_turbo { JOINT_POSITION_TURBO_GAIN } else { 1.0 }; self.joint_trajectory_client .send_joint_positions(pos, self.step_duration) .await .unwrap(); } } fn mode(&self) -> &str { &self.mode } fn submode(&self) -> &str { &self.submode } } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct JoyJointTeleopNodeConfig { pub mode: String, pub joint_step: f64, pub step_duration_secs: f64, }