-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_threadmill.py
More file actions
133 lines (111 loc) · 3.07 KB
/
test_threadmill.py
File metadata and controls
133 lines (111 loc) · 3.07 KB
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
"""test_threadmill.py:
Read the data from thread-mill and analyze it.
"""
from __future__ import print_function
__author__ = "Dilawar Singh"
__copyright__ = "Copyright 2017-, Dilawar Singh"
__version__ = "1.0.0"
__maintainer__ = "Dilawar Singh"
__email__ = "dilawars@ncbs.res.in"
__status__ = "Development"
import sys
import logging
import os
import numpy as np
import time
import datetime
import scipy.signal as sig
import gnuplotlib as gpl
sys.path.append( 'pyblink' )
import arduino
res_file_ = '__results__tmp__.csv'
def stamp( ):
return datetime.datetime.now().isoformat( )
def line_to_data(line):
"""First element is timestamp, rest the data etc.
"""
if '>' in line:
return [ ]
line = filter(None, line.split(',') )
data = [ ]
for x in line:
try:
data.append( int(x) )
except ValueError as e:
data.append( x )
return data
def _readline( ser ):
eol = b'\n'
leneol = len(eol)
line = bytearray()
while True:
c = ser.read(1)
if c:
line += c
if line[-leneol:] == eol:
break
else:
break
if( len(line) > 100 ):
break
return bytes(line)
def compute_speed( tvec, yvec ):
"""Compute speed """
ydiff = np.diff( yvec )
minus1Ps = np.where( ydiff == -1 )
minus1Ts = tvec[ minus1Ps ]
tps = np.diff( minus1Ts )
velocity = np.mean( 4.1 / tps ) # m / sec
if np.isnan( velocity ):
velocity = 0.0
return velocity
def calculate_motion( t, dx, dy ):
"""Caculate speed and direction of motion """
t, dx, dy = [ np.array( x ) for x in [ t, dx, dy] ]
s = np.sqrt( dx ** 2.0 + dy ** 2.0 )
ds = np.mean( s )
if np.mean( dx ) < 0:
ds = -ds
dt = t[-1] - t[0]
return ds / dt, 1.0
def calculate_direction( s1, s2 ):
d = -1
if s1 > s2:
d = 1
else:
d = -1
return d
def main( port, baud ):
print( '[INFO] Using port %s baudrate %d' % (port, baud ) )
tvec, motion1, motion2 = [ ], [ ], [ ]
ar = arduino.ArduinoPort( port )
ar.open( )
speed, direction = 0.0, 0
with open( res_file_, 'w' ) as f:
f.write( "time s1 s2 v1 v2 dir\n" )
speedVec = [ ]
while True:
line = ar.read_line( )
data = line_to_data( line )
if len( data ) < 7:
continue
tvec.append( data[0] )
motion1.append( data[5] )
motion2.append( data[6] )
try:
N = 20
speed,direction = calculate_motion( tvec[-N:], motion1[-N:], motion2[-N:] )
speedVec.append( speed )
gpl.plot(
np.array( tvec[-1000:] ), np.array( speedVec[-1000:])
, set = ( 'yrange [-0.01:0.01]' )
)
except Exception as e:
speed = 0.0
direction = 0
print( e )
print( '%.5f %s' % (speed, direction))
if __name__ == '__main__':
port = sys.argv[1]
baud = 38400
main( port, baud )