22import socket
33import json
44import threading
5+ import fcntl
6+ import struct
7+
8+ class MercuryChassisError (Exception ):
9+ pass
510
611class MercuryChassis :
712 def __init__ (self ):
13+ self .ifname = b"wlan0"
814 self ._sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
9- self ._sock .connect (("192.168.123.167" , 9000 ))
15+ try :
16+ self .host = socket .inet_ntoa (fcntl .ioctl (self .server_socket .fileno (), 0x8915 , struct .pack ('256s' , self .ifname [:15 ]))[20 :24 ]) #IP
17+ except :
18+ self .host = "127.0.0.1"
19+ self ._sock .connect ((self .host , 9999 ))
1020 self .recv = threading .Thread (target = self .check_move_end , daemon = True )
1121 self .recv .start ()
1222 self .move_end = False
1323
1424 def check_move_end (self ):
1525 while True :
1626 data = self ._sock .recv (1024 )
17- print (data )
1827 data = json .loads (data )
1928 self .move_end = data
2029
2130 # @property
2231 def is_move_end (self ):
32+ """Is the movement over
33+
34+ Returns:
35+ _type_: _description_
36+ """
2337 return self .move_end
2438
2539 def go_straight (self , speed = 0.25 , exercise_duration = 5 ):
40+ """Forward control
41+
42+ Args:
43+ speed (float, optional): Movement speed. Defaults to 0.25. range 0 ~ 1
44+ exercise_duration (int, optional): Exercise duration. Defaults to 5s.
45+ """
46+ if speed < 0 or speed > 1 :
47+ raise MercuryChassisError ("The movement speed range is 0~1, but the received value is {}" .format (speed ))
2648 command = {"goStraight" : {"time" : exercise_duration , "speed" : speed }}
2749 self ._sock .sendall (json .dumps (command ).encode ())
2850
2951 def go_back (self , speed = 0.25 , exercise_duration = 5 ):
52+ """Back control
53+
54+ Args:
55+ speed (float, optional): Movement speed. Defaults to 0.25. range 0 ~ 1
56+ exercise_duration (int, optional): Exercise duration. Defaults to 5s.
57+ """
58+ if speed < 0 or speed > 1 :
59+ raise MercuryChassisError ("The movement speed range is 0~1, but the received value is {}" .format (speed ))
3060 command = {"goBack" : {"time" : exercise_duration , "speed" : speed }}
3161 self ._sock .sendall (json .dumps (command ).encode ())
3262
3363 def turn_left (self , speed = 0.5 , exercise_duration = 5 ):
64+ """left turn control
65+
66+ Args:
67+ speed (float, optional): Movement speed. Defaults to 0.25. range 0 ~ 1
68+ exercise_duration (int, optional): Exercise duration. Defaults to 5s.
69+ """
70+ if speed < 0 or speed > 1 :
71+ raise MercuryChassisError ("The movement speed range is 0~1, but the received value is {}" .format (speed ))
3472 command = {"turnLeft" : {"time" : exercise_duration , "speed" : speed }}
3573 self ._sock .sendall (json .dumps (command ).encode ())
3674
3775 def turn_right (self , speed = 0.5 , exercise_duration = 5 ):
76+ """_summary_
77+
78+ Args:
79+ speed (float, optional): Movement speed. Defaults to 0.25. range 0 ~ 1
80+ exercise_duration (int, optional): Exercise duration. Defaults to 5s.
81+ """
82+ if speed < 0 or speed > 1 :
83+ raise MercuryChassisError ("The movement speed range is 0~1, but the received value is {}" .format (speed ))
3884 command = {"turnRight" : {"time" : exercise_duration , "speed" : speed }}
3985 self ._sock .sendall (json .dumps (command ).encode ())
4086
4187 def stop (self ):
88+ """stop motion"""
4289 command = {"stop" : True }
4390 self ._sock .sendall (json .dumps (command ).encode ())
91+
92+ def init_position (self ,position_x ,position_y ,orientation_z ,orientation_w ,covariance ):
93+ """Set navigation starting position
94+
95+ Args:
96+ position_x (_type_): _description_
97+ position_y (_type_): _description_
98+ orientation_z (_type_): _description_
99+ orientation_w (_type_): _description_
100+ covariance (_type_): _description_
101+ """
102+ command = {"initPosition" : {"x" : position_x , "y" : position_y , "o_z" : orientation_z , "o_w" : orientation_w , "cov" : covariance }}
103+ self ._sock .sendall (json .dumps (command ).encode ())
104+
105+ def goto_position (self ,position_x ,position_y ,orientation_z ,orientation_w ):
106+ """Set navigation target location
107+
108+ Args:
109+ position_x (_type_): _description_
110+ position_y (_type_): _description_
111+ orientation_z (_type_): _description_
112+ orientation_w (_type_): _description_
113+ """
114+ command = {"goToPosition" : {"x" : position_x , "y" : position_y , "o_z" : orientation_z , "o_w" : orientation_w }}
115+ self ._sock .sendall (json .dumps (command ).encode ())
116+
117+ def cancel_navigation (self ):
118+ """Cancel navigation
119+ """
120+ command = {"movebaseCancel" : True }
121+ self ._sock .sendall (json .dumps (command ).encode ())
122+
123+ def getRobotVersion (self ):
124+ """"""
125+ command = {"getRobotVersion" : True }
126+ self ._sock .sendall (json .dumps (command ).encode ())
127+ while True :
128+ data = self .move_end .get ("getRobotVersion" , None )
129+ if data :
130+ return self .move_end ["getRobotVersion" ]["return" ]
131+
132+ def getSystemVersion (self ):
133+ command = {"getSystemVersion" : True }
134+ self ._sock .sendall (json .dumps (command ).encode ())
135+ while True :
136+ data = self .move_end .get ("getSystemVersion" , None )
137+ if data :
138+ return self .move_end ["getSystemVersion" ]["return" ]
139+
140+ def batteryState (self ):
141+ """Get battery level
142+ """
143+ command = {"batteryState" : True }
144+ self ._sock .sendall (json .dumps (command ).encode ())
145+ while True :
146+ data = self .move_end .get ("batteryState" , None )
147+ if data :
148+ return self .move_end ["batteryState" ]["return" ]
149+
150+
44151
0 commit comments