Pages

Monday, October 8, 2018

Connecting Gamepad with RaspberryPi in 4 Steps Using Java


In this blog post you will learn how to connect common gamepad with RaspberryPi using Robo4J framework.  
Imagine you are building a Java system like car, tank or maybe a drone :). You want to get realtime interaction with it. Your system is based on RaspberryPi platform. 
Although you may have implemented some self-controlling algorithms it's quite handy to have possibility to turn them off and take the control over the system. 
There is a good choice how to easily connect the common gamepad to take the control into your hands.

Whole process can be simplified in to following 4 steps:
1. Build Robo4J framework
2. Upload robo4j-units-rpi-alpha-0.4.jar fat jarfile to the RaspberryPi 
3. Connect the gamepad to the RaspberryPi 
4. Run the LF710PadExample example 

Requirements:
a) checkout git repository Robo4J
b) Java JDK 1.8 and above installed on RaspberryPi
    example uses Bellsoft openjdk version 11
    openjdk version "11-BellSoft" 2018-09-25
  OpenJDK Runtime Environment (build 11-BellSoft+0)
  OpenJDK Server VM (build 11-BellSoft+0, mixed mode)
c) GamePad Logitech F710  

1. Build the framework
Let's start with building the Robo4J framework. As you may have noticed it's a standard Gradle project. Currently Robo4J is still not fully ported to java 11 so you need build the binaries against JDK 1.8. Run following command in root directory:

$ ./gradlew clean build 

after successful build we need to build fatJar file for robo4j-units-rpi. This subproject does contains all Robo4J unit capable to operate upon the RaspberryPi platform. They contains hardware connectors and appropriate logic to handle them. Run the following command: 

$ ./gradlew :robo4j-units-rpi:fatJar 

2. Uploading robo4j-unit-rpi-alpha-0.4.jar fat jarfile to the RaspberryPi
In case you have built Robo4J inside the IDE you need to upload the jar file to your device. The fat jar file can be found on following path "<ROBO4J_ROOT>/robo4j-units-rpi/build/libs/robo4j-units-rpi-alpha-0.4.jar" . Now you need to upload it by the following command:

$ scp robo4j-units-rpi/build/libs/robo4j-units-rpi-alpha-0.4.jar pi@<RPI_IP>:/home/pi

I've built mine directly on the device. The created jar file can be found on the path mentioned above.

3. Connect your gamepad to the RaspberryPi
Make sure your gamepad is connected to the RaspberryPi before you start the demo. 

4. Run the LF710PadExample example
To run the example you need to execute the following command:

$ java -cp robo4j-units-rpi-alpha-0.4.jar com.robo4j.units.rpi.pad.LF710PadExample

Now the demo is running and you see every press or joystick move action. 

the demo output:
RoboSystem state Started
================================================
    lf710pad                          Started

Press enter to quit!
INFO: PAD pressed: LF710Message{time=4285110, amount=1, part=LF710Part{mask=1, name='button'}, input=LF710Button{mask=1, name='green'}, state=LF710State{mask=1, name='pressed'}}
INFO: PAD pressed: LF710Message{time=4285210, amount=0, part=LF710Part{mask=1, name='button'}, input=LF710Button{mask=1, name='green'}, state=LF710State{mask=0, name='released'}}
INFO: PAD pressed: LF710Message{time=4285820, amount=1, part=LF710Part{mask=1, name='button'}, input=LF710Button{mask=0, name='blue'}, state=LF710State{mask=1, name='pressed'}}
INFO: PAD pressed: LF710Message{time=4285970, amount=0, part=LF710Part{mask=1, name='button'}, input=LF710Button{mask=0, name='blue'}, state=LF710State{mask=0, name='released'}}
INFO: PAD pressed: LF710Message{time=5828640, amount=-4054, part=LF710Part{mask=2, name='joystick'}, input=LF710JoystickButton{mask=3, name='right y'}, state=LF710State{mask=1, name='pressed'}}

...


Final Thoughts
The Robo4J framework has implemented all possible events that can be caused by the gamepad. The gamepad is compatible with SONY Playstation and others.
The gamepad RoboUnit (LF710PadUnit) responsible for handling the gamepad events can be added into the any system. 
The LF710PadUnit unit does contain the target property. The target needs to accept LF710Message type. The LF710Message does contains all necessary information about the kind of the even that has been done. It means which button or joystick has been used and how long. 
It make fairly very usable tool in order to building very advanced system using RaspberryPi platform. It provides the possibility to enjoy realtime experience with those system all running on Java. It allows to implement more advanced algorithms or control techniques 

Happy Coding !










No comments: