Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

Other projects • Re: Raspberry Pi RC Car Script

$
0
0

Code:

import pigpioimport pygameimport timepygame.init()screen = pygame.display.set_mode((100, 100))pi = pigpio.pi()if not pi.connected:    exit()servo_pin = 18  esc_pin = 23   neutral_pulse_esc = 1500  neutral_pulse_servo = 1500 def control_esc(speed):    pulse_width = int(((speed + 100) / 200) * (2000 - 1000) + 1000)    pi.set_servo_pulsewidth(esc_pin, pulse_width)def set_servo_angle(angle):    pulsewidth = int(((angle + 90) / 180) * (2500 - 500) + 500)    pi.set_servo_pulsewidth(servo_pin, pulsewidth)control_esc(0)running = Truewhile running:    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = False        elif event.type == pygame.KEYDOWN:            if event.key == pygame.K_w:                control_esc(13)  # Forward            elif event.key == pygame.K_s:                control_esc(-29)  # Reverse       #### cant set the speed any lower than this ########            elif event.key == pygame.K_a:                set_servo_angle(-40)  # Left            elif event.key == pygame.K_d:                set_servo_angle(40)  # Right        elif event.type == pygame.KEYUP:            if event.key in [pygame.K_w, pygame.K_s]:                control_esc(0)             elif event.key in [pygame.K_a, pygame.K_d]:                set_servo_angle(0) pi.set_servo_pulsewidth(esc_pin, neutral_pulse_esc) pi.set_servo_pulsewidth(servo_pin, neutral_pulse_servo) pi.stop()pygame.quit()
I've added [code][/code] tags round your python code. That makes the difference between

if "code-tags" == "used":
print "Code is formatted correctly"
else:
print "Code is completely scrambled"

and

Code:

if "code-tags" == "used":  print "Code is formatted correctly"else:  print "Code is completely scrambled"

Statistics: Posted by DougieLawson — Wed Mar 27, 2024 7:37 am



Viewing all articles
Browse latest Browse all 4151

Trending Articles