Skip to content

Commit

Permalink
Add bindings for visual referee challenge results
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasselbring committed Apr 24, 2023
1 parent d11d012 commit 22f0e69
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions game_controller_msgs/headers/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@

static const size_t GAMECONTROLLER_STRUCT_SIZE = sizeof(struct RoboCupGameControlData);
static const size_t GAMECONTROLLER_RETURN_STRUCT_SIZE = sizeof(struct RoboCupGameControlReturnData);

static const uint8_t GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_MIN = GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_KICK_IN_BLUE_TEAM;
static const uint8_t GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_MAX = GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_SUBSTITUTION_RED_TEAM;
2 changes: 2 additions & 0 deletions game_controller_msgs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod bindings;
mod control_message;
mod monitor_request;
mod status_message;
mod vrc_message;

use bindings::{
GAMECONTROLLER_DATA_PORT, GAMECONTROLLER_RETURN_PORT, GAMECONTROLLER_RETURN_STRUCT_SIZE,
Expand Down Expand Up @@ -37,3 +38,4 @@ pub const TEAM_MESSAGE_PORT_BASE: u16 = 10000;
pub use control_message::ControlMessage;
pub use monitor_request::MonitorRequest;
pub use status_message::StatusMessage;
pub use vrc_message::VrcMessage;
69 changes: 69 additions & 0 deletions game_controller_msgs/src/vrc_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use anyhow::{bail, Error};
use bytes::{Buf, Bytes};

use crate::bindings::{
GAMECONTROLLER_RETURN_STRUCT_HEADER, GAMECONTROLLER_RETURN_STRUCT_SIZE,
GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_MAX, GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_MIN,
GAMECONTROLLER_RETURN_STRUCT_VRC_VERSION, MAX_NUM_PLAYERS,
};

/// This struct corresponds to `RoboCupGameControlReturnData`, modified for the visual referee
/// challenge result reports. `RoboCupGameControlReturnData::header`,
/// `RoboCupGameControlReturnData::version`, `RoboCupGameControlReturnData::pose`, and
/// `RoboCupGameControlReturnData::ball` are implicitly added/removed when converting to/from the
/// binary format.
pub struct VrcMessage {
/// This field corresponds to `RoboCupGameControlReturnData::playerNum`.
pub player_number: u8,
/// This field corresponds to `RoboCupGameControlReturnData::teamNum`.
pub team_number: u8,
/// This field corresponds to `RoboCupGameControlReturnData::fallen`.
pub gesture: u8,
/// This field corresponds to `RoboCupGameControlReturnData::ballAge`.
pub whistle_age: f32,
}

impl TryFrom<Bytes> for VrcMessage {
type Error = Error;

fn try_from(mut bytes: Bytes) -> Result<Self, Self::Error> {
if bytes.len() != GAMECONTROLLER_RETURN_STRUCT_SIZE {
bail!("wrong length");
}
let header = bytes.copy_to_bytes(4);
if header != GAMECONTROLLER_RETURN_STRUCT_HEADER[..4] {
bail!("wrong header");
}
let version = bytes.get_u8();
if version != GAMECONTROLLER_RETURN_STRUCT_VRC_VERSION {
bail!("wrong version");
}
let player_number = bytes.get_u8();
if !(1..=MAX_NUM_PLAYERS).contains(&player_number) {
bail!("invalid player number");
}
let team_number = bytes.get_u8();
let gesture = bytes.get_u8();
if !(GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_MIN
..=GAMECONTROLLER_RETURN_STRUCT_VRC_GESTURE_MAX)
.contains(&gesture)
{
bail!("invalid gesture");
}
// ignore pose
let _ = [bytes.get_f32_le(), bytes.get_f32_le(), bytes.get_f32_le()];
let whistle_age = bytes.get_f32_le();
if whistle_age.is_nan() {
bail!("invalid whistle age");
}
// ignore ball
let _ = [bytes.get_f32_le(), bytes.get_f32_le()];
assert!(!bytes.has_remaining());
Ok(VrcMessage {
player_number,
team_number,
gesture,
whistle_age,
})
}
}

0 comments on commit 22f0e69

Please sign in to comment.