You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've created a project to control the Cat D11 bulldozer with keyboard to get aquatinted with pybricks. Since it works rather nicely I would like to share it with the community. Could you please add it to the project page?
the code (I used other examples from the pybrick page if some stuff seems familiar):
# Import required MicroPython libraries.fromusysimportstdinfromuselectimportpollfrompybricks.pupdevicesimportMotor, Remotefrompybricks.parametersimportButton, Color, Direction, Portfrompybricks.toolsimportwait, StopWatchfrompybricks.hubsimportTechnicHub# Register hub and set light to soft white (gray)hub=TechnicHub()
hub.light.on(Color.GRAY)
# Register the standard input so we can read keyboard presses.keyboard=poll()
keyboard.register(stdin)
# Set defaultsleft_speed=0right_speed=0function="Blade up/down"functionID=1key=Nonestate="Idle"# Default speed and acceleration. You can change# them to make it more realistic.SWITCH_SPEED=720DRIVE_SPEED=100DRIVE_ACCELERATION=1000FUNCTION_POWER=100MAX_LOAD=75# Initialize the motors.left_motor=Motor(Port.A, Direction.COUNTERCLOCKWISE)
right_motor=Motor(Port.B)
function_motor=Motor(Port.C)
switch_motor=Motor(Port.D)
left_motor.control.limits(acceleration=DRIVE_ACCELERATION)
right_motor.control.limits(acceleration=DRIVE_ACCELERATION)
# Define FunctionsCOMMANDS= {
1: ("Blade up/down", 0, Color.BLUE),
2: ("Blade tilt", 270, Color.RED),
3: ("Ladder", 180, Color.YELLOW),
4: ("Ripper", 90, Color.GREEN),
}
# Find the right end left endstop positionsright_end=switch_motor.run_until_stalled(500, duty_limit=50)
left_end=switch_motor.run_until_stalled(-500, duty_limit=50)
# The full switch motion ranges 270 degrees. Everything beyond that# is play towards the endstops, equal in both directions. Since we# are currently at the left endpoint, we can reset the angle# accordingly. This way, the functions # are simply at the# relative positions 0, 90, 180, 270.switch_motor.reset_angle(-(right_end-left_end-270) /2)
defswitch_target(target_angle):
# Try up to 5 times.foriinrange(5):
# Keep track of how long we've tried.watch=StopWatch()
switch_motor.run_target(SWITCH_SPEED, target, wait=True)
# Wait until the stopwatch times out.whilewatch.time() <2000:
# We're done if the motor is on target, so exit# this function.ifswitch_motor.control.done():
switch_motor.stop()
returnwait(10)
# Otherwise, we got stuck, so try wiggling around to# release it.print("Getting unstuck.")
switch_motor.run_target(SWITCH_SPEED, 0, wait=False)
wait(1500)
switch_motor.run_target(SWITCH_SPEED, 270, wait=False)
wait(1500)
# Always turn off motor to safe powerswitch_motor.stop()
# Print usage informationprint(
"\n\n\nKeys (FWD/REV/STP):\tw/s/x: Left track, e/d/c: Right Track, r/f/v: Both tracks full speed,",
"\n\t\t\tq/a/z: Function, 0: Full stop, !: Shutdown.",
"\nFuntions:\t\t1: Blade up/down 2: Blade tilt, 3: Ladder, 4: Ripper\n\n")
inc=0whileTrue:
wait(10)
# Stop function motor when jammed or blockediffunction_motor.load() >MAX_LOADorfunction_motor.load() <-MAX_LOAD:
function_motor.stop()
# Print statistical informationinc+=1ifinc%50==0:
LINE_CLEAR='\x1b[2K'print(LINE_CLEAR,
" State", state, "| Left: ",left_motor.speed(), left_motor.load(), "[", left_speed,
"], Right: ", right_motor.speed(), right_motor.load(), "[", right_speed,
"] | Func.:", function,
"| Bat:", hub.battery.voltage(), hub.battery.current(), end="\r"
)
inc=0# Turn of motors when state it stopped for more then 5 secondsifstate=="Stopped":
ifstopInc>500:
right_motor.stop()
left_motor.stop()
state="Idle"stopInc+=1else:
stopInc=0# Restart loop of no keyboard input is givenifnotkeyboard.poll(0):
continue# Register key pressifkeyboard.poll(0):
key=stdin.read(1)
# Full stopifkey=="0":
state="Idle"right_speed=0left_speed=0right_motor.stop()
left_motor.stop()
function_motor.stop()
switch_motor.stop()
# Shutdown hubifkey=='!':
print(LINE_CLEAR, "Good Bye!")
wait(100)
hub.system.shutdown()
ifkey=="1"orkey=="2"orkey=="3"orkey=="4":
# Stop function motor and switch motorfunction_motor.stop()
switch_motor.stop()
# Go through the commands.forbutton, commandinCOMMANDS.items():
# Check if the command has a matching button.ifint(key) ==button:
# Now we can unpack the command.name, target, color=command# Execute command.switch_target(target)
# Set colorhub.light.on(color)
# Update status infofunction=namefunctionID=button# Full speed aheadifkey=="r":
right_speed=1000left_speed=1000state="Running"# Full speed reverseifkey=="f":
right_speed=-1000left_speed=-1000state="Running"# Full stopifkey=="v":
right_speed=0left_speed=0# Increase left track speedifkey=="w":
left_speed+=DRIVE_SPEEDifleft_speed>1000:
left_speed=1000state="Running"# Decrase left track speedifkey=="s":
left_speed-=DRIVE_SPEEDifleft_speed<-1000:
left_speed=-1000state="Running"# Stop left trackifkey=="x":
left_speed=0# Increate right track speedifkey=="e":
right_speed+=DRIVE_SPEEDifright_speed>1000:
right_speed=1000state="Running"# Decrease right track speedifkey=="d":
right_speed-=DRIVE_SPEEDifright_speed<-1000:
right_speed=-1000state="Running"# Stop right trackifkey=="c":
right_speed=0# Function motor upifkey=="q":
function_motor.dc(-FUNCTION_POWER)
iffunctionID==1:
MAX_LOAD=150else:
MAX_LOAD=75# Function motor downifkey=="a":
function_motor.dc(FUNCTION_POWER)
MAX_LOAD=170# Stop function motorifkey=="z":
function_motor.stop()
# Activate the driving motors.ifstate!="Idle":
left_motor.run(left_speed)
right_motor.run(right_speed)
# Set state to idle when speed is 0 on both tracksifleft_speed==0andright_speed==0andstate=="Running":
state="Stopped"
The text was updated successfully, but these errors were encountered:
Hi,
I've created a project to control the Cat D11 bulldozer with keyboard to get aquatinted with pybricks. Since it works rather nicely I would like to share it with the community. Could you please add it to the project page?
the code (I used other examples from the pybrick page if some stuff seems familiar):
The text was updated successfully, but these errors were encountered: