Using Legos to turn a Raspberry Pi into a mobile device

Controlling the GPIO Pins

A Java application on the Rasp Pi controls the GPIO pins by either manually programming the application or by using a library like Pi4J [7].

The commands from Listing 2 that are in the source code of this Java application work in conjunction with an application that is included in the WiringPi [8] library to configure the GPIO pins. Initially, these pins are configured as output pins.

Listing 2

Configure GPIO Pins

private void setupPins() throws IOException {
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio mode 0 out");
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio mode 1 out");
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio mode 3 out");
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio mode 4 out");
}

The numbering of the pins from 0 to 4 is based on the fact that pins 11, 12, 15, and 16 used in WiringPi are designated GPIO0, GPIO1, GPIO3, and GPIO4. After configuring the pins, you should set them in combinations according to the specifications of the motor driver chips on either HIGH/LOW or 1/0, so that the motor runs in one or the other direction, or so that it idles.

The motor will idle when both input pins for the L293E chip have an equal allocation. Allocating one of the pins with HIGH and one with LOW will cause the motor to turn in one direction or the other, depending on which of the two pins is on HIGH. Listing 3 shows the relevant Java code.

Listing 3

Java Code to Allocate Pins

private void motor1Forward() throws IOException {
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio write 0 1");
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio write 1 0");
}
private void motor1Backward() throws IOException {
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio write 1 0");
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio write 0 1");
}
private void motor1Stop() throws IOException {
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio write 0 0");
 Runtime.getRuntime().exec("sudo /home/pi/wiringPi/gpio/gpio write 0 0");
}

Remote Method Invocation

The remote method invocation, or RMI, runs in another application on a second computer, from which you then control the application via the Internet. You will need to define an interface that has all of the methods that the server, in this case the application running on the Raspberry Pi, makes available. The server will then need to implement the interface.

You should register the server so that it is accessible by the client, which, for purposes of this project, is the second application. Listing 4 shows how a registry is created and a server object RMIServerImpl is instantiated and registered as part of the source code of the server application.

Listing 4

Create Registry

Registry r = LocateRegistry.createRegistry(Port);
r.rebind("RMIServer", new RMIServerImpl());

The client accesses the server object by first retrieving the registry that has been created on the server and, from there, retrieving the object that was registered under the name RMIServer. After the client has retrieved the server object and stored it in a local object, the local object can be used to invoke the methods defined as part of the interface. Listing 5 shows the relevant source code of the client application.

Listing 5

Get Registry

Registry r = LocateRegistry.getRegistry("RasPi-IP", Registry-Port);
RMIServerI server = (RMIServerI) r.lookup("RMIServer");

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content