Infineon Team
Published © MIT

Control everything with a gaming controller

Learn how you can control your projects with a gaming controller.

IntermediateFull instructions provided6 hours900
Control everything with a gaming controller

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
TLE94112ES Raspberry Pi HAT
Infineon TLE94112ES Raspberry Pi HAT
×1
Generic Powerbank
Optional
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

Physical connections of the setup

Pi HAT connections

Code

project_christmas.py

Python
Final Project code
import multi_half_bridge_py as mhb
from pyPS4Controller.controller import Controller


#------------------------------------------------------------------PI HAT SKELETON--------------------------------------------------------------------------------

#instances-----------------------------
controller = mhb.Tle94112Rpi()

motor1 = mhb.Tle94112Motor(controller)
motor2 = mhb.Tle94112Motor(controller)
motor3 = mhb.Tle94112Motor(controller)
#--------------------------------------

#controller configuration-------------
controller.begin()

controller.clearErrors()
#-------------------------------------

#motor configuration---------------------------------------

#motor 1 configuration-----------------------------
motor1.connect(motor1.HIGHSIDE, controller.TLE_HB1)
motor1.connect(motor1.LOWSIDE,  controller.TLE_HB5)
motor1.setPwm(motor1.HIGHSIDE, controller.TLE_PWM1)
motor1.begin()
#--------------------------------------------------

#motor 2 configuration-----------------------------
motor2.connect(motor2.HIGHSIDE, controller.TLE_HB7)
motor2.connect(motor2.LOWSIDE,  controller.TLE_HB9)
motor2.setPwm(motor2.HIGHSIDE, controller.TLE_PWM2)
motor2.begin()
#--------------------------------------------------

#motor 3 configuration-----------------------------
motor3.connect(motor2.HIGHSIDE, controller.TLE_HB6)
motor3.connect(motor2.LOWSIDE,  controller.TLE_HB4)
motor2.setPwm(motor3.HIGHSIDE, controller.TLE_PWM3)
motor3.begin()
#--------------------------------------------------
#----------------------------------------------------------

#------------------------------------------------------------------------------------------------------------------------------------------------------------


#-----------------------------------------------------------Pi HAT Active control part ----------------------------------------------------------------------

#-------------------------custom functions--------------------------------- 
def motors_stop():
    motor3.stop(255)
    motor2.stop(255)
    motor1.stop(255)
def move_forward():
    motor1.start(255)
    motor3.start(-255)
def turn_right():
    motor2.start(-255)
def turn_left():
    motor2.start(255)
def move_backward():
    motor1.start(-255)
    motor3.start(255)
def coast_vertical():
    motor1.coast()
    motor3.coast()
def coast_horizontal():
    motor2.coast()
def rotate_clockwise():
    motor1.start(-255)
    motor2.start(-255)
    motor3.start(-255)
def rotate_anticlockwise():
    motor1.start(255)
    motor2.start(255)
    motor3.start(255)
def motors_coast():
    motor1.coast()
    motor3.coast()
    motor2.coast()
#-------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------------------------------------


#---------------------------------------------------------------------Dualschock4 part-----------------------------------------------------------------------

class MyController(Controller):

    def __init__(self, **kwargs):
        Controller.__init__(self, **kwargs)

    def on_x_press(self):
        motors_stop()        
    def on_up_arrow_press(self):
        move_forward()
    def on_left_arrow_press(self):
        turn_left()
    def on_right_arrow_press(self):
        turn_right()
    def on_down_arrow_press(self):
        move_backward()
    def on_up_down_arrow_release(self):
        coast_vertical()
    def on_left_right_arrow_release(self):
        coast_horizontal()
    def on_R1_press(self):
        rotate_clockwise()
    def on_R1_release(self):
        motors_coast()
    def on_L1_press(self):
        rotate_anticlockwise()
    def on_L1_release(self):
        motors_coast()
    def on_R2_press(self, value):
        move_forward()
        return super().on_R2_press(value)
    def on_R2_release(self):
        coast_vertical()
        return super().on_R2_release()
    def on_L2_press(self, value):
        move_backward()
        return super().on_L2_press(value)
    def on_L2_release(self):
        coast_vertical()
        return super().on_L2_release()


controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
# you can start listening before controller is paired, as long as you pair it within the timeout window
controller.listen(timeout=60)

Infineon Multi-Half-Bridge Library

pyPS4Controller

Credits

Infineon Team

Infineon Team

89 projects • 134 followers

Comments