forked from HULKs/hulk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooster_walking.rs
More file actions
92 lines (77 loc) · 3.13 KB
/
Copy pathbooster_walking.rs
File metadata and controls
92 lines (77 loc) · 3.13 KB
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
use booster::{ImuState, MotorState};
use color_eyre::Result;
use context_attribute::context;
use framework::{AdditionalOutput, MainOutput};
use hardware::PathsInterface;
use serde::{Deserialize, Serialize};
use types::{
cycle_time::CycleTime,
joints::Joints,
motion_command::MotionCommand,
parameters::{MotorCommandParameters, RLWalkingParameters},
};
use walking_inference::{inference::WalkingInference, inputs::WalkingInferenceInputs};
#[derive(Deserialize, Serialize)]
pub struct RLWalking {
walking_inference: WalkingInference,
smoothed_target_joint_positions: Joints,
}
#[context]
pub struct CreationContext {
prepare_motor_command_parameters: Parameter<MotorCommandParameters, "prepare_motor_command">,
hardware_interface: HardwareInterface,
}
#[context]
pub struct CycleContext {
walking_parameters: Parameter<RLWalkingParameters, "rl_walking">,
common_motor_command_parameters: Parameter<MotorCommandParameters, "common_motor_command">,
walking_inference_inputs: AdditionalOutput<WalkingInferenceInputs, "walking_inference_inputs">,
imu_state: Input<ImuState, "imu_state">,
serial_motor_states: Input<Joints<MotorState>, "serial_motor_states">,
cycle_time: Input<CycleTime, "cycle_time">,
motion_command: Input<MotionCommand, "motion_command">,
}
#[context]
#[derive(Default)]
pub struct MainOutputs {
pub target_joint_positions: MainOutput<Joints>,
}
impl RLWalking {
pub fn new(context: CreationContext<impl PathsInterface>) -> Result<Self> {
let paths = context.hardware_interface.get_paths();
let neural_network_folder = paths.neural_networks;
let walking_inference = WalkingInference::new(
&neural_network_folder,
context.prepare_motor_command_parameters,
)?;
Ok(Self {
walking_inference,
smoothed_target_joint_positions: context
.prepare_motor_command_parameters
.default_positions,
})
}
pub fn cycle(&mut self, mut context: CycleContext) -> Result<MainOutputs> {
let (walking_inference_inputs, inference_output_positions) =
self.walking_inference.do_inference(
*context.cycle_time,
context.motion_command,
context.imu_state,
*context.serial_motor_states,
context.walking_parameters,
context.common_motor_command_parameters,
)?;
context
.walking_inference_inputs
.fill_if_subscribed(|| walking_inference_inputs.clone());
let target_joint_positions = context.common_motor_command_parameters.default_positions
+ inference_output_positions * context.walking_parameters.control.action_scale;
self.smoothed_target_joint_positions = self.smoothed_target_joint_positions
* context.walking_parameters.joint_position_smoothing_factor
+ target_joint_positions
* (1.0 - context.walking_parameters.joint_position_smoothing_factor);
Ok(MainOutputs {
target_joint_positions: self.smoothed_target_joint_positions.into(),
})
}
}