source: internals/2016/screencastmultiplayergame_control/trunk/SampleApps/Android_Sensor_Aquisition_example.m @ 16437

Last change on this file since 16437 was 16434, checked in by jesteves, 4 years ago

Added Matlab scripts that do up/down motion detection

File size: 5.7 KB
1%% Acquire and Plot Angular Velocity and Orientation Data from Your Android(TM) Device
3% This example shows how to plot data from multiple sensors on an Android
4% device together in one timeline. Measurements of angular velocity and
5% orientation will be collected from the device and plotted over absolute
6% time. This will allow correlations between data from two sensors to be
7% visualized based on the actual time of measurement.
9% Copyright 2014 The MathWorks, Inc.
11%% Setting up the connection to your mobile device
12% In order to receive data from an Android device in MATLAB(R), you will
13% need the MATLAB Mobile(TM) app (version 2.0). You can download this app
14% from Google(R) Play(TM).
17% Use MATLAB Connector to allow connection between your desktop MATLAB
18% session and MATLAB Mobile on your Android device. Your device must be
19% able to connect to your desktop, either by being on the same network,
20% using a VPN, or through a similar configuration.
22% Execute the |connector| command with a password of your choice.
24connector on yourpassword;
27% Your computer's DNS name and IP address are displayed on
28% the MATLAB Command Window, along with a test link. Click on the test link
29% to ensure that your desktop is ready to connect. The last digits after
30% the colon on the test link correspond to the port that has been opened.
33% Now you can connect MATLAB Mobile to your desktop.
35% If you are launching MATLAB Mobile for the first time, select Connect to
36% Your Computer. If you previously connected to the cloud or a different
37% desktop, go to the Settings screen and select Add a Computer from the
38% Connect to Your Computers section.
39% On the Add a Computer screen, enter the DNS name or IP address that was
40% displayed in the previous step. Then, enter the Connector password that
41% you previously specified, as well as the port that was opened.
42% Finally, press the Connect button.
43% MATLAB Mobile should now be connected to your desktop MATLAB session.
45%% Create a link to your mobile device
46% Use the |mobiledev| command to create an object that represents your
47% mobile device.
49m = mobiledev
52% The displayed output should show |Connected: 1|, indicating that the
53% |mobiledev| object has successfully established a connection to the app.
54%% Prepare for data acquisition from multiple sensors
55% Use the appropriate |mobiledev| properties to enable sensors on the
56% device.
58m.AngularVelocitySensorEnabled = 1;
59m.OrientationSensorEnabled = 1;
61%% Start acquiring data
62% After enabling the sensors, the Sensors screen of MATLAB Mobile will show
63% the current data measured by the sensors. The |Logging| property allows
64% you to begin sending sensor data to |mobiledev|.
66m.Logging = 1;
69% The device is now transmitting sensor data. A pause is included to allow
70% for some measurements to be made before moving to the next steps, but is
71% not required for logging to occur.
76% At the start of logging, the device is lying flat on a table with the
77% screen facing up. The positive Y-axis of the angular velocity sensor is
78% defined to extend out from the top of the device. Positive roll of the
79% orientation sensor is defined as a clockwise rotation about the Y-axis
80% when facing the positive direction of the axis.
83% During logging, the device is turned face-up and face-down a few times
84% about its Y-axis. This generates Y-axis changes in angular velocity and
85% roll changes in orientation.
87%% Stop acquiring data
88% The |Logging| property is used again to have the device stop sending
89% sensor data to |mobiledev|.
91m.Logging = 0;
93%% Retrieve logged data
94% Each sensor's data can be accessed from the |mobiledev| object.
95% Two different timestamp variables are created because the angular
96% velocity and orientation data may not be recorded by the device at the
97% same time.
99[av, tav] = angvellog(m);
100[o, to] = orientlog(m);
102%% Plot raw sensor data
103% The data of interest from the sensors are isolated into separate
104% variables and plotted.
106yAngVel = av(:,2);
107roll = o(:, 3);
108plot(tav, yAngVel, to, roll);
109legend('Y Angular Velocity', 'Roll');
110xlabel('Relative time (s)');
113% The data is plotted in seconds relative to |mobiledev|'s
114% |InitialTimestamp| property. This property provides the absolute time of
115% the first data point recorded by a sensor and sent to MATLAB. It acts as
116% the reference point for the timestamps of all sensor data accessed using
117% functions such as |angvellog| and |orientlog|, which are given in seconds
118% relative to InitialTimestamp.
120%% Convert to absolute timestamps
121% To convert all sensor timestamps into absolute timestamps, the
122% InitialTimestamp value is converted into a |datetime| object. The
123% individual sensor timestamps, which are in units of seconds, are
124% converted into |seconds|. This simplifies the date arithmetic of turning
125% relative timestamps into absolute timestamps.
127tInit = datetime(m.InitialTimestamp, 'InputFormat', 'dd-MM-yyyy HH:mm:ss.SSS');
128tAngVel = tInit + seconds(tav);
129tOrient = tInit + seconds(to);
131%% Plot multiple sensors over absolute time
132% Both sensors now have absolute timestamps represented using |datetime|.
133% This makes it easy to visualize the actual time that significant events
134% occurred in sensor readings.
136% The orientation sensor uses units of degrees, while the angular velocity
137% sensor uses units of radians per second. Before plotting again, |yAngVel|
138% is converted to degrees per second.
140yAngVelDeg = yAngVel * 180/pi;
142plot(tAngVel, yAngVelDeg, tOrient, roll);
143legend('Y Angular Velocity', 'Roll');
144xlabel('Absolute time (s)');
147%% Clean up
148% Turn off the enabled sensors and clear |mobiledev|.
150m.AngularVelocitySensorEnabled = 0;
151m.OrientationSensorEnabled = 0;
153clear m;
Note: See TracBrowser for help on using the repository browser.