-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrobot.pde
More file actions
235 lines (171 loc) · 3.93 KB
/
robot.pde
File metadata and controls
235 lines (171 loc) · 3.93 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
/************************
ROBOT STUFF
*************************/
float previousY = 0.01;
float dt = 1.0/30.0;
float velocityAmount;
float rotationAmount;
float rotationWheelLeft, rotationWheelRight;
int pathIntervalCounter;
int pathInterval = 0;
float R = 10; // wheel radius
float L = 50; // wheel distance
Robot RobotOne = new Robot (0, 0, 0, R, L);
ArrayList pontos;
void RobotSetup()
{
RobotOne.heading = -PI/2;
RobotOne.vr = 3;
RobotOne.vl = 2;
pontos = new ArrayList();
pontos.add(new Ponto(0, 0, 0));
}
/********************************************************
CALL MOVE + DRAW + PATH
********************************************************/
void RobotDraw()
{
rectMode(CENTER);
if (robotMove)
RobotOne.Move();
if (drawRobot)
RobotOne.Draw();
if (addPath && activePath)
{
if (pathIntervalCounter > pathInterval) {
RobotOne.addPath();
pathIntervalCounter = 0;
}
pathIntervalCounter++;
}
if (drawPath)
RobotDrawPath();
}
/************************
CLASS ROBOT
*************************/
class Robot {
float vl, vr; // left and right wheel velocity
float v; // "unicycle" velocity
float w; // angular velocity
float heading; // direction in radians;
float x, y;
float R; // wheel radius
float L; // wheel distance
int id;
Robot (float _x, float _y, float _heading, float _r, float _l) {
x = x;
y = y;
heading = _heading;
vl = 0;
vr = 0;
v = 0;
L = _l;
R = _r;
}
void Draw () {
pushMatrix();
translate(width/2+x, height/2+y);
rotate (heading);
strokeWeight(1);
stroke(0, 150, 255, 220);
fill(0, 150, 255, 50);
// chassis
drawChassis();
//wheels
pushMatrix();
translate(0, -L/2-3);
if (robotMove)
rotateY(rotationWheelLeft -= (motorValLeft/50)/20);
drawWheel();
popMatrix();
pushMatrix();
translate(0, L/2+3);
if (robotMove)
rotateY(rotationWheelRight -= (motorValRight/50)/20);
drawWheel();
popMatrix();
//sensor
pushMatrix();
translate(20, 0, 10);
drawSensor();
popMatrix();
//rect (20, 0, 10, 10);
popMatrix();
}
void addPath()
{
pontos.add(new Ponto(id, width/2+x, height/2+y));
id++;
}
void Move() {
// v = R * (vr+vl)/2;
// w = (R/L) * (vl-vr);
x += (v * cos(heading))*dt;
y += (v * sin(heading))*dt;
heading += w*dt;
/* velocityAmount = (motorValRight/50)+(motorValLeft/50);
rotationAmount = (motorValRight/50)-(motorValLeft/50);
*/
int vel = 25;
velocityAmount = (motorValRight/vel)+(motorValLeft/vel);
rotationAmount = (motorValRight/vel)-(motorValLeft/vel);
v = -velocityAmount*17;
w = rotationAmount;
vr = R/2*(v/2 + w/L);
vl = R/2*(v/2 - w/L);
//println(vr+" "+vl+" "+motorValLeft+" "+motorValRight);
}
}
/********************************************************
ROBOT DRAW PATH
********************************************************/
void RobotDrawPath()
{
stroke(0, 255, 150, 100);
for (int i = pontos.size()-1; i >= 0; i--) {
Ponto ponto = (Ponto) pontos.get(i);
ponto.display();
if (ponto.finished()) {
pontos.remove(i);
}
}
}
/********************************************************
ROBOT 3D STAGE
********************************************************/
void robotDrawStage()
{
pushMatrix();
translate(mainX, mainY, mainZ);
if (cameraActive)
{
noFill();
stroke(0, 255, 0, 100);
pushMatrix();
translate(width/2.0+xpos, height/2.0+ypos, zpos);
rotateX(rX);
rotateY(rY);
rotateZ(rZ);
/*
stroke(255, 0, 0);
rect(0, 0, 20, 20);
*/
//translate(-600, -500);
/*
stroke(0, 255, 0);
rect(0, 0, 20, 20);
*/
if (gridActive)
drawGrid();
RobotDraw();
popMatrix();
}
else
{
if (gridActive)
drawGrid();
RobotDraw();
}
popMatrix();
}