-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHummingbird.java
More file actions
285 lines (252 loc) · 10.8 KB
/
Copy pathHummingbird.java
File metadata and controls
285 lines (252 loc) · 10.8 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* This class extends the Microbit class to incorporate functions to control the inputs and outputs
* of the Hummingbird Bit. It includes methods to set the values of motors and LEDs, as well
* as methods to read the values of the sensors.
*
* Mike Yuan and Bambi Brewer, BirdBrain Technologies LLC
* November 2018
*/
public class Hummingbird extends Robot {
/**
* Default constructor for the library. Set the default device to be A.
*/
public Hummingbird() {
deviceInstance = "A";
if (!isConnectionValid()) System.exit(0);
if (!isHummingbird()) System.exit(0);
}
/**
* General constructor for the library. Set the device to be "A", "B", or "C".
*
* @param device The letter corresponding to the Hummingbird device, which much be "A", "B", or "C".
* The letter that identifies the Hummingbird device is assigned by the BlueBird Connector.
* */
public Hummingbird(String device) {
if (!((device == "A")||(device == "B")||(device == "C"))) {
System.out.println("Error: Device must be A, B, or C.");
System.exit(0);
} else {
deviceInstance = device;
if (!isConnectionValid()) System.exit(0);
if (!isHummingbird()) System.exit(0);
}
}
/** This function tries to read sensor 4 (the Hummingbird battery) to determine whether or not
* the device is a Hummingbird.
*/
private boolean isHummingbird() {
StringBuilder newURL = new StringBuilder(baseUrl);
String testURL = (newURL.append("in/isHummingbird/static/")
.append(deviceInstance)).toString();
String stringResponse = sendHttpRequest(testURL);
if (stringResponse.equals("false")) {
System.out.println("Error: Device " + deviceInstance + " is not a Hummingbird");
return false;
} else if (stringResponse.equals("Not Connected")) {
System.out.println("Error: Device " + deviceInstance + " is not connected.");
return false;
} else {
return true;
}
/*
try {
StringBuilder newURL = new StringBuilder(baseUrl);
String testURL = (newURL.append("in/")
.append("sensor/4/")
.append(deviceInstance)).toString();
requestUrl = new URL(testURL);
connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
String stringResponse = verifyResponse();
if (stringResponse.equals("255")) {
System.out.println("Error: Device "+deviceInstance+" is not a Hummingbird");
return false;
}
else {
return true;
}
} catch (IOException e) {
System.out.println("Error: Device " + deviceInstance + " is not connected");
return false;
}
*/
}
/* This function checks whether a port is within the given bounds. It returns a boolean value
that is either true or false and prints an error if necessary. */
protected boolean isPortValid(int port, int portMax) {
if ((port < 1) || (port > portMax)) {
System.out.println("Error: Please choose a port value between 1 and " + portMax);
return false;
}
else
return true;
}
/**
* setPositionServo sets the positionServo at a given port to a specific angle.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the position servo is attached to. (Range: 1-4)
* @param position The angle of the position servo. (Range: 0-180)
*/
public void setPositionServo(int port, int angle) {
if (!isPortValid(port,4)) { // Check that port is valid
return;
}
angle = clampParameterToBounds(angle, 0, 180);
int degrees = (int) (angle * 254.0 / 180.0);
StringBuilder resultUrl = new StringBuilder(baseUrl);
String servoUrl = (resultUrl.append("out/")
.append("servo/")
.append(Integer.toString(port) + "/")
.append(Integer.toString(degrees) + "/")
.append(deviceInstance)).toString();
httpRequestOut(servoUrl);
}
/**
* setRotationServo sets the rotationServo at a given port to a specific speed.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the rotation servo is attached to. (Range: 1-4)
* @param speed The speed of the rotation servo. (Range: -100-100)
*/
public void setRotationServo(int port, int speed) {
if (!isPortValid(port,4)) { // Check that port is valid
return;
}
speed = clampParameterToBounds(speed,-100,100);
if ((speed > -10) && (speed < 10)) { // dead zone to turn off the motor
speed = 255;
} else { // Scale the speed so that it is semi-linear
speed = ((speed * 23) / 100) + 122;
}
// Create the http command
StringBuilder resultUrl = new StringBuilder(baseUrl);
String rotationUrl = (resultUrl.append("out/")
.append("rotation/")
.append(Integer.toString(port) + "/")
.append(Integer.toString(speed) + "/")
.append(deviceInstance)).toString();
httpRequestOut(rotationUrl);
}
/**
* setLED sets the LED at a given port to a specific light intensity.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the LED is attached to. (Range: 1-4)
* @param intensity The intensity of the LED. (Range: 0-100)
*/
public void setLED(int port, int intensity) {
if (!isPortValid(port,3)) { // Check that port is valid
return;
}
intensity = clampParameterToBounds(intensity,0,100);
// Scale and send http request
intensity = (int) (intensity * 255.0 / 100.0);
StringBuilder resultUrl = new StringBuilder(baseUrl);
String ledUrl = (resultUrl.append("out/")
.append("led/")
.append(Integer.toString(port) + "/")
.append(Integer.toString(intensity) + "/")
.append(deviceInstance)).toString();
httpRequestOut(ledUrl);
}
/**
* setTriLED sets the triLED at a given port to a specific color.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the LED is attached to. (Range: 1-4)
* @param redIntensity The intensity of red light of the triLED. (Range: 0-100)
* @param greenIntensity The intensity of green light of the triLED. (Range: 0-100)
* @param blueIntensity The intensity of blue light of the triLED. (Range: 0-100)
*/
public void setTriLED(int port, int redIntensity, int greenIntensity, int blueIntensity) {
if (!isPortValid(port,2)) { // Check that port is valid
return;
}
redIntensity = clampParameterToBounds(redIntensity,0,100);
greenIntensity = clampParameterToBounds(greenIntensity,0,100);
blueIntensity = clampParameterToBounds(blueIntensity,0,100);
// Scale
redIntensity = (int) (redIntensity * 255.0 / 100.0);
greenIntensity = (int) (greenIntensity * 255.0 / 100.0);
blueIntensity = (int) (blueIntensity * 255.0 / 100.0);
// Build http request
StringBuilder resultUrl = new StringBuilder(baseUrl);
String triLedUrl = (resultUrl.append("out/")
.append("triled/")
.append(Integer.toString(port) + "/")
.append(Integer.toString(redIntensity) + "/")
.append(Integer.toString(greenIntensity) + "/")
.append(Integer.toString(blueIntensity) + "/")
.append(deviceInstance)).toString();
httpRequestOut(triLedUrl);
}
/**
* getSensorValue returns the raw sensor value at a given port
*
* @param port The port that the sensor is attached to. (Range: 1-3)
*/
private int getSensorValue(int port) {
if (!isPortValid(port,3)) { // Check that port is valid
return -1;
}
StringBuilder resultUrl = new StringBuilder(baseUrl);
String sensorUrl = (resultUrl.append("in/")
.append("sensor/")
.append(Integer.toString(port) + "/")
.append(deviceInstance)).toString();
return (int) httpRequestInDouble(sensorUrl);
}
/**
* getLight returns light sensor value at a given port after processing the raw sensor value retrieved.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the light sensor is attached to. (Range: 1-3)
*/
public int getLight(int port) {
int sensorResponse = getSensorValue(port);
return (int) (sensorResponse * 100.0 / 255.0);
}
/**
* getSound returns sound sensor value at a given port after processing the raw sensor value retrieved.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the sound sensor is attached to. (Range: 1-3)
*/
public int getSound(int port) {
int sensorResponse = getSensorValue(port);
return (int) (sensorResponse * 200.0 / 255.0);
}
/**
* getDistance returns distance sensor value at a given port after processing the raw sensor value retrieved.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the distance sensor is attached to. (Range: 1-3)
*/
public int getDistance(int port) {
int sensorResponse = getSensorValue(port);
return (int) (sensorResponse * 117.0 / 100.0);
}
/**
* getDial returns dial value at a given port after processing the raw sensor value retrieved.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the dial is attached to. (Range: 1-4)
*/
public int getDial(int port) {
int sensorResponse = getSensorValue(port);
int processedResponse = (int) (sensorResponse * 100.0 / 230.0);
return processedResponse >= 100 ? 100 : processedResponse;
}
/**
* getDial returns dial value at a given port after processing the raw sensor value retrieved.
* The function shows a warning dialog if the inputs are not in the specified range.
*
* @param port The port that the dial is attached to. (Range: 1-4)
*/
public double getVoltage(int port) {
int sensorResponse = getSensorValue(port);
return (sensorResponse * 3.3 / 255);
}
}