forked from ob-f/OpenBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.java
195 lines (165 loc) · 6.29 KB
/
GameController.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
//Created by Matthias Mueller - Intel Intelligent Systems Lab - 2020
package org.openbot.env;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Toast;
import org.openbot.CameraActivity;
import org.openbot.CameraActivity.ControlSignal;
import org.openbot.CameraActivity.DriveMode;
public class GameController {
private float left = 0;
private float right = 0;
private DriveMode driveMode;
public GameController(DriveMode driveMode) {
this.driveMode = driveMode;
}
public void setDriveMode(DriveMode mode) {
driveMode = mode;
}
public DriveMode getDriveMode() {return driveMode;}
private static float getCenteredAxis(MotionEvent event,
InputDevice device,
int axis,
int historyPos) {
final InputDevice.MotionRange range =
device.getMotionRange(axis, event.getSource());
// A joystick at rest does not always report an absolute position of
// (0,0). Use the getFlat() method to determine the range of values
// bounding the joystick axis center.
if (range != null) {
final float flat = range.getFlat();
final float value =
historyPos < 0 ? event.getAxisValue(axis):
event.getHistoricalAxisValue(axis, historyPos);
// Ignore axis values that are within the 'flat' region of the
// joystick axis center.
if (Math.abs(value) > flat) {
return value;
}
}
return 0;
}
public void processButtonInput(KeyEvent event){
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BUTTON_A:
Toast.makeText(CameraActivity.getContext(),"A recognized", Toast.LENGTH_SHORT).show();
return;
case KeyEvent.KEYCODE_BUTTON_B:
Toast.makeText(CameraActivity.getContext(),"B recognized", Toast.LENGTH_SHORT).show();
return;
case KeyEvent.KEYCODE_BUTTON_Y:
Toast.makeText(CameraActivity.getContext(),"Y recognized", Toast.LENGTH_SHORT).show();
return;
case KeyEvent.KEYCODE_BUTTON_X:
Toast.makeText(CameraActivity.getContext(),"X recognized", Toast.LENGTH_SHORT).show();
return;
case KeyEvent.KEYCODE_BUTTON_L1:
Toast.makeText(CameraActivity.getContext(),"L1 recognized", Toast.LENGTH_SHORT).show();
return;
case KeyEvent.KEYCODE_BUTTON_R1:
Toast.makeText(CameraActivity.getContext(),"R1 recognized", Toast.LENGTH_SHORT).show();
return;
default:
Toast.makeText(CameraActivity.getContext(),"Key " + event.getKeyCode() + " not recognized", Toast.LENGTH_SHORT).show();
return;
}
}
public ControlSignal processJoystickInput(MotionEvent event, int historyPos) {
InputDevice inputDevice = event.getDevice();
switch (driveMode) {
case DUAL:
float y = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_Y, historyPos);
float rz = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_RZ, historyPos);
left = -y;
right = -rz;
break;
case GAME:
float r_trigger = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_GAS, historyPos);
if (r_trigger == 0) {
r_trigger = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_RTRIGGER, historyPos);
}
float l_trigger = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_BRAKE, historyPos);
if (l_trigger == 0) {
l_trigger = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_LTRIGGER, historyPos);
}
// Calculate the steering magnitude by
// using the input value from one of these physical controls:
// the left control stick, hat axis, or the right control stick.
float steering_offset = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_X, historyPos);
if (steering_offset == 0) {
steering_offset = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_HAT_X, historyPos);
}
if (steering_offset == 0) {
steering_offset = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_Z, historyPos);
}
left = r_trigger - l_trigger;
right = r_trigger - l_trigger;
if (steering_offset > 0) {
if (right >= 0) right -= steering_offset;
else right += steering_offset;
}
if (steering_offset < 0)
{
if (left >= 0) left += steering_offset;
else left -= steering_offset;
}
break;
case JOYSTICK:
// Calculate the vertical distance to move by
// using the input value from one of these physical controls:
// the left control stick, hat switch, or the right control stick.
float y_axis = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_Y, historyPos);
if (y_axis == 0) {
y_axis = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_HAT_Y, historyPos);
}
if (y_axis == 0) {
y_axis = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_RZ, historyPos);
}
// Calculate the horizontal distance to move by
// using the input value from one of these physical controls:
// the left control stick, hat axis, or the right control stick.
float x_axis = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_X, historyPos);
if (x_axis == 0) {
x_axis = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_HAT_X, historyPos);
}
if (x_axis == 0) {
x_axis = getCenteredAxis(event, inputDevice,
MotionEvent.AXIS_Z, historyPos);
}
left = -y_axis;
right = -y_axis;
if (x_axis > 0) {
if (right >= 0) right -= x_axis;
else right += x_axis;
}
if (x_axis < 0)
{
if (left >= 0) left += x_axis;
else left -= x_axis;
}
break;
default:
left = 0;
right = 0;
break;
}
left *= 255;
right *= 255;
return new ControlSignal((int)left,(int)right);
}
}