Skip to content

Commit b11ce4e

Browse files
committed
add tools which could help with pre-processing of the data collected over USB serial
1 parent 8b96a9f commit b11ce4e

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# process_nicla_bhy2_log_base64.py
2+
This script is used to convert the log encoded in base64 to ascii
3+
## Usage
4+
```
5+
./process_nicla_bhy2_log_base64.py [log_file_name]
6+
```
7+
## Example
8+
./process_nicla_bhy2_log_base64.py log_nicla_bhy2.txt
9+
10+
11+
# check_for_data_loss.sh
12+
This script is used to check for any potential data loss during the transfer,
13+
and it reports some errors if it does find any data loss
14+
15+
## Usage
16+
```
17+
./check_for_data_loss.sh [OPTION] [log_file_name]
18+
```
19+
20+
## Example
21+
- Example 1
22+
```
23+
./check_for_data_loss.sh -b ./minicom.log
24+
```
25+
The above command check for the data loss using the log "./minicom.log" which is base on base64 encoding
26+
- Example 2
27+
```
28+
./check_for_data_loss.sh -a ./minicom.log
29+
```
30+
The above command check for the data loss using the log "./minicom.log" which is base on ascii encoding
31+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# this script resamples the data from the log to 1/60hz
3+
4+
import sys
5+
import time
6+
import datetime
7+
import pandas as pd
8+
import math
9+
10+
11+
#DEBUG = True
12+
DEBUG = False
13+
SEP_COLUMN = ','
14+
15+
LINE_TO_SKIP = 500
16+
17+
DOWNSAMPLE_FACTOR=1
18+
19+
def PDBG(*args):
20+
if DEBUG:
21+
print("DEBUG:", *args, file=sys.stderr)
22+
23+
24+
PDBG('Number of arguments:', len(sys.argv))
25+
26+
if (len(sys.argv) < 2):
27+
raise BaseException("missing argument")
28+
29+
filename_in = sys.argv[1]
30+
file_out = None
31+
32+
if (len(sys.argv) > 2):
33+
filename_out = sys.argv[2]
34+
file_out = open(filename_out, 'w')
35+
sys.stdout = file_out
36+
37+
38+
line_cnt = 1 #first row is the header
39+
df_in = pd.read_csv(filename_in, sep=SEP_COLUMN)
40+
41+
seq_last = None
42+
43+
44+
for index, row in df_in.iterrows():
45+
line_cnt += 1
46+
if (line_cnt <= LINE_TO_SKIP):
47+
continue
48+
49+
seq = row['seq']
50+
if (seq_last is not None):
51+
try:
52+
delta = seq - seq_last
53+
if (seq < seq_last):
54+
delta += 10
55+
if (delta != 1):
56+
if (line_cnt < len(df_in.index)):
57+
print("error: line: ", line_cnt, " has data missing, delta:", delta, "seq:", seq)
58+
else:
59+
PDBG("last line: ignored")
60+
seq_last = seq
61+
except:
62+
if (line_cnt < len(df_in.index)):
63+
print("error: line: ", line_cnt, " has data missing")
64+
else:
65+
PDBG("last line: ignored")
66+
pass
67+
else:
68+
PDBG("first row")
69+
seq_last = seq
70+
71+
72+
if file_out is not None:
73+
file_out.close()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
4+
log_encoding_is_base64=false
5+
6+
if [ ":""$1" = ":-b" ] ; then
7+
log_encoding_is_base64=true
8+
echo "log is using base64 encoding"
9+
elif [ ":""$1" = ":-a" ] ; then
10+
log_encoding_is_base64=false
11+
echo "log is using ascii encoding"
12+
else
13+
echo "usage: ./check_for_data_loss.sh [OPTION] [log_filename]"
14+
echo "\t[OPTION]:"
15+
echo "\t\t" "-a"
16+
echo "\t\t" "\t:log use ascii encoding"
17+
echo "\t\t" "-b"
18+
echo "\t\t" "\t:log use base64 encoding"
19+
20+
echo "\texample: ./check_for_data_loss.sh -b minicom.cap"
21+
return
22+
fi
23+
24+
if [ ":""$2" = ":" ] ; then
25+
log_file="./minicom.cap"
26+
else
27+
log_file="$2"
28+
fi
29+
30+
31+
tmp_file="./tmp.csv"
32+
log_file_cp="${log_file}.cp"
33+
log_file_in="${log_file_cp}.tmp"
34+
35+
echo "log_file:$log_file"
36+
37+
38+
if [ $log_encoding_is_base64 = true ] ; then
39+
cp $log_file $log_file_cp
40+
./process_nicla_bhy2_log_base64.py $log_file_cp > $log_file_in
41+
else
42+
cp $log_file $log_file_in
43+
fi
44+
45+
echo "log_id,seq,ax,ay,az,gx,gy,gz" > $tmp_file
46+
47+
sed 's/.*\([a-zA-Z]\)\([0-9]\)/\1,\2/g' $log_file_in >> $tmp_file
48+
./check_for_data_loss.py $tmp_file
49+
50+
rm -f $tmp_file
51+
rm -f $log_file_cp
52+
rm -f $log_file_in
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
import math
3+
import base64
4+
import struct
5+
import sys
6+
7+
#configurations
8+
#DEBUG = True
9+
DEBUG = False
10+
lines_to_ignore = 500
11+
12+
# the following should be set according to the fimrware settings on the Nicla Senes ME firmware/sketch
13+
data_include_acc = True
14+
data_include_gyr = True
15+
#data_include_gyr = True
16+
data_include_meta_info = True
17+
18+
19+
if (len(sys.argv) > 1):
20+
log_file_name = sys.argv[1]
21+
else:
22+
log_file_name = 'log_nicla_bhy2.txt'
23+
24+
25+
#working variables
26+
file_log = open(log_file_name, 'r')
27+
lines = file_log.readlines()
28+
29+
lineCnt = 0
30+
record_len = 0
31+
record_len_before_encoding = 0
32+
33+
if data_include_meta_info:
34+
record_len_before_encoding += 2
35+
36+
if data_include_acc:
37+
record_len_before_encoding += 6
38+
39+
if data_include_gyr:
40+
record_len_before_encoding += 6
41+
42+
43+
record_len = int(math.ceil(record_len_before_encoding / 3)) * 4
44+
if DEBUG:
45+
print("record_len:", record_len)
46+
47+
48+
DEBUG = False
49+
# Strips the newline character
50+
for line in lines:
51+
lineCnt += 1
52+
53+
if (lineCnt <= lines_to_ignore):
54+
continue
55+
56+
line = line.strip()
57+
len_line = len(line)
58+
59+
if (len_line < record_len) or (line[len_line - 1] != '='):
60+
if (lineCnt < len(lines)):
61+
print("line:", lineCnt, " skipped")
62+
else:
63+
if DEBUG:
64+
print("last line:", lineCnt, " skipped")
65+
continue
66+
67+
line = line[-record_len:]
68+
try:
69+
data_imu = base64.b64decode(line)
70+
if DEBUG:
71+
print(data_imu)
72+
print(len(data_imu))
73+
74+
try:
75+
if (data_include_acc and data_include_gyr):
76+
(log_id, seq, ax,ay,az,gx,gy,gz) = struct.unpack("<cBhhhhhh", data_imu)
77+
log_id = log_id.decode('ascii')
78+
print(log_id.strip(), ",", seq, ",", ax, ",", ay, ",", az, ",", gx,",", gy, ",", gz, sep='')
79+
else:
80+
(log_id,seq, x,y,z) = struct.unpack("<cBhhh", data_imu)
81+
log_id = log_id.decode('ascii')
82+
print(log_id, ",", seq, ",", x,",", y, ",", z, sep='')
83+
except struct.error:
84+
print("error: line:", lineCnt, " unpack")
85+
86+
except base64.binascii.Error:
87+
print("error: line:", lineCnt, " incomplete data")
88+
pass
89+

0 commit comments

Comments
 (0)