Votes Internally!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.3 KiB

6 years ago
6 years ago
6 years ago
  1. import socket
  2. import cmd
  3. import threading
  4. import time
  5. from packet import Packet, CMD
  6. class KAThread(threading.Thread):
  7. def run(self):
  8. tbot = self._args[0]
  9. pkt = Packet()
  10. pkt.write_ubyte(CMD.KEEPALIVE)
  11. while True:
  12. if tbot.addr:
  13. try:
  14. time.sleep(0.1)
  15. tbot.socket.sendto(bytes(pkt), tbot.addr)
  16. except Exception:
  17. pass
  18. class TBot(cmd.Cmd):
  19. socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  20. addr = None
  21. def do_connect(self, rest):
  22. 'connect [<host>[:<port>]]'
  23. if rest:
  24. host, _, port = rest.partition(':')
  25. if not port:
  26. port = '13676'
  27. self.addr = (host, int(port))
  28. else:
  29. self.addr = None
  30. print('Remote set to', self.addr)
  31. def do_fw(self, rest):
  32. 'fw <velocity:m/s>'
  33. if not self.addr:
  34. print('Not connected')
  35. return
  36. if not rest:
  37. rest = '0'
  38. pkt = Packet()
  39. pkt.write_ubyte(CMD.MOTION)
  40. pkt.write_double(float(rest))
  41. pkt.write_double(0.0)
  42. self.socket.sendto(bytes(pkt), self.addr)
  43. def do_lt(self, rest):
  44. 'lt <velocity:rad/s>'
  45. if not self.addr:
  46. print('Not connected')
  47. return
  48. if not rest:
  49. rest = '0'
  50. pkt = Packet()
  51. pkt.write_ubyte(CMD.MOTION)
  52. pkt.write_double(0.0)
  53. pkt.write_double(float(rest))
  54. self.socket.sendto(bytes(pkt), self.addr)
  55. def do_snd(self, rest):
  56. 'snd <sound, [0,6]>'
  57. if not self.addr:
  58. print('Not connected')
  59. return
  60. if not rest:
  61. rest = '0'
  62. pkt = Packet()
  63. pkt.write_ubyte(CMD.SOUND)
  64. pkt.write_ubyte(int(rest))
  65. self.socket.sendto(bytes(pkt), self.addr)
  66. def do_EOF(self, rest):
  67. print('Goodbye.')
  68. if self.addr:
  69. pkt = Packet()
  70. pkt.write_ubyte(CMD.MOTION)
  71. pkt.write_double(0.0)
  72. pkt.write_double(0.0)
  73. self.socket.sendto(bytes(pkt), self.addr)
  74. exit()
  75. if __name__ == '__main__':
  76. tbot = TBot()
  77. tthr = KAThread(args=(tbot,))
  78. tthr.setDaemon(True)
  79. tthr.start()
  80. tbot.cmdloop()