Skip to content

Commit df5d0e0

Browse files
author
Uxugin
committed
write example for new device system
1 parent 809473f commit df5d0e0

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

examples/devices.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright 2024-2025 UxuginPython
3+
use rrtk::devices::provided::Differential;
4+
use rrtk::devices::*;
5+
use rrtk::*;
6+
#[cfg(not(feature = "devices"))]
7+
compile_error!("Enable the `devices` feature to run this example.");
8+
///Format [`AngularState`] like a row vector for ease of reading.
9+
fn format_state(state: AngularState) -> String {
10+
format!(
11+
"[ {} {} {} ]",
12+
state.position.into_inner(),
13+
state.velocity.into_inner(),
14+
state.acceleration.into_inner()
15+
)
16+
}
17+
fn main() {
18+
let mut system = System::<6>::new();
19+
let diff_left = system.new_node().unwrap();
20+
let diff_right = system.new_node().unwrap();
21+
let diff_top = system.new_node().unwrap();
22+
let enc_left = system.new_node().unwrap();
23+
let enc_right = system.new_node().unwrap();
24+
let demo_top = system.new_node().unwrap();
25+
system.connect(diff_left, enc_left);
26+
system.connect(diff_right, enc_right);
27+
system.connect(diff_top, demo_top);
28+
//The provided Differential device adds the states of its left and right nodes.
29+
let mut differential = Differential::new(diff_left, diff_right, diff_top);
30+
//Fake encoders by directly setting the states.
31+
system.set_state_local(
32+
enc_left,
33+
Some(AngularState::new(
34+
Dimensionless::new(1.0),
35+
InverseSecond::new(2.0),
36+
InverseSecondSquared::new(3.0),
37+
)),
38+
);
39+
system.set_state_local(
40+
enc_right,
41+
Some(AngularState::new(
42+
Dimensionless::new(4.0),
43+
InverseSecond::new(5.0),
44+
InverseSecondSquared::new(6.0),
45+
)),
46+
);
47+
//There's only one device in this example, and it's not actually a continuously updating
48+
//system, so we only update once. However, when you actually use the device system, you should
49+
//call device_update for all your devices in a loop. The order of the devices in this loop
50+
//shouldn't usually matter significantly, but you may want to experiment with it if you need
51+
//the utmost updating speed.
52+
differential.device_update(&mut system);
53+
//We directly set the encoder states, so we use get_state_local to access them.
54+
let enc_left_state = system.get_state_local(enc_left).unwrap();
55+
let enc_right_state = system.get_state_local(enc_right).unwrap();
56+
//demo_top is connected to diff_top, and diff_top is providing the state, so we use
57+
//get_state_connected. This is one of a few cases where get_state_true would also be fine to
58+
//use.
59+
let demo_state = system.get_state_connected(demo_top).unwrap();
60+
println!(
61+
"{} + {} = {}",
62+
format_state(enc_left_state),
63+
format_state(enc_right_state),
64+
format_state(demo_state)
65+
);
66+
}

0 commit comments

Comments
 (0)