Skip to content

Commit aac24ea

Browse files
committed
Receiving udp packets successfully and also using bitflags now.
1 parent 21a1697 commit aac24ea

File tree

6 files changed

+61
-36
lines changed

6 files changed

+61
-36
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ authors = ["Jack Greenberg <[email protected]>"]
66
[dependencies]
77
chrono = "0.4.6"
88
byteorder = "1.2.7"
9+
bitflags = "1.0.4"

src/connection.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ impl DSConnection {
3131

3232
let t = thread::spawn(move || {
3333
println!("udp start");
34-
let udp = UdpSocket::bind("169.254.65.205:1150").unwrap();
35-
println!("udp 2");
34+
let udp = UdpSocket::bind("169.254.65.205:1149").unwrap();
35+
3636
udp.connect(SocketAddr::new(addr.clone(), 1110)).unwrap();
37-
udp.set_nonblocking(true).unwrap();
37+
println!("udp 2");
38+
let udp_recv = UdpSocket::bind("169.254.65.205:1150").unwrap();
39+
udp_recv.set_nonblocking(true).unwrap();
3840
println!("udp started");
3941

4042
udp.send(state.lock().unwrap().udp_packet().as_ref())
@@ -51,16 +53,15 @@ impl DSConnection {
5153
}
5254

5355
let mut udp_buf = vec![0u8; 100];
54-
match udp.recv_from(&mut udp_buf) {
55-
Ok((n, f)) => println!("packet received: {:?}", udp_buf),
56+
match udp_recv.recv_from(&mut udp_buf) {
57+
Ok((n, f)) => if let Some(packet) = RioUdpPacket::from_bytes(Vec::from(&udp_buf[0..n])) {
58+
state.lock().unwrap().update_from_udp(packet);
59+
},
5660
Err(e) => {
5761
if e.kind() != io::ErrorKind::WouldBlock {
5862
if let Err(e) = sender_res.send(Err(e)) {
5963
break;
6064
}
61-
println!("uh oh");
62-
} else {
63-
println!("got nothing");
6465
}
6566
}
6667
}

src/ds.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl DriverStationState {
3232
self.sequence_num += 1;
3333

3434
packet.write_u8(0x01); // comm version
35-
packet.write_u8(self.control_byte()); // control byte
35+
packet.write_u8(self.control_byte().bits()); // control byte
3636
packet.write_u8(0); // TODO: actually restart code or rio with this byte.
3737
packet.write_u8(self.alliance.to_position_u8()); // alliance
3838

@@ -64,17 +64,17 @@ impl DriverStationState {
6464
packet.into_vec()
6565
}
6666

67-
fn control_byte(&self) -> u8 {
68-
let mut byte: u8 = 0;
67+
fn control_byte(&self) -> Control {
68+
let mut byte = Control::empty();
6969
if self.estop {
70-
byte |= 0b1000_0000;
70+
byte |= Control::ESTOP;
7171
}
7272
// fms is never connected, but if it were that would go here
7373
if self.enabled {
74-
byte |= 0b0000_0100;
74+
byte |= Control::ENABLED;
7575
}
7676

77-
byte |= self.mode as u8;
77+
byte |= Control::from_bits(self.mode as u8).unwrap();
7878

7979
byte
8080
}
@@ -121,3 +121,29 @@ impl Default for DriverStationState {
121121
}
122122
}
123123
}
124+
125+
bitflags! {
126+
pub struct Control: u8 {
127+
const ESTOP = 0b1000_0000;
128+
const FMS_CONNECTED = 0b0000_1000;
129+
const ENABLED = 0b0000_0100;
130+
131+
const TELEOP = 0b00;
132+
const TEST = 0b01;
133+
const AUTO = 0b10;
134+
}
135+
}
136+
137+
impl Control {
138+
pub fn robot_mode(&self) -> Option<RobotMode> {
139+
return if self.contains(Control::AUTO) {
140+
Some(RobotMode::Auto)
141+
} else if self.contains(Control::TELEOP) {
142+
Some(RobotMode::Teleop)
143+
} else if self.contains(Control::TEST) {
144+
Some(RobotMode::Test)
145+
} else {
146+
None
147+
}
148+
}
149+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate byteorder;
22
extern crate chrono;
3+
#[macro_use]
4+
extern crate bitflags;
35

46
use byteorder::{ByteOrder, NetworkEndian};
57
use chrono::prelude::*;

src/messages.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@ use std::convert::From;
33
use packet::PacketReader;
44
use states::RobotMode;
55

6-
pub struct Trace {
7-
robot_code: bool,
8-
is_roborio: bool,
9-
// these modes don't seem to quite line up with what we send
10-
test_mode: bool,
11-
auto_mode: bool,
12-
teleop_code: bool,
13-
disabled: bool,
14-
}
15-
16-
impl From<u8> for Trace {
17-
fn from(byte: u8) -> Self {
18-
Trace {
19-
robot_code: byte & 0b0010_0000 != 0,
20-
is_roborio: byte & 0b0001_0000 != 0,
21-
test_mode: byte & 0b0000_1000 != 0,
22-
auto_mode: byte & 0b0000_0100 != 0,
23-
teleop_code: byte & 0b0000_0010 != 0,
24-
disabled: byte & 0b0000_0001 != 0,
25-
}
26-
}
6+
bitflags! {
7+
pub struct Trace: u8 {
8+
const ROBOT_CODE = 0b0010_0000;
9+
const IS_ROBORIO = 0b0001_0000;
10+
const TEST_MODE = 0b0000_1000;
11+
const AUTO_MODE = 0b0000_0100;
12+
const TELEOP_CODE = 0b0000_0010;
13+
const DISABLED = 0b0000_0001;
14+
}
2715
}
2816

2917
pub struct Status {
@@ -66,7 +54,7 @@ impl RioUdpPacket {
6654
sequence_num: packet.next_u16().unwrap(),
6755
comm_version: packet.next_u8().unwrap(),
6856
status: packet.next_u8().unwrap().into(),
69-
trace: packet.next_u8().unwrap().into(),
57+
trace: Trace::from_bits(packet.next_u8().unwrap()).unwrap(),
7058
battery_voltage: f32::from(packet.next_u8().unwrap())
7159
+ f32::from(packet.next_u8().unwrap()) / 256.0,
7260
request_date: packet.next_u8().unwrap() == 0x01,

0 commit comments

Comments
 (0)