|
| 1 | +use color_eyre::Result; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use context_attribute::context; |
| 5 | +use coordinate_systems::Robot; |
| 6 | +use filtering::low_pass_filter::LowPassFilter; |
| 7 | +use framework::deserialize_not_implemented; |
| 8 | +use framework::MainOutput; |
| 9 | +use hardware::PathsInterface; |
| 10 | +use linear_algebra::Vector3; |
| 11 | +use motionfile::{InterpolatorState, MotionFile, MotionInterpolator}; |
| 12 | +use types::{ |
| 13 | + condition_input::ConditionInput, |
| 14 | + cycle_time::CycleTime, |
| 15 | + joints::Joints, |
| 16 | + motion_selection::{MotionSafeExits, MotionSelection, MotionType}, |
| 17 | + stand_up::RemainingStandUpDuration, |
| 18 | +}; |
| 19 | + |
| 20 | +#[derive(Deserialize, Serialize)] |
| 21 | +pub struct StandUpFrontSlow { |
| 22 | + #[serde(skip, default = "deserialize_not_implemented")] |
| 23 | + interpolator: MotionInterpolator<Joints<f32>>, |
| 24 | + state: InterpolatorState<Joints<f32>>, |
| 25 | + filtered_gyro: LowPassFilter<nalgebra::Vector3<f32>>, |
| 26 | +} |
| 27 | + |
| 28 | +#[context] |
| 29 | +pub struct CreationContext { |
| 30 | + hardware_interface: HardwareInterface, |
| 31 | + gyro_low_pass_factor: Parameter<f32, "stand_up_front_slow.gyro_low_pass_factor">, |
| 32 | +} |
| 33 | + |
| 34 | +#[context] |
| 35 | +pub struct CycleContext { |
| 36 | + leg_balancing_factor: |
| 37 | + Parameter<nalgebra::Vector2<f32>, "stand_up_front_slow.leg_balancing_factor">, |
| 38 | + speed_factor: Parameter<f32, "stand_up_front_slow.speed_factor">, |
| 39 | + condition_input: Input<ConditionInput, "condition_input">, |
| 40 | + cycle_time: Input<CycleTime, "cycle_time">, |
| 41 | + motion_selection: Input<MotionSelection, "motion_selection">, |
| 42 | + angular_velocity: |
| 43 | + Input<Vector3<Robot>, "sensor_data.inertial_measurement_unit.angular_velocity">, |
| 44 | + |
| 45 | + motion_safe_exits: CyclerState<MotionSafeExits, "motion_safe_exits">, |
| 46 | + stand_up_front_slow_estimated_remaining_duration: |
| 47 | + CyclerState<RemainingStandUpDuration, "stand_up_front_slow_estimated_remaining_duration">, |
| 48 | +} |
| 49 | + |
| 50 | +#[context] |
| 51 | +#[derive(Default)] |
| 52 | +pub struct MainOutputs { |
| 53 | + pub stand_up_front_slow_positions: MainOutput<Joints<f32>>, |
| 54 | +} |
| 55 | + |
| 56 | +impl StandUpFrontSlow { |
| 57 | + pub fn new(context: CreationContext<impl PathsInterface>) -> Result<Self> { |
| 58 | + let paths = context.hardware_interface.get_paths(); |
| 59 | + Ok(Self { |
| 60 | + interpolator: MotionFile::from_path(paths.motions.join("stand_up_front.json"))? |
| 61 | + .try_into()?, |
| 62 | + state: InterpolatorState::INITIAL, |
| 63 | + filtered_gyro: LowPassFilter::with_smoothing_factor( |
| 64 | + nalgebra::Vector3::zeros(), |
| 65 | + *context.gyro_low_pass_factor, |
| 66 | + ), |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + pub fn cycle(&mut self, context: CycleContext) -> Result<MainOutputs> { |
| 71 | + let estimated_remaining_duration = if context.motion_selection.current_motion |
| 72 | + == MotionType::StandUpFrontSlow |
| 73 | + { |
| 74 | + let last_cycle_duration = context |
| 75 | + .cycle_time |
| 76 | + .last_cycle_duration |
| 77 | + .div_f32(*context.speed_factor); |
| 78 | + let condition_input = context.condition_input; |
| 79 | + |
| 80 | + self.interpolator |
| 81 | + .advance_state(&mut self.state, last_cycle_duration, condition_input); |
| 82 | + |
| 83 | + RemainingStandUpDuration::Running( |
| 84 | + self.interpolator |
| 85 | + .estimated_remaining_duration(self.state) |
| 86 | + .mul_f32(*context.speed_factor), |
| 87 | + ) |
| 88 | + } else { |
| 89 | + self.state.reset(); |
| 90 | + RemainingStandUpDuration::NotRunning |
| 91 | + }; |
| 92 | + context.motion_safe_exits[MotionType::StandUpFrontSlow] = self.state.is_finished(); |
| 93 | + |
| 94 | + self.filtered_gyro.update(context.angular_velocity.inner); |
| 95 | + let gyro = self.filtered_gyro.state(); |
| 96 | + |
| 97 | + let mut positions = self.interpolator.value(self.state); |
| 98 | + positions.left_leg.ankle_pitch += context.leg_balancing_factor.y * gyro.y; |
| 99 | + positions.left_leg.ankle_roll += context.leg_balancing_factor.x * gyro.x; |
| 100 | + positions.left_leg.hip_yaw_pitch += context.leg_balancing_factor.x * gyro.x; |
| 101 | + positions.right_leg.ankle_pitch += context.leg_balancing_factor.y * gyro.y; |
| 102 | + positions.right_leg.ankle_roll += context.leg_balancing_factor.x * gyro.x; |
| 103 | + positions.right_leg.hip_yaw_pitch += context.leg_balancing_factor.x * gyro.x; |
| 104 | + |
| 105 | + *context.stand_up_front_slow_estimated_remaining_duration = estimated_remaining_duration; |
| 106 | + |
| 107 | + Ok(MainOutputs { |
| 108 | + stand_up_front_slow_positions: positions.into(), |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments