e_drone/system/
mod.rs

1use num_enum::IntoPrimitive;
2use num_enum::TryFromPrimitive;
3use std::convert::TryFrom;
4use byteorder::{ByteOrder, LittleEndian};
5
6
7// -- ModelNumber -------------------------------------------------------------------------------------------
8#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
9#[repr(u32)]
10pub enum ModelNumber {
11    None = 0x00000000,
12
13    //                       AAAABBCC, AAAA(Project Number), BB(Device Type), CC(Revision)
14    Drone3DroneP1        = 0x00031001,      // Lightrone / GD65 / HW2181 / 3.7v / barometer / RGB LED / Shaking binding
15    Drone3DroneP2        = 0x00031002,      // Soccer Drone / HW2181 / 7.4v / barometer / RGB LED / Shaking binding
16    Drone3DroneP3        = 0x00031003,      // GD240 / HW2181 / power button / u30 flow / 3.7v / geared motor / barometer
17    Drone3DroneP4        = 0x00031004,      // GD50N / HW2181 / power button / 3.7v / barometer
18    Drone3DroneP5        = 0x00031005,      // GD30 / HW2181 / 3.7v / normal binding
19    Drone3DroneP6        = 0x00031006,      // Soccer Drone 2 / HW2181 / 7.4v / barometer / RGB LED / Shaking binding
20    Drone3DroneP7        = 0x00031007,      // SKYKICKV2 / SPI / HW2181 / 7.4v / barometer / RGB LED / Shaking binding
21    Drone3DroneP8        = 0x00031008,      // GD65 / SPI / HW2181 / 3.7v / barometer / RGB LED / Shaking binding
22    Drone3DroneP9        = 0x00031009,      // GD65 / SPI / HW2181 / 3.7v / barometer / RGB LED / Shaking binding / BladeType Power Connector
23    Drone3DroneP10       = 0x0003100A,      // Battle Drone / SPI / HW2181 / 3.7v / barometer / RGB LED / Shaking binding
24    
25    Drone3ControllerP1   = 0x00032001,      // GD65 Controller / HW2181
26    Drone3ControllerP2   = 0x00032002,      // Skykick Controller / HW2181
27    Drone3ControllerP3   = 0x00032003,      // GD65 Controller / USB / HW2181
28    Drone3ControllerP4   = 0x00032004,      // Battle Drone Controller / USB / HW2181B
29    Drone3ControllerP5   = 0x00032005,      // E-Drone 4m Controller / USB / HW2181B
30    
31    Drone3LinkP0         = 0x00033000,      // 
32
33    Drone3AnalyzerP0     = 0x00034100,      // for soccer drone
34
35    Drone3TesterP4       = 0x0003A004,      // (obsolete)
36    Drone3TesterP6       = 0x0003A006,      // Battle Drone, Controller Tester / Drone3DroneP10, Drone3ControllerP4
37    Drone3TesterP7       = 0x0003A007,      // Tester / Drone3ControllerP5 테스터
38    
39    Drone4DroneP5        = 0x00041005,      // HW2000, 2m range sensor
40    Drone4DroneP6        = 0x00041006,      // HW2000B, 4m range sensor
41    Drone4DroneP7        = 0x00041007,      // HW2000B, 4m range sensor, BLDC Motor
42    
43    Drone4ControllerP2   = 0x00042002,      // HW2000
44    Drone4ControllerP3   = 0x00042003,      // HW2000B
45    Drone4ControllerP4   = 0x00042004,      // HW2000B + PA
46    Drone4ControllerP5   = 0x00042005,      // HW2000B + PA
47    
48    Drone4LinkP0         = 0x00043000,      // 
49    
50    Drone4TesterP6       = 0x0004A006,      // 
51    Drone4TesterP7       = 0x0004A007,      // Drone4DroneP6 테스터(E-Drone 4m 버전)
52    
53    Drone7DroneP2        = 0x00071002,      // Coding Car
54
55    Drone7BleClientP0    = 0x00073200,      // Coding Car Link
56    Drone7BleClientP5    = 0x00073205,      // Coding Car Tester BLE
57
58    Drone7BleServerP2    = 0x00073302,      // Coding Car Ble Module
59
60    Drone7TesterP5       = 0x0007A005,      // 
61    Drone7TesterP6       = 0x0007A006,      // 
62    
63    Drone7MonitorP4      = 0x0007A104,      // (obsolete)
64    Drone7MonitorP5      = 0x0007A105,      // 
65    
66    Drone8DroneP0        = 0x00081000,      // (obsolete)
67    Drone8DroneP1        = 0x00081001,      // Coding Drone
68    
69    Drone8TesterP6       = 0x0008A006,      // 
70
71    Drone8MonitorP6      = 0x0008A106,      // 
72    
73    Drone9DroneP0        = 0x00091000,      // 
74    Drone9DroneP1        = 0x00091001,      // 
75    Drone9DroneP2        = 0x00091002,      // 
76    Drone9DroneP3        = 0x00091003,      // STM32F412RET6, HW2000B + PA, OPT3101, Raspberry PI
77    Drone9DroneP4        = 0x00091004,      // 
78    Drone9DroneP5        = 0x00091005,      // 
79    
80    Drone9TesterP6       = 0x0009A006,      // 
81    Drone9TesterP7       = 0x0009A007,      // 
82}
83
84
85impl ModelNumber {
86    /*
87    // 사용하지는 않지만 참고용으로 남겨둠
88    pub fn from_slice(data_array: &[u8]) -> ModelNumber {
89        if data_array.len() == 4 {
90            let model_number_u32: u32 = LittleEndian::read_u32(&data_array[0..4]);
91            ModelNumber::from_u32( model_number_u32 )
92        }
93        else
94        {
95            ModelNumber::None
96        }
97    }
98    // */
99
100    pub fn from_u32(data_u32: u32) -> ModelNumber {
101        match ModelNumber::try_from( data_u32 ) {
102            Ok(data) => { data },
103            _ => { ModelNumber::None },
104        }
105    }
106
107    pub fn to_array(&self) -> [u8; 4] {
108        let mut buf = [0; 4];
109        LittleEndian::write_u32(&mut buf, self.clone().into());
110        buf
111    }
112
113    pub fn get_device_type(&self) -> DeviceType {
114        DeviceType::from_u8(self.to_array()[1])
115    }
116}
117
118
119// -- DeviceType -------------------------------------------------------------------------------------------
120#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
121#[repr(u8)]
122pub enum DeviceType {
123    None            = 0x00,
124
125    Drone           = 0x10, // 드론(Server)
126
127    Controller      = 0x20, // 조종기(Client)
128
129    LinkClient      = 0x30, // 링크 모듈(Client)
130    LinkServer      = 0x31, // 링크 모듈(Server)
131    BleClient       = 0x32, // BLE 클라이언트
132    BleServer       = 0x33, // BLE 서버
133
134    Range           = 0x40, // 거리 센서 모듈
135    Analyzer        = 0x41, // 분석 모듈
136
137    Base            = 0x70, // 베이스(응용 프로그램)
138    Base2           = 0x71, // 
139
140    ByScratch       = 0x80, // 바이스크래치
141    Scratch         = 0x81, // 스크래치
142    Entry           = 0x82, // 네이버 엔트리
143
144    Tester          = 0xA0, // 테스터
145    Monitor         = 0xA1, // 모니터
146    Updater         = 0xA2, // 펌웨어 업데이트 도구
147    Encrypter       = 0xA3, // 암호화 도구
148
149    UWB             = 0xE0, // UWB
150
151    Whispering      = 0xFE, // 바로 인접한 장치까지만 전달(받은 장치는 자기 자신에게 보낸 것처럼 처리하고 타 장치에 전달하지 않음)
152    Broadcasting    = 0xFF, // 연결된 모든 장치에 전달
153}
154
155
156impl DeviceType {
157    // https://crates.io/crates/num_enum
158    pub fn from_u8(data_u8: u8) -> DeviceType {
159        match DeviceType::try_from( data_u8 ) {
160            Ok(data) => { data },
161            _ => { DeviceType::None },
162        }
163    }
164}
165
166
167
168// -- ModeSystem -------------------------------------------------------------------------------------------
169#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
170#[repr(u8)]
171pub enum ModeSystem {
172    None            = 0x00,    // 
173    
174    Boot            = 0x10,
175    Start           = 0x11,
176    Running         = 0x12,
177    ReadyToReset    = 0x13,
178
179    Error           = 0xA0,
180}
181
182
183impl ModeSystem {
184    pub fn from_u8(data_u8: u8) -> ModeSystem {
185        match ModeSystem::try_from( data_u8 ) {
186            Ok(data) => { data },
187            _ => { ModeSystem::None },
188        }
189    }
190}
191
192
193
194// -- ModeControlFlight -------------------------------------------------------------------------------------------
195#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
196#[repr(u8)]
197pub enum ModeControlFlight {
198    None        = 0x00,    // 
199    
200    Attitude    = 0x10,     // 자세 - X,Y는 각도(deg)로 입력받음, Z,Yaw는 속도(m/s)로 입력 받음
201    Position    = 0x11,     // 위치 - X,Y,Z,Yaw는 속도(m/s)로 입력 받음
202    Manual      = 0x12,     // 고도를 수동으로 조종함
203    Rate        = 0x13,     // Rate - X,Y는 각속도(deg/s)로 입력받음, Z,Yaw는 속도(m/s)로 입력 받음
204    Function    = 0x14,     // 기능
205
206    Error       = 0xA0,
207}
208
209
210impl ModeControlFlight {
211    pub fn from_u8(data_u8: u8) -> ModeControlFlight {
212        match ModeControlFlight::try_from( data_u8 ) {
213            Ok(data) => { data },
214            _ => { ModeControlFlight::None },
215        }
216    }
217}
218
219
220
221// -- ModeFlight -------------------------------------------------------------------------------------------
222#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
223#[repr(u8)]
224pub enum ModeFlight {
225    None        = 0x00,
226    
227    Ready       = 0x10,
228
229    Start       = 0x11,
230    Takeoff     = 0x12,
231    Flight      = 0x13,
232    Landing     = 0x14,
233    Flip        = 0x15,
234    Reverse     = 0x16,
235
236    Stop        = 0x20,
237
238    Accident    = 0x30,
239    Error       = 0x31,
240
241    Test        = 0x40,
242}
243
244
245impl ModeFlight {
246    pub fn from_u8(data_u8: u8) -> ModeFlight {
247        match ModeFlight::try_from( data_u8 ) {
248            Ok(data) => { data },
249            _ => { ModeFlight::None },
250        }
251    }
252}
253
254
255// -- ModeMovement -------------------------------------------------------------------------------------------
256#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
257#[repr(u8)]
258pub enum ModeMovement {
259    None        = 0x00,
260    
261    Ready       = 0x01,
262    Hovering    = 0x02,
263    Moving      = 0x03,
264    ReturnHome  = 0x04
265}
266
267
268impl ModeMovement {
269    pub fn from_u8(data_u8: u8) -> ModeMovement {
270        match ModeMovement::try_from( data_u8 ) {
271            Ok(data) => { data },
272            _ => { ModeMovement::None },
273        }
274    }
275}
276
277
278// -- ModeUpdate -------------------------------------------------------------------------------------------
279#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
280#[repr(u8)]
281pub enum ModeUpdate {
282    None            = 0x00, // 
283    
284    Ready           = 0x01, // 업데이트 가능 상태
285    Update          = 0x02, // 업데이트 중
286    Complete        = 0x03, // 업데이트 완료
287        
288    Failed          = 0x04, // 업데이트 실패(업데이트 완료까지 갔으나 body의 CRC16이 일치하지 않는 경우 등)
289    
290    NotAvailable    = 0x05, // 업데이트 불가능 상태(Debug 모드 등)
291    RunApplication  = 0x06, // 어플리케이션 동작 중
292    NotRegistered   = 0x07, // 등록되지 않음
293    
294    EndOfType = 0x08,
295}
296
297
298impl ModeUpdate {
299    pub fn from_u8(data_u8: u8) -> ModeUpdate {
300        match ModeUpdate::try_from( data_u8 ) {
301            Ok(data) => { data },
302            _ => { ModeUpdate::None },
303        }
304    }
305}
306
307
308// -- Version -------------------------------------------------------------------------------------------
309#[derive(Debug, Copy, Clone)]
310pub struct Version {
311    pub build: u16,
312    pub minor: u8,
313    pub major: u8,
314}
315
316
317impl Version {
318    pub fn from_slice(data_array: &[u8]) -> Version {
319        if data_array.len() == 4 {
320            Version {
321                build: LittleEndian::read_u16(&data_array[0..2]),
322                minor: data_array[2],
323                major: data_array[3],
324            }
325        }
326        else {
327            Version {
328                build: 1,
329                minor: 1,
330                major: 21,
331            }
332        }
333    }
334
335    pub fn from_u32(version: u32) -> Version {
336        Version {
337            build: (version & 0xFFFF) as u16,
338            minor: ((version >> 16) & 0xFF) as u8,
339            major: ((version >> 24) & 0xFF) as u8,
340        }
341    }
342
343    pub fn to_array(&self) -> [u8; 4] {
344        let mut buf = [0; 4];
345        LittleEndian::write_u32(&mut buf, self.to_u32());
346        buf
347    }
348    
349    pub fn to_u32(&self) -> u32 {
350        ((self.major as u32) << 24) | ((self.minor as u32) << 16) | self.build as u32
351    }
352}
353
354
355// -- ErrorFlagsForSensor -------------------------------------------------------------------------------------------
356#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
357#[repr(u32)]
358pub enum ErrorFlagsForSensor {
359    None                                = 0x00000000,
360
361    MotionNoAnswer                      = 0x00000001,    // Motion 응답 없음
362    MotionWrongValue                    = 0x00000002,    // Motion 잘못된 값
363    MotionNotCalibrated                 = 0x00000004,    // Gyro Bias 보정이 완료되지 않음
364    MotionCalibrating                   = 0x00000008,    // Gyro Bias 보정 중
365
366    PressureNoAnswer                    = 0x00000010,    // 압력 센서 응답 없음
367    PressureWrongValue                  = 0x00000020,    // 압력 센서 잘못된 값
368
369    RangeGroundNoAnswer                 = 0x00000100,    // 바닥 거리 센서 응답 없음
370    RangeGroundWrongValue               = 0x00000200,    // 바닥 거리 센서 잘못된 값
371    RangeFrontNoAnswer                  = 0x00000400,    // 정면 거리 센서 응답 없음
372    RangeFrontWrongValue                = 0x00000800,    // 정면 거리 센서 잘못된 값
373
374    FlowNoAnswer                        = 0x00001000,    // Flow 응답 없음
375    FlowWrongValue                      = 0x00002000,    // Flow 잘못된 값
376    FlowCannotRecognizeGroundImage      = 0x00004000,    // 바닥 이미지를 인식할 수 없음
377
378    RfNoAnswer                          = 0x10000000,    // RF 응답 없음
379    RfPaired                            = 0x20000000,    // RF 응답 없음
380    RfConnected                         = 0x40000000,    // RF 응답 없음
381}
382
383
384impl ErrorFlagsForSensor {
385    pub fn from_u8(data_u32: u32) -> ErrorFlagsForSensor {
386        match ErrorFlagsForSensor::try_from( data_u32 ) {
387            Ok(data) => { data },
388            _ => { ErrorFlagsForSensor::None },
389        }
390    }
391}
392
393
394// -- ErrorFlagsForState -------------------------------------------------------------------------------------------
395#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
396#[repr(u32)]
397pub enum ErrorFlagsForState {
398    None                                    = 0x00000000,
399
400    NotRegistered                           = 0x00000001,    // 장치 등록이 안됨
401    FlashReadLockUnLocked                   = 0x00000002,    // 플래시 메모리 읽기 Lock이 안 걸림
402    BootloaderWriteLockUnLocked             = 0x00000004,    // 부트로더 영역 쓰기 Lock이 안 걸림
403    LowBattery                              = 0x00000008,    // Low Battery
404    
405    TakeoffFailureCheckPropellerAndMotor    = 0x00000010,    // 이륙 실패
406    CheckPropellerVibration                 = 0x00000020,    // 프로펠러 진동발생
407    AttitudeNotStable                       = 0x00000040,    // 자세가 많이 기울어져 있거나 뒤집어져 있을때
408    
409    CanNotFlipLowBattery                    = 0x00000100,    // 배터리가 30이하
410    CanNotFlipTooHeavy                      = 0x00000200,    // 기체가 무거움
411}
412
413
414impl ErrorFlagsForState {
415    pub fn from_u8(data_u32: u32) -> ErrorFlagsForState {
416        match ErrorFlagsForState::try_from( data_u32 ) {
417            Ok(data) => { data },
418            _ => { ErrorFlagsForState::None },
419        }
420    }
421}
422
423
424
425// -- Direction -------------------------------------------------------------------------------------------
426#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
427#[repr(u8)]
428pub enum Direction {
429    None = 0,
430
431    Left,
432    Front,
433    Right,
434    Rear,
435    
436    Top,
437    Bottom,
438    
439    Center,
440}
441
442
443impl Direction {
444    pub fn from_u8(data_u8: u8) -> Direction {
445        match Direction::try_from( data_u8 ) {
446            Ok(data) => { data },
447            _ => { Direction::None },
448        }
449    }
450}
451
452
453
454// -- Rotation -------------------------------------------------------------------------------------------
455#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
456#[repr(u8)]
457pub enum Rotation {
458    None                = 0x00,
459    
460    Clockwise           = 0x01, // 시계 방향
461    Counterclockwise    = 0x02, // 반시계 방향
462}
463
464
465impl Rotation {
466    pub fn from_u8(data_u8: u8) -> Rotation {
467        match Rotation::try_from( data_u8 ) {
468            Ok(data) => { data },
469            _ => { Rotation::None },
470        }
471    }
472}
473
474
475// -- FlightEvent -------------------------------------------------------------------------------------------
476#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
477#[repr(u8)]
478pub enum FlightEvent {
479    None            = 0x00,     // 없음
480            
481    Stop            = 0x10,     // 정지
482    Takeoff         = 0x11,     // 이륙
483    Landing         = 0x12,     // 착륙
484    
485    Reverse         = 0x14,     // 뒤집기
486    
487    FlipFront       = 0x15,     // 회전
488    FlipRear        = 0x16,     // 회전
489    FlipLeft        = 0x17,     // 회전
490    FlipRight       = 0x18,     // 회전
491    
492    ReturnHone      = 0x19,     // Return Home
493    
494    Shot            = 0x90,     // Shot
495    UnderAttack     = 0x91,     // UnderAttack
496    
497    ResetHeading    = 0xA0,     // 헤딩 리셋(Headless 모드 일 때 현재 heading을 0도로 변경)
498}
499
500
501impl FlightEvent {
502    pub fn from_u8(data_u8: u8) -> FlightEvent {
503        match FlightEvent::try_from( data_u8 ) {
504            Ok(data) => { data },
505            _ => { FlightEvent::None },
506        }
507    }
508}
509
510
511// -- SensorOrientation -------------------------------------------------------------------------------------------
512#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
513#[repr(u8)]
514pub enum SensorOrientation {
515    None = 0,       // 없음
516    
517    Normal,         // 정상
518    ReverseStart,   // 뒤집히기 시작
519    Reversed,       // 뒤집힘
520}
521
522
523impl SensorOrientation {
524    pub fn from_u8(data_u8: u8) -> SensorOrientation {
525        match SensorOrientation::try_from( data_u8 ) {
526            Ok(data) => { data },
527            _ => { SensorOrientation::None },
528        }
529    }
530}
531
532
533// -- Headless -------------------------------------------------------------------------------------------
534#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
535#[repr(u8)]
536pub enum Headless {
537    None = 0,       // 없음
538    
539    Headless,       // 헤드리스(앱솔루트)
540    Normal,         // 일반
541}
542
543
544impl Headless {
545    pub fn from_u8(data_u8: u8) -> Headless {
546        match Headless::try_from( data_u8 ) {
547            Ok(data) => { data },
548            _ => { Headless::None },
549        }
550    }
551}
552
553
554// -- TrimDirection -------------------------------------------------------------------------------------------
555#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
556#[repr(u8)]
557pub enum TrimDirection {
558    None = 0,           // 없음
559
560    RollIncrease,       // Roll 증가
561    RollDecrease,       // Roll 감소
562    PitchIncrease,      // Pitch 증가
563    PitchDecrease,      // Pitch 감소
564    YawIncrease,        // Yaw 증가
565    YawDecrease,        // Yaw 감소
566    ThrottleIncrease,   // Throttle 증가
567    ThrottleDecrease,   // Throttle 감소
568    
569    Reset,              // 전체 트림 리셋
570}
571
572
573impl TrimDirection {
574    pub fn from_u8(data_u8: u8) -> TrimDirection {
575        match TrimDirection::try_from( data_u8 ) {
576            Ok(data) => { data },
577            _ => { TrimDirection::None },
578        }
579    }
580}
581
582