<commit_before>#from roboclaw import Roboclaw
#import enum
import re
import enum

class subMessagePrefix(enum.Enum):
    LEFT_MOTOR = 'l'
    RIGHT_MOTOR = 'r'
    ACTUATOR = 'a'
    BUCKET = 'b'
    SERVO = 's'

motorMessageRegex = re.compile('([\w])([-]*[\d]+)\|')

class MotorConnection():
    def __init__(self, communicationPort = '/dev/roboclaw', baudRate = 115200,
                 driveAddress = '0x80', bucketAddress = '0x81'):
        print 'MotorConnnection initialized.'
        #self.controller = Roboclaw(communicationPort, baudRate)
        #self.controller.open()
        #self.driveAddress = driveAddress

        #self.bucketAddress = bucketAddress

    def leftDrive(self, speed):
        print 'Left motor at speed:', speed
        #self.controller.SpeedM1(driveAddress, speed)

    def rightDrive(self, speed):
        print 'Right motor at speed:', speed
        #self.controller.SpeedM2(driveAddress, speed)

    def bucketActuate(self, speed):
        print 'Actuator motor at speed:', speed
        #self.controller.SpeedM1(bucketAddress, value)

    def bucketRotate(self, speed):
        print 'Bucket motor at speed:', speed
        #self.controller.SpeedM2(bucketAddress, value)

    def parseMessage(self, message):
        subMessages = motorMessageRegex.findall(message)

        for subMessage in subMessages:
            motorPrefix = subMessage[0]
            speed = subMessage[1]

            if motorPrefix == subMessagePrefix.LEFT_MOTOR:
                self.leftDrive(speed)
            elif motorPrefix == subMessagePrefix.RIGHT_MOTOR:
                self.rightDrive(speed)
            elif motorPrefix == subMessagePrefix.ACTUATOR:
                self.bucketActuate(speed)
            elif motorPrefix == subMessagePrefix.BUCKET:
                self.bucketRotate(speed)
            else:
                print 'MotorPrefix "', motorPrefix, '" unrecognized.'












<commit_msg>Added motor power conversions.<commit_after>#from roboclaw import Roboclaw
#import enum
import re
import enum
import time

DEFAULT_TIME_TO_DELAY_MOTOR = 0.02 # delay for 20 milliseconds
MAX_MOTOR_SPEED = 100 # max speed from client
MAX_MOTOR_POWER = 50000 # max power to motor controllers

class subMessagePrefix(enum.Enum):
    LEFT_MOTOR = 'l'
    RIGHT_MOTOR = 'r'
    ACTUATOR = 'a'
    BUCKET = 'b'
    SERVO = 's'

motorMessageRegex = re.compile('([\w])([-]*[\d]+)\|')

class MotorConnection():
    def __init__(self, communicationPort = '/dev/roboclaw', baudRate = 115200,
                 driveAddress = '0x80', bucketAddress = '0x81'):
        print 'MotorConnnection initialized.'
        #self.controller = Roboclaw(communicationPort, baudRate)
        #self.controller.open()
        #self.driveAddress = driveAddress

        #self.bucketAddress = bucketAddress

        self.leftMotorSpeed = 0
        self.rightMotorSpeed = 0
        self.actuatorMotorSpeed = 0
        self.bucketMotorSpeed = 0

    def directionOfSpeed(self, speed):
        if speed >= 0:
            return 1
        else:
            return -1

    def areSpeedDirectionsEqual(self, speed1, speed2):
        if self.directionOfSpeed(speed1) is self.directionOfSpeed(speed2):
            return True
        else:
            return False

    def convertSpeedToPower(self, speed):
        if abs(speed) > MAX_MOTOR_SPEED:
            return 0
        else:
            powerPercentage = float(speed) / float(MAX_MOTOR_SPEED)
            power = powerPercentage * MAX_MOTOR_POWER
            return power

    def leftDrive(self, speed):
        if not self.areSpeedDirectionsEqual(speed, self.leftMotorSpeed):
            print 'Left motor speed changed direction.'
            # self.controller.SpeedM1(driveAddress, 0)
            # time.sleep(DEFAULT_TIME_TO_DELAY_MOTOR)

        print 'Left motor at speed:', speed
        self.leftMotorSpeed = speed
        power = self.convertSpeedToPower(speed)
        print 'Left motor at power:', power
        #self.controller.SpeedM1(driveAddress, power)

    def rightDrive(self, speed):
        if not self.areSpeedDirectionsEqual(speed, self.rightMotorSpeed):
            print 'Right motor speed changed direction.'
            #self.controller.SpeedM2(driveAddress, 0)
            #time.sleep(DEFAULT_TIME_TO_DELAY_MOTOR)

        print 'Right motor at speed:', speed
        self.rightMotorSpeed = speed
        power = self.convertSpeedToPower(speed)
        print 'Right motor at power:', power
        #self.controller.SpeedM2(driveAddress, power)

    def bucketActuate(self, speed):
        if not self.areSpeedDirectionsEqual(speed, self.actuatorMotorSpeed):
            print 'Actuator motor speed changed direction.'
            # self.controller.SpeedM1(driveAddress, 0)
            # time.sleep(DEFAULT_TIME_TO_DELAY_MOTOR)

        print 'Actuator motor at speed:', speed
        self.actuatorMotorSpeed = speed
        power = self.convertSpeedToPower(speed)
        print 'Actuator motor at power:', power
        #self.controller.SpeedM1(bucketAddress, power)

    def bucketRotate(self, speed):
        if not self.areSpeedDirectionsEqual(speed, self.bucketMotorSpeed):
            print 'Bucket motor speed changed direction.'
            # self.controller.SpeedM2(driveAddress, 0)
            # time.sleep(DEFAULT_TIME_TO_DELAY_MOTOR)

        print 'Bucket motor at speed:', speed
        self.bucketMotorSpeed = speed
        power = self.convertSpeedToPower(speed)
        print 'Bucket motor at power:', power
        #self.controller.SpeedM2(bucketAddress, power)

    def parseMessage(self, message):
        subMessages = motorMessageRegex.findall(message)

        for subMessage in subMessages:
            motorPrefix = subMessage[0]
            speed = int(subMessage[1])

            if motorPrefix == subMessagePrefix.LEFT_MOTOR:
                self.leftDrive(speed)
            elif motorPrefix == subMessagePrefix.RIGHT_MOTOR:
                self.rightDrive(speed)
            elif motorPrefix == subMessagePrefix.ACTUATOR:
                self.bucketActuate(speed)
            elif motorPrefix == subMessagePrefix.BUCKET:
                self.bucketRotate(speed)
            else:
                print 'MotorPrefix "', motorPrefix, '" unrecognized.'
{'fraction_non_alphanumeric': 0.09101087651951376, 'fraction_numerical': 0.011036468330134356, 'mean_word_length': 2.4972035794183447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6240526', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1757, 'n_words': 415}