This repository was archived by the owner on Jun 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrantCarMain.java
More file actions
328 lines (284 loc) · 9.11 KB
/
GrantCarMain.java
File metadata and controls
328 lines (284 loc) · 9.11 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.roboeaters.grantbot;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.IOIOLooperProvider;
import ioio.lib.util.android.IOIOAndroidApplicationHelper;
import java.text.DecimalFormat;
import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.SurfaceView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
public class GrantCarMain extends Activity implements IOIOLooperProvider {
private static final String TAG = "Sample::Activity";
private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
this, this);
GrantCarMain app;
// UI stuff
TextView mountX, mountY, motorPW, wheelPW, frontIR, backIR, sideRIR,
sideLIR, diagRIR, diagLIR, halifact;
TextView mountXValue, mountYValue, motorPWValue, wheelPWValue,
frontIRValue, backIRValue, sideRIRValue, sideLIRValue,
diagRIRValue, diagLIRValue, halifactValue;
TextView stateText;
TextView state;
LinearLayout head_parent;
LinearLayout UI;
LinearLayout UIValues;
// Threadings
IOIOThread ioio_thread;
ROSBridge ros_thread;
Cam_thread cam_thread;
SurfaceView view; // needed?
byte[] ip_address;
String portNumber;
DecimalFormat df = new DecimalFormat("#.####"); // format to print voltages
// BLUETOOTH
// Local Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
// private BluetoothService mChatService = null;
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
private String mConnectedDeviceName = null;
int menuSelection;
// makes a "number" into a "dp" value
// ex (scale) * 3 == "3dp"
private float scale;
public GrantCarMain() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
/** Called when the activity is first created. */
@TargetApi(5)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = this;
scale = getResources().getDisplayMetrics().density;
Log.i(TAG, "onCreate");
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setLayout();
helper_.create();
// BLUETOOTH METHODS
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available",
Toast.LENGTH_LONG).show();
finish();
return;
}
// ensureDiscoverable(); // makes the device discoverable
menuSelection = 0; // set the inital selection
ip_address = new byte[]{10,42,0,1};
portNumber = "9090"; // default ROSBridge
view = new SurfaceView(this);
ros_thread = new ROSBridge(ip_address, portNumber);
// cam_thread = new Cam_thread(view, ros_thread);
// cam_thread.start_thread();
}
// 0:MountX
// 1:MountY
// 2:MotorPW
// 3:WheelPW
// 4:Front IR
// 5:Diag Left IR
// 6:Diag Right IR
// 7:Side Left IR
// 8:Side Right IR
// 9:Back IR
public void setTextFields(double[] values, Boolean halifact, final String currentState) {
// This method is being called often by the IOIO thread,
// aka asking the UI thread to do a lot of work, may lag with more
// updates.
final double[] val = values;
final Boolean hal = halifact;
runOnUiThread(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
motorPWValue.setText("" + df.format(val[2]));
wheelPWValue.setText("" + df.format(val[3]));
frontIRValue.setText("" + df.format(val[4]));
diagLIRValue.setText("" + df.format(val[5]));
diagRIRValue.setText("" + df.format(val[6]));
sideLIRValue.setText("" + df.format(val[7]));
sideRIRValue.setText("" + df.format(val[8]));
backIRValue.setText("" + df.format(val[9]));
halifactValue.setText("" + hal);
state.setText("" + currentState);
}
});
}
public void setLayout() {
head_parent = new LinearLayout(this);
head_parent.setWeightSum(2.0f);
LinearLayout.LayoutParams headlp = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
head_parent.setLayoutParams(headlp);
head_parent.setOrientation(LinearLayout.HORIZONTAL);
UI = new LinearLayout(this);
UI.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp;
lp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
UI.setLayoutParams(lp);
UI.setGravity(Gravity.CENTER);
UIValues = new LinearLayout(this);
UIValues.setOrientation(LinearLayout.VERTICAL);
UIValues.setLayoutParams(lp);
UIValues.setGravity(Gravity.CENTER);
TextView[] names = new TextView[12];
TextView[] values = new TextView[12];
mountXValue = new TextView(this);
mountYValue = new TextView(this);
motorPWValue = new TextView(this);
wheelPWValue = new TextView(this);
frontIRValue = new TextView(this);
backIRValue = new TextView(this);
sideRIRValue = new TextView(this);
sideLIRValue = new TextView(this);
diagRIRValue = new TextView(this);
diagLIRValue = new TextView(this);
halifactValue = new TextView(this);
stateText = new TextView(this);
mountX = new TextView(this);
mountY = new TextView(this);
motorPW = new TextView(this);
wheelPW = new TextView(this);
frontIR = new TextView(this);
backIR = new TextView(this);
sideRIR = new TextView(this);
sideLIR = new TextView(this);
diagRIR = new TextView(this);
diagLIR = new TextView(this);
halifact = new TextView(this);
state = new TextView(this);
values[0] = mountXValue;
names[0] = mountX;
values[1] = mountYValue;
names[1] = mountY;
values[2] = motorPWValue;
names[2] = motorPW;
values[3] = wheelPWValue;
names[3] = wheelPW;
values[4] = frontIRValue;
names[4] = frontIR;
values[5] = backIRValue;
names[5] = backIR;
values[6] = sideRIRValue;
names[6] = sideRIR;
values[7] = sideLIRValue;
names[7] = sideLIR;
values[8] = diagRIRValue;
names[8] = diagRIR;
values[9] = diagLIRValue;
names[9] = diagLIR;
values[10] = halifactValue;
names[10] = halifact;
values[11] = state;
names[11] = stateText;
for (TextView t : names) {
t.setPadding((int) (5 * scale), (int) (5 * scale),
(int) (5 * scale), (int) (5 * scale));
t.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
UI.addView(t);
}
for (TextView t : values) {
t.setPadding((int) (5 * scale), (int) (5 * scale),
(int) (5 * scale), (int) (5 * scale));
t.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
UIValues.addView(t);
}
mountX.setText("Mount-X PW");
mountY.setText("Mount-Y PW");
motorPW.setText("Motor PW");
wheelPW.setText("Wheel PW");
frontIR.setText("Front IR");
backIR.setText("Back IR");
sideRIR.setText("Side Right IR");
sideLIR.setText("Side Left IR");
diagRIR.setText("Diag Right IR");
diagLIR.setText("Diag Left IR");
halifact.setText("Hall Effect Sensor");
stateText.setText("State");
// frame.addView(viewScreen);
head_parent.addView(UI);
head_parent.addView(UIValues);
setContentView(head_parent);
}
/****************************************************** functions from IOIOActivity *********************************************************************************/
protected IOIOLooper createIOIOLooper() {
// perhaps pass in a reference to the main application so the IOIO
// thread can post the results
// of the IR and PW calculations to the textViews in the main
// applications UI
ioio_thread = new IOIOThread(app, ros_thread);
Log.d("YOLO TEST", "YOLO TEST");
return ioio_thread;// send them the viewscreen thread
}
@Override
public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
return createIOIOLooper();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
// BLUETOOTH
@Override
public synchronized void onResume() {
super.onResume();
if (true)
Log.e(TAG, "+ ON RESUME +");
}
@Override
protected void onStop() {
helper_.stop();
ros_thread.end();
//cam_thread.stop_thread();
super.onStop();
}
@Override
protected void onDestroy() {
ros_thread.end();
//cam_thread.stop_thread();
helper_.destroy();
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
if (true)
Log.e(TAG, "++ ON START ++");
helper_.start();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
helper_.restart();
}
}
}