velodyne 0.1.1

Crate for processing Velodyne LiDAR data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::packet::{PacketSource, StatusBytes, get_status};
use std::net::Ipv4Addr;
use chrono::{DateTime, NaiveDate, Utc};
use std::time;
use std::io::{self, ErrorKind, Cursor};
use byteorder::{LittleEndian, ReadBytesExt};
use log::{debug, info, warn};

use super::calib::CalibDb;

use super::{Status, ReturnType, GpsStatus, PowerLevel};

const INIT_TIMEOUT: u64 = 5;

#[derive(Default)]
pub(super) struct StatusAccumulator {
    init: bool,

    dt: [u8; 6],
    gps_val: u8,
    temp_val: u8,
    version_val: u8,

    // Internall state for cycle accumulation
    cycle_ids: [u8; 7],
    cycle_values: [u8; 7],
    cycle_pos: usize,

    // Internall state for cycles processing
    cycle_state: CycleState,

    lasers: LasersCalib,
    sensor_state: [u8; 21],
}

// TODO: CRC check, check radians/degrees
impl StatusAccumulator {
    /// See `StatusListener::init(..)` method docs
    pub(super) fn init<T: PacketSource>(&mut self, packets: &mut T)
        -> io::Result<(Status, CalibDb)>
    {
        let mut sensor_status = default_sensor_status();
        let mut calib_db = CalibDb::default();

        let t = time::Instant::now();
        loop {
            if t.elapsed().as_secs() > INIT_TIMEOUT {
                return Err(io::Error::new(ErrorKind::TimedOut,
                    "Failed to initialize listener in 5 seconds"));
            }
            let status = packets.next_packet()?
                .map(|(_, packet)| get_status(packet))
                .ok_or(io::Error::new(ErrorKind::Other,
                    "Failed to get packet data from packet listener"))?;

            self.feed(status, &mut sensor_status, &mut calib_db);
            if self.init { return Ok((sensor_status, calib_db)); }
        }
    }

    fn process_warning(&mut self, b: u8, status: &mut Status) {
        /*
        status.lens_contamination = (b & 0b1000_000) != 0;
        status.hot = (b & 0b0100_0000) != 0;
        status.cold = (b & 0b0010_0000) != 0;
        status.pps = (b & 0b0000_0100) != 0;
        status.gps_time = (b & 0b0000_0010) != 0;
        */

        status.lens_contamination = (b & 0b0000_0001) != 0;
        status.hot = (b & 0b0000_0010) != 0;
        status.cold = (b & 0b0000_0100) != 0;
        status.pps = (b & 0b0010_0000) != 0;
        status.gps_time = (b & 0b0100_0000) != 0;
    }

    fn process_calib_db(&self, db: &mut CalibDb) {
        let iter = self.lasers.0.iter().zip(db.lasers.iter_mut()).enumerate();
        for (i, (data, dbl)) in iter {
            assert_eq!(data[0] as usize, i, "wrong data for calibration db");
            let mut rdr = Cursor::new(&data[1..19]);
            let vert_corr = read_i16(&mut rdr) as f32 / 100.;
            let rot_corr = read_i16(&mut rdr) as f32 / 100.;

            let (vert_corr_sin, vert_corr_cos) = vert_corr.to_radians().sin_cos();
            let (rot_corr_sin, rot_corr_cos) = rot_corr.to_radians().sin_cos();

            dbl.rot_corr_sin = rot_corr_sin;
            dbl.rot_corr_cos = rot_corr_cos;
            dbl.vert_corr_sin = vert_corr_sin;
            dbl.vert_corr_cos = vert_corr_cos;

            dbl.dist_correction = read_i16(&mut rdr) as f32/10.;
            dbl.dist_corr_x = read_i16(&mut rdr) as f32/10.;
            dbl.dist_corr_y = read_i16(&mut rdr) as f32/10.;
            dbl.vert_offset = read_i16(&mut rdr) as f32/10.;
            dbl.horiz_offset = read_i16(&mut rdr) as f32/10.;
            dbl.focal_dist = read_i16(&mut rdr) as f32/10.;
            dbl.focal_slope = read_i16(&mut rdr) as f32/10.;

            dbl.min_intensity = data[19];
            dbl.max_intensity = data[20];
        }
    }

    fn process_full_cycle(&mut self, status: &mut Status,
        calib_db: &mut CalibDb) -> Result<(), &'static str>
    {
        debug!("full cycle");
        if !self.init {
            info!("Initialization complete");
            self.init = true;
        }

        let d = self.sensor_state;
        let mut rdr = Cursor::new(&d[..]);
        status.rpm = read_u16(&mut rdr);
        status.fov_start = read_u16(&mut rdr);
        status.fov_end = read_u16(&mut rdr);
        status.real_life_time = read_u16(&mut rdr);
        status.ip_source = Ipv4Addr::new(d[8], d[9], d[10], d[11]);
        status.ip_dest = Ipv4Addr::new(d[12], d[13], d[14], d[15]);
        status.return_type = match d[16] {
            0 => ReturnType::Strongest,
            1 => ReturnType::Last,
            2 => ReturnType::Both,
            _ => return Err("invalid return type"),
        };
        status.power_level = match d[18] {
            0xA8 => PowerLevel::AutoNormalized,
            0xA0 => PowerLevel::AutoRaw,
            v if v & 0x0f == 8 && ((v & 0xf0) >> 4) < 8 => {
                PowerLevel::Manual((v & 0xf0) >> 4)
            },
            _ => return Err("invalid power level")
        };

        self.process_calib_db(calib_db);
        Ok(())
    }

    /// Consumes cycle data and checks its content
    ///
    /// Returns:
    ///     Ok(true) -- Cycle consumed without problems
    ///     Ok(false) -- Out of order cycle, caller must reset state
    ///     Err(desc) -- Bug in the code or bad data (e.g. incorect datetime),
    ///         reset state and raise warning
    fn consume_cycle(&mut self, status: &mut Status,
            calib_db: &mut CalibDb)
        -> Result<bool, &'static str>
    {
        let ids = self.cycle_ids;
        let vals = self.cycle_values;
        self.cycle_state = match self.cycle_state {
            CycleState::FirstCycle => {
                debug!("First cycle");
                if !(&ids[..5] == b"12345" && ids[5] == 0xf7 && ids[6] == 0xf6) {
                    return Ok(false);
                }
                if !(&vals[..5] == b"UNIT#") {
                    return Ok(false);
                }
                status.upper_threshold = vals[5];
                status.lower_threshold = vals[6];
                CycleState::Lasers{ laser: 0, part: 0 }
            },
            CycleState::Lasers{ laser, part } => {
                debug!("lasers {} {}", laser, part);
                match part {
                    0 | 1 | 2 => {
                        if &ids != b"1234567" { return Ok(false); }
                        if part == 0 && vals[0] != laser as u8 {
                            return Ok(false);
                        }
                        let s = 7*part;
                        if !self.init {
                            self.lasers.0[laser][s..s+7].copy_from_slice(&vals);
                        }
                        if laser == 63 && part == 2 {
                            CycleState::CalibrationDt
                        } else {
                            CycleState::Lasers{ laser: laser, part: part + 1 }
                        }
                    },
                    3 => {
                        if &ids != b"W234567" { return Ok(false); }
                        self.process_warning(vals[0], status);
                        CycleState::Lasers{ laser: laser + 1, part: 0 }
                    }
                    _ => return Err("Unreachable branch: laser cycles"),
                }
            },
            CycleState::CalibrationDt => {
                debug!("CalibrationDt");
                if &ids != b"1234567" { return Ok(false); }
                let dt = get_dt(vals[0], vals[1], vals[2],
                                vals[3], vals[4], vals[5])?;
                status.calib_dt = dt;
                status.humidity = vals[6];
                CycleState::SensorState{part: 0}
            },
            CycleState::SensorState{part} => {
                debug!("SensorState {}", part);
                match part {
                    0 => {
                        if ids != [0xfe, 0xff, 0xfc, 0xfd, 0xfa, 0xfb, 0x37] {
                            return Ok(false);
                        }
                        self.sensor_state[..7].copy_from_slice(&vals);
                        CycleState::SensorState{part: 1}
                    },
                    1 => {
                        if &ids != b"1234567" { return Ok(false); }
                        self.sensor_state[7..14].copy_from_slice(&vals);
                        CycleState::SensorState{part: 2}
                    },
                    2 => {
                        if ids != [0x31, 0x32, 0xf9, 0x34, 0xf8, 0x36, 0x37] {
                            return Ok(false);
                        }
                        self.sensor_state[14..21].copy_from_slice(&vals);
                        self.process_full_cycle(status, calib_db)?;
                        CycleState::FirstCycle
                    },
                    _ => return Err("Unreachable branch: state cycle"),
                }
            },
        };
        Ok(true)
    }

    fn update_status(&mut self, status: &mut Status)
        -> Result<(), &'static str>
    {
        let dt = self.dt;
        status.dt = get_dt(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5])?;
        status.gps = match self.gps_val {
            0x41 => GpsStatus::SyncNmea,
            0x56 => GpsStatus::NmeaOnly,
            0x50 => GpsStatus::SyncOnly,
            0x00 => GpsStatus::NotConnected,
            _ => { return Err("Unknown GPS status code") },
        };
        status.temperature = self.temp_val;
        status.version = self.version_val;
        Ok(())
    }

    /// See `StatusListener.feed(..)` docs
    pub(super) fn feed(&mut self, status: StatusBytes,
        sensor_status: &mut Status, calib_db: &mut CalibDb)
    {
        let is_ok = match status.id as char {
            'H' => {
                self.dt[3] = status.value;
                self.cycle_pos == 0
            },
            'M' => {
                self.dt[4] = status.value;
                self.cycle_pos == 1
            },
            'S' => {
                self.dt[5] = status.value;
                self.cycle_pos == 2
            },
            'D' => {
                self.dt[2] = status.value;
                self.cycle_pos == 3
            },
            'N' => {
                self.dt[1] = status.value;
                self.cycle_pos == 4
            },
            'Y' => {
                self.dt[0] = status.value;
                self.cycle_pos == 5            },
            'G' => {
                self.gps_val = status.value;
                self.cycle_pos == 6
            },
            'T' => {
                self.temp_val = status.value;
                self.cycle_pos == 7
            },
            'V' => {
                self.version_val = status.value;
                self.cycle_pos == 8
            },
            _ => self.cycle_pos > 8 && self.cycle_pos < 16
        };

        if !is_ok {
            let msg = "Wrong cycle position detected. Reseting.";
            if self.init { warn!("{}", msg); } else { debug!("{}", msg); }
            self.cycle_pos = 0;
            return;
        }

        if self.cycle_pos == 8 {
            if let Err(s) = self.update_status(sensor_status) {
                warn!("{}", s);
                self.cycle_state = CycleState::FirstCycle;
            }
        }

        if self.cycle_pos <= 8 {
            self.cycle_pos += 1;
            return;
        }

        self.cycle_ids[self.cycle_pos - 9] = status.id;
        self.cycle_values[self.cycle_pos - 9] = status.value;

        if self.cycle_pos == 15 {
            match self.consume_cycle(sensor_status, calib_db) {
                Ok(is_ok) => {
                    if !is_ok {
                        let msg = "Wrong cycle state. Reseting.";
                        if self.init {
                            warn!("{}", msg);
                        } else {
                            debug!("{}", msg);
                        }
                        self.cycle_state = CycleState::FirstCycle;
                    }
                },
                Err(s) => {
                    warn!("{}", s);
                    self.cycle_state = CycleState::FirstCycle;
                }
            };
            self.cycle_pos = 0;
        } else {
            self.cycle_pos += 1;
        }
    }
}

fn default_sensor_status() -> Status {
    let dt = get_dt(0, 1, 1, 0, 0, 0).unwrap();
    Status {
        dt: dt,
        gps: GpsStatus::NotConnected,
        temperature: 0,
        version: 0,
        lens_contamination: false,
        hot: false,
        cold: false,
        pps: false,
        gps_time: false,
        rpm: 0,
        fov_start: 0,
        fov_end: 0,
        real_life_time: 0,
        ip_source: Ipv4Addr::new(0, 0 ,0 ,0),
        ip_dest: Ipv4Addr::new(0, 0 ,0 ,0),
        return_type: ReturnType::Strongest,
        power_level: PowerLevel::AutoNormalized,
        humidity: 0,
        upper_threshold: 0,
        lower_threshold: 0,
        calib_dt: dt,
    }
}

#[inline(always)]
fn read_u16(rdr: &mut Cursor<&[u8]>) -> u16 {
     rdr.read_u16::<LittleEndian>().unwrap()
}

#[inline(always)]
fn read_i16(rdr: &mut Cursor<&[u8]>)-> i16 {
    rdr.read_i16::<LittleEndian>().unwrap()
}

fn get_dt(year: u8, month: u8, day: u8, h: u8, m: u8, s: u8)
    -> Result<DateTime<Utc>, &'static str>
{
    let y = year as i32 + 2000;
    let res = NaiveDate::from_ymd_opt(y, month as u32, day as u32)
        .and_then(|d| d.and_hms_opt(h as u32, m as u32, s as u32));
    if let Some(dt) = res {
        Ok(DateTime::<Utc>::from_utc(dt, Utc))
    } else {
        Err("Incorrect datetime")
    }
}

#[derive(Copy, Clone)]
enum CycleState {
    FirstCycle,
    Lasers{ laser: usize, part: usize},
    CalibrationDt,
    SensorState{ part: usize },
}

impl Default for CycleState {
    fn default() -> Self { CycleState::FirstCycle }
}


struct LasersCalib([[u8; 21]; 64]);

impl Default for LasersCalib {
    fn default() -> Self { LasersCalib([[0u8; 21]; 64]) }
}