Skip to content

Commit

Permalink
Fix bug retreaving data from SD Card
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonacchi committed Feb 21, 2018
1 parent cc77796 commit 917140e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
24 changes: 23 additions & 1 deletion examples/retrieve-data-from-sdcard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import time
import sys

sys.path.insert(0, '..')
from pybpod_rotaryencoder_module.module_api import RotaryEncoderModule

m = RotaryEncoderModule('/dev/ttyACM1')

m = RotaryEncoderModule('COM3')
"""
m.enable_stream()
#print the first 100 outputs
Expand All @@ -21,5 +26,22 @@
m.enable_thresholds([True, False, True, True, False, False, True, True])
print(m.current_position())
"""
print('--------')

m.enable_logging()

time.sleep(4)

m.disable_logging()

time.sleep(2)

data = m.get_logged_data()

while len(data)>0:
for r in data:
print("{0}\t{1}".format(*r))
data = m.get_logged_data()

m.close()
74 changes: 38 additions & 36 deletions pybpod_rotaryencoder_module/module_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def __init__(self, serialport=None):
Constructer of the RotaryEncoderModule object
A a serial connection to the Rotary Encoder board is oppened at the construction of the object.
:ivar str serialport: PC serial port where the module is connect
:ivar str serialport: PC serial port where the module is connect
"""
if serialport: self.open(serialport)

def open(self, serialport):
"""
Opens a serial connection to the Rotary Encoder board.
:ivar str serialport: PC serial port where the module is connect
:ivar str serialport: PC serial port where the module is connect
"""
self.arcom = ArCOM().open(serialport, 115200)
self.arcom.write_char(self.COM_HANDSHAKE)
Expand Down Expand Up @@ -64,16 +64,16 @@ def disable_evt_transmission(self):
return self.arcom.read_uint8()==1


def enable_stream(self):
def enable_stream(self):
"""
Enable the streaming of the position and the time measurements to the USB port.
"""
"""
self.arcom.write_array([ord(self.COM_TOGGLESTREAM), 1])

def disable_stream(self):
"""
Disable the streaming of the position and the time measurements to the USB port.
"""
"""
self.arcom.write_array([ord(self.COM_TOGGLESTREAM), 0])

def read_stream(self):
Expand All @@ -84,23 +84,23 @@ def read_stream(self):
available = self.arcom.bytes_available()

if available>5:
msg = self.arcom.read_bytes_array(available)
msg = self.arcom.read_bytes_array(available)
data_in_bytes = b''.join(msg)
for i in range(0, len(msg), 6):
data_in_bytes = b''.join(msg)
position = int.from_bytes( data_in_bytes[i:i+2], byteorder='little', signed=True)
evt_time = float(int.from_bytes( data_in_bytes[i+2:i+6], byteorder='little', signed=False))/1000.0
position_degrees = self.__pos_2_degrees(position)
data.append([evt_time, position_degrees])
evt_time = float(int.from_bytes( data_in_bytes[i+2:i+6], byteorder='little', signed=False))/1000.0
position_degrees = self.__pos_2_degrees(position)
data.append([evt_time, position_degrees])
return data


def enable_logging(self):
def enable_logging(self):
"""
Enable the logging to the SD Card.
"""
self.arcom.write_array([ord(self.COM_STARTLOGGING)])

def disable_logging(self):
def disable_logging(self):
"""
Disable the logging to the SD Card.
"""
Expand All @@ -113,15 +113,17 @@ def get_logged_data(self):
self.arcom.write_array([ord(self.COM_GETLOGDATA)])
msg = self.arcom.read_bytes_array(4)
n_logs = int.from_bytes( b''.join(msg), byteorder='little', signed=False)
data = []
for i in range(0, n_logs, 6):
msg = self.arcom.read_bytes_array(6)
data = []

for i in range(0, n_logs):
msg = self.arcom.read_bytes_array(8)
data_in_bytes = b''.join(msg)
position = int.from_bytes( data_in_bytes[i:i+2], byteorder='little', signed=True)
evt_time = float(int.from_bytes( data_in_bytes[i+2:i+6], byteorder='little', signed=False))/1000.0
position_degrees = self.__pos_2_degrees(position)

position = int.from_bytes( data_in_bytes[:4], byteorder='little', signed=True)
evt_time = float(int.from_bytes( data_in_bytes[4:], byteorder='little', signed=False))/1000.0
position_degrees = self.__pos_2_degrees(position)
data.append((evt_time, position_degrees))

return data


Expand All @@ -134,18 +136,18 @@ def current_position(self):
data_in_bytes = b''.join(self.arcom.read_bytes_array(2))
ticks = int.from_bytes( data_in_bytes, byteorder='little', signed=True)
return self.__pos_2_degrees(ticks)

def set_zero_position(self):
"""
Set current rotary encoder position to zero.
"""
Set current rotary encoder position to zero.
"""
self.arcom.write_array([ord(self.COM_SETZEROPOS)])

def set_prefix(self, prefix):
"""
:ivar char prefix: One character to be used as prefix.
Set 1-character prefix for module output stream.
"""
"""
self.arcom.write_array([ord(self.COM_SETPREFIX), prefix])
return self.arcom.read_uint8()==1

Expand All @@ -154,41 +156,41 @@ def set_thresholds(self, thresholds):
Set the thresholds values to trigger the events.
:ivar list(int) thresholds: List, in maximum, of 6 thresholds to trigger events.
"""
"""
data = ArduinoTypes.get_uint8_array([ord(self.COM_SETTHRESHOLDS), len(thresholds) ])
data += ArduinoTypes.get_uint16_array([ self.__degrees_2_pos(thresh) for thresh in thresholds])
self.arcom.write_array(data )
return self.arcom.read_uint8()==1

def set_position(self, degrees):
"""
Set the current position in degrees.
:ivar int degrees: current position in degrees.
"""
"""
ticks = self.__degrees_2_pos(degrees)
data = ArduinoTypes.get_uint8_array([ord(self.COM_SETPOS)])
data += ticks.to_bytes(2, byteorder='little', signed=True)

self.arcom.write_array(data)
return self.arcom.read_uint8()==1

def set_wrappoint(self, wrap_point):
"""
Set wrap point (number of tics in a half-rotation)
:ivar int wrap_point: number of tics in a half-rotation.
"""
"""
ticks = self.__degrees_2_pos(wrap_point)
self.arcom.write_array([ord(self.COM_SETWRAPPOINT)]+ ArduinoTypes.get_uint16_array([ticks]) )
return self.arcom.read_uint8()==1

def enable_thresholds(self, thresholds):
"""
Enable the thresholds.
:ivar list(boolean) thresholds: list of 6 booleans indicating which thresholds are active to trigger events.
"""
"""
if len(thresholds)!=8: raise Exception('Thresholds array has to be of length 8')
string = ''.join(map(lambda x: str(int(x)), thresholds))
bits = int(string, 2)
Expand All @@ -203,13 +205,13 @@ def enable_thresholds(self, thresholds):
m = RotaryEncoderModule('/dev/ttyACM1')

#m.start_logging()

#m.stop_logging()

m.enable_stream()



count = 0
while count<100 or True:
data = m.read_stream()
Expand All @@ -219,7 +221,7 @@ def enable_thresholds(self, thresholds):
count += 1

m.disable_stream()

print('set', m.set_position(179))
m.set_zero_position()

Expand Down

0 comments on commit 917140e

Please sign in to comment.