Skip to content

Commit ff7e4e1

Browse files
[FEAT] use of the battery level to travel
1 parent d49f44f commit ff7e4e1

File tree

1 file changed

+77
-24
lines changed

1 file changed

+77
-24
lines changed

elio.py

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
# Eliobot robot Library v1.1
2-
# 2023 ELIO, B3 ROBOTICS
1+
# Eliobot robot Library
2+
# version '1.2'
3+
# 2023 ELIO SAS
34
#
45
# Project home:
56
# https://eliobot.com
67
#
78

89
#--------------- LIBRARIES IMPORT ---------------#
910

10-
import time
11+
import time
12+
import math
1113
import board
1214
from digitalio import DigitalInOut, Direction, Pull
1315
from analogio import AnalogIn
14-
import pwmio
16+
import pwmio
1517
import wifi
1618
import json
1719

@@ -24,6 +26,17 @@
2426
'average_min_value': 10000
2527
}
2628

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+
2740
# --------------- PINS DECLARATION ---------------#
2841

2942
# Setup the BATTERY voltage sense pin
@@ -65,7 +78,9 @@ def get_battery_voltage():
6578
# This forumla should show the nominal 4.2V max capacity (approximately) when 5V is present and the
6679
# VBAT is in charge state for a 1S LiPo battery with a max capacity of 4.2V
6780
global vbat_voltage
68-
return (vbat_voltage.value / 5371)
81+
vbat = ((vbat_voltage.value / 2 ** 16) * 3.3) * 2
82+
83+
return vbat
6984

7085

7186
# Detect if there is a voltage on the USB connector
@@ -110,6 +125,16 @@ def getObstacle(obstacle_pos):
110125

111126
# --------------- MOTORS ---------------#
112127

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+
113138
# Convert the speed from 0 - 100% to 0 - 65535 for pwmio usage
114139
def setSpeed(speedValue):
115140
# 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):
127152
def moveForward(speed=100):
128153
pwm_value = setSpeed(speed)
129154

130-
# Faire avancer le robot à la vitesse spécifiée
131155
AIN1.duty_cycle = 0
132-
AIN2.duty_cycle = pwm_value
133156
BIN1.duty_cycle = 0
157+
AIN2.duty_cycle = pwm_value
134158
BIN2.duty_cycle = pwm_value
135159

136160

137161
# Move the robot Backward (0 - 100% speed)
138162
def moveBackward(speed=100):
139163
pwm_value = setSpeed(speed)
140164

141-
# Faire avancer le robot à la vitesse spécifiée
142-
AIN1.duty_cycle = pwm_value
165+
BIN2.duty_cycle = 0
143166
AIN2.duty_cycle = 0
167+
AIN1.duty_cycle = pwm_value
144168
BIN1.duty_cycle = pwm_value
145-
BIN2.duty_cycle = 0
146169

147170

148171
# Turn the robot to the Left (0 - 100% speed)
149172
def turnLeft(speed=100):
150173
pwm_value = setSpeed(speed)
151174

152-
# Faire avancer le robot à la vitesse spécifiée
153175
AIN1.duty_cycle = 0
176+
BIN2.duty_cycle = 0
154177
AIN2.duty_cycle = pwm_value
155178
BIN1.duty_cycle = pwm_value
156-
BIN2.duty_cycle = 0
157179

158180

159181
# turn to the right
160182
def turnRight(speed=100):
161183
pwm_value = setSpeed(speed)
162184

163-
# Faire avancer le robot à la vitesse spécifiée
164-
AIN1.duty_cycle = pwm_value
165185
AIN2.duty_cycle = 0
166186
BIN1.duty_cycle = 0
187+
AIN1.duty_cycle = pwm_value
167188
BIN2.duty_cycle = pwm_value
168189

169190

@@ -215,17 +236,48 @@ def spinRightWheelBackward(speed=100):
215236
AIN2.duty_cycle = 0
216237

217238

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)
226260
motorStop()
227261

228262

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+
229281
# --------------- BUZZER ---------------#
230282

231283
# Buzzer initialisation
@@ -239,7 +291,8 @@ def playFrequency(frequency, waitTime, volume):
239291
"""Joue une fréquence (en Hertz) pendant une durée donnée (en secondes) avec un volume spécifié."""
240292
buzzer = buzzerInit()
241293
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.
243296
time.sleep(waitTime)
244297
buzzer.deinit()
245298

@@ -406,8 +459,8 @@ def scanWifiNetworks():
406459
MIN_RSSI = -90 # 0% RSSI
407460
rssi = max(min(network.rssi, MAX_RSSI), MIN_RSSI)
408461
percentage = (rssi - MIN_RSSI) / (MAX_RSSI - MIN_RSSI) * 100
409-
410462
print("SSID:", network.ssid, ", Canal:", network.channel, ", RSSI:", network.rssi, " (", round(percentage),
411463
"%)")
412464
wifi.radio.stop_scanning_networks()
413465
return networks
466+

0 commit comments

Comments
 (0)