<commit_before>"""
actuators.py

Classes to control the motors and servos. These classes
are wrapped in a mixer class before being used in the drive loop.

"""

import time
import sys


def map_range(x, X_min, X_max, Y_min, Y_max):
    '''
    Linear mapping between two ranges of values
    '''
    X_range = X_max - X_min
    Y_range = Y_max - Y_min
    XY_ratio = X_range/Y_range

    y = ((x-X_min) / XY_ratio + Y_min) // 1

    return int(y)


class RasPiRobot_Controller:
    def __init__(self, driveLeft, driveRight):
        import rrb3
        rr = RRB3(9, 6)
        leftDir = 0
        rightDir = 0
        if driveLeft < 0:  # change direction if number is negative
            leftDir = 1
        if driveRight < 0:
            rightDir = 1
        rr.set_motors(abs(driveLeft), leftDir, abs(driveRight), rightDir)


class PCA9685_Controller:
    '''
    Adafruit PWM controler.
    This is used for most RC Cars
    '''
    def __init__(self, channel, frequency=60):
        import Adafruit_PCA9685
        # Initialise the PCA9685 using the default address (0x40).
        self.pwm = Adafruit_PCA9685.PCA9685()

        self.pwm.set_pwm_freq(frequency)
        self.channel = channel

    def set_pulse(self, pulse):
        self.pwm.set_pwm(self.channel, 0, pulse)

class Maestro_Controller:
    '''
    Pololu Maestro Servo controller
    Use the MaestroControlCenter to set the speed & acceleration values to 0!
    '''
    import threading

    maestro_device = None
    astar_device = None
    lock = threading.Lock()

    def __init__(self, channel, frequency = 60):
        import serial
        if Maestro_Controller.maestro_device == None:
            Maestro_Controller.maestro_device = serial.Serial('/dev/ttyACM0', 115200)

        self.channel = channel
        self.frequency = frequency

        if Maestro_Controller.astar_device == None:
            Maestro_Controller.astar_device = serial.Serial('/dev/ttyACM2', 115200, timeout= 0.05)

    def set_pulse(self, pulse):
        # Recalculate pulse width from the Adafruit values
        w = pulse * (1 / (self.frequency * 4096)) # in seconds
        w *= 1000 * 1000  # in microseconds
        w *= 4  # in quarter microsenconds the maestro wants
        w = int(w)

        with Maestro_Controller.lock:
            Maestro_Controller.maestro_device.write(bytearray([ 0x84,
                                                            self.channel,
                                                            (w & 0x7F),
                                                            ((w >> 7) & 0x7F)]))

    def set_turn_left(self, v):
        Maestro_Controller.astar_device.write(bytearray('L' if v else 'l', 'ascii'))

    def set_turn_right(self, v):
        Maestro_Controller.astar_device.write(bytearray('R' if v else 'r', 'ascii'))

    def set_headlight(self, v):
        Maestro_Controller.astar_device.write(bytearray('H' if v else 'h', 'ascii'))

    def set_brake(self, v):
        Maestro_Controller.astar_device.write(bytearray('B' if v else 'b', 'ascii'))

    def readline(self):
        ret = None
        # expecting lines like
        # E n nnn n
        if Maestro_Controller.astar_device.inWaiting() > 8:
            ret = Maestro_Controller.astar_device.readline().rstrip()

        return ret

class PWMSteeringActuator:
    #max angle wheels can turn
    LEFT_ANGLE = -1
    RIGHT_ANGLE = 1

    def __init__(self, controller=None,
                       left_pulse=290,
                       right_pulse=490):

        self.controller = controller
        self.left_pulse = left_pulse
        self.right_pulse = right_pulse<commit_msg>Extra locking around the serial communications to the A*<commit_after>"""
actuators.py

Classes to control the motors and servos. These classes
are wrapped in a mixer class before being used in the drive loop.

"""

import time
import sys


def map_range(x, X_min, X_max, Y_min, Y_max):
    '''
    Linear mapping between two ranges of values
    '''
    X_range = X_max - X_min
    Y_range = Y_max - Y_min
    XY_ratio = X_range/Y_range

    y = ((x-X_min) / XY_ratio + Y_min) // 1

    return int(y)


class RasPiRobot_Controller:
    def __init__(self, driveLeft, driveRight):
        import rrb3
        rr = RRB3(9, 6)
        leftDir = 0
        rightDir = 0
        if driveLeft < 0:  # change direction if number is negative
            leftDir = 1
        if driveRight < 0:
            rightDir = 1
        rr.set_motors(abs(driveLeft), leftDir, abs(driveRight), rightDir)


class PCA9685_Controller:
    '''
    Adafruit PWM controler.
    This is used for most RC Cars
    '''
    def __init__(self, channel, frequency=60):
        import Adafruit_PCA9685
        # Initialise the PCA9685 using the default address (0x40).
        self.pwm = Adafruit_PCA9685.PCA9685()

        self.pwm.set_pwm_freq(frequency)
        self.channel = channel

    def set_pulse(self, pulse):
        self.pwm.set_pwm(self.channel, 0, pulse)

class Maestro_Controller:
    '''
    Pololu Maestro Servo controller
    Use the MaestroControlCenter to set the speed & acceleration values to 0!
    '''
    import threading

    maestro_device = None
    astar_device = None
    maestro_lock = threading.Lock()
    astar_lock = threading.Lock()

    def __init__(self, channel, frequency = 60):
        import serial
        if Maestro_Controller.maestro_device == None:
            Maestro_Controller.maestro_device = serial.Serial('/dev/ttyACM0', 115200)

        self.channel = channel
        self.frequency = frequency
        self.lturn = False
        self.rturn = False
        self.headlights = False
        self.brakelights = False

        if Maestro_Controller.astar_device == None:
            Maestro_Controller.astar_device = serial.Serial('/dev/ttyACM2', 115200, timeout= 0.05)

    def set_pulse(self, pulse):
        # Recalculate pulse width from the Adafruit values
        w = pulse * (1 / (self.frequency * 4096)) # in seconds
        w *= 1000 * 1000  # in microseconds
        w *= 4  # in quarter microsenconds the maestro wants
        w = int(w)

        with Maestro_Controller.maestro_lock:
            Maestro_Controller.maestro_device.write(bytearray([ 0x84,
                                                            self.channel,
                                                            (w & 0x7F),
                                                            ((w >> 7) & 0x7F)]))

    def set_turn_left(self, v):
        if self.lturn != v:
            self.lturn = v
            b = bytearray('L' if v else 'l', 'ascii')
            with Maestro_Controller.astar_lock:
                Maestro_Controller.astar_device.write(b)

    def set_turn_right(self, v):
        if self.rturn != v:
            self.rturn = v
            b = bytearray('R' if v else 'r', 'ascii')
            with Maestro_Controller.astar_lock:
                Maestro_Controller.astar_device.write(b)

    def set_headlight(self, v):
        if self.headlights != v:
            self.headlights = v
            b = bytearray('H' if v else 'h', 'ascii')
            with Maestro_Controller.astar_lock:
                Maestro_Controller.astar_device.write(b)

    def set_brake(self, v):
        if self.brakelights != v:
            self.brakelights = v
            b = bytearray('B' if v else 'b', 'ascii')
            with Maestro_Controller.astar_lock:
                Maestro_Controller.astar_device.write(b)

    def readline(self):
        ret = None
        with Maestro_Controller.astar_lock:
            # expecting lines like
            # E n nnn n
            if Maestro_Controller.astar_device.inWaiting() > 8:
                ret = Maestro_Controller.astar_device.readline().rstrip()

        return ret

class PWMSteeringActuator:
    #max angle wheels can turn
    LEFT_ANGLE = -1
    RIGHT_ANGLE = 1

    def __init__(self, controller=None,
                       left_pulse=290,
                       right_pulse=490):

        self.controller = controller
        self.left_pulse = left_pulse
        self.right_pulse = right_pulse
{'fraction_non_alphanumeric': 0.08658596837944664, 'fraction_numerical': 0.021739130434782608, 'mean_word_length': 1.998888888888889, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1957533', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2432, 'n_words': 647}