- parse responses
This commit is contained in:
@@ -14,38 +14,58 @@ class Pololu1376:
|
|||||||
|
|
||||||
print("F/W-Version: ", self.get_firmware_version())
|
print("F/W-Version: ", self.get_firmware_version())
|
||||||
|
|
||||||
def cmd_send(self, cmd):
|
def send(self, data):
|
||||||
self.ser.write((cmd + '\r\n').encode())
|
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", '')
|
data = self.ser.readline().decode("utf-8").replace("\n", '').replace("\r", '')
|
||||||
return data
|
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):
|
def go(self):
|
||||||
self.cmd_send("GO")
|
self.cmd("GO")
|
||||||
|
|
||||||
def motor_forward(self):
|
def motor_forward(self):
|
||||||
self.cmd_send("F")
|
self.cmd("F")
|
||||||
|
|
||||||
def motor_reverse(self):
|
def motor_reverse(self):
|
||||||
self.cmd_send("R")
|
self.cmd("R")
|
||||||
|
|
||||||
def motor_brake(self):
|
def motor_brake(self):
|
||||||
self.cmd_send("B")
|
self.cmd("B")
|
||||||
|
|
||||||
def motor_stop(self):
|
def motor_stop(self):
|
||||||
self.cmd_send("X")
|
self.cmd("X")
|
||||||
|
|
||||||
def motor_set_limit(self, value):
|
def motor_set_limit(self, value):
|
||||||
self.cmd_send("L" + str(value))
|
self.cmd("L" + str(value))
|
||||||
|
|
||||||
def get_firmware_version(self):
|
def get_firmware_version(self):
|
||||||
self.cmd_send("V")
|
self.cmd("V")
|
||||||
return self.cmd_recv()
|
return self.recv()
|
||||||
|
|
||||||
def get_variable(self, var):
|
def get_variable(self, var):
|
||||||
self.cmd_send("L" + str(var))
|
self.cmd("L" + str(var))
|
||||||
return self.cmd_recv()
|
return self.recv()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user