forked from amperka/URM37
-
Notifications
You must be signed in to change notification settings - Fork 7
/
URMSerial.cpp
353 lines (279 loc) · 8.89 KB
/
URMSerial.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
URMSerial.cpp - URM 37 Control Library Version 2.0.0
Author: Miles Burton, [email protected]
Copyright (c) 2009 Miles Burton All Rights Reserved
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Ultrasound Sensor
------------------
* URM V3.2 ultrasonic sensor TTL connection with Arduino
* Reads values (0-300) from an ultrasound sensor (3m sensor)
* and writes the values to the serial->port.
* Pin12 (Arduino). Pin 1 VCC (URM V3.2)
* GND (Arduino) . Pin 2 GND (URM V3.2)
* Pin11 (Arduino) . Pin 7 (URM V3.2)
* Pin 0 (Arduino) . Pin 9 (URM V3.2)
* Pin 1 (Arduino) . Pin 8 (URM V3.2)
* www.yerobot.com
* Last modified 20/04/2009
*
* URM37 Library by Miles Burton 2009.
* Original code from Yerobot/DFRobot (thanks guys!)
Commands
0x11 + NC + NC + SUM = Temperature = 0x11 + HIGH + LOW + SUM
0x22 + DEGREE + NC+ SUM = Distance, Degree = Servo angle = 0x22 + HIGH + low + sum
0x33 + ADD + NC + SUM = Read from EEPROM = 0x33+Add+Data+Summ
0x44 + Add + DATA + SUM = Write to EEPROM = 0x44+Add+Data+Sum
*/
#include "URMSerial.h"
URMSerial::URMSerial(void)
{
}
// See whether the serial port has sufficient data for a 'reading'
bool URMSerial::hasReading(void)
{
// We require at least 3 bytes before it can be considered a valid reading
return (serial->available()>3);
}
// Read in serial data (max four bytes)
void URMSerial::readSerial(void)
{
for(int i=0;i<4;i++) serialData[i] = serial->read();
}
// Recieve distance from URM device
byte URMSerial::getMeasurement(int& value)
{
if(!hasReading()) return NOTREADY;
if(reqTimeout()) return TIMEOUT;
// Read in data
readSerial();
// Validate the reading type
switch(serialData[Header]) // Three types of command responses. 0x11 is a temperature, 0x22 Distance, 0x33 EEPROM
{
case COMMAND_DISTANCE:
return processDistance(value);
break;
case COMMAND_TEMP:
return processTemperature(value);
break;
}
// Strange return result!
distanceRequested = temperatureRequested = false; // This is a bug fix. If an error was returned (conrrupt byte array), reset distance measurements so we can re-request
value = 0;
return ERROR;
}
void URMSerial::forceReset(void)
{
distanceRequested = temperatureRequested = false; // This is a bug fix. If an error was returned (conrrupt byte array), reset distance measurements so we can re-request
}
// Process the 4 byte serial data and see whether the device reported an error (happens occationally)
bool URMSerial::hasError()
{
return((serialData[HighByte] & serialData[LowByte])==255); // If the High and Low byte are both 255: error
}
byte URMSerial::processDistance(int& value)
{
distanceRequested = false;
// If the serial data returned an error, quit
if(hasError()) return ERROR;
// 1x16bit value wrapped over two bytes
// HighByte must be multiplied by 255 to represent the correct byte
// Both are added together to calculate the value in Centimeters (I believe)
value = serialData[HighByte]*255+serialData[LowByte];
return DISTANCE;
}
byte URMSerial::processTemperature(int& value)
{
temperatureRequested = false;
// If the serial data returned an error, quit
if(hasError()) return ERROR;
// If 0, the value is the same regardless
if(serialData[LowByte]==0)
{
value = 0;
}else{
// Below 0
if(serialData[HighByte]==0x255)
{
value = -serialData[LowByte];
}else{ // above zero
value = serialData[LowByte]; // Consider below zero 1111
}
}
return TEMPERATURE;
//value = value >> 1; // Note: Bad practise, decimal value truncated
}
// Has the current request timed out?
bool URMSerial::reqTimeout()
{
if(temperatureRequested || distanceRequested)
{
int tDelta = millis() - timeRequested;
// If we've waited to long
if(tDelta>timeout || tDelta < 0) // If the delta is greater than our timeout or if it has wrapped around
{
return true;
}else{
return false;
}
}else{
// No pending requests
return true; // Return true anyway
}
}
// send the temperature request to the serial port [*note, no delay]
bool URMSerial::requestMeasurement(byte mType)
{
if(!reqTimeout()) return false;
timeRequested = millis(); // store request time
byte ttlCOMMAND[3] = {0,0,0};
// Select appropriate memory addresses
switch(mType)
{
case TEMPERATURE:
ttlCOMMAND[Header] = COMMAND_TEMP;
sendCommand(ttlCOMMAND);
temperatureRequested = true;
return true;
break;
case DISTANCE:
ttlCOMMAND[Header] = COMMAND_DISTANCE;
ttlCOMMAND[HighByte] = servoAngle;
sendCommand(ttlCOMMAND);
distanceRequested = true;
return true;
break;
default:
// Do nothing
return false;
break;
}
}
byte URMSerial::requestMeasurementOrTimeout(byte mType, int& value)
{
// Request measurement or fail
if(!requestMeasurement(mType))
{
return NOTREADY;
}
// Wait until we either timeout or we have a reading
while(!reqTimeout() && !hasReading())
{
delay(10); // Wait a bit
}
// Request has timed out, FAIL
if(reqTimeout()) return TIMEOUT;
// Pass control to getMeasurement which will return the requested measurement
return getMeasurement(value);
}
void URMSerial::sendCommand(byte* pCommand)
{
byte sum = 0;
// Send command
for(int i=0;i<3;i++)
{
serial->write(pCommand[i]);
sum += pCommand[i];
}
serial->write(sum);
}
// Setup our serial-> connection
void URMSerial::begin(uint8_t rxPin, uint8_t txPin, long baud)
{
// Initalise measurements
distanceRequested = false;
temperatureRequested = false;
servoAngle = 23; // 90*
servoMin = 0;
servoMax = 30;
timeout = 200;
// Initalise serial data
for(int i=0; i<4;i++) serialData[i]=0;
// Initalise the software serial to handle our serial communication
// Please note: using software serial over hardware-serial has some potential issues.
serial = new SoftwareSerial(rxPin, txPin);
serial->begin(baud);
}
void URMSerial::setServo(byte angle)
{
angle = map(angle, 0, 270, 0, 46); // Map decimal to URM37
angle = constrain(angle, servoMin, servoMax);
servoAngle = angle;
}
void URMSerial::setServoMax(byte val)
{
servoMax = (byte)map(val, 0, 270, 0, 46);
}
void URMSerial::setServoMin(byte val)
{
servoMin = (byte)map(val, 0, 270, 0, 46);
}
void URMSerial::setTimeout(byte val)
{
timeout = val;
}
// Send a request to the URM37
bool URMSerial::write(byte command, byte data)
{
// WARNING. VERY DANGEROUS. You can set the wrong mode
byte ttlCOMMAND[3] = {COMMAND_EEPROMWRITE,command,data};
sendCommand(ttlCOMMAND);
// Response should be immediate
while(!hasReading());
readSerial();
// Return whether the command was echoed
return ttlCOMMAND[0] == serialData[0]
&& ttlCOMMAND[1] == serialData[1]
&& ttlCOMMAND[2] == serialData[2];
}
// Read data from EEPROM
byte URMSerial::read(byte command)
{
byte ttlCOMMAND[3] = {COMMAND_EEPROMREAD,command,0};
sendCommand(ttlCOMMAND);
// Response should be immediate
while(!hasReading());
readSerial();
// Return result
return ttlCOMMAND[LowByte];
}
// Set the threshold at which the sensor will pull low
bool URMSerial::setSensorThresholdMin(byte val)
{
return write(0x00, val);
}
// Set the threshold at which the sensor will pull low
bool URMSerial::setSensorThresholdMax(byte val)
{
return write(0x01, val);
}
// Fetch the threshold value
byte URMSerial::getSensorThresholdMin()
{
return read(0x00);
}
// Fetch the threshold value
byte URMSerial::getSensorThresholdMax()
{
return read(0x01);
}
// Enable the user to set which mode the URL37 operates in. MODE_SERIAL is default (and recommended)
bool URMSerial::setMode(byte mode)
{
// Make sure we catch the right more
if(mode==MODE_SERIAL || mode==MODE_AUTO)
{
return write(0x02, mode);
}else{
return false;
}
}