airsim_client/types/
pwm.rs1#[derive(Debug, Clone, Copy)]
2pub struct PWM {
3 pub front_right_pwm: f32,
5 pub rear_left_pwm: f32,
7 pub front_left_pwm: f32,
9 pub rear_right_pwm: f32,
11}
12
13impl PWM {
14 pub fn new(front_right_pwm: f32, rear_left_pwm: f32, front_left_pwm: f32, rear_right_pwm: f32) -> Self {
15 if front_right_pwm.is_sign_negative() || front_right_pwm > 1.0 {
16 panic!("front_right_pwm outside of valid range 0.0 to 1.0")
17 }
18
19 if rear_left_pwm.is_sign_negative() || rear_left_pwm > 1.0 {
20 panic!("rear_left_pwm outside of valid range 0.0 to 1.0")
21 }
22
23 if front_left_pwm.is_sign_negative() || front_left_pwm > 1.0 {
24 panic!("front_left_pwm outside of valid range 0.0 to 1.0")
25 }
26
27 if rear_right_pwm.is_sign_negative() || rear_right_pwm > 1.0 {
28 panic!("rear_right_pwm outside of valid range 0.0 to 1.0")
29 }
30
31 Self {
32 front_right_pwm,
33 rear_left_pwm,
34 front_left_pwm,
35 rear_right_pwm,
36 }
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use crate::PWM;
43
44 #[test]
45 #[should_panic]
46 fn test_pwm_range() {
47 PWM::new(-1.0, 0.1, 0.1, 0.1);
48 }
49}