1
1
import math
2
2
3
+ import gc
4
+
3
5
from uart import uart
4
6
import _thread
5
7
from time import sleep_ms
@@ -42,6 +44,15 @@ def __init__(self):
42
44
self .roll = None
43
45
self .pitch = None
44
46
self .yaw = None
47
+ self .x = None
48
+ self .y = None
49
+ self .theta = None
50
+ self .ax = None
51
+ self .ay = None
52
+ self .az = None
53
+ self .gx = None
54
+ self .gy = None
55
+ self .gz = None
45
56
self .left_tof = None
46
57
self .center_left_tof = None
47
58
self .center_tof = None
@@ -65,7 +76,11 @@ def begin(self) -> int:
65
76
self ._begin_update_thread ()
66
77
sleep_ms (100 )
67
78
self ._reset_hw ()
79
+ while uart .any ():
80
+ uart .read (1 )
68
81
sleep_ms (1000 )
82
+ while self .last_ack != 0x00 :
83
+ sleep_ms (20 )
69
84
self .set_illuminator (True )
70
85
return 0
71
86
@@ -87,23 +102,45 @@ def _stop_update_thread(cls):
87
102
"""
88
103
cls ._update_thread_running = False
89
104
90
- def rotate (self , angle : float ):
105
+ def _wait_for_target (self ):
106
+ while not self .is_target_reached ():
107
+ pass
108
+
109
+ def is_target_reached (self ) -> bool :
110
+ if self .last_ack != ord ('M' ) and self .last_ack != ord ('R' ):
111
+ sleep_ms (50 )
112
+ return False
113
+ else :
114
+ self .packeter .packetC1B (ord ('X' ), ord ('K' ))
115
+ uart .write (self .packeter .msg [0 :self .packeter .msg_size ])
116
+ sleep_ms (200 )
117
+ return True
118
+
119
+ def rotate (self , angle : float , blocking : bool = True ):
91
120
"""
92
121
Rotates the robot by given angle
93
122
:param angle:
123
+ :param blocking:
94
124
:return:
95
125
"""
126
+ sleep_ms (200 )
96
127
self .packeter .packetC1F (ord ('R' ), angle )
97
128
uart .write (self .packeter .msg [0 :self .packeter .msg_size ])
129
+ if blocking :
130
+ self ._wait_for_target ()
98
131
99
- def move (self , distance : float ):
132
+ def move (self , distance : float , blocking : bool = True ):
100
133
"""
101
134
Moves the robot by given distance
102
135
:param distance:
136
+ :param blocking:
103
137
:return:
104
138
"""
139
+ sleep_ms (200 )
105
140
self .packeter .packetC1F (ord ('G' ), distance )
106
141
uart .write (self .packeter .msg [0 :self .packeter .msg_size ])
142
+ if blocking :
143
+ self ._wait_for_target ()
107
144
108
145
def stop (self ):
109
146
"""
@@ -119,6 +156,10 @@ def stop(self):
119
156
# stop the update thrad
120
157
self ._stop_update_thread ()
121
158
159
+ # delete instance
160
+ del self .__class__ .instance
161
+ gc .collect ()
162
+
122
163
@staticmethod
123
164
def _reset_hw ():
124
165
"""
@@ -156,19 +197,6 @@ def set_wheels_speed(self, left_speed: float, right_speed: float, unit: str = 'r
156
197
self .packeter .packetC2F (ord ('J' ), left_speed , right_speed )
157
198
uart .write (self .packeter .msg [0 :self .packeter .msg_size ])
158
199
159
- def set_pid (self , side : str , kp : float , ki : float , kd : float ):
160
- """
161
- Sets motor PID parameters. Side can be 'L' or 'R'
162
- :param side:
163
- :param kp:
164
- :param ki:
165
- :param kd:
166
- :return:
167
- """
168
-
169
- self .packeter .packetC1B3F (ord ('P' ), ord (side ), kp , ki , kd )
170
- uart .write (self .packeter .msg [0 :self .packeter .msg_size ])
171
-
172
200
def get_orientation (self ) -> (float , float , float ):
173
201
"""
174
202
Returns the orientation of the IMU
@@ -177,6 +205,27 @@ def get_orientation(self) -> (float, float, float):
177
205
178
206
return self .roll , self .pitch , self .yaw
179
207
208
+ def get_accelerations (self ) -> (float , float , float ):
209
+ """
210
+ Returns the 3-axial acceleration of the IMU
211
+ :return: ax, ay, az
212
+ """
213
+ return self .ax , self .ay , self .az
214
+
215
+ def get_gyros (self ) -> (float , float , float ):
216
+ """
217
+ Returns the 3-axial angular acceleration of the IMU
218
+ :return: gx, gy, gz
219
+ """
220
+ return self .gx , self .gy , self .gz
221
+
222
+ def get_imu (self ) -> (float , float , float , float , float , float ):
223
+ """
224
+ Returns all the IMUs readouts
225
+ :return: ax, ay, az, gx, gy, gz
226
+ """
227
+ return self .ax , self .ay , self .az , self .gx , self .gy , self .gz
228
+
180
229
def get_line_sensors (self ) -> (int , int , int ):
181
230
"""
182
231
Returns the line sensors readout
@@ -202,6 +251,25 @@ def get_drive_speed(self) -> (float, float):
202
251
"""
203
252
return self .linear_velocity , self .angular_velocity
204
253
254
+ def reset_pose (self , x : float , y : float , theta : float ):
255
+ """
256
+ Resets the robot pose
257
+ :param x: x coordinate of the robot
258
+ :param y: y coordinate of the robot
259
+ :param theta: angle of the robot
260
+ :return:
261
+ """
262
+ self .packeter .packetC3F (ord ('Z' ), x , y , theta )
263
+ uart .write (self .packeter .msg [0 :self .packeter .msg_size ])
264
+ sleep_ms (1000 )
265
+
266
+ def get_pose (self ) -> (float , float , float ):
267
+ """
268
+ Returns the current pose of the robot
269
+ :return: x, y, theta
270
+ """
271
+ return self .x , self .y , self .theta
272
+
205
273
def set_servo_positions (self , a_position : int , b_position : int ):
206
274
"""
207
275
Sets A/B servomotor angle
@@ -301,7 +369,7 @@ def _parse_message(self) -> int:
301
369
_ , self .red , self .green , self .blue = self .packeter .unpacketC3I ()
302
370
elif code == ord ('i' ):
303
371
# imu
304
- _ , ax , ay , az , gx , gy , gz = self .packeter .unpacketC6F ()
372
+ _ , self . ax , self . ay , self . az , self . gx , self . gy , self . gz = self .packeter .unpacketC6F ()
305
373
elif code == ord ('p' ):
306
374
# battery percentage
307
375
_ , self .battery_perc = self .packeter .unpacketC1F ()
@@ -330,6 +398,9 @@ def _parse_message(self) -> int:
330
398
elif code == ord ('x' ):
331
399
# robot ack
332
400
_ , self .last_ack = self .packeter .unpacketC1B ()
401
+ elif code == ord ('z' ):
402
+ # robot ack
403
+ _ , self .x , self .y , self .theta = self .packeter .unpacketC3F ()
333
404
elif code == 0x7E :
334
405
# firmware version
335
406
_ , * self .version = self .packeter .unpacketC3B ()
@@ -401,9 +472,9 @@ def get_touch_right(self) -> bool:
401
472
"""
402
473
return bool (self .touch_bits & 0b10000000 )
403
474
404
- def get_color (self ) -> (int , int , int ):
475
+ def get_color_raw (self ) -> (int , int , int ):
405
476
"""
406
- Returns the RGB color ( raw) readout
477
+ Returns the color sensor's raw readout
407
478
:return: red, green, blue
408
479
"""
409
480
0 commit comments