City Hub - Auto Detect Port / Motor Initialization #2241
-
|
I am a teacher and am using City Hubs / Simple Motors / Lego Remotes in the classroom for engineering classes. I am new to coding and have written a script that seems to work, but I could use some feedback on, as I'm sure it isn't efficient and I worry it may be unnecessarily draining my City Hub rechargeable batteries! I have been working on a python code for the City Hub to do the following for multiple hubs/remotes used in classes:
Everything seems to be working fine, but as I have little coding experience, I am worried the code puts too much strain on the processor? I have been teaching a ton and my Eneloop batteries seem to be draining pretty fast. This easily could be from normal use and a lot of teaching, but I wanted to check here to see if I am missing some more efficient way to code this? I will try to attach a copy of the code below. Any feedback would be greatly appreciated! Best, from pybricks.hubs import CityHub
from pybricks.parameters import Button, Direction, Port, Color, Side, Stop
from pybricks.pupdevices import Motor, Remote, DCMotor, Light
from pybricks.tools import wait
from pybricks.iodevices import PUPDevice
#motor id - 1
#servo motor id - 48
#sensor id - 37
hub=CityHub()
#variables to represent device plugged in. 0 means nothing, later updated by device id
last_a = 0
last_b = 0
#get the name of hub (named after colored stickers we use in class) so that later we can find its partnered remote (named similarly)
hub_info=hub.system.info()
color=hub_info["name"]
#allows us to toggle through colored lights on hub / remote
color_list = [Color.BLUE,Color.CYAN,Color.GREEN,Color.MAGENTA,Color.ORANGE,Color.RED,Color.VIOLET,Color.WHITE,Color.YELLOW]
colorindex=0
#dir variables to keep track of direction to be used when toggling motors on / off with red buttons on remote. 1 is clockwise, -1 is counter
r_dir=1
l_dir=1
#pow variables represent the toggle on / off. 1 means motor a has been toggled off (so + and - buttons wont work). 0 means motor a is toggled off so normal remote + / - buttons will work
r_pow=0
l_pow=0
def check_a():
global last_a
#check what is plugged in to port a, and if there is, initialize motor
try:
device_a = PUPDevice(Port.A)
a_info = device_a.info()
a_id = a_info["id"]
global A
A=DCMotor(Port.A, Direction.CLOCKWISE)
if a_id != last_a:
last_a = a_id
print ("Port A - " + str(last_a))
except OSError:
pass
def check_b():
global last_b
#check what is plugged in to port b, and if there is, initialize motor
try:
device_b = PUPDevice(Port.B)
b_info = device_b.info()
b_id = b_info["id"]
global B
B= DCMotor(Port.B, Direction.CLOCKWISE)
if b_id != last_b:
last_b = b_id
print ("Port B - " + str(last_b))
except OSError:
pass
def remotemove():
global colorindex
global r_dir
global r_pow
global l_dir
global l_pow
#only run motor function for A if device id is not 0 (something is plugged in).
if last_a != 0:
#run remote +/- only if toggled power is off (0)
if l_pow==0:
A.dc(100 if Button.LEFT_PLUS in remote.buttons.pressed() else (-100 if Button.LEFT_MINUS in remote.buttons.pressed() else 0))
if Button.LEFT_PLUS in remote.buttons.pressed():
l_dir = 1
elif Button.LEFT_MINUS in remote.buttons.pressed():
l_dir = -1
#toggle power on (if last state is off) and run power at dir x 100 until it is pressed again to toggle off
if Button.LEFT in remote.buttons.pressed():
if l_pow==0:
l_pow=1
A.dc(l_dir*100)
while Button.LEFT in remote.buttons.pressed():
wait (0)
else:
l_pow=0
A.stop()
while Button.LEFT in remote.buttons.pressed():
wait (0)
#all the same for b
if last_b != 0:
if r_pow==0:
B.dc(100 if Button.RIGHT_PLUS in remote.buttons.pressed() else (-100 if Button.RIGHT_MINUS in remote.buttons.pressed() else 0))
if Button.RIGHT_PLUS in remote.buttons.pressed():
r_dir = 1
elif Button.RIGHT_MINUS in remote.buttons.pressed():
r_dir = -1
if Button.RIGHT in remote.buttons.pressed():
if r_pow==0:
r_pow=1
B.dc(r_dir*100)
while Button.RIGHT in remote.buttons.pressed():
wait (0)
else:
r_pow=0
B.stop()
while Button.RIGHT in remote.buttons.pressed():
wait (0)
#like default lego firmware, change light color if button is pressed on hub. Same functionality added to remote green button
if Button.CENTER in hub.buttons.pressed():
hub.light.on(color_list[colorindex])
remote.light.on(color_list[colorindex])
colorindex=(colorindex + 1) %9
while Button.CENTER in hub.buttons.pressed():
wait (0)
if Button.CENTER in remote.buttons.pressed():
hub.light.on(color_list[colorindex])
remote.light.on(color_list[colorindex])
colorindex=(colorindex + 1) %9
while Button.CENTER in remote.buttons.pressed():
wait (0)
#print voltage to check battery level
print(color, 'voltage', hub.battery.voltage())
#connect to remote that shares a name with hub name with suffix
remote = Remote(color+"_r", timeout=None)
hub.system.set_stop_button(None)
hub.light.on(Color.RED)
remote.light.on(Color.RED)
while True:
#stop performing checks when a motor has been plugged in. This allows kids to unplug or plug in motors to each hub and everything still works.
if last_a==0:
check_a()
if last_b==0:
check_b()
remotemove() |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @Ben-Toolbox, nice to see you here again. In applications with motors, processing power consumption is pretty much negligible, so I wouldn't worry about it. Instead, it is more important to check that your motors are idle when you don't need them. For reference, here is another way to mimic a motor when it is absent. |
Beta Was this translation helpful? Give feedback.
Hi @Ben-Toolbox, nice to see you here again.
In applications with motors, processing power consumption is pretty much negligible, so I wouldn't worry about it.
Instead, it is more important to check that your motors are idle when you don't need them.
dc(0)as you do is fine. Usingstop()saves a little more power. You could also turn any lights off, but I would keep them on in a classroom so it's easier to see it is actually something.For reference, here is another way to mimic a motor when it is absent.