/
MainActivity.java
252 lines (198 loc) · 7.68 KB
/
MainActivity.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
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
package com.burfdevelopment.lejosandroidsample;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import lejos.hardware.Audio;
import lejos.hardware.sensor.EV3IRSensor;
import lejos.remote.ev3.RemoteRequestEV3;
import lejos.remote.ev3.RemoteRequestSampleProvider;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.SampleProvider;
public class MainActivity extends AppCompatActivity {
//todo Change to your IP
private final String ev3IP = "192.168.0.29";
TextView ipTextView, touchSensorTextView, irSensorTextView;
Button motorOnButton, motorOffButton;
Button connectButton, disconnectButton;
// EV3
private RegulatedMotor motorA;
private RemoteRequestEV3 ev3Brick;
private EV3IRSensor irSensor;
//There is a bug with touch sensor
//private EV3TouchSensor touchSensor;
private RemoteRequestSampleProvider sp;
private Audio audio;
private SampleProvider irSampler;
private float[] irSample;
private SampleProvider touchSampler;
private float[] touchSample;
private final ExecutorService threads = Executors.newCachedThreadPool();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
touchSensorTextView = (TextView) findViewById(R.id.touchSensorTextView);
irSensorTextView = (TextView) findViewById(R.id.irSensorTextView);
ipTextView = (TextView) findViewById(R.id.ipTextView);
ipTextView.setText("IP For EV3 : " + ev3IP);
motorOnButton = (Button) findViewById(R.id.motorOnButton);
motorOnButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (ev3Brick != null) {
new Control().execute("motorOn", null);
}
}
});
motorOffButton = (Button) findViewById(R.id.motorOffButton);
motorOffButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (ev3Brick != null) {
new Control().execute("motorOff", null);
}
}
});
connectButton = (Button) findViewById(R.id.connectButton);
connectButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (ev3Brick == null) {
new Control().execute("connectBrick", ev3IP);
}
}
});
disconnectButton = (Button) findViewById(R.id.disconnectButton);
disconnectButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (ev3Brick != null) {
new Control().execute("disconnect", null);
}
}
});
}
private void motorOn() {
motorA.forward();
// you can rotate to a certain degree.
//motorA.rotate(100);
Log.i("MOTOR", "MOTOR ON");
}
private void motorOff() {
motorA.stop();
Log.i("MOTOR", "MOTOR OFF");
}
private class Control extends AsyncTask<String, Integer, Long> {
protected Long doInBackground(String... cmd) {
if (cmd[0].equals("connectBrick")) {
try {
ev3Brick = new RemoteRequestEV3(cmd[1]);
motorA = ev3Brick.createRegulatedMotor("A", 'L');
motorA.setSpeed(7200);
audio = ev3Brick.getAudio();
audio.systemSound(3);
ev3Brick.getAudio().systemSound(1);
monitorSensors();
return 0l;
} catch (IOException e) {
return 1l;
}
} else if (cmd[0].equals("disconnect")) {
disconnect();
return 0l;
}
if (cmd[0].equals("motorOn")) {
motorOn();
} else if (cmd[0].equals("motorOff")) {
motorOff();
}
return 0l;
}
protected void onPostExecute(Long result) {
if (result == 1l) {
Toast.makeText(MainActivity.this, "Could not connect to EV3", Toast.LENGTH_LONG).show();
} else if (result == 2l) {
Toast.makeText(MainActivity.this, "Not connected", Toast.LENGTH_LONG).show();
}
}
}
public void monitorSensors() {
// Each sensor needs to get regularly sampled
irSensor = new EV3IRSensor(ev3Brick.getPort("S4"));
final int IR_PROX = 0;
irSensor.setCurrentMode(IR_PROX);
irSampler = irSensor.getMode("Distance");
irSample = new float[irSampler.sampleSize()];
sp = (RemoteRequestSampleProvider) ev3Brick.createSampleProvider("S1", "lejos.hardware.sensor.EV3TouchSensor", "Touch");
touchSample = new float[sp.sampleSize()];
threads.submit(new Runnable() {
@Override
public void run() {
while (!threads.isShutdown()) {
try {
TimeUnit.MILLISECONDS.sleep(50);
irSampler.fetchSample(irSample, 0);
//touchSampler.fetchSample(touchSample, 0);
sp.fetchSample(touchSample, 0);
runOnUiThread(new Runnable() {
public void run() {
irSensorTextView.setText("IR Distance: " + irSample[0]);
touchSensorTextView.setText("Touch: " + touchSample[0]);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (irSensor != null) {
irSensor.close();
}
if (motorA != null) {
motorA.close();
motorA = null;
}
if (ev3Brick != null) {
ev3Brick.disConnect();
ev3Brick = null;
}
if (sp != null) {
sp.close();
}
}
});
}
private void disconnect() {
audio = ev3Brick.getAudio();
audio.systemSound(4);
ev3Brick.getAudio().systemSound(1);
threads.shutdown();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}