-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sonar.ino
90 lines (74 loc) · 1.95 KB
/
Sonar.ino
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
#define sonar_time 100
#define rangedelta 50
const byte sonarAddress[] = {0x70, 0x71}; // page 4 of datasheet
int rangea=0;
int rangeb=745;
void sonar_init(void)
{
tv.set_vbi_hook(&sonar_int); //https://nootropicdesign.com/forum/viewtopic.php?f=25&t=2394&p=2648&hilit=I2C#p2648
}
void sonar_int(void)
{
static unsigned char sonar_num=0;
static unsigned long prev_time;
int tmprange;
unsigned long cur_time=millis();
if(sonar_num==0) //first time
{
sonar_num=1;
prev_time=cur_time;
interrupts();
sendByteI2C(sonarAddress[0], 0x51);
return;
}
if(cur_time-prev_time<sonar_time)
return;
prev_time=cur_time;
interrupts();
if(sonar_num==1)
{
tmprange=readWordWaitI2C(sonarAddress[0],1)-20;
if(abs(tmprange-rangea)<rangedelta) //whether to trust the range readings
{
rangea=tmprange;
imu_z=((long int)tmprange)<<15;
Serial.print("Range A = ");
Serial.println(rangea);
Serial2.write(2+sizeof(fixed));
Serial2.write("RU");
Serial2.write((unsigned char *)&imu_z, sizeof(fixed));
Serial2.write("\n\n\n");
}
else
{
Serial.print("Range A ignored (");
Serial.print(tmprange);
Serial.println(")");
}
sendByteI2C(sonarAddress[1], 0x51);
sonar_num=2;
}
else
{
tmprange=readWordWaitI2C(sonarAddress[1],1)-20;
if(abs(tmprange-rangeb)<rangedelta) //whether to trust the range readings
{
rangeb=tmprange;
wall=((long int)tmprange)<<15;
Serial2.write(2+sizeof(fixed));
Serial2.write("WU");
Serial2.write((unsigned char *)&wall, sizeof(fixed));
Serial2.write("\n\n\n");
}
sendByteI2C(sonarAddress[0], 0x51);
sonar_num=1;
}
}
void sonar_address_change(void) //only call it once and with only one sensor connected
{
Wire.beginTransmission(sonarAddress[0]);
Wire.write(0xAA);
Wire.write(0xA5);
Wire.write(sonarAddress[1]<<1);
Wire.endTransmission();
}