This repository has been archived by the owner on Mar 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
/
HelloDepthPerceptionActivity.java
200 lines (179 loc) · 7.52 KB
/
HelloDepthPerceptionActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.projecttango.examples.java.hellodepthperception;
import com.google.atap.tangoservice.Tango;
import com.google.atap.tangoservice.Tango.OnTangoUpdateListener;
import com.google.atap.tangoservice.TangoConfig;
import com.google.atap.tangoservice.TangoCoordinateFramePair;
import com.google.atap.tangoservice.TangoErrorException;
import com.google.atap.tangoservice.TangoEvent;
import com.google.atap.tangoservice.TangoInvalidException;
import com.google.atap.tangoservice.TangoOutOfDateException;
import com.google.atap.tangoservice.TangoPointCloudData;
import com.google.atap.tangoservice.TangoPoseData;
import com.google.atap.tangoservice.TangoXyzIjData;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.nio.FloatBuffer;
import java.util.ArrayList;
/**
* Main Activity class for the Depth Perception Sample. Handles the connection to the {@link Tango}
* service and propagation of Tango PointCloud data to Layout view.
*/
public class HelloDepthPerceptionActivity extends Activity {
private static final String TAG = HelloDepthPerceptionActivity.class.getSimpleName();
private Tango mTango;
private TangoConfig mConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_depth_perception);
}
@Override
protected void onResume() {
super.onResume();
// Initialize Tango Service as a normal Android Service, since we call mTango.disconnect()
// in onPause, this will unbind Tango Service, so every time when onResume gets called, we
// should create a new Tango object.
mTango = new Tango(HelloDepthPerceptionActivity.this, new Runnable() {
// Pass in a Runnable to be called from UI thread when Tango is ready, this Runnable
// will be running on a new thread.
// When Tango is ready, we can call Tango functions safely here only when there is no UI
// thread changes involved.
@Override
public void run() {
synchronized (HelloDepthPerceptionActivity.this) {
try {
mConfig = setupTangoConfig(mTango);
mTango.connect(mConfig);
startupTango();
} catch (TangoOutOfDateException e) {
Log.e(TAG, getString(R.string.exception_out_of_date), e);
showsToastAndFinishOnUiThread(R.string.exception_out_of_date);
} catch (TangoErrorException e) {
Log.e(TAG, getString(R.string.exception_tango_error), e);
showsToastAndFinishOnUiThread(R.string.exception_tango_error);
} catch (TangoInvalidException e) {
Log.e(TAG, getString(R.string.exception_tango_invalid), e);
showsToastAndFinishOnUiThread(R.string.exception_tango_invalid);
}
}
}
});
}
@Override
protected void onPause() {
super.onPause();
synchronized (this) {
try {
mTango.disconnect();
} catch (TangoErrorException e) {
Log.e(TAG, getString(R.string.exception_tango_error), e);
}
}
}
/**
* Sets up the tango configuration object. Make sure mTango object is initialized before
* making this call.
*/
private TangoConfig setupTangoConfig(Tango tango) {
// Create a new Tango Configuration and enable the Depth Sensing API.
TangoConfig config = tango.getConfig(TangoConfig.CONFIG_TYPE_DEFAULT);
config.putBoolean(TangoConfig.KEY_BOOLEAN_DEPTH, true);
config.putInt(TangoConfig.KEY_INT_DEPTH_MODE, TangoConfig.TANGO_DEPTH_MODE_POINT_CLOUD);
return config;
}
/**
* Set up the callback listeners for the Tango service and obtain other parameters required
* after Tango connection.
* Listen to new Point Cloud data.
*/
private void startupTango() {
// Lock configuration and connect to Tango.
// Select coordinate frame pair.
final ArrayList<TangoCoordinateFramePair> framePairs =
new ArrayList<TangoCoordinateFramePair>();
framePairs.add(new TangoCoordinateFramePair(
TangoPoseData.COORDINATE_FRAME_START_OF_SERVICE,
TangoPoseData.COORDINATE_FRAME_DEVICE));
// Listen for new Tango data
mTango.connectListener(framePairs, new OnTangoUpdateListener() {
@Override
public void onPoseAvailable(final TangoPoseData pose) {
// We are not using TangoPoseData for this application.
}
@Override
public void onXyzIjAvailable(TangoXyzIjData xyzIj) {
// We are not using onXyzIjAvailable for this app.
}
@Override
public void onPointCloudAvailable(final TangoPointCloudData pointCloudData) {
logPointCloud(pointCloudData);
}
@Override
public void onTangoEvent(final TangoEvent event) {
// Ignoring TangoEvents.
}
@Override
public void onFrameAvailable(int cameraId) {
// We are not using onFrameAvailable for this application.
}
});
}
/**
* Log the point count and the average depth of the given PointCloud data
* in the Logcat as information.
*/
private void logPointCloud(TangoPointCloudData pointCloudData) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Point count: " + pointCloudData.numPoints);
stringBuilder.append(". Average depth (m): " +
calculateAveragedDepth(pointCloudData.points, pointCloudData.numPoints));
Log.i(TAG, stringBuilder.toString());
}
/**
* Calculates the average depth from a point cloud buffer.
*/
private float calculateAveragedDepth(FloatBuffer pointCloudBuffer, int numPoints) {
float totalZ = 0;
float averageZ = 0;
if (numPoints != 0) {
int numFloats = 4 * numPoints;
for (int i = 2; i < numFloats; i = i + 4) {
totalZ = totalZ + pointCloudBuffer.get(i);
}
averageZ = totalZ / numPoints;
}
return averageZ;
}
/**
* Display toast on UI thread.
*
* @param resId The resource id of the string resource to use. Can be formatted text.
*/
private void showsToastAndFinishOnUiThread(final int resId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(HelloDepthPerceptionActivity.this,
getString(resId), Toast.LENGTH_LONG).show();
finish();
}
});
}
}