Merge pull request 'Update utils/BatteryDiagnostics.py' (#3) from dev into main
Reviewed-on: Arcmyx/pybricks-utils#3
This commit is contained in:
@@ -4,30 +4,48 @@ from pybricks.tools import run_task, multitask
|
|||||||
from pybricks.tools import wait, StopWatch
|
from pybricks.tools import wait, StopWatch
|
||||||
from pybricks.robotics import DriveBase
|
from pybricks.robotics import DriveBase
|
||||||
from pybricks.hubs import PrimeHub
|
from pybricks.hubs import PrimeHub
|
||||||
|
import umath
|
||||||
# Initialize hub and devices
|
# Initialize hub and devices
|
||||||
hub = PrimeHub()
|
hub = PrimeHub()
|
||||||
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
|
class BatteryDiagnostics:
|
||||||
right_motor = Motor(Port.B,Direction.CLOCKWISE) # Specify default direction
|
def __init__(self):
|
||||||
left_arm = Motor(Port.C, Direction.CLOCKWISE) # Specify default direction
|
self.voltage = 0
|
||||||
right_arm = Motor(Port.D, Direction.CLOCKWISE,[[12,36],[12,20,24]]) #Added gear train list for gear ration
|
self.current = 0
|
||||||
lazer_ranger = UltrasonicSensor(Port.E)
|
def printVoltage(self):
|
||||||
color_sensor = ColorSensor(Port.F)
|
self.voltage = hub.battery.voltage()
|
||||||
|
if self.voltage > 7800:
|
||||||
|
print(f"Battery voltage is sufficient: {self.voltage}")
|
||||||
|
elif self.voltage < 7800 :
|
||||||
|
print(f"Charging needed: {self.voltage}")
|
||||||
|
def printCurrent(self):
|
||||||
|
self.current = hub.battery.current()
|
||||||
|
print("Battery current:", self.current)
|
||||||
|
def printAll(self):
|
||||||
|
timeelapsed = 0
|
||||||
|
voltageList = []
|
||||||
|
currentList = []
|
||||||
|
while True:
|
||||||
|
print("\n\n\n\n\n")
|
||||||
|
self.printVoltage()
|
||||||
|
voltageList.append(self.voltage)
|
||||||
|
self.printCurrent()
|
||||||
|
currentList.append(self.current)
|
||||||
|
wait(50)
|
||||||
|
timeelapsed += 50
|
||||||
|
|
||||||
|
if(timeelapsed >= 3000):
|
||||||
|
break
|
||||||
|
print("Voltage deviation:", self.stdev(voltageList))
|
||||||
|
print("Current deviation:", self.stdev(currentList))
|
||||||
|
def stdev(self, vals):
|
||||||
|
data = vals
|
||||||
|
|
||||||
# DriveBase configuration
|
# Calculate the mean
|
||||||
WHEEL_DIAMETER = 68.8 # mm (adjust for your wheels)
|
mean = sum(data) / len(data)
|
||||||
AXLE_TRACK = 180 # mm (distance between wheels)
|
|
||||||
drive_base = DriveBase(left_motor, right_motor, WHEEL_DIAMETER, AXLE_TRACK)
|
|
||||||
drive_base.settings(600, 500, 300, 200)
|
|
||||||
drive_base.use_gyro(True)
|
|
||||||
|
|
||||||
WALL_DISTANCE = 200 # mm
|
# Calculate the variance (sum of squared differences from the mean, divided by n-1 for sample standard deviation)
|
||||||
|
variance = sum([(x - mean) ** 2 for x in data]) / (len(data) - 1)
|
||||||
|
|
||||||
if hub.battery.voltage() > 7800:
|
# Calculate the standard deviation (square root of the variance)
|
||||||
print(f"Battery voltage is sufficient: {hub.battery.voltage()}")
|
std_dev_manual = umath.sqrt(variance)
|
||||||
elif hub.battery.voltage < 7800 :
|
return (std_dev_manual)
|
||||||
print(f"Charging needed: {hub.battery.voltage()}")
|
|
||||||
|
|
||||||
print(hub.battery.current())
|
|
||||||
print(hub.imu.heading())
|
|
||||||
print(hub.system.info())
|
|
||||||
Reference in New Issue
Block a user