-
Notifications
You must be signed in to change notification settings - Fork 0
/
hitechnic-irseeker-v2.h
453 lines (366 loc) · 14.7 KB
/
hitechnic-irseeker-v2.h
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*!@addtogroup HiTechnic
* @{
* @defgroup htirs2 IR Seeker V2
* HiTechnic IR Seeker V2
* @{
*/
/*
* $Id: hitechnic-irseeker-v2.h 123 2012-11-02 16:35:15Z xander $
*/
#ifndef __HTIRS2_H__
#define __HTIRS2_H__
/** \file hitechnic-irseeker-v2.h
* \brief HiTechnic IR Seeker V2 driver
*
* hitechnic-irseeker-v2.h provides an API for the HiTechnic IR Seeker V2.
*
* Changelog:
* - 0.1: Initial release
* - 0.2: Added SMUX functions
* - 0.3: All functions using tIntArray are now pass by reference.<br>
* HTIRS2_SMUXData removed
* - 0.4: Removed all calls to ubyteToInt()<br>
* Replaced all functions that used SPORT/MPORT macros
* - 0.5: Driver renamed to HTIRS2
*
* Credits:
* - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.
*
* License: You may use this code as you wish, provided you give credit where its due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.54 AND HIGHER.
* \author Xander Soldaat (xander_at_botbench.com)
* \date 06 April 2010
* \version 0.5
* \example hitechnic-irseeker-v2-test1.c
* \example hitechnic-irseeker-v2-test2.c
* \example hitechnic-irseeker-v2-SMUX-test1.c
*/
#pragma systemFile
#ifndef __COMMON_H__
#include "common.h"
#endif
#define HTIRS2_I2C_ADDR 0x10 /*!< IR Seeker I2C device address */
#define HTIRS2_DSP_MODE 0x41 /*!< AC DSP mode - 0 = 1200Hz, 1 = 600Hz */
#define HTIRS2_OFFSET 0x42 /*!< Offset for data registers */
#define HTIRS2_DC_DIR 0x00 /*!< DC Direction data */
#define HTIRS2_DC_SSTR1 0x01 /*!< DC Sensor 0 signal strength above average */
#define HTIRS2_DC_SSTR2 0x02 /*!< DC Sensor 1 signal strength above average */
#define HTIRS2_DC_SSTR3 0x03 /*!< DC Sensor 2 signal strength above average */
#define HTIRS2_DC_SSTR4 0x04 /*!< DC Sensor 3 signal strength above average */
#define HTIRS2_DC_SSTR5 0x05 /*!< DC Sensor 4 signal strength above average */
#define HTIRS2_DC_SAVG 0x06 /*!< DC sensor signal strength average */
#define HTIRS2_AC_DIR 0x07 /*!< DC Direction data */
#define HTIRS2_AC_SSTR1 0x08 /*!< AC Sensor 0 signal strength above average */
#define HTIRS2_AC_SSTR2 0x09 /*!< AC Sensor 1 signal strength above average */
#define HTIRS2_AC_SSTR3 0x0A /*!< AC Sensor 2 signal strength above average */
#define HTIRS2_AC_SSTR4 0x0B /*!< AC Sensor 3 signal strength above average */
#define HTIRS2_AC_SSTR5 0x0C /*!< AC Sensor 4 signal strength above average */
/*!< AC DSP modes */
typedef enum {
DSP_1200 = 0,
DSP_600 = 1
} tHTIRS2DSPMode;
// ---------------------------- DC Signal processing -----------------------------
int HTIRS2readDCDir(tSensors link);
bool HTIRS2readAllDCStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
int HTIRS2readDCAverage(tSensors link);
// ---------------------------- AC Signal processing -----------------------------
bool HTIRS2setDSPMode(tSensors link, tHTIRS2DSPMode mode);
int HTIRS2readACDir(tSensors link);
bool HTIRS2readAllACStrength(tSensors link, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5);
bool HTIRS2readEnhanced(tSensors link, int &dir, int &strength);
#ifdef __HTSMUX_SUPPORT__
// ---------------------------- DC Signal processing -----------------------------
int HTIRS2readDCDir(tMUXSensor muxsensor);
bool HTIRS2readAllDCStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5);
int HTIRS2readDCAverage(tMUXSensor muxsensor);
// ---------------------------- AC Signal processing -----------------------------
int HTIRS2readACDir(tMUXSensor muxsensor);
bool HTIRS2readAllACStrength(tMUXSensor muxsensor, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5);
tConfigParams HTIRS2_config = {HTSMUX_CHAN_I2C, 13, 0x10, 0x42}; /*!< Array to hold SMUX config data for sensor */
#endif // __HTSMUX_SUPPORT__
tByteArray HTIRS2_I2CRequest; /*!< Array to hold I2C command data */
tByteArray HTIRS2_I2CReply; /*!< Array to hold I2C reply data */
// ---------------------------- DC Signal processing -----------------------------
/**
* Read the value of the DC Direction data register and return it.
* @param link the HTIRS2 port number
* @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
*/
int HTIRS2readDCDir(tSensors link) {
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 2; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_DIR; // Start direction register
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 1))
return -1;
return HTIRS2_I2CReply[0];
}
/**
* Read the value of the DC Direction data register and return it.
* @param muxsensor the SMUX sensor port number
* @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
*/
#ifdef __HTSMUX_SUPPORT__
int HTIRS2readDCDir(tMUXSensor muxsensor) {
memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
HTSMUXconfigChannel(muxsensor, HTIRS2_config);
if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_DC_DIR)) {
return -1;
}
return HTIRS2_I2CReply[0];
}
#endif // __HTSMUX_SUPPORT__
/**
* Read the value of the all of the internal DC sensors above average.
* @param link the HTIRS2 port number
* @param dcS1 data from internal sensor nr 1
* @param dcS2 data from internal sensor nr 2
* @param dcS3 data from internal sensor nr 3
* @param dcS4 data from internal sensor nr 4
* @param dcS5 data from internal sensor nr 5
* @return true if no error occured, false if it did
*/
bool HTIRS2readAllDCStrength(tSensors link, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 2; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SSTR1; // Sensor 0 signal strength
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 5))
return false;
//if (!readI2C(link, HTIRS2_I2CReply, 5))
// return false;
dcS1 = HTIRS2_I2CReply[0];
dcS2 = HTIRS2_I2CReply[1];
dcS3 = HTIRS2_I2CReply[2];
dcS4 = HTIRS2_I2CReply[3];
dcS5 = HTIRS2_I2CReply[4];
return true;
}
/**
* Read the value of the all of the internal DC sensors above average.
* @param muxsensor the SMUX sensor port number
* @param dcS1 data from internal sensor nr 1
* @param dcS2 data from internal sensor nr 2
* @param dcS3 data from internal sensor nr 3
* @param dcS4 data from internal sensor nr 4
* @param dcS5 data from internal sensor nr 5
* @return true if no error occured, false if it did
*/
#ifdef __HTSMUX_SUPPORT__
bool HTIRS2readAllDCStrength(tMUXSensor muxsensor, int &dcS1, int &dcS2, int &dcS3, int &dcS4, int &dcS5) {
memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
HTSMUXconfigChannel(muxsensor, HTIRS2_config);
if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 5, HTIRS2_DC_SSTR1)) {
return false;
}
dcS1 = HTIRS2_I2CReply[0];
dcS2 = HTIRS2_I2CReply[1];
dcS3 = HTIRS2_I2CReply[2];
dcS4 = HTIRS2_I2CReply[3];
dcS5 = HTIRS2_I2CReply[4];
return true;
}
#endif // __HTSMUX_SUPPORT__
/**
* Read the value of the average data register and return it.
* @param link the HTIRS2 port number
* @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
*/
int HTIRS2readDCAverage(tSensors link) {
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 2; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SAVG; // DC sensor signal strength average
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 1))
return -1;
return HTIRS2_I2CReply[0];
}
/**
* Read the value of the average data register and return it.
* @param muxsensor the SMUX sensor port number
* @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
*/
#ifdef __HTSMUX_SUPPORT__
int HTIRS2readDCAverage(tMUXSensor muxsensor) {
memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
HTSMUXconfigChannel(muxsensor, HTIRS2_config);
if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_DC_SAVG)) {
return -1;
}
return HTIRS2_I2CReply[0];
}
#endif // __HTSMUX_SUPPORT__
// ---------------------------- AC Signal processing -----------------------------
/**
* Set the DSP mode of the AC carrier wave detector.
*
* Mode is one of:
* -DSP_1200
* -DSP_600
* @param link the HTIRS2 port number
* @param mode the frequency that should be detected
* @return true if no error occured, false if it did
*/
bool HTIRS2setDSPMode(tSensors link, tHTIRS2DSPMode mode) {
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 3; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_DSP_MODE; // Start direction register
HTIRS2_I2CRequest[3] = (ubyte)mode;
return writeI2C(link, HTIRS2_I2CRequest);
}
/**
* Read the value of the AC Direction data register and return it.
* @param link the HTIRS2 port number
* @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
*/
int HTIRS2readACDir(tSensors link) {
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 2; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_DIR; // Start direction register
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 1))
return -1;
return HTIRS2_I2CReply[0];
}
/**
* Read the value of the AC Direction data register and return it.
* @param muxsensor the SMUX sensor port number
* @return value of 0-9, the direction index of the detected IR signal or -1 if an error occurred.
*/
#ifdef __HTSMUX_SUPPORT__
int HTIRS2readACDir(tMUXSensor muxsensor) {
memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
HTSMUXconfigChannel(muxsensor, HTIRS2_config);
if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 1, HTIRS2_AC_DIR)) {
return -1;
}
return HTIRS2_I2CReply[0];
}
#endif // __HTSMUX_SUPPORT__
/**
* Read the value of the all of the internal AC sensors and copy into specified buffer.
* @param link the HTIRS2 port number
* @param acS1 data from internal sensor nr 1
* @param acS2 data from internal sensor nr 2
* @param acS3 data from internal sensor nr 3
* @param acS4 data from internal sensor nr 4
* @param acS5 data from internal sensor nr 5
* @return true if no error occured, false if it did
*/
bool HTIRS2readAllACStrength(tSensors link, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5) {
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 2; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_SSTR1; // Sensor 0 signal strength
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 5))
return false;
acS1 = HTIRS2_I2CReply[0];
acS2 = HTIRS2_I2CReply[1];
acS3 = HTIRS2_I2CReply[2];
acS4 = HTIRS2_I2CReply[3];
acS5 = HTIRS2_I2CReply[4];
return true;
}
/**
* Read the value of the all of the internal AC sensors and copy into specified buffer.
* @param muxsensor the SMUX sensor port number
* @param acS1 data from internal sensor nr 1
* @param acS2 data from internal sensor nr 2
* @param acS3 data from internal sensor nr 3
* @param acS4 data from internal sensor nr 4
* @param acS5 data from internal sensor nr 5
* @return true if no error occured, false if it did
*/
#ifdef __HTSMUX_SUPPORT__
bool HTIRS2readAllACStrength(tMUXSensor muxsensor, int &acS1, int &acS2, int &acS3, int &acS4, int &acS5) {
memset(HTIRS2_I2CReply, 0, sizeof(tByteArray));
if (HTSMUXSensorTypes[muxsensor] != HTSMUXSensorCustom)
HTSMUXconfigChannel(muxsensor, HTIRS2_config);
if (!HTSMUXreadPort(muxsensor, HTIRS2_I2CReply, 5, HTIRS2_AC_SSTR1)) {
return false;
}
acS1 = HTIRS2_I2CReply[0];
acS2 = HTIRS2_I2CReply[1];
acS3 = HTIRS2_I2CReply[2];
acS4 = HTIRS2_I2CReply[3];
acS5 = HTIRS2_I2CReply[4];
return true;
}
#endif // __HTSMUX_SUPPORT__
/**
* This function calculates the strength and direction based on both the DC and AC
* signal strengths.
* @param link the HTIRS2 port number
* @param dir direction where the ball is detected, value of 0-9 (0 when no ball is detected)
* @param strength the strength (and distance) of the ball's IR signal
* @return true if no error occured, false if it did
*/
bool HTIRS2readEnhanced(tSensors link, int &dir, int &strength)
{
ubyte iMax = 0;
long dcSigSum = 0;
// Read DC mode
memset(HTIRS2_I2CRequest, 0, sizeof(tByteArray));
HTIRS2_I2CRequest[0] = 2; // Message size
HTIRS2_I2CRequest[1] = HTIRS2_I2C_ADDR; // I2C Address
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_DC_SSTR1; // Sensor 0 signal strength
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 6))
return false;
// Find the max DC sig strength
for (int i = 1; i < 5; i++)
{
if (HTIRS2_I2CReply[i] > HTIRS2_I2CReply[iMax])
{
iMax = i;
}
}
// Calc base DC direction value
dir = iMax * 2 + 1;
// Set base dcStrength based on max signal and average
dcSigSum = HTIRS2_I2CReply[iMax] + HTIRS2_I2CReply[5];
// Check signal strength of neighbouring sensor elements
if ((iMax > 0) && (HTIRS2_I2CReply[iMax - 1] > (ubyte)(HTIRS2_I2CReply[iMax] / 2)))
{
dir--;
dcSigSum += HTIRS2_I2CReply[iMax - 1];
}
if ((iMax < 4) && (HTIRS2_I2CReply[iMax + 1] > (ubyte)(HTIRS2_I2CReply[iMax] / 2)))
{
dir++;
dcSigSum += HTIRS2_I2CReply[iMax + 1];
}
// Make DC strength compatible with AC strength.
// use: sqrt(dcSigSum*500)
strength = sqrt(dcSigSum * 500);
// Decide if using AC strength
if (strength <= 200)
{
writeDebugStreamLine("switching to AC");
// Use AC Dir
HTIRS2_I2CRequest[2] = HTIRS2_OFFSET + HTIRS2_AC_DIR; // Recycle rest of cmdBuf
if (!writeI2C(link, HTIRS2_I2CRequest, HTIRS2_I2CReply, 6))
return false;
dir = HTIRS2_I2CReply[0];
// Sum the sensor elements to get strength
if (dir > 0)
{
strength = HTIRS2_I2CReply[1] + HTIRS2_I2CReply[2] +
HTIRS2_I2CReply[3] + HTIRS2_I2CReply[4] +
HTIRS2_I2CReply[5];
}
}
return true;
}
#endif // __HTIRS2_H__
/*
* $Id: hitechnic-irseeker-v2.h 123 2012-11-02 16:35:15Z xander $
*/
/* @} */
/* @} */