SunFounder PiCar-X Kit
4.4 2. Keyboard Control
In this project, we will learn how to use the keyboard to remotely control the PiCar-X. You can control the PiCar-X to
move forward, backward, left, and right.
Run the Code
cd ~/picar-x/example
sudo python3 2.keyboard_control.py
Press keys on keyboard to control PiCar-X!
• w: Forward
• a: Turn left
• s: Backward
• d: Turn right
• i: Head up
• k: Head down
• j: Turn head left
• l: Turn head right
• ctrl + c: Press twice to exit the program
Code
from picarx import Picarx
from time import sleep
import readchar
manual = '''
Press keys on keyboard to control PiCar-X!
w: Forward
a: Turn left
s: Backward
d: Turn right
i: Head up
k: Head down
j: Turn head left
l: Turn head right
ctrl+c: Quit
'''
def show_info():
print("\033[H\033[J",end='') # clear terminal windows
print(manual)
if __name__ == "__main__":
try:
pan_angle = 0
tilt_angle = 0
(continues on next page)
4.4. 2. Keyboard Control 51