welcome to eq2430/eq2440 android lecture

47
1 Welcome to EQ2430/EQ2440 Android Lecture March 20, 2014 Per Zetterberg School of Electrical Engineering

Upload: portia

Post on 22-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Welcome to EQ2430/EQ2440 Android Lecture. March 20, 2014. Per Zetterberg School of Electrical Engineering. The project course goes Android!. Features used in our projects:. Display : plots and text. Microphone. Loudspeaker. Magnetometer measurements. WiFi RSSI measurements. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Welcome to EQ2430/EQ2440 Android Lecture

1

Welcome to

EQ2430/EQ2440 Android Lecture

March 20, 2014

Per Zetterberg

School of Electrical Engineering

Page 2: Welcome to EQ2430/EQ2440 Android Lecture

2

The project course goes Android!

• Display : plots and text.• Microphone.• Loudspeaker.• Magnetometer measurements.• WiFi RSSI measurements.• Send data using WiFi.• Accelerometers.• Touch screen.• Camera

Features used in our projects:

Page 3: Welcome to EQ2430/EQ2440 Android Lecture

3

FrameWork• FrameWork is a java project (program) in

Eclipse.• FrameWork is a Android 4.2.1 project that

runs (at least) Samsung S3. • FrameWork is a generic skeleton which can

be used as the starting point for many student projects (including yours!).

• FrameWork has an object StudentCode which is where you implement your prototype.

• Application name: StudentFrameWork (Change this)

• Creators: Martin Ohlsson and Per Zetterberg

Page 4: Welcome to EQ2430/EQ2440 Android Lecture

4

FrameWork: User interface

PlotView:

Graphs and/orCamera picture

TextView:

Textual output.

Page 5: Welcome to EQ2430/EQ2440 Android Lecture

5

Downloading FrameWork

Go to

EQ2440 web (KTH social) => Android support

Download: FrameWork.zip

Unzip and move the code to workspace directory.

Do “file->import->”existing project into workspace”-

>browse to FrameWork (the directory)

Page 6: Welcome to EQ2430/EQ2440 Android Lecture

6

StudentCode (i.e your code)Parameters:useSensors, loggingOn, logSound, useCameraGUI, useMessaging, messageServer, processInterval, introText, messageGroups, userInputString, StudentMessage (class), userInputString

useSensors (bitfield): “xor” the followingGPS,CAMERA,CAMERA_RGB,SOUND_OUT,SOUND_IN,WIFI_SCAN,ACCELEROMETER,MAGNETIC_FIELD,PROXIMITY,TIME_SYNC, PROXIMITY,LIGHT,SOUND_IN,CAMERA, GYROSCOPE

Page 7: Welcome to EQ2430/EQ2440 Android Lecture

7

Called by StudentCode 1(3)sound_out(short[] buffer, int length): Send data to DAC=>loudspeaker.

message_out(StudentMessage message): Send message to all the other phones.

message_out(StudentMessage message, String groupIdString): Send message to the phones in group defined by “groupIdString.

streaming_buffer_out(short[] buffer, int length, String groupIdString): Send buffer of int16 to the phones in group defined by “groupIdString”.

Page 8: Welcome to EQ2430/EQ2440 Android Lecture

8

Called by StudentCode 2(3)set_output_text(String text): Set the text too be displayed in TextView.

plot_camera_image(Canvas plotCanvas, byte[] image, int imageWidth,int imageHeight, int width, int height): Display a camera image.

plot_camera_image_rgb(…): Display color …

write_string_on_logfile(String): No ”\n” or ”;”.

load_sound_file(String fileName) + play_sound_file(int loop): Repeatedly plays the wav-file specified by fileName. No timing gitter (loop=-1 means forever)stop_sound_file(): Stops the above.

Page 9: Welcome to EQ2430/EQ2440 Android Lecture

9

Called by StudentCode 3(3)

save_rgb_frame_on_file(String filename,byte[] frame,int width,int height,boolean append) : Saves an RGB picture (=frame) on file.

public byte[] read_rgb_frame_from_file(int image_number, String filename) : loads an RGB picture (=frame) from file.

public byte[] read_data_from_file(String filename) : Reads binary data from file.

Page 10: Welcome to EQ2430/EQ2440 Android Lecture

10

Defined by StudentCode: Called by FrameWork 1(2)init(): Do all your initialization.start(): Called when menu button ”start” is pressed.stop (): Called when menu button ”stop” is pressed.message_in(…) Received message from other phone. gps(…) …… position estimatemagnetic_field(…) …… magnetic field measurementlight(…) : …. Light intensity (lux) event (trigger when changed)proximity(…): Proximity state event (trigger when changed)accelerometer(…) accelerometer measurementprocess(), Periodic processing with interval set by

processInterval parameter. The processInterval parameter can be changed during runtime.

You fill these functions with code!

Page 11: Welcome to EQ2430/EQ2440 Android Lecture

11

Defined by StudentCode: Called by FrameWork 2(2)gyroscope(…) gyroscope measurementsound_in(…) … sound input sampleswifi_ap(…) ... signal strength of surrounding WiFi APsscreen_touched(…) position eventstreaming_sound_in(…) buffer of samples (int16) received from other phonecamera_image(…) image received from camera (black and white)camera_image_rgb() image received from camera (color)plot_data(): Screen update (make plots with Canvas).list_ips(): Return a list (in string format) of the IP:s of the phones in your messaging group.stringFromUser(): Called when the user provides input.

All are inputs exceptplot_data!

You fill these functions with code!

Page 12: Welcome to EQ2430/EQ2440 Android Lecture

12

Your code structure

public void init() { … set parameters initialize}

public message_in () { … process … save}

public void process() { … e.g heavy calculations}

camera_image_rgb() { … save latest imagenew_image=true;

plot_data(Canvas plotCanvas, {… present results}

Data structuresalgorithm state

Page 13: Welcome to EQ2430/EQ2440 Android Lecture

13

The GUI

“Greeting message”

introText

Start FrameWork App-> calls init()

Press menu button

Start() is calledSensors switched +on etc.

Stop

Press Start

set_output_text(String text): Set the text too be displayed in TextView.

plot_data(Canvas plotCanvas,…)

Press menu + Stop

stop() is called

Start Kill String

Page 14: Welcome to EQ2430/EQ2440 Android Lecture

14

Get a string from the user 1(2)

Menu if userInputString=false:

Start Kill

Start Kill String

Menu if userInputString=true:

Pressing String gives:

Message

userInputStringMenuItem=“String”

Title

OK Cancel

userInputStringTitle=“Title”

userInputStringMessage=“Message”

Page 15: Welcome to EQ2430/EQ2440 Android Lecture

15

Get a string from the user 2(2)

void stringFromUser(String user_input){ }

Title

OK Cancel

Orange User types ”orange”

User presses OK

The input providedis sent to the FunctionstringFromUser insideStudentCode.java

Page 16: Welcome to EQ2430/EQ2440 Android Lecture

16

Browse for filename

To use it, somewhere inside StudentCode.java do:

browseForFile();

This opens a dialog where you can browse for a file (quite intuitive). When the user selects a file and presses ”Done” the function stringFromBrowseForFile is called back. You can capture the filename inside it.

stringFromBrowseForFile(String filename)

Only the variable filename is set in filename not the path. However, we will only use the sdcard directory.

Page 17: Welcome to EQ2430/EQ2440 Android Lecture

17

Threads (internals)“Parent thread”: Indirectly calls init().studentProcessThread: Calls process(). ipListenerThread: Calls message_in()guiTriggerThread: Renders what you provide with set_output_text() and plot_data(). t1: Calls sound_in(…)wifiScanThread: calls wifi_ap(…)ipSoundListenerThread: calls streaming_buffer_inFrameWork implements OnRecordPositionUpdateListener:

Calls gps()FrameWork implements SensorEventListeneraccelerometer(), magnetic_field(), light(), proximity()FrameWork extends Activity: sreen_touched()streaming_sound_in(…) buffer of samples (int16) sent from other phone

Page 18: Welcome to EQ2430/EQ2440 Android Lecture

18

How communication between phones works 1(2)

• A phone (or a Linux computer) acts as a server (the address of which is defined by messageServer).

• When using a phone as server, that phone is starts FrameWork first.

• When FrameWork starts (on the other phones), it contacts the server and asks for the ip adress of one of the other phones in the project (a project is defined by the projectName string e.g. ”yellow”).

• The phone then contacts that phone and gets a list of all other phones in the project.

• When one phone calls message_out(message) the message is sent to all other phones in the project.

• The other phones receives the message through a call of message_in (”callback”).

• The message is of type StudentMessage and contains the fields that you decide.

• Debugging hint: set_output_text(set_list_ips) lists current configuration.

Page 19: Welcome to EQ2430/EQ2440 Android Lecture

19

How communication between phones work 2(2)• Second form of message_out: message_out(message, groupIdString)

• Where groupIdString defines a group of users e.g. ”data_receivers” which should receive the message.

• Which groups exist ? You define the groups with the ”messageGroups” parameters.

• When FrameWork starts, it asks the user which group the phone belongs to.

• When sending buffers of int16 use the following version: streaming_buffer_out(buffer,length, groupIdString)

• This data is received through: streaming_buffer_in( buffer, int length, senderId)

Page 20: Welcome to EQ2430/EQ2440 Android Lecture

20

How to use communication between phones 1(2)Set useMessaging = true;

If you need groups:•String temp[] = {"MICROPHONE","SPEAKER"};•messageGroups = temp;Otherwise:•messageGroups = null;

In both cases:•messageServerType = LINUX_MESSAGE_SERVER; •messageServer = "192.168.0.68";•Define StudentMessage (defined in StudentMessage.java)

Which message group do this phone belong to ?:messageGroups[myGroupID]

Page 21: Welcome to EQ2430/EQ2440 Android Lecture

21

How to use communication between phones 2(2)public class StudentMessage extends StudentMessageBase {// Add your message fields here// do not use primitive types that begins with lower case letters Float dummy=0.0F; /* Example of student message data */ Boolean is_sync_message; Boolean is_distance_message; Float value1; Float value2; Float value3; Float value4;}

Same definition in all phones!

message_out(my_message,SPEAKER”);In transmitter

public void message_in(StudentMessage message){If (sync_message) correlation=value1;…

In receiver(SPEAKER)

Page 22: Welcome to EQ2430/EQ2440 Android Lecture

22

Logging on file 1(2)

• loggingOn = true;• Logs measurements from all active sensor except

camera • … and you own debug messages:

write_string_on_logfile(“correlation=“+correlation)• Creates a measurement file called sensorlogYYYYMMDDHHMM.csv on the SDCARD.

The measurement file is ASCI with measurents in clear text.

How retrieve file:method1: Copy using windows exlporer.methods: Using our matlab functions.

Page 23: Welcome to EQ2430/EQ2440 Android Lecture

23

Logging on file 2(2): matlab utilities

• Copy file to PC-harddisc (matlab command)copy_file_from_sdcard_to_working_directory.m • Obtain the data from the log-file into matlab:get_log_data_from_FrameWork.m• Extract data from some certain sensors:

• extract_acceloremeter_from_log_data.m,• extract_sound_from_log_data.m, • extract_magnetic_from_log_data.m

• Other sensors:• Do it yourself.

Problem: SOUND_IN fluds measurement log.

Solution: logSound=false;

Page 24: Welcome to EQ2430/EQ2440 Android Lecture

24

State-machine and functionspublic void process() {{

if (!buffer_available())return;

switch(State) {case START:

res1=func1(buf,arg2); res2=func2(signal1);

State=WAITIING;break;

case WAITING:sync=func3(buf,res2);if (sync) {State=RUNNING; transmit(signal2)}

else {State=WAITING; transmit(signal1)};break;

case RUNNING:.....

}write_string_on_logfile("State="+State);

}

public void message_in(StudentMessage message){

switch(State) {case WAITING:....

}

Functions that can be tested against matlab implementation using harness

Page 25: Welcome to EQ2430/EQ2440 Android Lecture

25

Test harnessPurpose: Test implementation of math-intensive functions/algorithms in an isolated and repeatable way.Method:Run your matlab- and java- (with android environment) -implementations side by side with exactly the same input .How?•Matlab produces data, and pushes it on the sdcard•FrameWork includes a function called ”test_harness()”.•Inside this function

the data can be read from the sdcard,

the function to be tested is called,and the results are saved on the sdcard.

•Matlab then pulls the data from sdcard.•Finally, the results from the matlab- and java-implementation are compared.What do I need to do ?The example test_harness_example.m shows how to automate the process. This code can be used as a starting point, however, you need to customize the code to the function you are going to test.

All, non-trivial functions should be tested like this.

Page 26: Welcome to EQ2430/EQ2440 Android Lecture

26

Using sound (input)

time4096 samples 4096 samples 4096 samples

useSensors = SOUND_IN | SOUND_OUT;

sampleRate = 44100;

sound_in(long time, short[] samples, int length)

sound_in(long time,…

Page 27: Welcome to EQ2430/EQ2440 Android Lecture

27

Sound output 1(2)

sound_out(short[] buffer, int length)

sound_out(short[] buffer, int length)

sound_out(short[] buffer, int length)

-2^15,2^15-1

Page 28: Welcome to EQ2430/EQ2440 Android Lecture

28

Sound output 2(2)

load_sound_file("bing.wav"); play_sound_file(-1);stop_sound_file();

wavwrite(y,44100,16,’bing.wav’);copy_file_from_working_directory_to_sdcard(‘bing.wav’);

Only in init(), process() and stop()

Constant gaps

Page 29: Welcome to EQ2430/EQ2440 Android Lecture

29

Create sound in matlab and play it on phone

In Matlab•[y,Fs] = wavread('C:\Downloads\lga.wav');•create_file_of_shorts('lga.dat', y(20001:1e6,1)*1000 );•copy_file_from_working_directory_to_sdcard('lga.dat' )

On the Phone:

See lines in in StudentCode.java with the word ”sound”

In matlab sound: [-1,1]. In android FrameWork: [-2^15,2^15-1]

Page 30: Welcome to EQ2430/EQ2440 Android Lecture

30

RGB Camera: Image formatvoid camera_image_rgb(byte[] image, int width, int height)

0

1

3

image={r0,g0,b0,r1,g1,b1,r2,g2,b2,…}

w

1w

1wh

Byte 0-255

1w 12 w

Page 31: Welcome to EQ2430/EQ2440 Android Lecture

31

RGB Camera: File format

File format (”.rec”):

width height r0,g0,b0,r1,g1,b1,….

Frame

Frame 1 Frame 2 Frame N

Always stored in /sdram/recordings

Filename (example): video201303181626.rec

Page 32: Welcome to EQ2430/EQ2440 Android Lecture

32

RGB_Camera: GUI

Alt1: useSensors = CAMERA_RGB; useCameraGUI=true;

Start Rec Play Stop

void camera_image_rgb(byte[] image, int width, int height)

Log to file: /recordings/videoyyyyMMddHHmm.rec

Browse for file and playback: Calls camera_image_rgb(byte [] image, int width, int height)

Stop recording

Page 33: Welcome to EQ2430/EQ2440 Android Lecture

33

RGB_Camera:No GUI

Alt1: useSensors = CAMERA_RGB; useCameraGUI=false;

Camera runs continuously.

void camera_image_rgb(byte[] image, int width, int height)

….is called….

save pictures to file using call:

save_rgb_frame_on_file(String filename,byte[] frame,int width,int height,boolean append)

Page 34: Welcome to EQ2430/EQ2440 Android Lecture

34

Load RGB picture from file

int read_rgb_frame_width_from_file(int image_number, String filename)

int read_rgb_frame_height_from_file(int image_number, String filename)

byte[] read_rgb_frame_from_file(int image_number, String filename)

Page 35: Welcome to EQ2430/EQ2440 Android Lecture

35

Display picture (example)

plot_data(Canvas plotCanvas, int width, int height) {

if((latestRGBImage != null) && ((useSensors & CAMERA_RGB) == CAMERA_RGB)){

plot_camera_image_rgb(plotCanvas,latestRGBImage,ImageWidth,imageHeight,width,height);

};};

Updates≈10Hz

Page 36: Welcome to EQ2430/EQ2440 Android Lecture

36

Pictures Matlab interface

list=list_video_recordings_on_sdcard(time0)

copy_video_recording_file_from_sdcard_to_working_directory(filename)

rgb_video= extract_video_recording_from_file(filename);

image(rgb_video.frames{1}.rgb)

create_video( filename, rgb_video )

rgb_video.frames{1}.rgb A 768x432x3 matrix(:,:,1)=red (:,:,2)=green (:,:,3)=blue

Page 37: Welcome to EQ2430/EQ2440 Android Lecture

37

Display pictures another way

void plot_data(Canvas plotCanvas, int width, int height) {

plotCanvas.drawLine(… plotCanvas.drawRect(……}

http://developer.android.com/reference/android/graphics/Canvas.html

More info:

Page 38: Welcome to EQ2430/EQ2440 Android Lecture

38

ZXING QR decoder

“ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java”

To test:useSensor=CAMERA_RGB; uncomment code in StudentCode.java between ”zxing start color” and ”zxing stop color”

Direct camera towards QR code

Source code located under ExternalSrc.com.google.zxing

Page 39: Welcome to EQ2430/EQ2440 Android Lecture

39

Give your App a unique identity• Right click on project folder (Top level folder named

FrameWork) within the Package Explorer window• Select Android Tools/Rename Application Package• Add a unique name after FrameWork like:

se.kth.android.FrameWork.GroupRed2013• Press Finish in the Refactoring window• Confirm update of Launch Configurations• Voila, you now have a separately installable version of

your app!

Page 40: Welcome to EQ2430/EQ2440 Android Lecture

40

Phone configuration

Samsung S3”Settings” -> ”developer options” -> tick ”USB debugging””Settings” -> ”Security and lock screen” -> tick ”Unknown sources”

HTC Desire”Settings” -> ”Applications” -> ”Development” -> tick ”USB debugging” ”Settings” -> Applications” -> tick ”Unknown sources”When connecting to PC: choose ”charge only”

Eduroam (both):http://www.lan.kth.se/eduroam/phones/phones_mobile_guide.html

Page 41: Welcome to EQ2430/EQ2440 Android Lecture

41

code versioning systems

CVS, SVN, git, ….purpose: Keep track of created textfiles (e.g. C++ code). Versions.

Helps merge changes by multiple users. We will use git: distributed versioning system … but

we will used in a centralized manner

Page 42: Welcome to EQ2430/EQ2440 Android Lecture

42

Set up local repository (=local copy) From the git bash command line:>git clone https://github.com/pzett/blue.git blue_alice>cd blue_alice>git config –-local user.email [email protected]>git config –-local user.name alice

Import the project into eclipse:1) File->Import->Existing Androind Code Into Workspace ->Browse to blue_alice/FrameWork and select it2) Right click FrameWork Package Explorer in eclipse ->build path -> source tab ->Add folder - > Mark External Src->OK ok

Reading:http://toroid.org/ams/git-central-repo-howto

Page 43: Welcome to EQ2430/EQ2440 Android Lecture

43

How we will workRepositoryblue

Alice:

* Implements or changes file1.cpp and file2.cpp

Updates repository:git add file1.cpp file2.cppgit commit -m “comment”

Bob:

* Implements or changes file1.hpp and file2.hpp

Updates repository:git add file1.hpp file2.hppgit commit -m “comment”

file1.hpp file2.hpp

file1.cpp file2.cpp

git pull git pull

file1.hpp file2.hpp

git pushgit push

file1.cpp file2.cpp

Page 44: Welcome to EQ2430/EQ2440 Android Lecture

44

Alice and Bob have clean updated local repositories. Alice modifies her file1.cpp Bob modifies his file1.cpp Bob does:

git add file1.cpp git commit -m “change initialization of i1” git push

Alice does: git add file1.cpp git commit -m “I changed initialization of i1 to 2” git pushCONFLICT (content): Merge conflict in file1.cppAutomatic merge failed; fix conflicts and then commit the result.

Conflict resolution

Page 45: Welcome to EQ2430/EQ2440 Android Lecture

45

Conflict resolution continued Alice opens file1.cpp:<<<<<<< HEADi1=2;=======I1=1;>>>>>>> 55f223d4ab24ae40f19ae551d51205b69a23177e

Alice and Bob discusses and finds that Alice was right. Alice removes Bobs code and pushes it.

Alice> git add file1.cpp

Alice> git commit -F .git/MERGE_MSG

Alice> git push

Bob> git pull Alice and Bob now has identical local repositories with Alice's code.

Alice's code

Bob's code

Page 46: Welcome to EQ2430/EQ2440 Android Lecture

46

Assignments1. Write letter ”G” and ”H” in plot_data using “draw” functions.

2. Switch between the letters of your group color every two seconds.

3. Record a sound and play it with two echoes. The sound should be read continuously like a reverb i.e. not record->pause->play.

4. Make an App that transmits a sinusoid. The App should first ask for the frequency – and then transmit a sound with that frequency using the loudspeaker.

5. Make an App similar to Audalyzer (but simpler). The App should use a bank of filters centered around 200Hz,300Hz,400Hz,500Hz, and 600Hz. The App should continously listen to these frequencies (audio input) and make a bar plot of the power at the output of the filters.

6. Make a test-harness for a non-trivial algorithm which is relevant for your project.

Deadline : Mid-term evaluation.

Page 47: Welcome to EQ2430/EQ2440 Android Lecture

47

Hints• Android software developers kit:

http://developer.android.com/sdk/index.html• Killing tasks. Download and install “Advanced Task

Killer Free (ATKF)”. To kill e.g. “StudentFrameWork” open ATFK press “StudentFrameWork” until a new menu appears where you can select “Kill”

• Error message: “ActivityManager: Warning: Activity not started, its current task has been brought to the front” => Change a single line in the source files code and try again.

• Error message:FrameWork_empty_130319] Re-installation failed due to different application signatures => Do “abd uninstall se.kth.android.FrameWork” from command line prompt on PC.

• Install Audalyzer to be able to view in the input sound.