tello/
rc_state.rs

1use std::time::SystemTime;
2
3/// represent the current input to remote control the drone.
4#[derive(Clone, Debug, Default)]
5pub struct RCState {
6    left_right: f32,
7    forward_back: f32,
8    turn: f32,
9    up_down: f32,
10
11    start_engines: bool,
12    start_engines_set_time: Option<SystemTime>,
13}
14
15impl RCState {
16    /// set the rc-controller to the mode to hold down the key-combination to do an manual take_off.
17    ///
18    pub fn start_engines(&mut self) {
19        self.start_engines = true;
20        self.start_engines_set_time = Some(SystemTime::now());
21    }
22
23    /// returns the current stick parameter to send them to the drone
24    ///
25    /// Actually, this is an workaround to keep the start_engines in this struct and
26    /// don't move them to the Drone it self
27    pub fn get_stick_parameter(&mut self) -> (f32, f32, f32, f32, bool) {
28        if self.start_engines {
29            if let Some(start) = self.start_engines_set_time {
30                let elapsed = SystemTime::now().duration_since(start);
31                if let Ok(time) = elapsed {
32                    if time.as_millis() > 350 {
33                        self.start_engines = false;
34                    }
35                } else {
36                    self.start_engines = false;
37                }
38            } else {
39                self.start_engines = false;
40            }
41            (-1.0, -1.0, -1.0, 1.0, true)
42        } else {
43            (
44                self.up_down,
45                self.forward_back,
46                self.left_right,
47                self.turn,
48                true,
49            )
50        }
51    }
52
53    /// stop moving left or right by setting the axis to 0.0
54    pub fn stop_left_right(&mut self) {
55        self.left_right = 0.0;
56    }
57
58    /// set a fixed value of -1.0 to the left right axis to fly to the left
59    pub fn go_left(&mut self) {
60        if self.left_right > 0.0 {
61            self.stop_left_right()
62        } else {
63            self.left_right = -1.0;
64        }
65    }
66
67    /// set a fixed value of 1.0 to the left right axis to fly to the right
68    pub fn go_right(&mut self) {
69        if self.left_right < 0.0 {
70            self.stop_left_right()
71        } else {
72            self.left_right = 1.0;
73        }
74    }
75
76    /// set a analog value to the left right axis
77    /// this value has to be between -1 and 1 (including), where -1 is left and 1 is right
78    pub fn go_left_right(&mut self, value: f32) {
79        assert!(value <= 1.0);
80        assert!(value >= -1.0);
81
82        self.left_right = value;
83    }
84
85    /// stop moving forward or back by setting the axis to 0.0
86    pub fn stop_forward_back(&mut self) {
87        self.forward_back = 0.0;
88    }
89
90    /// set a fixed value of -1.0 to the forward and back axis to fly back
91    pub fn go_back(&mut self) {
92        if self.forward_back > 0.0 {
93            self.stop_forward_back()
94        } else {
95            self.forward_back = -1.0;
96        }
97    }
98
99    /// set a fixed value of 1.0 to the forward and back axis to fly forward
100    pub fn go_forward(&mut self) {
101        if self.forward_back < 0.0 {
102            self.stop_forward_back()
103        } else {
104            self.forward_back = 1.0;
105        }
106    }
107
108    /// set a analog value to the forward or back axis
109    /// this value has to be between -1 and 1 (including), where -1 is back and 1 is forward
110    pub fn go_forward_back(&mut self, value: f32) {
111        assert!(value <= 1.0);
112        assert!(value >= -1.0);
113
114        self.forward_back = value;
115    }
116
117    /// stop moving up or down by setting the axis to 0.0
118    pub fn stop_up_down(&mut self) {
119        self.up_down = 0.0;
120    }
121
122    /// set a fixed value of -1.0 to the up and down axis to raise up
123    pub fn go_down(&mut self) {
124        if self.up_down > 0.0 {
125            self.stop_up_down()
126        } else {
127            self.up_down = -1.0;
128        }
129    }
130
131    /// set a fixed value of 1.0 to the up and down axis to go down
132    pub fn go_up(&mut self) {
133        if self.up_down < 0.0 {
134            self.stop_up_down()
135        } else {
136            self.up_down = 1.0;
137        }
138    }
139
140    /// set a analog value to the up or down axis
141    /// this value has to be between -1 and 1 (including), where -1 is going down and 1 is flying up
142    pub fn go_up_down(&mut self, value: f32) {
143        assert!(value <= 1.0);
144        assert!(value >= -1.0);
145
146        self.up_down = value;
147    }
148
149    /// stop turning by setting it to 0.0
150    pub fn stop_turn(&mut self) {
151        self.turn = 0.0;
152    }
153
154    /// set a fixed value of -1.0 to the turn axis to turn counter clock wise
155    pub fn go_ccw(&mut self) {
156        if self.turn > 0.0 {
157            self.stop_turn()
158        } else {
159            self.turn = -1.0;
160        }
161    }
162
163    /// set a fixed value of 1.0 to the forward and back axis to turn clock wise
164    pub fn go_cw(&mut self) {
165        if self.turn < 0.0 {
166            self.stop_turn()
167        } else {
168            self.turn = 1.0;
169        }
170    }
171
172    /// Set a analog value to turn the drone
173    /// This value has to be between -1 and 1 (including), where -1 is turning ccw and 1 is turning cw
174    pub fn turn(&mut self, value: f32) {
175        assert!(value <= 1.0);
176        assert!(value >= -1.0);
177
178        self.turn = value;
179    }
180}