Beispiel #1
0
 def update_new_port(self):
     """
     Upon update of LQ specified port, reinstantiates the device class.
     """
     del self.dev
     self.dev = PololuMaestroDevice(port=self.settings['port'])
     print('port updated:', self.settings['port'])
Beispiel #2
0
class PololuMaestroServoHW(HardwareComponent):

    name = 'pololu_maestro_servo'

    def setup(self):
        S = self.settings

        S.New(name='port', initial='COM1', dtype=str, ro=False)
        S.New(name='servo_num', dtype=int, initial=0, ro=False)
        S.New(name="raw_position", dtype=int, ro=False)

        S.New('raw_min', dtype=float, initial=2000, ro=False)
        S.New('raw_max', dtype=float, initial=10000, ro=False)
        S.New('pos_scale', dtype=float, initial=180, ro=False)
        S.New('position', dtype=float, ro=False)

        def pos_rev_func(new_pos, old_vals):
            raw, rmin, rmax, scale = old_vals
            new_raw = rmin + new_pos / scale * (rmax - rmin)
            return (new_raw, rmin, rmax, scale)

        S.position.connect_lq_math(
            (S.raw_position, S.raw_min, S.raw_max, S.pos_scale),
            func=lambda raw, rmin, rmax, scale: scale * ((raw - rmin) /
                                                         (rmax - rmin)),
            reverse_func=pos_rev_func)

        S.New('jog_step', dtype=float, initial=10.0)

        self.add_operation("Jog +", self.jog_fwd)
        self.add_operation("Jog -", self.jog_bkwd)

    def connect(self):
        self.dev = PololuMaestroDevice(port=self.settings['port'])

        self.settings.raw_position.connect_to_hardware(
            read_func=lambda: self.dev.read_position(self.settings['servo_num']
                                                     ),
            write_func=lambda pos: self.dev.write_position(
                self.settings['servo_num'], pos))

        self.settings.raw_position.read_from_hardware()

    def disconnect(self):

        self.settings.disconnect_all_from_hardware()

        if hasattr(self, 'dev'):
            self.dev.close()
            del self.dev

    def jog_fwd(self):
        S = self.settings
        S['position'] += S['jog_step']

    def jog_bkwd(self):
        S = self.settings
        S['position'] -= S['jog_step']
Beispiel #3
0
    def connect(self):
        self.dev = PololuMaestroDevice(port=self.settings['port'])

        self.settings.raw_position.connect_to_hardware(
            read_func=lambda: self.dev.read_position(self.settings['servo_num']
                                                     ),
            write_func=lambda pos: self.dev.write_position(
                self.settings['servo_num'], pos))

        self.settings.raw_position.read_from_hardware()
Beispiel #4
0
    def connect(self):
        self.dev = PololuMaestroDevice(port=self.settings['port'])

        for servo_num, name in self.servo_names:

            raw = self.settings.get_lq(name + "_raw")
            raw.connect_to_hardware(
                read_func=lambda n=servo_num: self.dev.read_position(n),
                write_func=lambda pos, n=servo_num: self.dev.write_position(
                    n, pos))

            raw.read_from_hardware()
Beispiel #5
0
    def connect(self):
        """
        Instantiates device class object, sets up read/write signals, sets up listeners which: update software servo limits, 
        and reinstantiates device object, should the port value be updated. Finally, the function reads all values from hardware.
        """
        self.dev = PololuMaestroDevice(port=self.settings['port'])

        for i in range(self.servo_range):
            self.settings.get_lq(
                'servo{}_position'.format(i)).connect_to_hardware(
                    write_func=getattr(self, 'write_position{}'.format(i)),
                    read_func=getattr(self, 'read_position{}'.format(i)))
            self.settings.get_lq('servo{}_type'.format(i)).add_listener(
                lambda servo_number=i: self.update_min_max(servo_number))
            self.settings.get_lq('servo{}_toggle'.format(i)).add_listener(
                lambda servo_number=i: self.toggle_servo(servo_number))

        self.settings.get_lq('flip_mirror').connect_to_hardware(
            write_func=self.write_position5, read_func=self.read_position5)

        self.settings.get_lq('port').add_listener(self.update_new_port)

        self.read_from_hardware()
Beispiel #6
0
class PololuHW(HardwareComponent):

    name = 'pololu_servo_hw'

    servo_type_choices = ("Linear", "Rotary")

    servo_type_limit = {'Rotary': (544, 2544), 'Linear': (1008, 2000)}

    servo_toggle_settings = {
        'Rotary': (600, 2100),
        'Linear': (1200, 1800),
    }

    def setup(self):
        """Sets up logged quantities. Sets presets and constants."""

        self.settings.New(name='port',
                          initial='/dev/tty.usbmodem2333',
                          dtype=str,
                          ro=False)

        ## Increase/decrease number of servo slots by modifying the below value.
        self.servo_range = 3

        for i in range(self.servo_range):
            self.settings.New(name="servo{}_type".format(i),
                              dtype=str,
                              initial='Linear',
                              choices=self.servo_type_choices,
                              ro=False)
            _vmin, _vmax = self.servo_type_limit[self.settings[
                'servo{}_type'.format(i)]]
            self.settings.New(name="servo{}_position".format(i),
                              dtype=int,
                              vmin=_vmin,
                              vmax=_vmax,
                              ro=False)
            self.settings.New(name="servo{}_toggle".format(i),
                              dtype=bool,
                              initial=False,
                              ro=False)

            self.settings.New(name="servo{}_toggle_on".format(i),
                              dtype=int,
                              initial=self.servo_toggle_settings[self.settings[
                                  "servo{}_type".format(i)]][0],
                              ro=False)

            self.settings.New(name="servo{}_toggle_off".format(i),
                              dtype=int,
                              initial=self.servo_toggle_settings[self.settings[
                                  "servo{}_type".format(i)]][1],
                              ro=False)

        ## In my particular setup, I want to override the default value set by the above for loop in the case of servo_0
        self.settings.get_lq('servo0_type').update_value('Rotary')
        self.update_min_max(0)

        # Flip Mirror
        self.flip_mirror_chan = 5
        self.settings.New(name='flip_mirror',
                          dtype=bool,
                          initial=True,
                          ro=False)

    def connect(self):
        """
        Instantiates device class object, sets up read/write signals, sets up listeners which: update software servo limits, 
        and reinstantiates device object, should the port value be updated. Finally, the function reads all values from hardware.
        """
        self.dev = PololuMaestroDevice(port=self.settings['port'])

        for i in range(self.servo_range):
            self.settings.get_lq(
                'servo{}_position'.format(i)).connect_to_hardware(
                    write_func=getattr(self, 'write_position{}'.format(i)),
                    read_func=getattr(self, 'read_position{}'.format(i)))
            self.settings.get_lq('servo{}_type'.format(i)).add_listener(
                lambda servo_number=i: self.update_min_max(servo_number))
            self.settings.get_lq('servo{}_toggle'.format(i)).add_listener(
                lambda servo_number=i: self.toggle_servo(servo_number))

        self.settings.get_lq('flip_mirror').connect_to_hardware(
            write_func=self.write_position5, read_func=self.read_position5)

        self.settings.get_lq('port').add_listener(self.update_new_port)

        self.read_from_hardware()

    def disconnect(self):
        if self.debug_mode:
            print('disconnecting Pololu')

        for lq in self.settings.as_list():
            lq.hardware_read_func = None
            lq.hardware_set_func = None

        #self.dev.close() #deletes serial connection object

    def update_flip_mirror(self, up):
        if up:
            self.dev.write_position(self.flip_mirror_chan, 1600)
        else:
            self.dev.write_position(self.flip_mirror_chan, 100)

    def update_new_port(self):
        """
        Upon update of LQ specified port, reinstantiates the device class.
        """
        del self.dev
        self.dev = PololuMaestroDevice(port=self.settings['port'])
        print('port updated:', self.settings['port'])

    def update_min_max(self, servo_number):
        """
        Reads the servo type from the logged quantity, updates servo specific software limits.
        """
        servo_type = self.settings['servo{}_type'.format(servo_number)]
        vmin, vmax = self.servo_type_limit[servo_type]
        self.settings.get_lq(
            "servo{}_position".format(servo_number)).change_min_max(
                vmin, vmax)

    def toggle_servo(self, servo_number):
        servo_type = self.settings['servo{}_type'.format(servo_number)]
        value = self.settings['servo{}_toggle'.format(servo_number)]
        off, on = self.settings['servo{}_toggle_off'.format(
            servo_number)], self.settings['servo{}_toggle_on'.format(
                servo_number)]
        if value:
            self.dev.write_position(servo_number, on)
        else:
            self.dev.write_position(servo_number, off)
        self.read_from_hardware()

    def write_position0(self, position):
        self.dev.write_position(0, target=position)

    def write_position1(self, position):
        self.dev.write_position(1, target=position)

    def write_position2(self, position):
        self.dev.write_position(2, target=position)

    def write_position3(self, position):
        self.dev.write_position(3, target=position)

    def write_position4(self, position):
        self.dev.write_position(4, target=position)

    def write_position5(self, position):
        self.dev.write_position(5, target=position)

    def read_position0(self):
        return self.dev.read_position(0) / 4

    def read_position1(self):
        return self.dev.read_position(1) / 4

    def read_position2(self):
        return self.dev.read_position(2) / 4

    def read_position3(self):
        return self.dev.read_position(3) / 4

    def read_position4(self):
        return self.dev.read_position(4) / 4

    def read_position5(self):
        return self.dev.read_position(5) / 4
Beispiel #7
0
class PololuMaestroHW(HardwareComponent):

    name = 'pololu_maestro'

    def __init__(self, app, debug=False, name=None, servo_names=None):
        self.servo_names = servo_names
        if not self.servo_names:
            self.servo_names = [(i, "ch{}".format(i)) for i in range(6)]
        HardwareComponent.__init__(self, app, debug=debug, name=name)

    def setup(self):
        S = self.settings

        S.New(name='port', initial='COM1', dtype=str, ro=False)

        for servo_num, name in self.servo_names:
            raw = S.New(
                name + "_raw", dtype=int, ro=False
            )  # raw value of servo position output (in units of 1/4 us for PWM pulses)
            raw_min = S.New(name + '_raw_min',
                            dtype=float,
                            initial=2000,
                            ro=False)
            raw_max = S.New(name + '_raw_max',
                            dtype=float,
                            initial=10000,
                            ro=False)
            pos_scale = S.New(name + '_pos_scale',
                              dtype=float,
                              initial=280,
                              ro=False)
            pos = S.New(name + '_position', dtype=float, ro=False)

            def pos_rev_func(new_pos, old_vals):
                raw, rmin, rmax, scale = old_vals
                new_raw = rmin + new_pos / scale * (rmax - rmin)
                return (new_raw, rmin, rmax, scale)

            pos.connect_lq_math((raw, raw_min, raw_max, pos_scale),
                                func=lambda raw, rmin, rmax, scale: scale *
                                ((raw - rmin) / (rmax - rmin)),
                                reverse_func=pos_rev_func)

            S.New(name + '_jog_step', dtype=float, initial=10.0)

            self.add_operation(name + " Jog +", lambda n=name: self.jog_fwd(n))
            self.add_operation(name + " Jog -",
                               lambda n=name: self.jog_bkwd(n))

    def connect(self):
        self.dev = PololuMaestroDevice(port=self.settings['port'])

        for servo_num, name in self.servo_names:

            raw = self.settings.get_lq(name + "_raw")
            raw.connect_to_hardware(
                read_func=lambda n=servo_num: self.dev.read_position(n),
                write_func=lambda pos, n=servo_num: self.dev.write_position(
                    n, pos))

            raw.read_from_hardware()

    def disconnect(self):

        self.settings.disconnect_all_from_hardware()

        if hasattr(self, 'dev'):
            self.dev.close()
            del self.dev

    def jog_fwd(self, servo_name):
        S = self.settings
        S[servo_name + '_position'] += S[servo_name + '_jog_step']

    def jog_bkwd(self, servo_name):
        S = self.settings
        S[servo_name + '_position'] -= S[servo_name + '_jog_step']