Using Legos to turn a Raspberry Pi into a mobile device

Xbox 360 Controller

The key that you press on an Xbox 360 controller determines which of the methods found on the server application you will invoke. At this point, you will need to connect the Xbox controller with the client PC and install the correct driver. Then, you should add the Jinput library to the client application [9].

The next step is to use the library to test whether a controller is connected to the client at all. If so, you should run a loop that tests whether the state of the control keys has changed. If this is the case, start a function call. You can decide for yourself what happens when each key is pressed.

For example, the code in Listing 6 defines the effect of pressing the A button of the controller; the state of the appropriate GPIO pins on the Rasp Pi is set to HIGH/LOW or 1/0, as is required by the method motor1Forward(). When the button is released, the routine will set the appropriate GPIO pins to LOW or 0 as prescribed by the motor1Stop() method.

Listing 6

Pressing the A Button

while (isRunning) {
 gamepad.poll();
 for (Component c : gamepad.getComponents()) {
  if (c.getIdentifier() == button0) {
   if (!oldValue == c.getPollData())) {
    if (oldValue == 0) {
     server.motor1Forward();
     oldValue = 1;
    } else {
     server.motor1Stop();
     oldValue = 0;
    }
   }
  }
 }
}

Conclusion and Outlook

The costs of implementing the do-it-yourself project presented in this article will run about EUR 200 if you buy all new components. To make the project easier, you could use a BrickPi [10] instead of building the electronics yourself [11]. The BrickPi makes it easy to connect the motor and sensor components from Lego Mindstorms to the Rasp Pi. However, the convenience you gain with this approach raises the price of the project considerably.

You can add as many motor controller chips as you want, provided you have unassigned GPIO pins available. You then define and implement the appropriate methods as part of the interface for the RMI server to control the motors. Additionally, you can add a test for the desired button press, including method invocation. This technique allows you to add additional functions to the car. For example, you could add a motorized version of the grappler from Lego set 42006.

Infos

  1. Lego Mindstorms: http://mindstorms.lego.com
  2. Lego Power Functions: http://powerfunctions.lego.com
  3. Power Functions internals: http://www.philohome.com/pf/pf.htm
  4. "Tricks zum Programmieren der GPIO-Schnittstelle" by Richard Ryniker, Raspberry Pi GEEK, Issue 5: http://www.raspi-geek.de/29891 (in German)
  5. 5V USB Battery: http://elinux.org/RPi_VerifiedPeripherals
  6. "First Steps" by Paul C. Brown, Joseph Guarino, and Joe Casad, Raspberry Pi Geek, Special Edition 17, pg.  8: http://www.linuxpromagazine.com/Resources/Special-Editions
  7. WiFi Adapter: http://elinux.org/RPi_USB_Wi-Fi_Adapters
  8. Pi4J Library: http://www.pi4j.com
  9. WiringPi Library: https://projects.drogon.net/raspberry-pi/wiringpi/
  10. Jinput Library: https://java.net/projects/jinput
  11. BrickPi: http://www.dexterindustries.com/BrickPi/
  12. "BrickPi" by Marko Dragicevic, Raspberry Pi Geek, issue 3: http://www.raspberry-pi-geek.com/Archive/2014/03/Building-Rasp-Pi-into-Mindstorms-projects-with-BrickPi

The Author

Martin Filusch is currently working on a Master's degree in Computer Science and Distributed and Mobile Applications at the Osnabruck University of Applied Sciences. He has a Bachelor's degree in Computer Science and Media Technology, also from HS Osnabruck.

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