Pages

Monday, October 8, 2018

How to Connect Servos using PWM Breakout and Java through I2C

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!

No comments: