forked from DexterInd/GoPiGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGopigo.java
More file actions
353 lines (327 loc) · 9.79 KB
/
Copy pathGopigo.java
File metadata and controls
353 lines (327 loc) · 9.79 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* #%L
* **********************************************************************
* ORGANIZATION : DexterIndustries
* PROJECT : GoPiGo Java Library
* FILENAME : Gopigo.java
* AUTHOR : Marcello Barile <marcello.barile@gmail.com>
*
* This file is part of the GoPiGo Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GoPiGo
* **********************************************************************
* %%
* GoPiGo for the Raspberry Pi: an open source robotics platform for the Raspberry Pi.
* Copyright (C) 2015 Dexter Industries
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
*
* #L%
*/
package com.dexterind.gopigo;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import com.dexterind.gopigo.behaviours.*;
import com.dexterind.gopigo.components.*;
import com.dexterind.gopigo.events.*;
import com.dexterind.gopigo.utils.*;
/**
* The main class for the Gopigo. It instanciates all the components and
* behaviours and is in charge to handle the events.
* @author marcello
*
*/
public final class Gopigo {
/**
* The instance of the current object.
*/
private static Gopigo instance;
/**
* The flag for the initialized status.
*/
private boolean isInit = false;
/**
* The flag for the halted status.
*/
private boolean isHalt = false;
/**
* The timer which will be in charge to check the voltage value.
*/
private Timer voltageTimer;
/**
* The time in milliseconds between successive task executions.
*/
private int voltageTimerTime = 60000;
/**
* The minimum voltage value. If the voltage drops under this value a VoltageEvent will be fired.
*/
private double minVoltage = 5.5;
/**
* The critical voltage value. If the voltage drops under this value a VoltageEvent will be fired
* and the GoPiGo will be flagged as "halted".
*/
private double criticalVoltage = 1.0;
/**
* The main object which handles the methods to get access to the resources
* connected to the board.
*/
public Board board;
/**
* The encoders.
*/
public Encoders encoders;
/**
* The servo motor.
*/
public Servo servo;
/**
* The ultrasonic sensor.
*/
public UltraSonicSensor ultraSonicSensor;
/**
* The IR Receiver sensor.
*/
public IRReceiverSensor irReceiverSensor;
/**
* The left led.
*/
public Led ledLeft;
/**
* The right led.
*/
public Led ledRight;
/**
* The left motor.
*/
public Motor motorLeft;
/**
* The right motor.
*/
public Motor motorRight;
/**
* The motion behaviour object.
*/
public Motion motion;
/**
* The list of the listeners which are listening for some event.
*/
private final CopyOnWriteArrayList<GopigoListener> listeners;
/**
* The debug object.
*/
private Debug debug;
/**
* Instanciates the components and the behaviours of the Gopigo.
*/
public Gopigo() {
debug = new Debug("com.dexterind.gopigo.Gopigo");
debug.log(Debug.FINEST, "Instancing a new Gopigo!");
try {
board = Board.getInstance();
encoders = Encoders.getInstance();
servo = Servo.getInstance();
ultraSonicSensor = UltraSonicSensor.getInstance();
irReceiverSensor = IRReceiverSensor.getInstance();
ledLeft = new Led(Led.LEFT);
ledRight = new Led(Led.RIGHT);
motorLeft = new Motor(Motor.LEFT);
motorRight = new Motor(Motor.RIGHT);
motion = Motion.getInstance();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
voltageTimer = new Timer();
listeners = new CopyOnWriteArrayList<GopigoListener>();
}
/**
* Provides a global point of access to the Gopigo instance.
* @return the <code>Gopigo</code> instance.
*/
public static Gopigo getInstance() {
if(instance == null) {
instance = new Gopigo();
}
return instance;
}
/**
* Initializes the Gopigo and fires an event once the init is done.
*/
public void init() {
debug.log(Debug.FINE, "Init " + isInit);
board.init();
if (!isHalt) {
isInit = true;
initVoltageCheck();
StatusEvent statusEvent = new StatusEvent(this, Statuses.INIT);
fireEvent(statusEvent);
}
}
/**
* Resets the Gopigo.
* @throws IOException
*/
public void reset() throws IOException {
debug.log(Debug.INFO, "Reset");
servo.move(motion.directions.get("e"));
ledLeft.off();
ledRight.off();
motion.setSpeed(255);
free();
}
/**
* Adds a <code>GopigoListener</code> to the listeners list.
* @param listener The <code>GopigoListener</code> to register.
*/
public void addListener(GopigoListener listener) {
debug.log(Debug.INFO, "Adding listener");
listeners.addIfAbsent(listener);
}
/**
* Removes a <code>GopigoListener</code> from the listeners list.
* @param listener The <code>GopigoListener</code> to remove.
*/
public void removeListener(GopigoListener listener) {
if (listeners != null) {
debug.log(Debug.INFO, "Removing listener");
listeners.remove(listener);
}
}
/**
* Fires an event to the listeners.
* @param event The event to fire.
*/
protected void fireEvent(EventObject event) {
int i = 0;
debug.log(Debug.INFO, "Firing event [" + listeners.toArray().length + " listeners]");
for (GopigoListener listener : listeners) {
debug.log(Debug.INFO, "listener[" + i + "]");
debug.log(Debug.INFO, event.getClass().toString());
if (event instanceof StatusEvent) {
listener.onStatusEvent((StatusEvent) event);
} else if (event instanceof VoltageEvent) {
listener.onVoltageEvent((VoltageEvent) event);
}
i++;
}
}
/**
* Initializes a timer which will checks the voltage and will fires and event in case of low voltage.
*/
private void initVoltageCheck() {
voltageTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new Thread(new Runnable() {
public void run() {
double voltage;
Boolean dispatchEvent = false;
try {
Gopigo.getInstance().debug.log(Debug.INFO, "Voltage check");
voltage = Gopigo.getInstance().board.volt();
if (voltage < Gopigo.getInstance().getMinVoltage()) {
Gopigo.getInstance().debug.log(Debug.WARNING, "Low voltage detected");
Gopigo.getInstance().free();
dispatchEvent = true;
} else if (voltage <= Gopigo.getInstance().getCriticalVoltage()) {
Gopigo.getInstance().debug.log(Debug.SEVERE, "Critical voltage detected. GoPiGo is now halted.");
Gopigo.getInstance().halt();
dispatchEvent = true;
} else {
Gopigo.getInstance().free();
}
if (dispatchEvent) {
VoltageEvent voltageEvent = new VoltageEvent(Gopigo.getInstance(), voltage);
Gopigo.getInstance().fireEvent(voltageEvent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}, 0, voltageTimerTime);
}
/**
* Sets the GoPiGo free. It removes the "halted" flag if necessary.
*/
public void free() {
if (isHalt) {
debug.log(Debug.INFO, "The GoPigo was halted. I'm setting it free.");
isHalt = false;
}
}
/**
* It returns the operativity status.
* @return True if operative, False if not.
*/
public Boolean isOperative() {
return isHalt == false;
}
/**
* Halts the GoPiGo if necessary.
*/
public void halt() {
if (!isHalt) {
debug.log(Debug.WARNING, "The GoPigo was free, now it's halted.");
isHalt = true;
}
}
/**
* It returns the halt status.
* @return True if halted, False if not.
*/
public Boolean isHalt() {
return isHalt;
}
/**
* Fires a "HALT" <code>StatusEvent</code>
*/
public void onHalt() {
debug.log(Debug.WARNING, "The GoPigo seems to be halted, it will be probably difficult to execute the commands.");
StatusEvent statusEvent = new StatusEvent(this, Statuses.HALT);
fireEvent(statusEvent);
}
/**
* Sets the min. voltage value.
* @param value The minimum voltage value to set.
*/
public void setMinVoltage(double value) {
debug.log(Debug.INFO, "Setting minVoltage to " + Double.toString(value));
minVoltage = value;
}
/**
* Returns the min. voltage value.
* @return The min. voltage value.
*/
public Double getMinVoltage() {
debug.log(Debug.INFO, "Getting minVoltage");
return minVoltage;
}
/**
* Sets the critical voltage value. Under this value the GoPiGo will be
* flagged as "halted".
* @param value The critical voltage value to set.
*/
public void setCriticalVoltage(double value) {
debug.log(Debug.INFO, "Setting criticalVoltage to " + Double.toString(value));
criticalVoltage = value;
}
/**
* Returns the critical voltage value.
* @return The Critical voltage value.
*/
public Double getCriticalVoltage() {
debug.log(Debug.INFO, "Getting criticalVoltage");
return criticalVoltage;
}
}