Quick and easy with PySimpleGUI
Formatting Display Elements
The next step is to adjust the fonts, colors, and size properties of the graphic elements. With just three lines you can change the FORWARD button to 32 characters wide and three lines high with added color and a larger font:
[sg.Button("FORWARD", size=(32,3), font="Ariel 32", button_color=('white','green'))]
To make the rover control interface more usable, you can enlarge and add color to all the control buttons (Figure 5).
Raspberry Pi Rover
For my rover, I used a low-cost Arduino car chassis (~$15) and a portable phone charger to power the Raspberry Pi. Duct or painters tape works well to secure the Pi and charger to the car chassis.
Connecting motors directly to the Raspberry Pi GPIO pins is not recommended because their power requirements could damage your Raspberry Pi. If you're on a budget, you can create your own motor protection circuit with an L298N dual H-bridge chip (~$2); otherwise, you can find a variety of Pi motor or relay tops. For this project, I used a Pimoroni Explorer HAT Pro [2] (~$22).
For the final code (Listing 2), I added a command-line option (lines 6-10) that allows either a local Tkinter interface or a web interface. The program default is a Tkinter GUI; however, if any command-line text is entered, the PySimpleGUIWeb interface is used.
Listing 2
PySimpleGUI/Web Rover
01 # SGui_rover.py - use PySimpleGUI/Web to control a Pi Rover Pi 02 # 03 04 import sys 05 # Pass any command line argument for Web use 06 if len(sys.argv) > 1: # if there is use the Web Interface 07 import PySimpleGUIWeb as sg 08 mode = "web" 09 else: # default uses the tkinter GUI 10 import PySimpleGUI as sg 11 12 import RPi.GPIO as gpio 13 gpio.setmode(gpio.BOARD) 14 # Define the motor pins to match your setup 15 motor1pin = 38 # left motor 16 motor2pin = 37 # right motor 17 gpio.setup(motor1pin, gpio.OUT) 18 gpio.setup(motor2pin, gpio.OUT) 19 20 # Send Action to Control Rover 21 def rover(action): 22 if action == "FORWARD": 23 gpio.output(motor1pin, gpio.HIGH) 24 gpio.output(motor2pin, gpio.HIGH) 25 if action == "LEFT": 26 gpio.output(motor1pin, gpio.HIGH) 27 gpio.output(motor2pin, gpio.LOW) 28 if action == "RIGHT": 29 gpio.output(motor1pin, gpio.LOW) 30 gpio.output(motor2pin, gpio.HIGH) 31 if action == "STOP": 32 gpio.output(motor1pin, gpio.LOW) 33 gpio.output(motor2pin, gpio.LOW) 34 35 # All the stuff inside your window. 36 myfont = "Ariel 32" 37 layout = [ [sg.Text(" ",size=(20,1) , key="feedback")], 38 [sg.Button("FORWARD", size=(32,3), font=myfont, button_color=('white','green'))], 39 [sg.Button("LEFT", size=(15,3), font=myfont),sg.Button("RIGHT", size=(15,3), font=myfont)], 40 [sg.Button("STOP", size=(32,3), font=myfont, button_color=('white','red'))], 41 [sg.Button("QUIT")] 42 ] 43 # Create the Window 44 if mode == "web": 45 window = sg.Window('PySimpleGUI Rover Control', layout, 46 web_ip='192.168.0.106', web_port = 8888, web_start_browser=False) 47 else: 48 window = sg.Window('PySimpleGUI Rover Control', layout ) 49 50 # Event Loop to process "events" and pass them to the rover function 51 while True: 52 event, values = window.read() 53 print(event,values) 54 if event in (None, 'QUIT'): # if user closes window or clicks cancel 55 break 56 window['feedback'].Update(event) # show the button in the feedback text 57 rover(event) 58 59 window.close() # exit cleanly
The forward left and right motor pins are defined on lines 15 and 16. If you are using a Pi motor HAT or an L298N circuit, you can also define backward left and right motor pins. (Direct-wiring the Pi pins or using a relay top only supports one direction for the motors.)
A rover
function (lines 21-33) controls the motors according to button events. Figure 6 shows the Raspberry Pi rover with the web interface.
Summary
I was very happy with the performance and features offered by the PySimpleGUI library, and I found that the Pi rover project was a simple way to get started. For small Internet of Things (IoT) projects, you can use PySimpleGUI and PySimpleGUIWeb to create dashboard interfaces with bar and real-time charts.
Infos
- PySimpleGUI docs: https://pysimpleGUI.readthedocs.io/
- Pimoroni Explorer HAT Pro: https://www.adafruit.com/product/2427
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.