1+
2+ import socket
3+ import json
4+ import threading
5+
6+ class MercuryChassis :
7+ def __init__ (self ):
8+ self ._sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
9+ self ._sock .connect (("192.168.99.102" , 9000 ))
10+ self .recv = threading .Thread (self .is_move_end , daemon = True )
11+ self .recv .start ()
12+ self .move_end = False
13+
14+ def is_move_end (self ):
15+ while True :
16+ data = self ._sock .recv (1024 )
17+ data = json .load (data )
18+ key = data .keys ()[0 ]
19+ self .move_end = key ["return" ]
20+
21+ @property
22+ def move_end (self ):
23+ return self .move_end
24+
25+ def go_straight (self , speed = 0.5 , exercise_duration = 5 ):
26+ command = {"goStraight" : {"time" : exercise_duration , "speed" : speed }}
27+ self ._sock .sendall (json .dump (command ))
28+
29+ def go_back (self , speed = 0.5 , exercise_duration = 5 ):
30+ command = {"goBack" : {"time" : exercise_duration , "speed" : speed }}
31+ self ._sock .sendall (json .dump (command ))
32+
33+ def turn_left (self , speed = 0.5 , exercise_duration = 5 ):
34+ command = {"turnLeft" : {"time" : exercise_duration , "speed" : speed }}
35+ self ._sock .sendall (json .dump (command ))
36+
37+ def go_straight (self , speed = 0.5 , exercise_duration = 5 ):
38+ command = {"turnRight" : {"time" : exercise_duration , "speed" : speed }}
39+ self ._sock .sendall (json .dump (command ))
40+
41+ def stop (self ):
42+ command = {"stop" : True }
43+ self ._sock .sendall (json .dump (command ))
44+
0 commit comments