I am currently building a remote controlled car with the RPi model A+. I am using pygame to run a continuous loop to check the states of the desired keys on the keyboard(w,a,s,d) and based on that run different functions. That cause the RC car to move in the respective direction. The problem I am having is that, the cpu reaches 100% load, and then it freezes(this happened while at stock, and with a medium overclock). I am running both an ssh server and an x11vnc server to control the RPi. Soon after it freezes, it closes both the vnc connection and the ssh connection to the client(the computer I'm using to control the pi). Is all this too much for the Pi to handle? This is my code for the car:
import pygame import RCcarFunctions RCcarFunctions.init() global forward, reverse, left, right forward, reverse, left, right = False, False, False, False pygame.init() pygame.display.init() size = (640,480) pygame.display.set_mode((size)) pygame.key.set_repeat(500,10) print ("Succesfully initialized") try: while True: keypressed = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if keypressed[pygame.K_w]: forward = True if keypressed[pygame.K_a]: left = True if keypressed[pygame.K_s]: reverse = True if keypressed[pygame.K_d]: right = True elif event.type == pygame.KEYUP: if event.key == pygame.K_w: forward = False elif event.key == pygame.K_a: left = False elif event.key == pygame.K_s: reverse = False elif event.key == pygame.K_d: right = False elif event.type == pygame.QUIT: RCcarFunctions.cleanup() pygame.quit() if forward & right: RCcarFunctions.right() elif forward & left: RCcarFunctions.left() elif left: RCcarFunctions.left() elif right: RCcarFunctions.right() elif forward: RCcarFunctions.forward() RCcarFunctions.straight() elif reverse: RCcarFunctions.reverse() RCcarFunctions.straight() else: RCcarFunctions.brake() except KeyboardInterrupt: print ("\ncleaning up") RCcarFunctions.cleanup() pygame.quit()
If needed the RCcarFunctions library can be found here.