Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
E E 2 8 9 Lab April 20, 2015
Lab 13. ANDROID LOW PASS FILTER
Overview & Introduction:
In this lab we will be experimenting with Processing and Android programming. Tablets and smartphones
have a number of sensors that require some type of digital signal. With the increasing number of ever more
powerful ARM (Advanced Risc Machine) processors available in the marketplace, it is possible to do digital
signal processing on the device.
Android: The Android operating system is an open source operating system made by Google and is used by
the majority of smart phone and tablet manufacturers. Android Apps are written in Java and the Android
development software is freely available from Google. However the standard Android development software
has a large learning curve so we will be using an alternative for this lab.
Processing: Processing is an open source programming language built on Java that was originally designed
for computer graphics. In this lab we will be taking advantage of the fact that Android apps are written in
Java and that Processing is built on Java to write an Android application without the steep learning curve
associated with Google’s Android development software. Figure 1 shows the standard Processing window
and explains what the various buttons do.
Figure 1: Processing
What all does Processing do?
(i) Run: Compiles and runs the program.
(ii) Stop: Stops the program that is running.
(iii) New: Opens a new window for editing.
(iv) Save: Saves current program.
(v) Export: compiles program into a standalone application.
(vi) Language: This menu selects the language that will be used for programming. For this Lab it should
say Android.
System Overview: The system will operate as follows: The tablet’s accelerometer is poled for it’s x, y,
and z acceleration values. Next these values are sent through a low pass filter to remove noise. Finally the
filtered values update the position of the 3d cube on the screen to maintain it’s orientation.
First Start Processing: Open up Processing and after it has opened go to file and choose preferences. At
the top of this window there is a box for specifying the “Sketch book location.” Change whatever is in the
box to
C:\Users\Public\Documents\Processing
and click OK. Before we do anything else we need to go up to the drop down menue in the upper right hand
corner and change the language setting from Java to Android. This tells Processing to compile the program
in a way that the Android tablet can understand. Now we will open up the program that we will modify
to become our rotating cube. To do this go to File → Open and navigate to the DSP Lab folder on the
desktop. Open the program called Android Cube located in the folder by the same name.
Complete the Program: The Android Cube program is missing pieces of code. We will now complete
the program by adding in the missing code. First need to add some things in void setup. . . .
In order to use the tablet’s accelerometer we must create an accelerometer object. Do this by placing the
following line:
accel = new AccelerometerManager(this);
below the size(1024, 560, OPENGL); line. Next we must tell the tablet which way to orient the background
of the app. Do this by adding the following line below the one that was just added:
orientation(LANDSCAPE);
Void setup. . . should now like the picture below.
Figure 2:
We will now turn our attention to the public void accelerationEvent(. . . ) function. This function is auto-
matically called by the Android operating system every time the acceleration changes on one to the 3 axis
of the onboard accelerometer. First we will try the program without any filtering. To do this add the lines
to public void accelerationEvent(. . . ) function:
ax = x;
ay = y;
az = z;
It should now look like this:
Figure 3:
With the tablet connected to the computer, on and unlocked (tablet on home screen) press the run button.
A blue cube with a green background will appear on the tablet. Try moving the tablet being careful that
the USB cable stays attached.
Q1 Does the cube maintain its orientation?
Q2 Does the cube twitch when the tablet is stationary on the table?
Now let’s try smoothing the accelerometer’s output with a simple low pass filter. To do this change ax = x;
ay = y; az = z; to the following:
ax = (x + ax)/2; ay = (y + ay)/2; az = (z + az)/2;
This averages the new accelerometer value with the old accelerometer value. Try uploading this to the tablet.
Q3 Does this make the cube less twitchy?
For even more smoothing let?s try making the cube even more stable (and less responsive) by changing it
to the following:
ax = (x + 2 * ax)/3; ay = (y + 2 * ay)/3; az = (z + 2 * az)/3;
This weights the running average twice as much as the new value. Does this make the cube less twitchy?
Q4 Is there a limit to this approach?
Q5 How would this filter compare to a Kalman filter?
This Lab was written and developed by Iain Portalupi and Ramon Zhi.