示例#1
0
 def configure(self):
     if self.RE_PORT == 'COM#':
         return
     m = RotaryEncoderModule(self.RE_PORT)
     m.set_zero_position()  # Not necessarily needed
     m.set_thresholds(self.SET_THRESHOLDS)
     m.enable_thresholds(self.ENABLE_THRESHOLDS)
     m.close()
示例#2
0
class BpodRotaryEncoder():
    def __init__(self, com_port, settings, bpod):
        """helper class to deal with rotary encoder module, set thresholds, set and reset position aswell as read prosition

        Args:
            com_port (str): com port (usb) where rotary encoder module is connected to
            settings (TrialParameterHandler object):  the object for all the session parameters from TrialPArameterHandler      
        """
        # rotary encoder settings
        self.com_port = com_port
        self.rotary_encoder = RotaryEncoderModule(self.com_port)
        self.bpod = bpod
        self.reset = settings.RESET_ROTARY_ENCODER
        self.WRAP_POINT = 0

        # set thresholds
        self.all_thresholds = settings.thresholds
        self.enable_thresholds = [(True if x != 0 else False)
                                  for x in self.all_thresholds]
        while len(self.enable_thresholds) < 8:
            self.enable_thresholds.append(False)
        self.events = [
            "RotaryEncoder1_{}".format(x)
            for x in list(range(1,
                                len(self.all_thresholds) + 1))
        ]

    def get_events(self):
        return self.events

    def load_message(self):
        """load reset messag to rotary encoder so bpod can reset rotary encoder position

        Args:
            bpod (Bpod object): 
        """
        rotary_encoder = [
            x for x in self.bpod.modules if x.name == "RotaryEncoder1"
        ][0]
        self.bpod.load_serial_message(rotary_encoder, self.reset,
                                      [ord('Z'), ord('E')])

    def configure(self):
        """loads rotary enoder module with thresholds
        """
        self.rotary_encoder.set_thresholds(self.all_thresholds)
        self.rotary_encoder.enable_thresholds(self.enable_thresholds)

        self.rotary_encoder.enable_evt_transmission()
        self.set_wrap_point(self.WRAP_POINT)

    def close(self):
        self.rotary_encoder.close()

    def enable_stream(self):
        self.rotary_encoder.enable_stream()

    def disable_stream(self):
        self.rotary_encoder.disable_stream()

    def read_stream(self):
        return self.rotary_encoder.read_stream()

    def read_position(self):
        position = self.rotary_encoder.read_stream()
        if len(position) != 0:
            return position[0][2]

    # log postition
    def enable_logging(self):
        self.rotary_encoder.enable_logging()

    def disable_logging(self):
        self.rotary_encoder.disable_logging()

    def get_logging(self):
        return self.rotary_encoder.get_logged_data()

    def set_zero_position(self):
        self.rotary_encoder.set_zero_position()

    def set_wrap_point(self, wrap_point):
        """set the point at which the position is automatically set back to 0 => one half rotation

        Args:
            wrap_point (int): one half rotation wehre set to zero again

        Returns:
            [type]: [description]
        """
        array = np.array([np.uint8(wrap_point)])
        self.rotary_encoder.arcom.write_array([ord('W')] + array)
        return self.rotary_encoder.arcom.read_uint8() == 1
示例#3
0
reset_re = 1

bpod.load_serial_message(rotary_encoder, 1, [ord('Z'), ord('E')])
bpod.load_serial_message(rotary_encoder, 2, [ord("#"), 2])

#====================================
# onfigure encoder
#====================================
rotary_encoder = RotaryEncoderModule('COM6')

ALL_THRESHOLDS = [-10, 10]
ENABLE_THRESHOLDS = [True, True, False, False, False, False, False, False]

rotary_encoder.set_zero_position()  # Not necessarily needed
rotary_encoder.set_thresholds(ALL_THRESHOLDS)
rotary_encoder.enable_thresholds(ENABLE_THRESHOLDS)
rotary_encoder.enable_evt_transmission()
rotary_encoder.close()

movement_left = "RotaryEncoder1_1"
movement_right = "RotaryEncoder1_2"


#====================================
# softcode handler
#====================================
def softcode_handler(data):
    global sph
    if data == 0:
        print("soft code = 0")