If you want to build a cool robot or another system that needs to control servos this tutorial blog post is exactly for you. You will learn how to easily connect servos with your RaspberryPi system using pure Java and Robo4J framework. The Robo4J framework is separated into the modules (see documentation). The each module can be used separately! The module used in this example is "robo4j-hw-rpi". This module contains necessary drivers and abstractions that operates on the RaspberryPi platform.
Requirements:
a) Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface - PCA9685 (here)
b) Micro Servo SG92R (example, different one can be used) (here)
c) RaspberryPi (example, RaspberryPi 3) (here)
d) Installed JDK on your Raspberry Pi (example, uses Bellsoft JDK)
e) Robo4J framework build (see previous post) (GitHub repo)
f) Connect the servo to the PWM channel 0
The Robo4J contains very useful set of examples, one of those is focused on connecting servos over I2C bus. I2C is very useful and powerful bus that can be used for communication with multiple connected devices (master slave pattern).
Only one servo is used in the example for its simplification. You should receive a clear view how you can create your own RoboUnits that help you to build the advanced system.
Let's start with testing wired servos
After having all properly wired together, we do upload the fatJar file to the RaspberryPi by using the following command:
$ scp robo4j-hw-rpi/build/libs/robo4j-hw-rpi-alpha-0.4.jar pi@<RPI_IP>:/home/pi
In case you have build the framework directly on your RaspberryPi the fatJar file is found exactly on the same path (robo4j-hw-rpi/build/libs/robo4j-hw-rpi-alpha-0.4.jar)
Running the ServoTester example
While being logged in to your RaspberryPi you need to run the following command:
$ java -cp robo4j-hw-rpi-alpha-0.4.jar com.robo4j.hw.rpi.i2c.pwm.ServoTester <pwm_channel_id>
important note: <pwm_channel_id> represents the channel (plug) of connect servo (Channel 0)
It will open the command loop and now you can test the wired servo. The ServoTester allows to have connected max 16 servos at the same time (based on PWM specification). In the example is used only one connected to the channel 0. You should see the following output:
Creating device...done! Type the id of the channel of the servo to control and how much to move the servo,
between -1 and 1. For example:
known servos=0>15 -1.0 Type q and enter to quit!
When you are inside the loop you can type the channel followed by the normalized servo position example: known servo <channel_id> <position>. When you are done with the testing you can simply type q letter to quit the loop.
The following output represents example test:
known servos=0>0 0
known servos=0>0 -1
known servos=0>0 0
known servos=0>0 1
known servos=0>0 0
known servos=0>q
Bye!
Final Thoughts
Now you should be able to test your wired servo and start to working on the new RoboUnits. When you follow the previous post focused on how to connect the Gamepad you can already build some controlled system.
Happy Coding!
Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts
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 !
Subscribe to:
Posts (Atom)