Replies: 3 comments 3 replies
-
The AI assistents like chatGPT are good helpers on python and micropthon. Could you share your python program here, so we can have a look? ```python A simple hint: if you want to test if a part of the program can be removed, make it a comment: Bert |
Beta Was this translation helpful? Give feedback.
-
Not sure who will see this, but I have some updated code that seems to work somewhat well. Here it is. from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Stop, Color
from pybricks.tools import wait, StopWatch
from pybricks.hubs import TechnicHub
import umath
# Import required MicroPython libraries.
from usys import stdin
from uselect import poll
# Register the standard input so we can read keyboard presses.
keyboard = poll()
keyboard.register(stdin)
# Initialize motors and hub
drive1 = Motor(Port.C) # Existing drive motor on Port C
drive2 = Motor(Port.D) # New drive motor on Port D, works in sync with drive1
shift = Motor(Port.A)
hub = TechnicHub()
# Initialize variables as in your original code
watch = StopWatch()
t = watch.time()
t0 = watch.time()
pst = 0
st0 = 0
stmax = 60
trot = 3000
strot = stmax - 5
trace = 5000
race = False
ready = False
rrl = [1.2, 1.275, 1.35, 1.35]
rrll = [0.6, 0.6, 0.6, 0.75]
gears = [290, 190, 100, 10]
spwr = [-60, -60, -90, -90]
dpwr = 10
g0 = 30
ta = 100
x = 0
rr = 0
rr0 = 0
pwr = 0
y = 0
stf = 1
stpw = 90
new = True
key = None
nn = 100
ty = 0
rot = 0
gear = 0
gearm = 0
run = False
a0 = drive1.angle() # Initialize with drive1's angle
dri = 1
# Brings the shifting into the first gear
while t - t0 <= 300:
shift.track_target(g0)
t = watch.time()
# Function to implement smooth braking
def smooth_brake():
global pwr, dri, gear, gearm, run
braking_power = pwr
while abs(braking_power) > 0:
# Gradually reduce the power
braking_power += 5 * dri # Adjust increment for smoother braking
if abs(braking_power) < 5:
braking_power = 0 # Stop when power is very low
drive1.dc(braking_power)
drive2.dc(braking_power) # Sync the second motor
wait(100) # Adjust wait time for smoother braking
# Stop the motors completely
drive1.dc(0)
drive2.dc(0)
pwr = 0
run = False
# Main loop
while True:
# Check if a key has been pressed
if keyboard.poll(0):
# Read the key and handle it
key = stdin.read(1)
print("You pressed:", key)
# Drive backward
if key == 'a':
if new and not run:
gear = 0
gearm = 0
dri = -1
run = True
if new and gearm > 0:
gear = gear - 1
gearm = gearm - 1
new = False
nn = 0
rot = 0
stf = 1
race = False
# Drive forward
elif key == 'd':
if new and gearm < 3:
if run:
gearm = gearm + 1
dri = 1
new = False
dri = 1
nn = 0
rot = 0
run = True
stf = 1
race = False
# Smooth Brake
elif key == 's':
smooth_brake()
# Kick down acceleration
elif key == 'w':
gearm = 3
dri = 1
rot = 0
run = True
stf = 1
race = False
# Start race mode
elif key == ' ':
gearm = 3
dri = 1
rot = 0
run = True
stf = 1
race = True
ready = True
# Steer right
elif key == 'l':
if new:
y = stmax
new = False
nn = 0
rot = 0
stf = 1
# Steer left
elif key == 'j':
if new:
y = -stmax
new = False
nn = 0
rot = 0
stf = 1
# Center steering
elif key == 'k':
y = 0
rot = 0
# Tune steering left
elif key == 'u':
st0 = st0 - 1
hub.light.on(Color.GREEN)
stf = 5
# Tune steering right
elif key == 'o':
st0 = st0 + 1
hub.light.on(Color.GREEN)
stf = 5
# Rotate right
elif key == 'q':
rot = 1
dir = 1
gearm = 0
gear = 0
run = False
x = 0
y = 0
stf = 1
# Rotate left
elif key == 'e':
rot = -1
gearm = 0
gear = 0
run = False
dir = 1
x = 0
y = 0
stf = 1
# Rest of your existing code handling motor controls
# execution of routine for rotating left or right, etc.
# Update motor positions, power, and other conditions
# ...
# Steer angle adjustment and motor control
pw = (st0 + y - pst) * stf
if abs(pw) > stpw:
pw = pw * stpw / abs(pw)
# steer.dc(pw)
# Update status light on hub
if t - ty > 100:
ty = watch.time()
hub.light.off()
# Set the gear shift angle
an = gears[gear] + g0
shift.track_target(an)
t = watch.time()
dt = t - t0
if race:
if ready:
trace0 = watch.time()
ready = False
if t - trace0 >= trace:
run = False
race = False
gear = 0
# Drive operations (negative power causes forward driving)
if run:
if dt >= ta:
rr = (drive1.angle() - a0) / dt
t0 = watch.time()
a0 = drive1.angle()
if pwr > -100:
pwr = (pwr - dpwr)
else:
pwr = -100
if abs(rr * 100 / pwr) <= rrll[gear] and rr - rr0 >= 0 and abs(pwr) > 50:
if gear != 0:
gear = gear - 1
pwr = spwr[gear]
hub.light.on(Color.RED)
rr0 = 0
else:
run = False
pwr = 0
rr0 = 0
if abs(rr) >= rrl[gear] and gear < gearm:
gear = gear + 1
pwr = spwr[gear]
hub.light.on(Color.BLUE)
rr0 = 0
# Synchronize both drive motors
drive1.dc(dri * pwr)
drive2.dc(dri * pwr) # Add this line to sync drive2 with drive1
rr0 = rr
else:
drive1.dc(0)
drive2.dc(0) # Stop drive2 when drive1 stops
rr = 0
rr0 = 0 |
Beta Was this translation helpful? Give feedback.
-
If you're new to programming, adapting someone else's large program can be a bit daunting. You may find it easier to start out with block coding or with a smaller program. You can find example projects with the Technic Hub and the Xbox Controller here: https://pybricks.com/project/xbox-controller/ This includes both block programs and Python programs. The bulldozer also has a function selector that may act as good inspiration for controlling a gearbox. You could also start without a gearbox and use some of the car examples to get familiar with driving around. |
Beta Was this translation helpful? Give feedback.
-
Hi everyone!
Recently I got a technic hub and motors from the RC Audi set and I want to repurpose the electronics to make a custom RC car. I have found a gearbox online on rebrickable, here, and want to use this design in my own car. The MOC maker also had instruction on how to repurpose the hub to use pybricks which I had no idea about, until I saw the MOC. I've been browsing and troubleshooting for a few days now and can't seem to get the program to work. Errors that pop up are in lines 21 and 22 saying there's no module named, "math" and "can't import name getchar" and I have no idea how to fix them as I'm still new to pybricks.
On top of that, I want to use two motors for driving the gearbox instead of one for more torque and speed. This, of course, is mainly a building and space challenge, but I am also struggling to program a second drive motor. It's been a few years since I've coded anything major, and I've never used python, so I'm not quite familiar with everything yet.
Even more, I want to use an Xbox controller to control my RC car, as shown, here. How should I go about combining both the code for the gearbox, as well as drive it using an Xbox controller.
My two main issues I need help with, other than needed a crash course in python, is adding a second motor to drive the RC car, and paring that code run with an Xbox controller. Any help is much appreciated, and if anyone has any good advice / videos to give an overview of coding in python that would be amazing as well. <3
Edit: I got my xbox controller to connect! following the instructions in the second link worked perfectly and now all I need is to import the code for the gearbox! My problems from before still persist in the gearbox aspect, "no module named math" and "can't import name getchar" are the bane of my existence at the moment.
Edit2: I asked ChatGPT for help with coding and removing the "getchar" function, and I discovered that the variable isn't called "math" and is instead called "umath" so that problem is solved. For now my problems are, having automatic load detection for automatic gear shifting, and making sure the xbox controller powers the motors with the right buttons. (currently left trigger works for reverse, and right trigger isn't doing anything)
Edit3: There seems to be a "race" mode in the gearbox code, I don't care much for a race mode and would like it removed, I'm a little scared to do so incase it messes with other code. any help on this topic would be much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions