class Motor: def __init__(self, address=RC_ADDRESS): self.rc = Roboclaw(COMPORT, 115200) self.rc.Open() logger.info('Opened Roboclaw') self.address = address self.dir = None self.speed = INITIAL_SPEED self.throttle(Throttle.NEUTRAL) self.rc.TurnRightMixed(address, 0) def set_speed(self, speed: int) -> None: self.speed = speed logger.info('Set motor speed to %d' % speed) def throttle(self, dir: Throttle) -> None: if dir != self.dir: self.dir = dir logger.debug('Throttling into direction: %s' % dir) if dir == Throttle.FORWARD: try: self.rc.BackwardMixed(self.address, self.speed) except: pass elif dir == Throttle.NEUTRAL: try: self.rc.ForwardMixed(self.address, 0) except: pass elif dir == Throttle.REVERSE: try: self.rc.ForwardMixed(self.address, self.speed) except: pass
class MotorController(object): def __init__(self): # Initialize addresses = [0x80, 0x81] self.addresses = [0x80, 0x81] # Connect to roboclaw serial_port = "/dev/ttyS0" baudrate = 38400 self.roboclaw = Roboclaw(serial_port, baudrate) self.roboclaw.Open() self.robotspeed = 10 def forward(self, drivetime): for address in self.addresses: self.roboclaw.ForwardMixed(address, self.robotspeed) sleep(drivetime) for address in self.addresses: self.roboclaw.ForwardMixed(address, 0) def backward(self, drivetime): for address in self.addresses: self.roboclaw.BackwardMixed(address, self.robotspeed) sleep(drivetime) for address in self.addresses: self.roboclaw.BackwardMixed(address, 0) def right(self, drivetime): self.roboclaw.TurnRightMixed(self.addresses[0], self.robotspeed) self.roboclaw.TurnLeftMixed(self.addresses[1], self.robotspeed) sleep(drivetime) self.roboclaw.TurnRightMixed(self.addresses[0], 0) self.roboclaw.TurnLeftMixed(self.addresses[1], 0) def left(self, drivetime): self.roboclaw.TurnLeftMixed(self.addresses[0], self.robotspeed) self.roboclaw.TurnRightMixed(self.addresses[1], self.robotspeed) sleep(drivetime) self.roboclaw.TurnLeftMixed(self.addresses[0], 0) self.roboclaw.TurnRightMixed(self.addresses[1], 0) def diagonals(self, direction, drivetime): if direction == 'northeast' or direction == "wd": for address in self.addresses: self.roboclaw.ForwardM1(address, self.robotspeed) sleep(drivetime) elif direction == 'northwest' or direction == "wa": for address in self.addresses: self.roboclaw.ForwardM2(address, self.robotspeed) sleep(drivetime) elif direction == 'southeast' or direction == "sd": for address in self.addresses: self.roboclaw.BackwardM2(address, self.robotspeed) sleep(drivetime) elif direction == 'southwest' or direction == "sa": for address in self.addresses: self.roboclaw.BackwardM1(address, self.robotspeed) sleep(drivetime) def rotate(self, direction, drivetime): if direction == 'clockwise' or direction == 'e': for address in self.addresses: self.roboclaw.TurnLeftMixed(address, self.robotspeed) sleep(drivetime) elif direction == 'counter-clockwise' or direction == 'q': for address in self.addresses: self.roboclaw.TurnRightMixed(address, self.robotspeed) sleep(drivetime)
if command.strip() == 'THROTTLE REVERSE': rc.BackwardMixed(address, 32) if input() == 'c': conn.close() # Windows comport name comport = "COM5" rc = Roboclaw(comport, 115200) rc.Open() address = 0x80 rc.ForwardMixed(address, 0) rc.TurnRightMixed(address, 0) serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.bind(('10.24.178.220', 8080)) serv.listen(5) while True: conn, addr = serv.accept() from_client = '' while True: data = conn.recv(4096) if not data: break from_client = data.decode() print(from_client.strip()) if from_client.strip() == "HANDSHAKE":