|
1 | 1 | use std::f32::consts::PI; |
2 | 2 |
|
3 | | -use color_eyre::Result; |
4 | | -use context_attribute::context; |
5 | 3 | use coordinate_systems::Ground; |
6 | | -use framework::MainOutput; |
7 | 4 | use linear_algebra::{Rotation2, Vector2}; |
8 | | -use serde::{Deserialize, Serialize}; |
9 | | -use types::ball_position::BallPosition; |
10 | | -use types::motion_command::{HeadMotion, ImageRegion, MotionCommand}; |
11 | | -use types::parameters::WalkWithVelocityParameters; |
12 | | - |
13 | | -#[derive(Deserialize, Serialize)] |
14 | | -pub struct WalkToBall {} |
15 | | - |
16 | | -#[context] |
17 | | -pub struct CreationContext {} |
18 | | - |
19 | | -#[context] |
20 | | -pub struct CycleContext { |
21 | | - ball_position: Input<Option<BallPosition<Ground>>, "ball_position?">, |
22 | | - |
23 | | - walk_with_velocity_parameter: |
24 | | - Parameter<WalkWithVelocityParameters, "behavior.walk_with_velocity">, |
25 | | -} |
26 | | - |
27 | | -#[context] |
28 | | -#[derive(Default)] |
29 | | -pub struct MainOutputs { |
30 | | - pub motion_command: MainOutput<MotionCommand>, |
31 | | -} |
32 | | - |
33 | | -impl WalkToBall { |
34 | | - pub fn new(_context: CreationContext) -> Result<Self> { |
35 | | - Ok(Self {}) |
36 | | - } |
37 | | - |
38 | | - pub fn cycle(&mut self, context: CycleContext) -> Result<MainOutputs> { |
39 | | - let next_motion_command = match context.ball_position { |
40 | | - Some(ball_position) => { |
41 | | - let ball_coordinates_in_ground = ball_position.position.coords(); |
42 | | - let head = HeadMotion::LookAt { |
43 | | - target: ball_position.position, |
44 | | - image_region_target: ImageRegion::Center, |
45 | | - }; |
46 | | - let max_angular_velocity_abs = context |
47 | | - .walk_with_velocity_parameter |
48 | | - .max_angular_velocity |
49 | | - .abs(); |
50 | | - let normalized_angle_to_ball = |
51 | | - Rotation2::rotation_between(Vector2::x_axis(), ball_coordinates_in_ground) |
52 | | - .angle() |
53 | | - / (0.5 * PI); |
54 | | - MotionCommand::WalkWithVelocity { |
55 | | - head, |
56 | | - velocity: ball_coordinates_in_ground.normalize() |
57 | | - * context.walk_with_velocity_parameter.max_velocity, |
58 | | - angular_velocity: (normalized_angle_to_ball |
59 | | - * context |
60 | | - .walk_with_velocity_parameter |
61 | | - .angular_velocity_scaling_factor) |
62 | | - .clamp(-max_angular_velocity_abs, max_angular_velocity_abs), |
63 | | - } |
| 5 | +use types::{ |
| 6 | + ball_position::BallPosition, |
| 7 | + motion_command::{HeadMotion, ImageRegion, MotionCommand}, |
| 8 | + parameters::WalkWithVelocityParameters, |
| 9 | +}; |
| 10 | + |
| 11 | +pub fn execute( |
| 12 | + ball_position: Option<BallPosition<Ground>>, |
| 13 | + walk_with_velocity_parameter: WalkWithVelocityParameters, |
| 14 | +) -> Option<MotionCommand> { |
| 15 | + let next_motion_command = match ball_position { |
| 16 | + Some(ball_position) => { |
| 17 | + let ball_coordinates_in_ground = ball_position.position.coords(); |
| 18 | + let head = HeadMotion::LookAt { |
| 19 | + target: ball_position.position, |
| 20 | + image_region_target: ImageRegion::Center, |
| 21 | + }; |
| 22 | + let max_angular_velocity_abs = walk_with_velocity_parameter.max_angular_velocity.abs(); |
| 23 | + let normalized_angle_to_ball = |
| 24 | + Rotation2::rotation_between(Vector2::x_axis(), ball_coordinates_in_ground).angle() |
| 25 | + / (0.5 * PI); |
| 26 | + MotionCommand::WalkWithVelocity { |
| 27 | + head, |
| 28 | + velocity: ball_coordinates_in_ground.normalize() |
| 29 | + * walk_with_velocity_parameter.max_velocity, |
| 30 | + angular_velocity: (normalized_angle_to_ball |
| 31 | + * walk_with_velocity_parameter.angular_velocity_scaling_factor) |
| 32 | + .clamp(-max_angular_velocity_abs, max_angular_velocity_abs), |
64 | 33 | } |
65 | | - None => MotionCommand::Stand { |
66 | | - head: HeadMotion::Center, |
67 | | - }, |
68 | | - }; |
69 | | - |
70 | | - Ok(MainOutputs { |
71 | | - motion_command: next_motion_command.into(), |
72 | | - }) |
73 | | - } |
| 34 | + } |
| 35 | + None => MotionCommand::Stand { |
| 36 | + head: HeadMotion::Center, |
| 37 | + }, |
| 38 | + }; |
| 39 | + Some(next_motion_command) |
74 | 40 | } |
0 commit comments