- parse responses
This commit is contained in:
@@ -14,38 +14,58 @@ class Pololu1376:
|
||||
|
||||
print("F/W-Version: ", self.get_firmware_version())
|
||||
|
||||
def cmd_send(self, cmd):
|
||||
self.ser.write((cmd + '\r\n').encode())
|
||||
def send(self, data):
|
||||
self.ser.write((data + '\r\n').encode())
|
||||
|
||||
def cmd_recv(self):
|
||||
def recv(self):
|
||||
data = self.ser.readline().decode("utf-8").replace("\n", '').replace("\r", '')
|
||||
return data
|
||||
|
||||
def cmd(self, cmd):
|
||||
self.send(cmd)
|
||||
rsp = self.recv()
|
||||
|
||||
success = False
|
||||
if "." in rsp[0]:
|
||||
print("The last command was understood and no errors are stopping the motor.")
|
||||
success = True
|
||||
elif "!" in rsp[0]:
|
||||
print("The last command was understood and errors are stopping the motor.")
|
||||
success = True
|
||||
elif "?" in rsp[0]:
|
||||
print("The last command was not understood (a Serial Format Error has occurred).")
|
||||
success = False
|
||||
elif rsp is None:
|
||||
print("Timeout.")
|
||||
success = False
|
||||
|
||||
return rsp[1:]
|
||||
|
||||
def go(self):
|
||||
self.cmd_send("GO")
|
||||
self.cmd("GO")
|
||||
|
||||
def motor_forward(self):
|
||||
self.cmd_send("F")
|
||||
self.cmd("F")
|
||||
|
||||
def motor_reverse(self):
|
||||
self.cmd_send("R")
|
||||
self.cmd("R")
|
||||
|
||||
def motor_brake(self):
|
||||
self.cmd_send("B")
|
||||
self.cmd("B")
|
||||
|
||||
def motor_stop(self):
|
||||
self.cmd_send("X")
|
||||
self.cmd("X")
|
||||
|
||||
def motor_set_limit(self, value):
|
||||
self.cmd_send("L" + str(value))
|
||||
self.cmd("L" + str(value))
|
||||
|
||||
def get_firmware_version(self):
|
||||
self.cmd_send("V")
|
||||
return self.cmd_recv()
|
||||
self.cmd("V")
|
||||
return self.recv()
|
||||
|
||||
def get_variable(self, var):
|
||||
self.cmd_send("L" + str(var))
|
||||
return self.cmd_recv()
|
||||
self.cmd("L" + str(var))
|
||||
return self.recv()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user