-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathlib.rs
More file actions
242 lines (223 loc) · 8.16 KB
/
Copy pathlib.rs
File metadata and controls
242 lines (223 loc) · 8.16 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::{boxed::Box, future::Future, pin::Pin};
use std::{sync::Arc, time::SystemTime};
use color_eyre::Result;
use coordinate_systems::{Field, Ground};
use filtering::hysteresis::greater_than_with_hysteresis;
use hsl_network_messages::{GamePhase, SubState, Team};
use linear_algebra::{Isometry2, Point2, Vector2, point};
use ros_z::{prelude::*, qos::QosDurability};
use types::{
ball_position::BallPosition,
field_dimensions::{FieldDimensions, Side},
filtered_game_controller_state::FilteredGameControllerState,
primary_state::PrimaryState,
world_state::{BallSource, BallState, LastBallState},
};
pub fn run_boxed(ctx: Arc<Context>) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
Box::pin(run(ctx))
}
async fn run(ctx: Arc<Context>) -> Result<()> {
let node = ctx.create_node("ball_state_composer").build().await?;
let field_dimensions_cache = node
.subscriber::<FieldDimensions>("field_dimensions")
.qos(QosProfile {
durability: QosDurability::TransientLocal,
..Default::default()
})
.cache(1)
.build()
.await?;
let ball_position_sub = node
.subscriber::<Option<BallPosition<Ground>>>("ball_filter/ball_position")
.build()
.await?;
let ground_to_field_cache = node
.subscriber::<Isometry2<Ground, Field>>("ground_to_field")
.cache(10)
.build()
.await?;
let team_ball_sub = node
.subscriber::<BallPosition<Field>>("team_ball")
.build()
.await?;
let primary_state_cache = node
.subscriber::<PrimaryState>("primary_state")
.qos(QosProfile {
durability: QosDurability::TransientLocal,
..Default::default()
})
.cache(10)
.build()
.await?;
let filtered_game_controller_state_sub = node
.subscriber::<FilteredGameControllerState>("filtered_game_controller_state")
.build()
.await?;
let additional_last_ball_state_pub = node
.publisher::<Option<LastBallState>>("last_ball_state")
.build()
.await?;
let ball_state_pub = node
.publisher::<Option<BallState>>("ball_state")
.build()
.await?;
let rule_ball_state_pub = node
.publisher::<Option<BallState>>("rule_ball_state")
.build()
.await?;
let mut last_ball_field_side = Side::Left;
let mut last_ball_state = None;
loop {
let now = node.clock().now().to_wallclock();
additional_last_ball_state_pub
.publish_if_subscribed(|| async { last_ball_state })
.await?;
tokio::select! {
received_ball_position = ball_position_sub.recv() => {
let Some(ball_position) = received_ball_position? else {
ball_state_pub.publish(&None).await?;
last_ball_state = None;
continue;
};
let Some(ground_to_field) = ground_to_field_cache.get_latest() else {
continue;
};
let ground_to_field = *ground_to_field;
let ball = create_ball_state(
ball_position.position,
ground_to_field * ball_position.position,
ball_position.velocity,
ball_position.last_seen.to_wallclock(),
BallSource::Own,
&mut last_ball_field_side,
);
ball_state_pub.publish(&Some(ball)).await?;
last_ball_state = Some(LastBallState {
time: now,
ball,
});
}
received_team_ball = team_ball_sub.recv() => {
let team_ball = received_team_ball?;
let Some(ground_to_field) = ground_to_field_cache.get_latest() else {
continue;
};
let ground_to_field = *ground_to_field;
let ball = create_ball_state(
ground_to_field.inverse() * team_ball.position,
team_ball.position,
ground_to_field.inverse() * team_ball.velocity,
team_ball.last_seen.to_wallclock(),
BallSource::Team,
&mut last_ball_field_side,
);
ball_state_pub.publish(&Some(ball)).await?;
last_ball_state = Some(LastBallState {
time: now,
ball,
});
}
received_filtered_game_controller_state = filtered_game_controller_state_sub.recv() => {
let filtered_game_controller_state = received_filtered_game_controller_state?;
let Some(field_dimensions) = field_dimensions_cache.get_latest() else {
continue;
};
let field_dimensions = *field_dimensions;
let Some(ground_to_field) = ground_to_field_cache.get_latest() else {
continue;
};
let ground_to_field = *ground_to_field;
let Some(primary_state) = primary_state_cache.get_latest() else { continue };
let rule_ball = compose_rule_ball_state(
*primary_state,
ground_to_field,
Some(&filtered_game_controller_state),
field_dimensions,
now,
&mut last_ball_field_side,
);
rule_ball_state_pub.publish(&rule_ball).await?;
}
}
}
}
fn compose_rule_ball_state(
primary_state: PrimaryState,
ground_to_field: Isometry2<Ground, Field>,
filtered_game_controller_state: Option<&FilteredGameControllerState>,
field_dimensions: FieldDimensions,
cycle_start_time: SystemTime,
last_ball_field_side: &mut Side,
) -> Option<BallState> {
match (primary_state, filtered_game_controller_state) {
(
PrimaryState::Ready | PrimaryState::Set,
Some(FilteredGameControllerState {
sub_state: Some(SubState::PenaltyKick),
kicking_team,
..
})
| Some(FilteredGameControllerState {
game_phase:
GamePhase::PenaltyShootout {
kicking_team: Team::Hulks,
},
kicking_team,
..
}),
) => {
let side_factor = match kicking_team {
Some(Team::Opponent) => -1.0,
Some(Team::Hulks) => 1.0,
_ => -1.0,
};
let penalty_spot_x =
field_dimensions.length / 2.0 - field_dimensions.penalty_marker_distance;
let penalty_spot_location = point![side_factor * penalty_spot_x, 0.0];
Some(create_ball_state(
ground_to_field.inverse() * penalty_spot_location,
penalty_spot_location,
Vector2::zeros(),
cycle_start_time,
BallSource::Own,
last_ball_field_side,
))
}
(PrimaryState::Ready, _) => Some(create_ball_state(
ground_to_field.inverse() * Point2::origin(),
Point2::origin(),
Vector2::zeros(),
cycle_start_time,
BallSource::Own,
last_ball_field_side,
)),
_ => None,
}
}
fn create_ball_state(
ball_in_ground: Point2<Ground>,
ball_in_field: Point2<Field>,
ball_in_ground_velocity: Vector2<Ground>,
last_seen_ball: SystemTime,
source: BallSource,
last_ball_field_side: &mut Side,
) -> BallState {
let was_in_left_half = *last_ball_field_side == Side::Left;
let is_in_left_half =
greater_than_with_hysteresis(was_in_left_half, ball_in_field.y(), 0.0, 0.2);
let side = if is_in_left_half {
Side::Left
} else {
Side::Right
};
*last_ball_field_side = side;
let field_side = side;
BallState {
ball_in_ground,
ball_in_field,
ball_in_ground_velocity,
last_seen_ball,
field_side,
source,
}
}