An example python program for control a mobile robot with keyboard:
- keyboard control
- serial communication
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
# Author : Mace Robotics (www.macerobotics.com)
# Date : 05/04/2020
# Version : 0.1
#
import serial
import keyboard
import time
print("Control borvo with keyboard")
port = serial.Serial('COM6')
print(port.name)
print("wait..")
try:
# read keyboard
while 1:
if keyboard.is_pressed('8'): # if key '8' is pressed
print('forward')
port.write(b'FOR!')
time.sleep(0.5)
if keyboard.is_pressed('2'): # if key '2' is pressed
print('move back')
port.write(b'BAC!')
time.sleep(0.5)
if keyboard.is_pressed('4'):
print('turn left')
port.write(b'TUL!')
time.sleep(0.5)
if keyboard.is_pressed('6'):
print('turn right')
port.write(b'TUR!')
time.sleep(0.5)
if keyboard.is_pressed('5'):
print('stop')
port.write(b'STP!')
time.sleep(0.5)
except KeyboardInterrupt as exception:
print("\nEnd\n")