devices/
devices.rs

1// SPDX-License-Identifier: BSD-3-Clause
2// Copyright 2024 UxuginPython
3#[cfg(all(feature = "devices", feature = "alloc"))]
4const COMMAND: Command = Command::new(PositionDerivative::Position, 5.0);
5#[cfg(all(feature = "devices", feature = "alloc"))]
6const STATE: State = State::new_raw(0.0, 0.0, 0.0);
7#[cfg(all(feature = "devices", feature = "alloc"))]
8const K_VALUES: PositionDerivativeDependentPIDKValues = PositionDerivativeDependentPIDKValues::new(
9    PIDKValues::new(1.0, 0.01, 0.1),
10    PIDKValues::new(1.0, 0.01, 0.1),
11    PIDKValues::new(1.0, 0.01, 0.1),
12);
13#[cfg(all(feature = "devices", feature = "alloc"))]
14use rrtk::*;
15#[cfg(all(feature = "devices", feature = "alloc"))]
16struct Motor {
17    settable_data: SettableData<f32, ()>,
18}
19#[cfg(all(feature = "devices", feature = "alloc"))]
20impl Motor {
21    fn new() -> Self {
22        Self {
23            settable_data: SettableData::new(),
24        }
25    }
26}
27#[cfg(all(feature = "devices", feature = "alloc"))]
28impl Settable<f32, ()> for Motor {
29    fn impl_set(&mut self, value: f32) -> NothingOrError<()> {
30        println!("Motor voltage set to {:?}", value);
31        Ok(())
32    }
33    fn get_settable_data_ref(&self) -> &SettableData<f32, ()> {
34        &self.settable_data
35    }
36    fn get_settable_data_mut(&mut self) -> &mut SettableData<f32, ()> {
37        &mut self.settable_data
38    }
39}
40#[cfg(all(feature = "devices", feature = "alloc"))]
41impl Updatable<()> for Motor {
42    fn update(&mut self) -> NothingOrError<()> {
43        self.update_following_data().unwrap();
44        Ok(())
45    }
46}
47#[cfg(all(feature = "devices", feature = "alloc"))]
48#[derive(Default)]
49struct Encoder {
50    time: Time,
51}
52#[cfg(all(feature = "devices", feature = "alloc"))]
53impl Getter<State, ()> for Encoder {
54    fn get(&self) -> Output<State, ()> {
55        println!("Encoder returning state {:?}", STATE);
56        Ok(Some(Datum::new(self.time, STATE)))
57    }
58}
59#[cfg(all(feature = "devices", feature = "alloc"))]
60impl Updatable<()> for Encoder {
61    fn update(&mut self) -> NothingOrError<()> {
62        self.time += Time(1_000_000_000);
63        Ok(())
64    }
65}
66#[cfg(all(feature = "devices", feature = "alloc"))]
67fn main() {
68    println!("Commanding Motor to {:?}", COMMAND);
69    println!(
70        "K values are {:?}",
71        K_VALUES.get_k_values(PositionDerivative::from(COMMAND))
72    );
73    let motor = Motor::new();
74    let mut motor_wrapper =
75        devices::wrappers::PIDWrapper::new(motor, Time(0), STATE, COMMAND, K_VALUES);
76    let encoder = Encoder::default();
77    let mut encoder_wrapper = devices::wrappers::GetterStateDeviceWrapper::new(encoder);
78    connect(motor_wrapper.get_terminal(), encoder_wrapper.get_terminal());
79    for _ in 0..5 {
80        motor_wrapper.update().unwrap();
81        encoder_wrapper.update().unwrap();
82    }
83}
84#[cfg(all(not(feature = "devices"), feature = "alloc"))]
85fn main() {
86    println!("Enable the `devices` feature to run this example.\nAssuming you're using Cargo, add the `--features devices` flag to your command.");
87}
88#[cfg(all(not(feature = "alloc"), feature = "devices"))]
89fn main() {
90    println!("Enable the `alloc` feature to run this example.\nAssuming you're using Cargo, add the `--features alloc` flag to your command.");
91}
92#[cfg(all(not(feature = "alloc"), not(feature = "devices")))]
93fn main() {
94    println!("Enable the `alloc` and `devices` features to run this example.\nAssuming you're using Cargo, add the `--features alloc,devices` flag to your command.");
95}