Created
November 14, 2019 08:54
-
-
Save ahmdrz/39428fca77ab8d6ec83ceb7903252337 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import time | |
def lsb(value): | |
return value & 0xff | |
def msb(value): | |
return value >> 8 | |
class SerialConnection: | |
def __init__(self, port, baudrate): | |
self._connection = serial.Serial() | |
self._connection.port = port | |
self._connection.baudrate = baudrate | |
self._is_open = False | |
def open(self): | |
self._connection.open() | |
self._is_open = True | |
def close(self): | |
self._connection.close() | |
self._is_open = False | |
def _write(self, values): | |
for b in values: | |
self._connection.write(chr(b)) | |
def send(self, id, reg, val): | |
packets = [0xFF, 0xFF, id, 0x03 + len(val), 0x03, reg] + val | |
checksum = 0 | |
for i in range(2, len(packets)): | |
packet = packets[i] | |
checksum = checksum + packet | |
checksum = (~checksum) & 0xFF | |
packets.append(checksum) | |
self._write(packets) | |
time.sleep(0.00002) | |
def moving_speed(self, id, speed): | |
self.send(id, 0x20, [lsb(speed), msb(speed)]) | |
def goal_position(self, id, position): | |
self.send(id, 0x1E, [lsb(position), msb(position)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment