Skip to content

Commit 698ad04

Browse files
Merge pull request #1 from HuttNerd/main
Update DFRobot-Maqueenplus-Python based on HuttNerd great modifications
2 parents 331e441 + 5a13cb4 commit 698ad04

File tree

3 files changed

+278
-300
lines changed

3 files changed

+278
-300
lines changed

README.md

Lines changed: 122 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# DFRobot-Maqueenplus-Python
22

3-
Python library for maqueenplus robot developed by DFRobot.
3+
Python library for the Maqueen Plus robot developed by DFRobot.
44

55
This library is a python version of the one proposed by DFRobot used for block coding.
66

@@ -15,59 +15,83 @@ Link to DFRobot page : [https://www.dfrobot.com/product-2026.html]
1515
Instantiate the robot object
1616

1717
```python
18-
from microbit import *
19-
from lib_robot_maqueen import Robot
18+
import microbit
19+
import lib_robot_maqueen as mqn
2020
import time #can be removed if not used
2121

22-
mq = Robot()
22+
mq = mqn.MaqueenPlus()
2323
```
2424

2525
# Methods
2626

27-
- Move the robot
28-
27+
### Move the robot
2928
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
3433

3534
Function definition :
3635
```python
37-
def direction(speed, dir):
36+
def move(speed, dir):
3837
"""
3938
speed(pwm) : 0 -> 255
40-
dir(string) : "TD" or "AR" or "G" or "D"
39+
dir(string) : "F" or "B" or "L" or "R"
4140
"""
4241
```
4342

4443
Example :
4544
```python
46-
mq.direction(70, "TD")
45+
mq.move(70, "F")
4746
time.sleep(1) #wait for 1s
48-
mq.direction(70, "D")
47+
mq.move(70, "R")
4948
```
5049

51-
To move the robot you can also use the run method.
50+
To move the robot you can also use the motorControl method.
5251

5352
Function definition :
5453
```python
55-
def run(self, mot, sens, vit):
54+
def motorControl(self, mot, dir, spd):
5655
"""
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
6059
"""
6160
```
6261

6362
Example :
6463
```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+
def goto(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+
"""
6783
```
6884

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+
```
7093

94+
### Stop
7195
Stop the robot
7296

7397
Function definition:
@@ -82,8 +106,7 @@ Example:
82106
mq.stop()
83107
```
84108

85-
- Control ServoMotor
86-
109+
### Control ServoMotor
87110
You can move three servos with the version of the library, S1, S2, S3.
88111

89112
Function definition:
@@ -104,63 +127,63 @@ Example:
104127
mq.servo(mq.S3,0)
105128
```
106129

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.
111133
It is up to you to do so.
112134

113-
Function declaration:
135+
Function definition:
114136
```python
115-
def readLineSensor(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+
def getLine(self):
138+
"""Read line tracking state
120139
121140
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"
123143
"""
124144
```
125145

126146
Example:
147+
127148
```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"])
133153
```
134154

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.
138157

139-
Function defition:
158+
Function definition:
140159
```python
141-
def ultrasonic(self):
160+
def ultrasonic(self,maxDist=0.4):
142161
"""Get the distance between the robot and an object.
143162
163+
Args:
164+
[float, optional] The maximum distance in meters
165+
If function is called with no arguments, defaults to 0.4
166+
144167
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.
146169
"""
147170
```
148171

149172
Example:
150173

151174
```python
152175
distance = mq.ultrasonic()
176+
distance = mq.ultrasonic(0.8)
153177
```
154178

155-
- Motor Speed
156-
179+
### Motor Speed
157180
Get the linear speed of a given motor.
158181
INFO : This method has no been tested yet but you can test it and set an issue for feedback.
159182

160183

161-
Function defition:
184+
Function definition:
162185
```python
163-
def motor_speed(self, mot):
186+
def motorSpeed(self, mot):
164187
"""Get the linear speed of a given motor
165188
166189
Args:
@@ -174,21 +197,21 @@ Function defition:
174197
Example:
175198

176199
```python
177-
mq.direction(70, "TD")
200+
mq.move(70, "F")
178201
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))
181204
time.sleep(5)
182205
mq.stop()
183206
```
184207

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.
187210

188211
* 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
192215

193216
* Color choice:
194217
RED,
@@ -208,9 +231,9 @@ Function declaration:
208231
209232
Args:
210233
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
214237
215238
color (int): color of the led:
216239
RED,GREEN,
@@ -222,9 +245,44 @@ Function declaration:
222245

223246
Example:
224247
```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+
def getEncoders(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])
226269
```
227270

271+
### Clear Encoders
272+
Reset the values of the wheel encoders to 0
273+
274+
Function definition:
275+
```python
276+
def clearEncoders(self):
277+
"""Reset the values of the wheel encoders to 0
278+
"""
279+
```
280+
281+
Example:
282+
```python
283+
mq.clearEncoders()
284+
```
228285

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

Comments
 (0)