pub struct DataPacket {
pub host_timestamp: f64,
pub emotibit_timestamp: f64,
pub packet_id: u32,
pub data_points: u8,
pub version: u8,
pub reliability: u8,
pub data_type: DataType,
}Expand description
Emotibit Data Packet
Fields§
§host_timestamp: f64Local timestamp on a host PC
emotibit_timestamp: f64Milliseconds since start of EmotiBit
packet_id: u32Packet count since start of EmotiBit
data_points: u8Number of data points in the payload
version: u8Version of packet protocol
reliability: u8Data reliability score out of 100, currently always 100
data_type: DataTypeType of data being sent and its payload
Implementations§
Source§impl DataPacket
impl DataPacket
Sourcepub fn inject_host_timestamp(self, map: &TimeSyncMap) -> Self
pub fn inject_host_timestamp(self, map: &TimeSyncMap) -> Self
Performs linear interpolation based on TimeSyncMap and returns a new DataPacket with a host timestamp.
Examples found in repository?
examples/to_csv.rs (line 94)
13fn read_write(path_buf: Option<PathBuf>) -> Result<()> {
14 let filename: &str = path_buf
15 .as_ref()
16 .and_then(|name| name.file_stem())
17 .and_then(|name| name.to_str())
18 .unwrap_or("default");
19
20 let (datapackets, errors): (Vec<_>, Vec<_>) = parser::get_packets(
21 &path_buf
22 .as_ref()
23 .unwrap()
24 .clone()
25 .into_os_string()
26 .into_string()
27 .unwrap(),
28 )?
29 .into_iter()
30 .partition(Result::is_ok);
31
32 let mut output_file = path_buf.clone().unwrap();
33 output_file.set_file_name(format!("{}_ERROR.csv", filename));
34
35 // Write Errors
36 let mut output = File::create(output_file.clone())?;
37 for err in errors.into_iter().filter_map(|result| result.err()) {
38 writeln!(output, "{}", err)?;
39 }
40
41 // Write TimeSyncs
42 output_file.set_file_name(format!("{}_timesyncs.csv", filename));
43 let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
44 match parser::find_syncs(&datapackets) {
45 Ok(syncs) => {
46 let header =
47 StringRecord::from(vec!["RD", "TS_received", "TS_sent", "AK", "RoundTrip"]);
48 writer.write(&header)?;
49 for packet in syncs {
50 writer.write(&packet)?;
51 }
52 }
53 Err(e) => {
54 writer.write(&StringRecord::from(vec![format!("{:?}", e)]))?;
55 }
56 }
57
58 // Extract TypeTags
59 let set: HashSet<&str> = HashSet::from_iter(
60 datapackets
61 .iter()
62 .map(|result| result.as_ref().unwrap().data_type.as_str()),
63 );
64
65 // Write TimeSyncsMap
66 output_file.set_file_name(format!("{}_timeSyncMap.csv", filename));
67 let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
68 let syncmap = parser::generate_sync_map(&datapackets);
69 match &syncmap {
70 Ok(map) => {
71 let header = StringRecord::from(vec![
72 "TE0",
73 "TE1",
74 "TL0",
75 "TL1",
76 "TimeSyncsReceived",
77 "EmotiBitStartTime",
78 "EmotiBitEndTime",
79 "DataParserVersion",
80 ]);
81 writer.write(&header)?;
82 writer.write(map)?;
83 }
84 Err(e) => {
85 writer.write(&StringRecord::from(vec![format!("{:?}", e)]))?;
86 }
87 }
88
89 // Write Packets
90 let packets: Vec<DataPacket> = match syncmap {
91 Ok(map) => datapackets
92 .into_iter()
93 .filter_map(|result| result.ok())
94 .map(|p| p.inject_host_timestamp(&map))
95 .collect(),
96 Err(_) => datapackets
97 .into_iter()
98 .filter_map(|result| result.ok())
99 .collect(),
100 };
101
102 for t in set.iter() {
103 output_file.set_file_name(format!("{}_{}.csv", filename, t));
104 let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
105 let header = StringRecord::from(vec![
106 "LocalTimestamp",
107 "EmotiBitTimestamp",
108 "PacketNumber",
109 "DataLength",
110 "TypeTag",
111 "ProtocolVersion",
112 "DataReliability",
113 t,
114 ]);
115 writer.write(&header)?;
116 for packet in packets.iter().filter(|x| x.data_type.as_str() == *t) {
117 writer.write(packet)?;
118 }
119 }
120
121 Ok(())
122}Trait Implementations§
Source§impl Clone for DataPacket
impl Clone for DataPacket
Source§fn clone(&self) -> DataPacket
fn clone(&self) -> DataPacket
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Csv for DataPacket
impl Csv for DataPacket
fn csv(&self) -> Vec<StringRecord>
Source§impl Debug for DataPacket
impl Debug for DataPacket
Source§impl TryFrom<&StringRecord> for DataPacket
impl TryFrom<&StringRecord> for DataPacket
Auto Trait Implementations§
impl Freeze for DataPacket
impl RefUnwindSafe for DataPacket
impl Send for DataPacket
impl Sync for DataPacket
impl Unpin for DataPacket
impl UnwindSafe for DataPacket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more