Reading Your Android Phone Sensor into MATLAB


As a MATLAB fan, I am following MATLAB on facebook to know the latest news about MATLAB or anthing interesting that worth a download in the file exchange center. So ,last week, being a good fan, I was reading MATLAB’s update on facebook then I saw this post about reading accelerometer and gyroscope sensor from your Android phone. Since I’m also a fan of android phone (and now is foolishly struggling to learn some programming in Android), that post sounds very interesting to me. So, I took a good look at the post.

So, in quick summary, the post is about using this SensorUDP program developed by Takashi Sasaki to send the sensor data from your Android phone to a MATLAB session through Local Area Network’s UDP. The guys on mathworks also have created this amazing file exchange that allows you to read the upcoming UDP data that were sent by the SensorUDP program and three example application that reads your sensor into graph. You can download this easily from the file exchange file and play around with it. However, as for me (and maybe most of you too), there is a slight glitch with the file exchange developed by the guys from the Mathworks: It only works on MATLAB R2013a (MALTAB 8.01) and I’m still using MATLAB R2011b. (Sad…)

Fortunately, somewhere in the last year I was working on a project in my university and I happened to learn a little bit about UDP because aat that time, I have to read aircraft’s data from X-Plane into MATLAB through UDP. So, last saturday, I decided to play around with the SensorUDP program and try to read the UDP data directly without using the file exchange file (Well, I can’t use it anyway due to version incompatibility, so, nothing to lose) and it actually worked. Yup, you don’t need that fancy files from the file exchange. As long as you have Instrument Control Toolbox installed in your MATLAB, you are good to go. I don’t know if the support files that the Mathwork guys provide you allows you to read those sensor even though you don’t have Instrument Control Toolbox installed. However, if you are using older version of MATLAB and have Instrument Control Toolbox, you can read your phone sensor directly through a simple UDP connection. Yup, and that’s what I’m trying to show in this post (Finally, so useful post in this very blog :D).

So, here goes the post, How to read your Android Phone Sensor into MATLAB (version older that R2013b) by using Instrument Control Toolbox:

1. Setting Up LAN Connection between your PC and your Phone

Before you start playing with MATLAB on your PC and SensorUDP on you phone, you must make sure that your PC and your phone are in the same LAN network. If you are using your phone to tether Wi-Fi to your computer so that you can browse some internet (which is what I do), then you can’t do this because your phone acts just as a modem and your PC and your phone ended up having same IP address. However, if you do it the other way around (use your PC to tether Wi-Fi), your PC will acts as a LAN server (I think, I’m no expert in networking) and your PC and your phone will have each its own IP address that you can use to establish UDP connection.

The easiest thing that you can do to check if your PC and your phone is already in the same LAN network is to open command window (Press Win+R, then type cmd) and try to ping the IP address of your phone. The command to ping is as simple as ping <IP address>. If everything is fine, you should see something like below. If it is not, then try to google to solve your LAN problem (I’m sorry, I don’t have many experience with LAN).

Ping

2. Setting Up SensorUDP

After you’ve establish a LAN connection between you PC and you phone, download and install SensorUDP from GooglePlay and run it. Type your PC’s IP address into the host destination box and as for the port number, you can use any port as long as it is now used by other program or reserved by your system. This wikipedia article might give you a little idea about which port number that cannot be used because it is reserved. As for me, I used the default ‘12345’. Whatever you use, remember it because you’ll need this information for your MATLAB session. After you are done with it, just send whatever sensor that you want to sent.

SensorUDP

3. Reading the sensor data in MATLAB

To read the phone sensor (and perhaps any other data sent through UDP connections), first you may need to turn off your Window Firewall or made an exception or else the data won’t get through. After that, you can easily read the phone sensor data but using the following 10 lines of code (22 if you count the useless comments:D). Note that ‘fscanf’ command was used instead of ‘fread’ because SensorUDP sends the sensor data in CSV-formatted string instead of packed data.

%CodeStart--------------------------------------------------------
%Resetting MATLAB environment
  instrreset
  clear
  clc
%Creating UDP object
  UDPComIn=udp('192.168.16.3','LocalPort',12345);
  set(UDPComIn,'DatagramTerminateMode','off')
%Opening UDP communication
  fopen(UDPComIn);
%Reading data
  data=fscanf(UDPComIn);
  disp(data)
%Closing UDP communication
  fclose(UDPComIn);
%Deleting UDP communication
  delete(UDPComIn)
%CodeEnd----------------------------------------------------------

The code above basically just read one single data from your phone once. It is useful to make sure that you set everything right already. If you want to read the data continuously, you can always add an infinite loop like in the following code. Note that this program will make you stuck in infinite loop forever. So, if you need to terminate the program, just fit ctrl+C on MATLAB’s command window.

%CodeStart--------------------------------------------------------
%Resetting MATLAB environment
  instrreset
  clear
  clc
%Creating UDP object
  UDPComIn=udp('192.168.16.3','LocalPort',12345);
  set(UDPComIn,'DatagramTerminateMode','off')
%Opening UDP communication
  fopen(UDPComIn);
%Reading data
  while 1
    data=fscanf(UDPComIn);
    disp(data)
  end
%Closing UDP communication
  fclose(UDPComIn);
%Deleting UDP communication
  delete(UDPComIn)
%CodeEnd----------------------------------------------------------

Note that before you run the code about, you have to make sure that your PC and your phone are connected and the SensorUDP has started sending the data.

4. Processing and visualizing the sensor data

Since the sensor data is sent in CSV formatted string, the variable ‘data’ will contain string data instead of numbers. To read the string and get the actual numbers, I used ‘textscan’ command (may not be the best choice for crunching CSV in real time, but for now, at least it gets the job done). From textscan, you can use the number right away. The following example code reads phone’s orientation and plot it into a bar graph. A cool (or maybe not) video shows how the bar plot changes as I tilt my phone. Note that Takashi Sasaki also uses string as its first value in the CSV line to identify what data that was sent. The second and the third value seems to be related with run time. That’s why the only data that I took from textscan is the fourth, fifth, and sixth value, which is the three axis orientation of the phone.

%CodeStart--------------------------------------------------------
%Resetting MATLAB environment
  instrreset
  clear
  clc
%Creating UDP object
  UDPComIn=udp('192.168.16.3','LocalPort',12345);
  set(UDPComIn,'DatagramTerminateMode','off')
%Creating bar plot for sensor visualization
  sensorbar=bar([0,0,0]);
%Reading sensor data continuously
  longestLag=0;
  while 1
    tic
    fopen(UDPComIn);
    csvdata=fscanf(UDPComIn);
    scandata=textscan(csvdata,'%s %f %f %f %f %f','Delimiter',',');
    data=[scandata{4},scandata{5},scandata{6}];
    if size(data,1)==1
      data(1)=data(1)-180;
      set(sensorbar,'YData',data(1,:))
      axis([0.5,3.5,-180,180])
      pause(0.00001)
    end
  clc
  disp('Data received:')
  disp(data)
  disp('Longest Lag:')
  disp(longestLag)
  fclose(UDPComIn);
  t=toc;
  longestLag=max(t,longestLag);
end
%CodeEnd----------------------------------------------------------

So, to sum this up and to show you how your Android phone’s sensor is really being read into MATLAB, I made this simple video tutorial. It covers up from the first up which is setting up the LAN code and until the very end. It wasn’t the best tutorial because it is self directed, self-voiced (Hell, actually I want this to be voiced by Jon Bailey (If you don’t know, he is the voice of Optimus Prime and most movie trailer narrator)), and self-held. But at least, it gets the job done. I’m also sorry for the smell. I haven’t took a bath for three days when I made that video.

Okay, back to the data reading thing again, one thing that I noticed is that the SensorUDP does not provide an option to change the data transimission frequency/speed (Sure there is Fastest-Game-Normal-UI option, tried all of them and all were still too fast). While in MATLAB you must pause the code execution for awhile in order to give time for MATLAB to update the plot (and probably for everything else that is related with GUI), this causes data stacks. MATLAB pauses its program execution for a while to update the plot while the sensorUDP keeps sending data that ends up on a queue up to be read. When you read another data using ‘fscanf’, it will get the data on the sop of the queue, which is the sensor reading at several miliseconds ago, instead of the newest or current sensor reading. Hence, a snowball effect lag occured. This data will get stacked up and you’ll ended up with data lag even after a few seconds. I managed to get this around by closing and re-opening UDP communication in every loop but I don’t think this is a good practice. However, for now, this gets the job done.

Well, that’s is for this post. I hope this is useful for you guys fellow users of older version of MATLAB.

Regards,

Stepen

This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

8 Responses to Reading Your Android Phone Sensor into MATLAB

  1. ata soluki says:

    very nice
    I wanna sent data to cable robot via phone so your article help me alot
    thanks

  2. nice job, It was very useful for me, thanks.

  3. jesberth says:

    hi, how i can ploting history of sensor like sensor kinetics in matlab?

    thanks in advance X_X

  4. sani says:

    Thank u Stepen…. ur work helped me a lot…. I’m trying to get real time data in Matlab R2011a via bluetoooth on COM40…. (Sensoduino)…. Have u done any work from this angle….??

  5. SKY says:

    hi I have followed your steps but I am not able to ping my cellphone from my laptop.
    these are connected to same wirless router….
    Can you tell what is missing?

  6. Parani Kumar says:

    Warning: Unsuccessful read: The input buffer was filled before the Terminator was reached..any suggestions sie

  7. Getinet says:

    i want to stream video from my android devices to matlab gui somebody help me please?

  8. mngujral says:

    Greatt article!!
    Just what I was looking for !!
    However, I use R instead of MATLAB:/
    I want sensor data from my android into R. Do you know of a feasible way to do this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s