-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathRobot.java
234 lines (189 loc) · 5.26 KB
/
Robot.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
import processing.core.*;
import java.util.*;
public class Robot extends Simulatable implements ShapeCircle, Drawable, Runnable{
/*
Saves the GameSimulator in order to access global stuff such as objects from field
*/
private GameSimulator game;
/*
Robot Sensors ArrayList, where each of them can be read.
*/
private HashMap<String, Sensor> mappedSensors = new HashMap<String, Sensor>();
private ArrayList<Sensor> sensors = new ArrayList<Sensor>();
/*
Physics attributes used to simulate orientation and position of robot
*/
// Speed state attributes (N/m and m/s)
private float motorForce = 5f;
private float maxSpeed = 0.5f;
private PVector targetSpeed = new PVector(0,0);
// Orientation attributes
private float maxAngularAccel = (float)Math.PI * 10;
private float maxAngularSpeed = (float)Math.PI * 10;
private float targetAngularSpeed = 0f;
private float angularSpeed = 0f;
public float orientation = 0f;
Robot(GameSimulator g){
game = g;
// Initialize sensors
initializeSensors(game);
}
public void setState(PVector position, float orientation, boolean resetAll){
this.position = position.get();
this.orientation = orientation;
if(resetAll){
this.force = new PVector();
this.accel = new PVector();
this.speed = new PVector();
}
}
/*
This is where Sensor instantiation and setup should occur.
*/
protected void initializeSensors(GameSimulator game){}
/*
Register a sensor inside this robot, with the given ID
*/
protected void registerSensor(Sensor sensor, String ID){
mappedSensors.put(ID, sensor);
sensors.add(sensor);
}
/*
Returns the Sensor with the specified ID
*/
public Sensor getSensor(String ID){
return mappedSensors.get(ID);
}
/*
Physical properties
*/
public float getRadius(){
return 0.11f;
}
public float getMass(){
return 2.2f;
}
public float getKFactor(){
return 1f;
}
public boolean canCollide(Simulatable s){
if(s instanceof GoalWall)
return false;
return true;
}
/*
This is the method used by the Thread.
*/
public void run(){
}
/*
Methods publicly available
*/
// public void setSpeed(PVector speed){
// targetSpeed = speed.get();
// targetSpeed.limit(maxSpeed);
// }
// Change robot forward/backward speed only
public void setSpeed(float x){
targetSpeed.set(x, 0);
targetSpeed.limit(maxSpeed);
}
// Change robot's front/back left/right speed
public void setSpeed(float x, float y){
if(Float.isNaN(x) || Float.isNaN(y)){
// System.out.println("Excep: setSpeed "+x+" "+y);
return;
}
targetSpeed.set(x, y);
targetSpeed.limit(maxSpeed);
}
// Set the target angular speed
public void setRotation(float speed){
if(speed == Float.NaN){
System.out.println("Excep: setRotation");
return;
}
targetAngularSpeed = (float)Math.toRadians(speed);
}
public void stopMotors(){
setSpeed(0,0);
setRotation(0);
}
public float getOrientation(){
return orientation;
}
public long millis(){
return (long) (game.getTime() * 1000);
}
public boolean delay(int time){
try{
Thread.sleep(time);
}catch(Exception e){
return false;
}
return true;
}
public void onStateChanged(String state){
}
/*
Perform Simulation
*/
public void simulate(float dt){
// Accelerate angular speed
float requiredSpeed = targetAngularSpeed - angularSpeed;
float angularAccel = requiredSpeed / dt;
angularAccel = PApplet.constrain(angularAccel, -maxAngularAccel, maxAngularAccel);
// Limit Angular speed
angularSpeed += angularAccel * dt;
angularSpeed = PApplet.constrain(angularSpeed, -maxAngularSpeed, maxAngularSpeed);
// Orientation Simulation
orientation += angularSpeed * dt;
// Position simulation
PVector worldRequiredSpeed = targetSpeed.get();
worldRequiredSpeed.rotate(orientation);
worldRequiredSpeed.sub(speed);
// PVector worldRequiredSpeed = worldTargetSpeed.get();
float dSpeed = worldRequiredSpeed.mag();
float dAcell = dSpeed / dt;
float dForce = Math.min(dAcell * getMass(), motorForce);
worldRequiredSpeed.normalize();
worldRequiredSpeed.mult(dForce);
force.add(worldRequiredSpeed);
super.simulate(dt);
}
int teamColor = 0x000000;
public void setTeamColor(int color){
teamColor = color;
}
public void draw(PApplet canvas, float scale){
PVector orient = PVector.fromAngle(orientation);
orient.mult(scale * getRadius());
float x = (float) position.x * scale;
float y = (float) position.y * scale;
float diameter = getRadius() * 2 * scale;
canvas.fill(teamColor);
canvas.stroke(0);
canvas.ellipse(x, y, diameter, diameter);
canvas.line(x, y, x + (float)orient.x, y + (float)orient.y);
// Delegate Decoration to Robot
float heading = orient.heading();
float drawScale = 100f / scale * getRadius();
canvas.translate(x, y);
canvas.rotate(heading);
canvas.scale(drawScale);
// TODO: How to resolve scale, so that teams don't have to mind it also...
decorateRobot(canvas);
canvas.scale(1f/drawScale);
canvas.rotate(-heading);
canvas.translate(-x, -y);
}
/*
* This method can be used to decorate the robot appearence.
* It is called everytime after rendering itself
* The area that is drawable, is a 100px x 100px, sacaled to
* the robot. The center is at [0,0], and Y axis indicate the
* Robot's Front
*/
public void decorateRobot(PApplet canvas){
}
};