Tag Archives: Raspberry Pi

Leap Motion with the Raspberry Pi

Robots have the capability to work where humans would find it inconvenient. In fact, that is one of the reasons people build robots. For example, in areas where high amounts of nuclear radiation would be fatal for a human being, a robot can work happily. Science fiction movies have exploited this feature several times – a robot mimicking the hand movements of its human controller, when watched and manipulated from a safe distance. Now, with a few motion-controlled servos, Leap Motion and Raspberry Pi or RBPi, the tiny Single Board Computer, you too can make a robot with the ability to mirror the movement of your hands. Additionally, you can do this even when you are sitting on the opposite side of the Earth.

The project involves two servos, each mirroring the movement of your individual hands. A Leap Motion controller captures the motion of your arms and sends appropriate instructions to the RBPi, which drives the two servos using a PWM driver. Two 8×8 RGB LED matrices individually attached to the servos react to each finger movement on your hands. The Leap Motion controller communicates with the RBPi via PubNub Data Streams.

The project uses the RBPi Model B+, Leap Motion controller with Leap Motion Java SDK, four numbers of Tower Pro Micro Servo, the Adafruit PWM Servo Driver and an optional display case.

The Leap Motion controller is a powerful device. It is equipped with three infrared LEDs and two monochromatic IR cameras. The cameras capture the movement of your hands and Leap Motion publishes their attributes to a channel via PubNub. The Leap Motion SDK has the attributes pitch, yaw and roll pre-built in it and actually separates the movements of your hands into the three attributes.

For achieving real-time mirroring, Leap Motion sends the attribute information messages nearly twenty times in a second. It sends information about your individual arms and each of your fingers to PubNub. Since the RBPi subscribes to the same channel, it is able to parse these messages for controlling the servos and the RGB LEDs.

To start, you will need to open a Java IDE and create a new project. You will find a guide for the Leap Motion Java SDK here. Follow up this step with installing the PubNub Java SDK. Make your project implement Runnable, which will allow all the Leap activity to operate in its own thread.

Every second, Leap Motion captures nearly 300 frames. Each frame has a huge amount of information about the hands, such as the number of fingers presently extended and hand gestures such as pitch and yaw. To simulate the motion of the hands, one servo mirrors the pitch while the other mirrors the yaw. Incidentally, pitch is the rotation around the X-axis and yaw is the rotation around the Y-axis. Both servos rotate 180-degrees with a sweeping motion. The resulting servo mimics most of the movements your hands make.

Leap Motion outputs values for the pitch and yaw in radians. The RBPi is responsible for converting these radians into degrees and finally into PWM or pulse width modulation between 150 and 600 MHz for driving the servos.Leap Motion with the Raspberry Pi

Raspberry Pi Zero for a Real-Time Sensor Dashboard

Using the Raspberry Pi or RBPi, the single board computer (SBC), and a few applications from Google, you can have a functional dashboard showing real-time parameters from sensors. Google offers its App Engine in the form of a Platform as a Service or PaaS. The advantage is you can deploy and run your own applications using the Google infrastructure without bothering about exclusive ways of setting up hardware, servers, or Operating Systems.

Google also offers the free and powerful Google Charts that you can use as simple charting tools for plotting the data from the sensors into line charts. An HTML5 templates generator such as the Initializr is also useful for generating templates for the dashboard. Initalizr has several useful frontend resources such as Bootstrap and jQuery.

RBPi Zero is the perfect hardware platform to use for this project. This SBC is a full-fledged computer, but smaller than a credit card. It features a single-core CPU running at 1 GHz and 512 MB RAM. Along with a 40-pin GPIO header, the RBPi Zero has USB and a mini HDMI port.

When you connect a few sensors to the GPIO pins, the RBPi Zero sends their data over to the Google App Engine. On the dashboard, you can see the values and the charts updating in real-time as new data arrives from the sensors. Github carries the instructions for building and deploying the project for the RBPi Zero app and the App Engine dashboard.

For this project, Java is the programming language, as both the RBPi Zero and the Google App Engine support it – both use the Pi4J library. However, those who prefer Python can easily change the code, as both RBPi and the Google App Engine support Python as well. As the latest version of Raspbian, the Operating System of the RBPi comes pre-installed with Oracle Java 8, it is easy to deploy and run an executable JAR on the RBPi Zero.

The JAR acts as the go-between with the sensors and the Google App Engine – it reads inputs from the sensors and passes them on to the Google App Engine. You can use the Apache Maven to compile and build the code on the RBPi Zero. Of course, you may also build the code on your laptop or desktop and copy the resulting JAR over to the RBPi Zero.

You can use Cloud Endpoint on the Google App Engine side. This is a powerful service for creating a backend API by using annotations. This includes the client libraries for web and mobiles. It generates a Java based Android client for use with the RBPi Zero application. Google Qauth 2.0 authenticates the API for installed applications.

The RBPi Zero based hardware provides readings from three sensors – voltage generated by a solar cell, temperature from an analog temperature sensor, and illuminance or LUX from a photocell. A 10-bit Analog to Digital converter with SPI interface is necessary to covert the analog signal to a digital format suitable for the RBPi Zero. All the sensors work with a supply of 3.3V, and the RBPi Zero is capable of sourcing this.

Play Chess with the Raspberry Pi

You could be an ardent chess player searching for a worthy opponent. A human opponent may not always be conveniently present, but a computerized player can be relied upon to be available at any time of your choosing. With the Single Board Computer, the Raspberry Pi, or RBPi, you can now play a complicated game of chess, provided you are willing to build a chessboard first.

You will need an Arduino to control the chessboard and an RBPi to run the actual chess engine Stockfish, along with Chessboard, which is the chess rules library. The entire arrangement is completely automated – plug in the different parts, press the green button and you start playing. If there is no automated arm, you must move the pieces manually and the computer signals its move by flashing LEDs. You get 21 levels of play along with the ability to set the personality of the computer – coward or aggressive.

Apart from the personality setting and the 21 levels, Stockfish allows several features. Choose to play with black or with white pieces, and play against the computer or another human. Along with providing hints if stuck, Stockfish recognizes and makes special moves such as Castling, En Passent, and Pawn Promotion. It validates all moves against all rules of chess, signaling errors and allowing re-moves. The chess engine has a maximum rating of GM and an ELO level of 2900.

Although you can use the RBPi alone to control the board and play the chess engine, using an Arduino relieves the RBPi of many functions, speeding up the chess engine running on it. Since the Arduino does not use an Operating System, it is not possible to run Stockfish on it. Although there are chess programs to run on the Arduino, none is as strong as the Stockfish. Moreover, if you are using a computerized arm, the Arduino can take care of operating the motors. The combination of RBPi and Arduino for the chessboard works efficiently.

You can make the board out of wood or plastic according to the materials readily available. A chessboard has 64 squares with alternate black and white colors. To sense the pieces, you need reed switches under each square. These will be wired in the form of a matrix with eight rows and eight columns, with a single reed-switch straddling each junction. By numbering the rows as 1-8 and the columns as A-H, a command E2E4 tells the computer to move the piece from the E2 square to the E4 square.

To let the computer signal its move with LEDs, you will need a second matrix similar to that of the reed switches. Only this time, instead of reed switches, you must place LEDs at the junction points. Using sockets for both the reed switches and the LEDs is advisable as it becomes easy for maintenance. Unlike reed switches, LEDs are polarized, and need to be properly oriented to function. Placing them in sockets helps to re-orient them if they are inserted the wrong way. The Arduino controls both the matrices with data from the RBPi.

Driving Steppers with the RasPiRobot Board

The Raspberry Pi or RBPi is an inexpensive, tiny single board computer running the Linux operating system. As such, the standalone RBPi is not suitable for running motors, but when combined with an expansion board such as the RasPiRobot Board, you can easily run DC motors as well as Stepper motors off the RBPi. For this, you must use the version 2 of the RasPiRobot or RRBv2 board. Please note you can run only 5V steppers with the RBPi RRBv2 combination, as this board does not support 12V motors.
In practice, the RRBv2 board sits over the RBPi fitting over the latter’s GPIO connector. The stepper motor wires connect to the RRBv2 board, using its L & R screw terminals. To do that, you must first strip the wire ends of their PVC insulation, until about 10 mm of bare copper wire is exposed. Unscrew the terminal sufficiently to allow insertion of the copper part of the wire into the hole. Turn the screw clockwise to let the jaws hold the wire firmly.
One of the advantages of using the RRBv2 board is you can run the stepper motors from a battery pack. The board has a switch-mode power circuit to provide stable power to the motors. Additionally, you can even run your RBPi from this on-board power supply. That makes the entire arrangement completely portable.
When connecting the battery pack to the RRBv2 Board, take care to observe the correct polarity of the flying leads from the battery pack. Some battery packs terminate the wires on a plug. Therefore, you must use a matching female socket adapter that has flying leads. In either case, connect the positive or red lead to the screw terminal marked Vin on the RRBv2 board. Connect the negative or black lead to the screw terminal marked as GND on the RRBv2 board. Powering on/off through a battery pack becomes simpler if there is a built-in switch.
If you have connected your RBPi to the RRBv2 board, throwing the switch to the on position will allow the RBPi to start booting. To run the stepper motor with commands from the RBPi, you will need to download the RRBv2 Python library codes. For this, you will have to connect your RBPi to the Internet.
You can use the Ethernet connection to connect your RBPi to the Internet. Alternately, you may use a suitable Wi-Fi dongle. Once online, use SSH to establish connection to the RBPi from a PC and proceed to download the RRBv2 Python library from here and install it.
To run a stepper motor, you can write some simple Python codes, following the tutorial here. For example, you will have to provide the delay between the steps, the total number of steps you want the stepper motor to move and the direction of rotation – backwards or forwards.
The delay between the steps governs the speed of rotation of the stepper motor. For example, as you make the steps larger, the motor turns more slowly to make the total number of steps.

Room Automation and Raspberry Pi

Most people prefer to come back to a cozy room after a full day’s work. For many, this may not always be possible, unless someone turns on the AC at the right time. For those living alone, help is available in the form of a single board computer, the Raspberry Pi or the RBPi. In addition, the RBPi operates the blinds and you can control it from anywhere in the world – the RBPi is connected to the Internet.

For this project, you will need an RBPi with a suitable SD card, a Wi-Fi dongle, a stepper motor. You will also need a power source capable of driving the RBPi and the motor, a stepper motor driver board, an IR receiver, an IR LED and an NPN transistor.

Controlling the AC is a simple affair, with the RBPi simulating the infrared information the remote control normally uses. You need to use the LIRC library for the RBPi to record this IR information via the IR receiver. The infrared LED driven through the NPN transistor duplicates the signal sent by the remote control of the AC. Initially, you must let RBPi learn the IR codes by recording those using commands in the LIRC library. LIRC produces a configuration file that holds the IR codes for your AC. Playing back these codes through the IR LED allows you to control the AC just as its own remote does.

The RBPi and the motor driver board control a stepper motor for driving the blinds. The RBPi merely drives a GPIO pin to let the motor driver board know if it must operate the stepper. The driver board already has the necessary parameters stored within it for driving the motor. By default, the motor remains off so that it does not waste power when it is not needed. The software takes care of this by turning off the Enable pin on the stepper driver board. When you need to operate the blinds, a script on the RBPi turns the GPIO pin on and off.

To operate the unit from remote, you need to connect the RBPi to the Internet via a wireless network. Use the Wi-Fi dongle for this, configuring the RBPi to switch on the wireless connection immediately after booting. Web access to the stepper motor controller is through Nginx and PHP.

The entire setup works when the RBPi connects wirelessly to the network. You access a web interface and use it to send commands to the controller script running on the RBPi. Depending on the commands sent, you can access either the blind opener or the AC control. For opening the blinds, the RBPi sends on or off signals to the stepper motor controller board.

On the other hand, the RBPi sends the appropriate commands to the air conditioner via the IR link. Depending on the code transmitted over the IR link, the AC will switch either on or off. Additionally, with proper codes transmitted from the RBPi to your AC, you can even set the temperature of the room before returning at the end of the day.

Drive a 16-Channel Servo with the Raspberry Pi

To drive servomotors micro-controllers must have PWM outputs. These are output pins on which the micro-controller will generate pulse outputs with controlled or modulated variable widths. Most embedded micro-controller units have one or more of these outputs. The famous single board computer, the tiny credit card sized Raspberry Pi or RBPi also has one IO pin dedicated for PWM. This is the PWM channel available at the GPIO18 of the RBPi and with this, you can drive a single servo at best. However, if you want the RBPi to drive more than one servo, it will need additional circuitry.

A PWM driver IC such as the PCA9685 can drive 16 servos at a time, but requires commands and data through its I2C interface. Fortunately, the RBPi can also communicate using the I2C protocol, enabling it to control 16 servos via the PCA9685. Adafruit has a very convenient breakout board with the PCA9685 on it and that makes it very convenient to connect to the RBPi. Not only can you drive servos with the PWM outputs, you can use the PWMs for controlling LED lighting as well.

To let RBPi communicate with the I2C protocol, it will require a special OS available from Adafruit. This is the Occidentalis flavor and it has all the libraries required for invoking I2C. However, if you are using the stock Raspbian OS, you must install the python-smbus and the i2c-tools using the “sudo apt-get install” command. To learn more about using I2C, refer Adafruit’s rather informative tutorial.

The two packages will allow you to search for any I2C device connected to the RBPi. The easiest way you can connect the servo breakout board to your RBPi is with the help of the Adafruit Pi Cobbler. Here, VCC is the digital supply for the IC or 3.3V, and V+ is the supply for the servomotors (typically 5V).

The actual chip that drives the servos, the PCA9685, needs 3.3V, and connects to the VCC on the cobbler board. Servos usually require much higher currents to operate. Therefore, they are powered from a separate power supply, typically 5V, and are connected to the V+ on the Cobbler. Note that this 5V is different from the 5V supply for the RBPi. The PWM operation on the servos creates a huge amount of electrical noise, which can cause the 5V supply voltage to fluctuate significantly. RBPi may not be able to tolerate such voltage fluctuations, and this may cause it to crash and lock up.

If you are driving many servos, it will be a good idea to add a capacitor to the driver board. There is a spot already marked for such a capacitor. As a thumb rule, you need a capacitor with a value n x 100uF, where n is the number of servos you are driving. Capacitors are manufactured in standard ratings, and you may have to go for the next higher standard value that you have calculated.

Depending on whether you are using a standard or continuous rotation servo, your python code will vary. For the actual code with which you can control the various parameters of I2C and hence the servo, you may refer to this site.

Is the Odroid SBC Better than Raspberry Pi 3?

The world of inexpensive SBCs or single board computers has been taken by a storm with the unveiling of the new Raspberry Pi board or the RBPi 3. The claim being it blows the competition away, and that no one can match its price. However, that may not be entirely true, as the Odroid C2 SBC seems to best the RBPi 3.

Hardkernel promotes its Odroid C2 as another cheap and speedy SBC with a 64-bit ARM-based quad core processor. A comparison of the specifications shows the C2 may be giving the RBPi 3 a run for its money. Compare for instance, the BCM2837 of the RBPi 3 with the Amlogic S905 SoC of the C2. Although both are quad-core ARM Cortex-A53, the C2’s processor runs at 2GHz to the 1.2GHz of the RBPi 3. At 2GB, the C2 has double the RAM of the RBPi 3, which has only 1GB. Moreover, the C2 comes with a Mali-450 GPU, able to deliver 4K video.

Although the C2 does not have the on-board wireless and Bluetooth features of the RBPi 3, it has a high-speed Gigabit Ethernet port directly wired into the SoC. The RBPi 3 also has Ethernet on-board, but as this is a 100-megabit port and is on a USB interface, its speed is likely to be limited.
The two boards share a very similar form factor and are nearly identical in their GPIO capabilities. In addition, for both the boards, you can choose the storage to be either the usual micro SD card or eMMC. However, it is worth stating that the C2 comes with a built-in ADC or analog to digital converter. When it comes to operating systems, the C2 can operate with Ubuntu 16.04, or Android lollipop.

The RBPi family, just like Apple products, has always faced competition. However, most look good only on paper, but their prices always let them down in the end, and we never hear of them after some time. The price of $40 for the C2, being very close to that of the RBPi 3, may just escape this fate. Of course, there is the matter of adding shipping and customs to the price, as the origin of C2 is Korea.

So, which of them is preferable – the C2, or the RBPi 3 – and why? The faster processor of the C2 and its faster wired networking would make it attractive to someone working on network-attached data processing applications. Although one can add a USB wireless network adapter for only a few dollars, the onboard Wi-Fi and Bluetooth of the RBPi 3 makes it so much more attractive. Therefore, the RBPi 3 would be coveted by anyone who is a home user or planning to use a computer on a platform that will remain unfettered by wires.

Although the C2 may be more impressive when compared to the RBPi 3, the latter will likely outsell the C2 many times over. This may not be because of the massive publicity advantage that the RBPi 3 is receiving from the Pi foundation, but more likely due to the wide ecosystem of hardware and software developers the RBPi family has at present.

The ExaGear Desktop for the Raspberry Pi

Normally, the Raspberry Pi or RBPi does not allow running Intel x86 applications. This is because the RBPi is ARM-based. That means it has a different architecture from the Intel-based PCs we are used to using. This is as if a letter addressed to a Russian town landing up in Denmark – the address is all wrong, so it is tough to deliver.
Virtual machines are available that create a local environment for running applications where the basic architecture differs. For the x86 platform, the most popular virtual machine software are VMware and VirtualBox. With virtual machines, you may be running Linux as your main operating system, but you can also run a full-fledged Windows operating system simultaneously and vice versa. The main operating system is termed the Host, while the OS running under the virtual machine is termed the Guest.
Eltech has produced such a virtual machine for RBPi that have ARM platforms as their base. This is the ExaGear Desktop and it allows you to run Intel x86 applications directly on your RBPi through a virtual x86 Linux container on ARM. For example, on the ExaGear Desktop, if you install Wine, the open source compatibility layer software application will allow you to run even Windows applications on your RBPi.
You can run the ExaGear Desktop on most ARM-based Mini PCs operating with Linux such as the RBPi, Banana Pi, Wandboard, Jetson TK1, Utilite, CuBox, CubieBoard, ODROID including the ARM-based Chromebook. Unlike Linux, ExaGear Desktop is not free and you can download it only after paying for its license key. However, before buying, it is prudent to check up if your Mini PC has the proper hardware and software base to allow ExaGear Desktop to run on it.
If you are using the RBPi ver1, you will need the ARMv6 instruction set with VFP32. For other RBPi versions and ARM devices, you will require the ARMv7 instruction set with VFP32. If you are planning to use x86 applications that require MMX/SSE, you will also need NEON as support. On the software side, you must be using the Linux operating system variants such as Raspbian, Debian 7, Ubuntu 14.04 or Ubuntu 12.04. Check with the Eltechs Tech Forum if you still harbor doubts about system requirements.
Eltech has published some test results to demonstrate the speed with which the ExaGear Desktop works. For benchmarking, they have used SysBench, which was built for ARM and Intel x86 platforms. Using the same ARM machine for both tests, they have compared the results of ARM-based tests against x86 tests running under the ExaGear Desktop. The tests cover parameters such as File IO read/write, CPU cycles, Memory usage, Threads speed and Mutex. Results show ExaGear to be superior to QEMU in almost all parameters.
Using their setup, Eltech has also compared the performance of ExaGear against the performance of QEMU, the user mode emulator. For benchmarking, they used GeoBenchmark and found that ExaGear Desktop was nearly five times as fast as QEMU was.
Eltech has also compared the ExaGear Desktop performance against QEMU using the nbench benchmark. Here too, ExaGear Desktop was able to show far superior performance compared to the performance of QEMU when both were run on the same platform.

Stackable Pi-Plates for the Raspberry Pi

If you are faced with a paucity of projects for your Raspberry Pi or RBPi, the tiny, credit card sized single board computer, you should get the circuit boards from Pi-Plates and connect your RBPi to the outside world. Pi-Plates offer a family of stackable, add-on boards that provide your SBC with a robust set of features at a minimal cost.

Pi-Plates design their circuit boards to be economical with the GPIO pins they use from the RBPi header. For example, when using the DAQCplate board, it uses only two dedicated GPIO pins. However, you can stack eight of these Pi-Plates to get 64 digital inputs, 56 open-collector outputs, 64 analog inputs and 16 analog outputs. Whether you are an experimenter, a hobbyist or a professional, Pi-Plates have designed these boards to be useful for all. Additionally, these are mechanically and electrically compatible with all revisions of the RBPi. That includes versions A, B, A+, B+ and the new version 2.

At present, Pi-Plates offer four products. The flagship product is the DAQCplate board that has ADCs or Analog to Digital Converters, DACs or Digital to Analog Converters and expanded digital IO. MOTORplate is a new product for controlling motors and you can use it to drive two stepper motors or four DC motors, while its onboard software can handle all drive logic including acceleration profiles. If you want to add custom hardware on your Pi-Plate stack, you can use the PROTOplate board.

When stacking Pi-Plates, you will need a secure structure and this is provided by the BASEplate mounting system. All hardware necessary for mounting to the BASEplate is already available with each Pi-Plate board. Pi-Plate also offers two great kits.

The DAQC kit comprises two BASEplates and one DAQCplate boards for the price of a single unit. This makes a great beginning for those starting with the DAQCplate for the first time.

For those starting with a MOTORCplate, the MOTOR Kit may be very useful. This kit comprises one MOTORplate and two BASEplate boards for the price of a single unit.

For example, the DAQCplate is a data acquisition and control board. Its digital output section has a connector that provides seven open-collector outputs and a pair of 5VDC outputs that you can use for driving loads. You can protect these with a flyback diode connected to the terminals.

You can use these outputs to drive incandescent automotive light bulbs, ultrasonic rangefinders, resistive heating elements, unipolar stepper motors, buzzers, solenoids, relays, DC motors or LED strings. Green LEDs connected to each digital output light up to indicate a high on the output. To light up these LEDs, you do not require connecting anything to these outputs. At the same time, these LEDs will not affect anything that you connect to these outputs.

Darlington pair transistors drive the seven open-collector digital outputs. They can sink a maximum of 350mA and handle a maximum load voltage of 12VDC. With a load voltage of 200mA, the on voltage is typically 1.1V. When using inductive loads such as solenoids or relays, you must connect the high side power supply to the flyback protection terminal.

Playing 4-Bot with the Raspberry Pi

Sometime or the other we have all played Connect-4 or Four-in-a-row against either a human or a computer opponent. It is a simple game where you and your opponent each try to get four same-color pieces in a row, while trying to prevent the other from doing so. The first one to line up four adjacent pieces of the same color wins the game.

Conventionally, the game board has 42 squares made of six rows and seven columns. Players start with several discs of two colors each, and to be successful, each player has to constantly plan and revise their strategy. Therefore, an SBC or single board computer such as the Raspberry Pi, or RBPi is a suitable candidate for playing Connect-4. Besides enjoying the game, you hone your skills as a DIY enthusiast by building the game. Of course, this project will require some skill in mechanical assembly, and in coding as well.

You can have a horizontal board and an X, Y arm mechanism to let the RBPi deliver its pieces to the required square. However, a vertical board makes the mechanism simpler, as the arm then has to travel only in one axis, gravity taking care of the other. The vertical board is actually made of two faces, with a gap in between and separators to mark the columns to allow the discs to be dropped in one of the columnar spaces between the two faces. Both faces have 42 matching circular cutouts, so it is easy to see where each disc is positioned. A claw on the arm mechanism picks up a disc from a stack, positions itself above the required column, and releases the disc, allowing it to fall in the column between the board faces.

The software requires the use of Python Imaging Library for processing the image of the game board. To enhance readability, the image can be down-sampled to 16 colors, and then divided into a grid. It is only required to identify each of the 42 spaces on the board as red, yellow, or empty. This is easily done by reading the RGB value of each space in the grid, and saving this data in the form of an array. This forms the board state after every move and this is passed on to the AI or Artificial Intelligence on the RBPi for calculating the next move.

The AI used is a well-known algorithm known as Minimax – applicable to games of this nature, and there is a Python library for Minimax. Using tree-searching methods, the algorithm looks several steps ahead to calculate the next best move. Getting the RBPi to play effectively can be quite a challenge, as even a small Connect-4 board of 6×7 squares can have 4,531,985,219,092 possible game positions. Therefore, the program tries to trade-off between absolute perfect play and reasonable time for each move. If you can strike a balance between the two, the RBPi can play quite intelligently, but still complete each move in about 25 seconds – this is acceptable for a flowing game.