1
- # Eliobot robot Library v1.1
2
- # 2023 ELIO, B3 ROBOTICS
1
+ # Eliobot robot Library
2
+ # version '1.2'
3
+ # 2023 ELIO SAS
3
4
#
4
5
# Project home:
5
6
# https://eliobot.com
6
7
#
7
8
8
9
#--------------- LIBRARIES IMPORT ---------------#
9
10
10
- import time
11
+ import time
12
+ import math
11
13
import board
12
14
from digitalio import DigitalInOut , Direction , Pull
13
15
from analogio import AnalogIn
14
- import pwmio
16
+ import pwmio
15
17
import wifi
16
18
import json
17
19
24
26
'average_min_value' : 10000
25
27
}
26
28
29
+ # ----------------- CONSTANTS -----------------#
30
+
31
+ # Distance between the two wheels in mm
32
+ space_between_wheels = 77.5
33
+
34
+ # Diameter of the wheels in mm
35
+ wheel_diameter = 33.5
36
+
37
+ # Distance traveled by the robot in cm for one revolution of the wheel
38
+ distance_per_revolution = (wheel_diameter * math .pi ) / 10
39
+
27
40
# --------------- PINS DECLARATION ---------------#
28
41
29
42
# Setup the BATTERY voltage sense pin
@@ -65,7 +78,9 @@ def get_battery_voltage():
65
78
# This forumla should show the nominal 4.2V max capacity (approximately) when 5V is present and the
66
79
# VBAT is in charge state for a 1S LiPo battery with a max capacity of 4.2V
67
80
global vbat_voltage
68
- return (vbat_voltage .value / 5371 )
81
+ vbat = ((vbat_voltage .value / 2 ** 16 ) * 3.3 ) * 2
82
+
83
+ return vbat
69
84
70
85
71
86
# Detect if there is a voltage on the USB connector
@@ -110,6 +125,16 @@ def getObstacle(obstacle_pos):
110
125
111
126
# --------------- MOTORS ---------------#
112
127
128
+ # Get the number of repetitions per second of the motor
129
+ def repetitionPerSecond ():
130
+ vBatt = get_battery_voltage ()
131
+ rpm = 20.3 * vBatt
132
+ print (rpm )
133
+ rps = rpm / 60
134
+ print (rps )
135
+ return rps
136
+
137
+
113
138
# Convert the speed from 0 - 100% to 0 - 65535 for pwmio usage
114
139
def setSpeed (speedValue ):
115
140
# Some filtering to fit the 0-100% range and increasing the minimum value (motors won't spin under 15%)
@@ -127,43 +152,39 @@ def setSpeed(speedValue):
127
152
def moveForward (speed = 100 ):
128
153
pwm_value = setSpeed (speed )
129
154
130
- # Faire avancer le robot à la vitesse spécifiée
131
155
AIN1 .duty_cycle = 0
132
- AIN2 .duty_cycle = pwm_value
133
156
BIN1 .duty_cycle = 0
157
+ AIN2 .duty_cycle = pwm_value
134
158
BIN2 .duty_cycle = pwm_value
135
159
136
160
137
161
# Move the robot Backward (0 - 100% speed)
138
162
def moveBackward (speed = 100 ):
139
163
pwm_value = setSpeed (speed )
140
164
141
- # Faire avancer le robot à la vitesse spécifiée
142
- AIN1 .duty_cycle = pwm_value
165
+ BIN2 .duty_cycle = 0
143
166
AIN2 .duty_cycle = 0
167
+ AIN1 .duty_cycle = pwm_value
144
168
BIN1 .duty_cycle = pwm_value
145
- BIN2 .duty_cycle = 0
146
169
147
170
148
171
# Turn the robot to the Left (0 - 100% speed)
149
172
def turnLeft (speed = 100 ):
150
173
pwm_value = setSpeed (speed )
151
174
152
- # Faire avancer le robot à la vitesse spécifiée
153
175
AIN1 .duty_cycle = 0
176
+ BIN2 .duty_cycle = 0
154
177
AIN2 .duty_cycle = pwm_value
155
178
BIN1 .duty_cycle = pwm_value
156
- BIN2 .duty_cycle = 0
157
179
158
180
159
181
# turn to the right
160
182
def turnRight (speed = 100 ):
161
183
pwm_value = setSpeed (speed )
162
184
163
- # Faire avancer le robot à la vitesse spécifiée
164
- AIN1 .duty_cycle = pwm_value
165
185
AIN2 .duty_cycle = 0
166
186
BIN1 .duty_cycle = 0
187
+ AIN1 .duty_cycle = pwm_value
167
188
BIN2 .duty_cycle = pwm_value
168
189
169
190
@@ -215,17 +236,48 @@ def spinRightWheelBackward(speed=100):
215
236
AIN2 .duty_cycle = 0
216
237
217
238
218
- # Move the robot forward one step (= approx. 15cm)
219
- def moveOneStep (speed = 100 ):
220
- pwm_value = setSpeed (speed )
221
- AIN1 .duty_cycle = 0
222
- AIN2 .duty_cycle = pwm_value
223
- BIN1 .duty_cycle = 0
224
- BIN2 .duty_cycle = pwm_value
225
- time .sleep (1 )
239
+ # Move the robot forward one step (= approx. 20cm)
240
+ def moveOneStep (direction , distance = 20 ):
241
+ global distance_per_revolution
242
+
243
+ required_rps = distance / distance_per_revolution
244
+ required_time = required_rps / repetitionPerSecond ()
245
+
246
+ pwm_value = 65535
247
+
248
+ if direction == "forward" :
249
+ AIN1 .duty_cycle = 0
250
+ BIN1 .duty_cycle = 0
251
+ AIN2 .duty_cycle = pwm_value
252
+ BIN2 .duty_cycle = pwm_value
253
+ elif direction == "backward" :
254
+ BIN2 .duty_cycle = 0
255
+ AIN2 .duty_cycle = 0
256
+ AIN1 .duty_cycle = pwm_value
257
+ BIN1 .duty_cycle = pwm_value
258
+
259
+ time .sleep (required_time )
226
260
motorStop ()
227
261
228
262
263
+ def moveFromAngle (direction , angle = 90 ):
264
+ global space_between_wheels
265
+ global wheel_diameter
266
+
267
+ gear_ratio = space_between_wheels / wheel_diameter
268
+
269
+ required_time = (angle / (360 * repetitionPerSecond ())) * gear_ratio
270
+
271
+ if direction == "left" :
272
+ turnLeft (100 )
273
+ time .sleep (required_time )
274
+ motorStop ()
275
+ elif direction == "right" :
276
+ turnRight (100 )
277
+ time .sleep (required_time )
278
+ motorStop ()
279
+
280
+
229
281
# --------------- BUZZER ---------------#
230
282
231
283
# Buzzer initialisation
@@ -239,7 +291,8 @@ def playFrequency(frequency, waitTime, volume):
239
291
"""Joue une fréquence (en Hertz) pendant une durée donnée (en secondes) avec un volume spécifié."""
240
292
buzzer = buzzerInit ()
241
293
buzzer .frequency = round (frequency )
242
- buzzer .duty_cycle = int (2 ** (0.06 * volume + 9 )) # La valeur 32768 correspond à un cycle de service de 50 % pour obtenir une onde carrée.
294
+ buzzer .duty_cycle = int (2 ** (
295
+ 0.06 * volume + 9 )) # La valeur 32768 correspond à un cycle de service de 50 % pour obtenir une onde carrée.
243
296
time .sleep (waitTime )
244
297
buzzer .deinit ()
245
298
@@ -406,8 +459,8 @@ def scanWifiNetworks():
406
459
MIN_RSSI = - 90 # 0% RSSI
407
460
rssi = max (min (network .rssi , MAX_RSSI ), MIN_RSSI )
408
461
percentage = (rssi - MIN_RSSI ) / (MAX_RSSI - MIN_RSSI ) * 100
409
-
410
462
print ("SSID:" , network .ssid , ", Canal:" , network .channel , ", RSSI:" , network .rssi , " (" , round (percentage ),
411
463
"%)" )
412
464
wifi .radio .stop_scanning_networks ()
413
465
return networks
466
+
0 commit comments