Skip to main content
Code Review

Return to Revisions

2 of 4
added 27 characters in body
Phrancis
  • 20.5k
  • 6
  • 69
  • 155

Raspberry PI controllable car (code for a 6-year old)

I've been trying to show my 6-year old daughter a way to build and create a simple toy car. We've got a 4-motor chassis kit, Raspberry PI and a motozero shield that can operate these 4 motors. We've put a power bank and a separate battery for the motors:

enter image description here

Now, as far as coding goes, I've created a simple Car class with "forward", "backward", "forward_left", "forward_right", "backward_left" and "backward_right" actions. For the demonstration purposes mostly, I've tried to be explicit and violated the DRY principle a lot.

I've also initialized an endless loop listening for keyboard inputs - F would be for forward, B for backwards and S for stop (have not decided which keys would go for those "left-ish" and "right-ish" actions yet).

The code:

from RPi import GPIO
from gpiozero import Motor, OutputDevice
class Car:
 def __init__(self):
 self.front_right = Motor(forward=24, backward=27)
 self.front_left = Motor(forward=6, backward=22)
 self.rear_right = Motor(forward=13, backward=18)
 self.rear_left = Motor(forward=23, backward=16)
 OutputDevice(5).on()
 OutputDevice(17).on()
 OutputDevice(25).on()
 OutputDevice(12).on()
 def forward(self):
 self.front_right.forward()
 self.front_left.backward()
 self.rear_right.backward()
 self.rear_left.backward()
 def backward(self):
 self.front_right.backward()
 self.front_left.forward()
 self.rear_right.forward()
 self.rear_left.forward()
 def backward_right(self):
 self.front_right.backward(1)
 self.front_left.forward(1)
 self.rear_right.forward(1)
 self.rear_left.forward(0.2)
 def backward_left(self):
 self.front_right.backward(1)
 self.front_left.forward(1)
 self.rear_right.forward(0.2)
 self.rear_left.forward(1)
 def forward_left(self):
 self.front_right.forward()
 self.front_left.backward(0.2)
 self.rear_right.backward(1)
 self.rear_left.backward(1)
 def forward_right(self):
 self.front_right.forward(0.2)
 self.front_left.backward(1)
 self.rear_right.backward(1)
 self.rear_left.backward(1)
 def stop(self):
 self.front_right.stop()
 self.front_left.stop()
 self.rear_right.stop()
 self.rear_left.stop()
if __name__ == '__main__':
 GPIO.setwarnings(False)
 GPIO.cleanup()
 commands = {
 "f": "forward",
 "b": "backward",
 "s": "stop"
 }
 try:
 car = Car()
 while True:
 command = raw_input()
 getattr(car, commands[command])()
 finally:
 try:
 GPIO.cleanup()
 except: # ignore cleanup errors
 pass

(I've also mixed up some motors and pins on the board which led to some motors accepting "backwards" as "forward")

It is, of course, difficult to explain this kind of code to a 6-year old, since she still expects a printer to print something when I show her the Python's print() command, but, how would you change this code to make it more understandable for a kid?

I would also appreciate any points about the code organization and quality since the next step would be to control the car "via bluetooth".

alecxe
  • 17.5k
  • 8
  • 52
  • 93
lang-py

AltStyle によって変換されたページ (->オリジナル) /