forked from Elsie4ever/DpmTeam2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Navigation.java
256 lines (230 loc) · 6.83 KB
/
Navigation.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* File: Navigation.java
* Written by: Sean Lawlor
* ECSE 211 - Design Principles and Methods, Head TA
* Fall 2011
* Ported to EV3 by: Francois Ouellet Delorme
* Fall 2015
*
* Movement control class (turnTo, travelTo, flt, localize)
*/
package trotty02;
import lejos.hardware.Sound;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
/**
*
* @author Adam
*
*/
public class Navigation {
double TILE_LENGTH = 30.48;
final static int FAST = 250, SLOW = 100, ACCELERATION = 111;
final static double DEG_ERR = 1.0, CM_ERR = 22;
final static double RADIUS = 2.1;
final static double TRACK = 12.7;
int resolution;
private Odometer odometer;
private EV3LargeRegulatedMotor leftMotor, rightMotor;
/**
* constructor for Navigation object
* @param odo the odometer
*/
public Navigation(Odometer odo) {
this.odometer = odo;
EV3LargeRegulatedMotor[] motors = this.odometer.getMotors();
this.leftMotor = motors[0];
this.rightMotor = motors[1];
// set acceleration
this.leftMotor.setAcceleration(ACCELERATION);
this.rightMotor.setAcceleration(ACCELERATION);
}
/*
* Functions to set the motor speeds jointly
*/
/**
* sets the speeds of each wheel and has them go forward
* @param lSpd the speed of the left wheel
* @param rSpd the speed of the right wheel
*/
public void setSpeeds(float lSpd, float rSpd) {
this.leftMotor.setSpeed(lSpd);
this.rightMotor.setSpeed(rSpd);
if (lSpd < 0)
this.leftMotor.backward();
else
this.leftMotor.forward();
if (rSpd < 0)
this.rightMotor.backward();
else
this.rightMotor.forward();
}
/**
* sets the speeds of each wheel and has them go forward
* @param lSpd the speed of the left wheel
* @param rSpd the speed of the right wheel
*/
public void setSpeeds(int lSpd, int rSpd) {
this.leftMotor.setSpeed(lSpd);
this.rightMotor.setSpeed(rSpd);
if (lSpd < 0)
this.leftMotor.backward();
else
this.leftMotor.forward();
if (rSpd < 0)
this.rightMotor.backward();
else
this.rightMotor.forward();
}
/**
* Float the two motors jointly
*/
public void setFloat() {
this.leftMotor.stop();
this.rightMotor.stop();
this.leftMotor.flt(true);
this.rightMotor.flt(true);
}
/**
* TravelTo function which takes as arguments the x and y position in cm Will travel to designated position, while
* constantly updating it's heading
*
* @param x the x coordinate destination
* @param y the y coordinate destination
* @param cutShort ???
*/
public void travelTo(double x, double y, boolean cutShort) {
double minAng;
minAng = (Math.atan2(y - odometer.getY(), x - odometer.getX())) * (180.0 / Math.PI);
if (minAng < 0)
minAng += 360.0;
double distance = Math.sqrt((x-odometer.getX())*(x-odometer.getX()) + (y-odometer.getY())*(y-odometer.getY()));
if (cutShort) {
if (distance>TILE_LENGTH/resolution) {
distance = TILE_LENGTH/resolution;
}
}
//while (distance >= this.CM_ERR) {
minAng = (Math.atan2(y - odometer.getY(), x - odometer.getX())) * (180.0 / Math.PI);
while (minAng < 0)
minAng += 360.0;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
turnTo(minAng, true);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
this.leftMotor.setSpeed(FAST);
this.rightMotor.setSpeed(FAST);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
leftMotor.rotate(convertDistance(RADIUS, distance), true);
rightMotor.rotate(convertDistance(RADIUS, distance), false);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
distance = Math.sqrt((x-odometer.getX())*(x-odometer.getX()) + (y-odometer.getY())*(y-odometer.getY()));
//}
}
/*
* TurnTo function which takes an angle and boolean as arguments The boolean controls whether or not to stop the
* motors when the turn is completed
*/
/**
* TurnTo function which takes an angle and boolean as arguments The boolean controls whether or not to stop the
* motors when the turn is completed
* @param angle the angle to turn to
* @param stop if true, the bot will stop after turning to the desired angle
*/
public void turnTo(double angle, boolean stop) {
angle = angle - odometer.getTheta();
while (angle < 0){
angle = angle + 360;
}
while (angle > 360){
angle = angle - 360;
}
leftMotor.setSpeed(SLOW);
rightMotor.setSpeed(SLOW);
if (angle <= 180){
leftMotor.rotate(-convertAngle(RADIUS, TRACK, angle), true);
rightMotor.rotate(convertAngle(RADIUS, TRACK, angle), false);
} else {
angle = 360 - angle;
leftMotor.rotate(convertAngle(RADIUS, TRACK, angle), true);
rightMotor.rotate(-convertAngle(RADIUS, TRACK, angle), false);
}
}
/**
*
* Go forward a set distance in cm
*
* @param distance the distance to travel forwards
*/
public void travelDistance(double distance) {
this.travelTo(odometer.getX() + Math.cos(Math.toRadians(this.odometer.getAng())) * distance, odometer.getY() + Math.sin(Math.toRadians(this.odometer.getAng())) * distance, false);
}
/**
* another method to go forwards without using travel to
* @param distance the distance to go forwards
*/
public void goForward(double distance) {
//Sound.beep();
this.leftMotor.setSpeed(SLOW);
this.rightMotor.setSpeed(SLOW);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
leftMotor.rotate(convertDistance(RADIUS, distance), true);
rightMotor.rotate(convertDistance(RADIUS, distance), false);
}
/**
* has the bot go backwards
* @param x the x destination
* @param y the y destination
*/
public void travelToBackwards(double x, double y) {
while((odometer.getX() > x + 2)||(odometer.getX() < x - 2)||(odometer.getY() > y + 2)||(odometer.getY() < y - 2)){
this.setSpeeds(-150, -150);
}
this.setSpeeds(0, 0);
}
/**
* stops the bot
*/
public void stop(){
this.setSpeeds(0, 0);
}
/**
* converts a distance to degree
* @param radius the wheel radius
* @param distance the distance to convert
* @return the converted angle
*/
private static int convertDistance(double radius, double distance) {
return (int) ((180.0 * distance) / (Math.PI * radius));
}
/**
* converts an angle to a distance
* @param radius the wheel radius
* @param width the track length of the bot
* @param angle the angle to convert to an arclength
* @return the distance calculated from the angle
*/
private static int convertAngle(double radius, double width, double angle) {
return convertDistance(radius, Math.PI * width * angle / 360.0);
}
/**
* sets a resolution
* @param resolution used to adjust the for loop to prevent skipping tiles
*/
public void setResolution(int resolution){
this.resolution = resolution;
}
}