I purchased the 4tronix 4TR-ROVERPI kit nearly one year ago.
Managed to get the various test programs to work. eg. drive and steer servo test etc.
Tried to control the robot using the Pimorini game controller to no avail.
Now struggling to control the robot via a wireless keyboard.
The robot uses the Rpi zero 2W.
SOS (because I am nearly age 77 and my brain hurts)
Has anyone got an ‘oven ready’ python program example that would allow me to make some progress with this great little robot.
Regards
Hugh Hamilton
Glasgow UK
Have a look see at this.
Controlling Your Robot: USB HID/Wireless Keyboards (pimoroni.com)
I got it working with a Mini Wireless keyboard and an Explorer pHat. It was a few years ago, I might still have my code, will have a look see for it.
Found a file with keyboard setup in it.
#sudo apt-get install libusb-dev
#git clone https://github.com/walac/pyusb
#cd pyusb
#sudo python3 setup.py install
#!/usr/bin/env python
import os
import usb.core
import usb.util
import explorerhat
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
x=(2) #shutdown variable
S=(100) #speed default value.
M=(0) # 0 = Not moving, 1 = Moving Forward, 2 = Moving Backward,
USB_VENDOR = 0x1997 # Rii
USB_PRODUCT = 0x2433 # Mini Wireless Keyboard
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS
BTN_LEFT = 80 # <
BTN_RIGHT = 79 # >
BTN_DOWN = 81 # V
BTN_UP = 82 # ^
BTN_STOP = 44 # Space
BTN_EXIT = 41 # ESC
BTN_OK = 40 # OK
BTN_ONE = 30 # 1
BTN_TWO = 31 # 2
BTN_THREE = 32 # 3
BTN_FOUR = 33 # 4
BTN_FIVE = 34 # 5
BTN_SIX = 35 # 6
BTN_SEVEN = 36 # 7
BTN_EIGHT = 37 # 8
BTN_NINE = 38 # 9
BTN_ZERO = 39 # 0
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
endpoint = dev[0][(0,0)][0]
if dev.is_kernel_driver_active(USB_IF) is True:
dev.detach_kernel_driver(USB_IF)
usb.util.claim_interface(dev, USB_IF)
def Shutdown(channel):
global x
x = (0)
GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
while True:
control = None
try:
control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
print(control)
except:
pass
if control != None:
if BTN_ONE in control:
S = (10)
if BTN_TWO in control:
S = (20)
if BTN_THREE in control:
S = (30)
if BTN_FOUR in control:
S = (40)
if BTN_FIVE in control:
S = (50)
if BTN_SIX in control:
S = (60)
if BTN_SEVEN in control:
S = (70)
if BTN_EIGHT in control:
S = (10)
if BTN_NINE in control:
S = (90)
if BTN_ZERO in control:
S = (100)
if BTN_DOWN in control and M != 1: # Go into reverse if not moving forward, and stop turning if turning.
explorerhat.motor.backwards(100)
M = 2
if BTN_UP in control and M != 2: # Go into forward if not moving in reverse, and stop turning if turning.
explorerhat.motor.forwards(100)
M = 1
if BTN_LEFT in control and M == 0: # Turn left while stoped
explorerhat.motor.two.forwards(50)
explorerhat.motor.one.backwards(50)
if BTN_RIGHT in control and M == 0: # Turn right while stoped
explorerhat.motor.two.backwards(50)
explorerhat.motor.one.forwards(50)
if BTN_LEFT in control and M == 1: # Turn left while moving forwards
explorerhat.motor.two.forwards(100)
explorerhat.motor.one.forwards(50)
if BTN_RIGHT in control and M == 1: # Turn right while moving forwards
explorerhat.motor.two.forwards(50)
explorerhat.motor.one.forwards(100)
if BTN_LEFT in control and M == 2: # Turn left while moving backwards
explorerhat.motor.two.backwards(100)
explorerhat.motor.one.backwards(50)
if BTN_RIGHT in control and M == 2: # Turn right while moving backwards
explorerhat.motor.two.backwards(50)
explorerhat.motor.one.backwards(100)
if BTN_OK in control: # Stop moving and stop turning
explorerhat.motor.stop()
M = 0
if BTN_EXIT in control:
raise SystemExit
if BTN_STOP in control:
os.system("sudo shutdown now -P")
time.sleep(0.02)
if x == 0:
os.system("sudo shutdown now -P")
elif x == 1:
raise SystemExit
# Last edited on Jun 11th 2018
# run sudo crontab -e
# add
# @reboot sudo python3 /home/pi/RoverKB.py &