This project shows you the parts, source code and hardware requirements in order to control a motor using an Android computer. In order to achieve this goal, a power inverter is then used.
Basically, the main elements of this project are the following:
- Android board with 7" touch screen (such as Ltouch Single Board PC)
- Power inverter
- Android app
The Android application communicates with slave devices (in this particular case with a power inverter) by using the modbus RTU protocol over rs485 and control a 5.5KW Motor.
The Android board used in this project is a single board computer equipped with Cortex A8 MCU at 1Ghz, 512MB of DDR2, rs485 port and 7" capacitive touch screen (see specs). This board supports other operating systems such as Linux and WinCE 6.0.
The inverter we used is a Alley-brandey Powerflex 4M 380V 3PH for 5.5KW motors. In order to use this device as a slave that responds to modbus requests and in particular to function 16 (WriteHoldingRegisters) and 3 (ReadHoldingRegister), we set it with the following parameters:
- P106 (start mode = 5)
- P108 (reference speed = 5)
- C302 (baud rate = 38400)
- C303 (node address = 1)
- C304 (power inverter replies when errors occur)
together with other hardware-related settings that are extensively described in the manual.
The Android application is formed by the following elements:
- MainActivity.java that defines the main activity of the project
- ModbusLib.java class in which are defined the signature of the modbus native fuctions
- The native modbus library (the library is stored inside the jni folder)
- The xml file that defines the layout of the activity (activity_main.xml, stored inside res/layout folder)
However, other elements are needed in order to successfully execute this project, such as some graphical elements that must be stored in the res/drawable-hdpi/ folder and colors and strings stored inside res/values folder.
This wiki page is not intended to explain how to create an Android application, so we assumed that the reader might have basic knowledge of java programming and knowledge about the building blocks of a simple Android application. However, for those of you interested, we wrote a simple step-by-step tutorial to learn how to create a basic Android app with Eclipse.
The most important parts of the source code is the MainActivity.java file where the follwing methods are managed:
- onCreate method (is first triggered whenever the activity starts) in which the activity has been connected to the xml layout by means of the statement:
Indeed, is the place where are created the handles to the graphical objects such as buttons, imageviews, ect and where the Listeners have been registered, for instance with
and finally where the default visibility for graphical elements has been set.
- onResume method (method triggered after onCreate) is the place where the modbus connection is opened and where a thread devoted to send modbus read requests (DelayedRead function) at fixed intervals is created.
- DelayedRead function (that accounts for modbus function 3), with the signature
DelayedRead(final int device_id, final int starting_address, final long delay)
As we can guess from the parameters' name, it accepts the address of the device (device_id), the starting address of the first register to read, the delay with which the thread will be executed and the number of registers to be read. For this specific demo project, this latter parameter is neglected and the thread itself manage to read the right number of registers neeeded. The thread is executed every 1800 ms. However, a different value could be set by specifying it in the statement
tR.scheduleAtFixedRate(tt, delay, 1800);
- DelayedWrite function (accounts for modbus function 16), with the signature
DelayedWrite(final int address, final int value, final int starting_address, final long delay)
The meaning of the parameters are the same as the DelayedWrite. The value parameter specify the value to be written. A future version of this method might consider to accept an array instead of a single integer value.
This project could be applied in many industrial applications.
The source code of the project is available at our GitHub page.