forked from houcy/OpenBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicle.java
189 lines (150 loc) · 4.91 KB
/
Vehicle.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
package org.openbot.env;
import android.content.Context;
import java.util.Locale;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
public class Vehicle {
private Control control = new Control(0, 0);
private final Noise noise = new Noise(1000, 2000, 5000);
private boolean noiseEnabled = false;
private int indicator = 0;
private static int speedMultiplier = 192; // 128,192,255
private final SensorReading batteryVoltage = new SensorReading();
private final SensorReading leftWheelTicks = new SensorReading();
private final SensorReading rightWheelTicks = new SensorReading();
private final SensorReading sonarReading = new SensorReading();
private static final int diskHoles = 20;
private static final int millisPerMinute = 60000;
private UsbConnection usbConnection;
protected boolean usbConnected;
private final Context context;
private final int baudRate;
public Vehicle(Context context, int baudRate) {
this.context = context;
this.baudRate = baudRate;
connectUsb();
}
public static class Control {
private float left = 0;
private float right = 0;
public Control(float left, float right) {
this.left = Math.max(-1.f, Math.min(1.f, left));
this.right = Math.max(-1.f, Math.min(1.f, right));
}
public float getLeft() {
return left * speedMultiplier;
}
public float getRight() {
return right * speedMultiplier;
}
}
public float getBatteryVoltage() {
return batteryVoltage.getReading();
}
public void setBatteryVoltage(float batteryVoltage) {
this.batteryVoltage.setReading(batteryVoltage);
}
public float getLeftWheelTicks() {
return leftWheelTicks.getReading();
}
public float getLeftWheelRPM() {
if (leftWheelTicks.getAge() > 0 && leftWheelTicks.getReading() != 0)
return leftWheelTicks.getReading() * millisPerMinute / leftWheelTicks.getAge() / diskHoles;
else return 0;
}
public void setLeftWheelTicks(float leftWheelTicks) {
this.leftWheelTicks.setReading(leftWheelTicks);
}
public float getRightWheelTicks() {
return rightWheelTicks.getReading();
}
public float getRightWheelRPM() {
if (rightWheelTicks.getAge() > 0 && rightWheelTicks.getReading() != 0)
return rightWheelTicks.getReading() * millisPerMinute / rightWheelTicks.getAge() / diskHoles;
else return 0;
}
public void setRightWheelTicks(float rightWheelTicks) {
this.rightWheelTicks.setReading(rightWheelTicks);
}
public float getSonarReading() {
return sonarReading.getReading();
}
public void setSonarReading(float sonarReading) {
this.sonarReading.setReading(sonarReading);
}
public Control getControl() {
return control;
}
public void setControl(Control control) {
this.control = control;
}
public void setControl(float left, float right) {
this.control = new Control(left, right);
}
private Timer noiseTimer;
private class NoiseTask extends TimerTask {
@Override
public void run() {
noise.update();
sendControl();
}
}
public void startNoise() {
noiseTimer = new Timer();
NoiseTask noiseTask = new NoiseTask();
noiseTimer.schedule(noiseTask, 0, 50); // no delay 50ms intervals
noiseEnabled = true;
}
public void stopNoise() {
noiseEnabled = false;
noiseTimer.cancel();
}
public int getSpeedMultiplier() {
return speedMultiplier;
}
public void setSpeedMultiplier(int speedMultiplier) {
this.speedMultiplier = speedMultiplier;
}
public int getIndicator() {
return indicator;
}
public void setIndicator(int indicator) {
this.indicator = indicator;
sendStringToUsb(String.format(Locale.US, "i%d\n", indicator));
}
public UsbConnection getUsbConnection() {
return usbConnection;
}
public void connectUsb() {
usbConnection = new UsbConnection(context, baudRate);
usbConnected = usbConnection.startUsbConnection();
}
public void disconnectUsb() {
Objects.requireNonNull(usbConnection)
.send(
String.format(
Locale.US,
"c%d,%d\n",
(int) (getControl().getLeft()),
(int) (getControl().getRight())));
Objects.requireNonNull(usbConnection).stopUsbConnection();
usbConnection = null;
usbConnected = false;
}
public boolean isUsbConnected() {
return usbConnected;
}
private void sendStringToUsb(String message) {
Objects.requireNonNull(usbConnection).send(message);
}
public void sendControl() {
int left = (int) (control.left * speedMultiplier);
int right = (int) (control.right * speedMultiplier);
if (noiseEnabled && noise.getDirection() < 0)
left = (int) ((control.left - noise.getValue()) * speedMultiplier);
if (noiseEnabled && noise.getDirection() > 0)
right = (int) ((control.right - noise.getValue()) * speedMultiplier);
sendStringToUsb(String.format(Locale.US, "c%d,%d\n", left, right));
}
}