You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python library for maqueenplus robot developed by DFRobot.
3
+
Python library for the Maqueen Plus robot developed by DFRobot.
4
4
5
5
This library is a python version of the one proposed by DFRobot used for block coding.
6
6
@@ -15,59 +15,83 @@ Link to DFRobot page : [https://www.dfrobot.com/product-2026.html]
15
15
Instantiate the robot object
16
16
17
17
```python
18
-
from microbit import*
19
-
from lib_robot_maqueen import Robot
18
+
importmicrobit
19
+
import lib_robot_maqueen as mqn
20
20
import time #can be removed if not used
21
21
22
-
mq =Robot()
22
+
mq =mqn.MaqueenPlus()
23
23
```
24
24
25
25
# Methods
26
26
27
-
- Move the robot
28
-
27
+
### Move the robot
29
28
Move the robot along 4 axis :
30
-
* TD -> forward
31
-
* AR -> backward
32
-
* G -> left
33
-
* D -> right
29
+
- F -> forward
30
+
- B -> backward
31
+
- L -> left
32
+
- R -> right
34
33
35
34
Function definition :
36
35
```python
37
-
defdirection(speed, dir):
36
+
defmove(speed, dir):
38
37
"""
39
38
speed(pwm) : 0 -> 255
40
-
dir(string) : "TD" or "AR" or "G" or "D"
39
+
dir(string) : "F" or "B" or "L" or "R"
41
40
"""
42
41
```
43
42
44
43
Example :
45
44
```python
46
-
mq.direction(70, "TD")
45
+
mq.move(70, "F")
47
46
time.sleep(1) #wait for 1s
48
-
mq.direction(70, "D")
47
+
mq.move(70, "R")
49
48
```
50
49
51
-
To move the robot you can also use the run method.
50
+
To move the robot you can also use the motorControl method.
52
51
53
52
Function definition :
54
53
```python
55
-
defrun(self, mot, sens, vit):
54
+
defmotorControl(self, mot, dir, spd):
56
55
"""
57
-
mot left: MG ; mot right: MD
58
-
sens (forward): 1; sens (backward) :2
59
-
vit max :255; arret :0
56
+
mot left: MT_L ; mot right: MT_R
57
+
dir (forward): 1; dir (backward): 2
58
+
spd max: 255; stop: 0
60
59
"""
61
60
```
62
61
63
62
Example :
64
63
```python
65
-
mq.run(mq.MG,2,50)
66
-
mq.run(mq.MD,2,70)
64
+
mq.motorControl(mq.MT_L,2,50)
65
+
mq.motorControl(mq.MT_R,2,70)
66
+
```
67
+
68
+
### Move the robot for a precise distance
69
+
MaqueenPlus is equipped with wheel encoders. The function goto uses the wheel encoders to turn or move the robot over an exact number of encoder ticks:
70
+
- F -> drive a number of encoder ticks forward
71
+
- L -> turn left for a number of encoder ticks
72
+
- R -> turn right for a number of encoder ticks
73
+
74
+
Function definition :
75
+
```python
76
+
defgoto(self, dir, spd, dist):
77
+
"""
78
+
mot left: MT_L ; mot right: MT_R
79
+
dir(string) : "F" or "L" or "R"
80
+
spd max: 255; stop: 0
81
+
dist: the number of encoder ticks the robot should move
82
+
"""
67
83
```
68
84
69
-
- Stop
85
+
Example :
86
+
```python
87
+
# drive forward with PWM speed 200 for 400 encoder ticks
88
+
mq.goto("F", 200, 400)
89
+
90
+
# turn left with PWM speed 70 for 144 ticks (about 180 degrees)
91
+
mq.goto("L", 70, 144)
92
+
```
70
93
94
+
### Stop
71
95
Stop the robot
72
96
73
97
Function definition:
@@ -82,8 +106,7 @@ Example:
82
106
mq.stop()
83
107
```
84
108
85
-
- Control ServoMotor
86
-
109
+
### Control ServoMotor
87
110
You can move three servos with the version of the library, S1, S2, S3.
88
111
89
112
Function definition:
@@ -104,63 +127,63 @@ Example:
104
127
mq.servo(mq.S3,0)
105
128
```
106
129
107
-
- Line tracking sensor
108
-
109
-
Read line tracking sensor state for a given sensor.
110
-
All the functions proposed by DFRobot have not been implemented.
130
+
### Line tracking sensor
131
+
Read line tracking sensor state.
132
+
Not all the functions proposed by DFRobot have been implemented.
111
133
It is up to you to do so.
112
134
113
-
Function declaration:
135
+
Function definition:
114
136
```python
115
-
defreadLineSensor(self, sensor_name):
116
-
"""Read line tracking state for a given sensor
117
-
118
-
Args:
119
-
sensor_name (int): object attribut define in constructor (R1,R2,R3,L1,L2,L3)
137
+
defgetLine(self):
138
+
"""Read line tracking state
120
139
121
140
Returns:
122
-
[int]: 0 : black / 1 : white
141
+
dictionary of [int]: 0 : white / 1 : black
142
+
valid keys for the dictionary are: "L3", "L2", "L1", "R1", "R2", "R3"
123
143
"""
124
144
```
125
145
126
146
Example:
147
+
127
148
```python
128
-
#Read R1 (Right #1) line tracking sensor state
129
-
state = mq.readLineSensor(mq.R1)
130
-
131
-
#you can display the value by doing :
132
-
#display.show(state)
149
+
#Read line tracking sensor state
150
+
line = mq.getLine()
151
+
print(line["L1"])
152
+
print(line["R1"])
133
153
```
134
154
135
-
- Ultrasonic sensor
136
-
137
-
Get the distance between the robot and an object.
155
+
### Ultrasonic sensor
156
+
Get the distance between the robot and an object. An optional argument maxDist was added so you can choose the range of the sensor, which can be important to limit delays in your program. The default setting is 0.4 meters.
138
157
139
-
Function defition:
158
+
Function definition:
140
159
```python
141
-
defultrasonic(self):
160
+
defultrasonic(self,maxDist=0.4):
142
161
"""Get the distance between the robot and an object.
143
162
163
+
Args:
164
+
[float, optional] The maximum distance in meters
165
+
If function is called with no arguments, defaults to 0.4
166
+
144
167
Returns:
145
-
[float]: distance to the object if one is detected else max value.
168
+
[float]: distance to the object if one is detected, else max value.
146
169
"""
147
170
```
148
171
149
172
Example:
150
173
151
174
```python
152
175
distance = mq.ultrasonic()
176
+
distance = mq.ultrasonic(0.8)
153
177
```
154
178
155
-
- Motor Speed
156
-
179
+
### Motor Speed
157
180
Get the linear speed of a given motor.
158
181
INFO : This method has no been tested yet but you can test it and set an issue for feedback.
159
182
160
183
161
-
Function defition:
184
+
Function definition:
162
185
```python
163
-
defmotor_speed(self, mot):
186
+
defmotorSpeed(self, mot):
164
187
"""Get the linear speed of a given motor
165
188
166
189
Args:
@@ -174,21 +197,21 @@ Function defition:
174
197
Example:
175
198
176
199
```python
177
-
mq.direction(70, "TD")
200
+
mq.move(70, "F")
178
201
time.sleep(1) #delay is necessary. Otherwise the robot read the speed before the motor started.
179
-
vitesse= mq.vitesse_moteur(mq.MG)
180
-
display.show(str(vitesse))
202
+
spd= mq.motorSpeed(mq.MT_L)
203
+
display.show(str(spd))
181
204
time.sleep(5)
182
205
mq.stop()
183
206
```
184
207
185
-
- RGB Lights
186
-
Turn on/off the rbg light of your choice with the color you want.
208
+
###RGB Lights
209
+
Turn on/off the rgb light of your choice with the color you want.
187
210
188
211
* RGB LED choice:
189
-
REB_G : left led,
190
-
REB_D : right led,
191
-
REB_G_D : center led
212
+
RGB_L : left led,
213
+
RGB_R : right led,
214
+
RGB_ALL : both leds
192
215
193
216
* Color choice:
194
217
RED,
@@ -208,9 +231,9 @@ Function declaration:
208
231
209
232
Args:
210
233
rgbshow (int): rgb light object attribute defined in the constructor :
211
-
REB_G : left led,
212
-
REB_D : right led,
213
-
REB_G_D : center led
234
+
RGB_L : left led,
235
+
RGB_R : right led,
236
+
RGB_ALL : both leds
214
237
215
238
color (int): color of the led:
216
239
RED,GREEN,
@@ -222,9 +245,44 @@ Function declaration:
222
245
223
246
Example:
224
247
```python
225
-
mq.RGBLight(mq.RGB_G,mq.RED)
248
+
mq.RGBLight(mq.RGB_L,mq.RED)
249
+
```
250
+
251
+
### Read Encoders
252
+
Read the values of the wheel encoders
253
+
254
+
Function definition:
255
+
```python
256
+
defgetEncoders(self):
257
+
"""Read the values of the wheel encoders
258
+
259
+
Returns:
260
+
[tuple of 2 int's]: Value of the left encoder and right encoder
261
+
"""
262
+
```
263
+
264
+
Example:
265
+
```python
266
+
encoders = mq.getEncoders()
267
+
print(encoders[0])
268
+
print(encoders[1])
226
269
```
227
270
271
+
### Clear Encoders
272
+
Reset the values of the wheel encoders to 0
273
+
274
+
Function definition:
275
+
```python
276
+
defclearEncoders(self):
277
+
"""Reset the values of the wheel encoders to 0
278
+
"""
279
+
```
280
+
281
+
Example:
282
+
```python
283
+
mq.clearEncoders()
284
+
```
228
285
229
-
# Updates
230
-
One major update that should be added soon concerns the rotation movements of the robot. In fact, when we ask the robot to move right of left no information is given concerning the angle to rotate. Only time.sleep(_) is used actually which is not accurate.
286
+
### Version history
287
+
- Version 1.0: Initial version
288
+
- Version 2.0: Compacted code style (short variable names, delete unnecessary space characters) to work around memory constraints and find space for adding functionality. Added goto function for moving the robot with use of the decoders. Added optional maximum distance argument to the ultrasonic function. Should still work with the micro:bit V1. if more functions to this library are added, you will probably need to equip your Maqueen Plus with the micro:bit V2. I am considering making a separate version of this library for the micro:bit V2
0 commit comments