forked from iNavFlight/INAV-Rangefinder-I2C-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inav_i2c_sonar.ino
198 lines (157 loc) · 3.63 KB
/
inav_i2c_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
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
/*
* Set I2C Slave address
*/
#define I2C_SLAVE_ADDRESS 0x14
#define MAX_READOUT_TIME 5 //in ms
#define PULSE_TO_CM 58
#define MAX_RANGE 400 //Range of 4 meters
#define PULSE_TIMEOUT (MAX_RANGE * PULSE_TO_CM) //this is an equivalent of 10 meters range
#define DEBUG;
#define TRIGGER_PIN 3
#define ECHO_PIN 4
#define LED_PIN 1
#define STATUS_OK 0
#define STATUS_OUT_OF_RANGE 1
#include <TinyWireS.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
volatile uint8_t i2c_regs[] =
{
0, //status
0, //older 8 of distance
0, //younger 8 of distance
};
const byte reg_size = sizeof(i2c_regs);
/*
* 0=16ms
* 1=32ms
* 2=64ms
* 3=128ms
* 4=250ms
* 5=500ms
* 6=1 sec,
* 7=2 sec,
* 8=4 sec,
* 9= 8sec
*/
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
volatile uint8_t wakeCounter = 0;
/*
* Watchdog Interrupt Service is executed when watchdog timed out
*/
ISR(WDT_vect) {
wakeCounter++;
}
volatile unsigned long lastRequestMillis;
volatile byte reg_position = 0;
/**
* This function is executed when there is a request to read sensor
* To get data, 2 reads of 8 bits are required
* First requests send 8 older bits of 16bit unsigned int
* Second request send 8 lower bytes
* Measurement is executed when request for first batch of data is requested
*/
void requestEvent()
{
// if (millis() - lastRequestMillis > MAX_READOUT_TIME || reg_position >= reg_size) {
// reg_position = 0;
// }
TinyWireS.send(i2c_regs[reg_position]);
// reg_position++;
lastRequestMillis = millis();
}
void receiveEvent(uint8_t howMany) {
if (howMany < 1) {
// Sanity-check
return;
}
if (howMany > TWI_RX_BUFFER_SIZE)
{
// Also insane number
return;
}
reg_position = TinyWireS.receive();
howMany--;
if (!howMany) {
// This write was only to set the buffer for next read
return;
}
while(howMany--) {
TinyWireS.receive();
// i2c_regs[reg_position] = TinyWireS.receive();
// reg_position++;
// if (reg_position >= reg_size)
// {
// reg_position = 0;
//
// }
}
}
void setup() {
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
/*
* Setup I2C
*/
TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onRequest(requestEvent);
TinyWireS.onReceive(receiveEvent);
/*
* Start watchdog timer
*/
setup_watchdog(0);
}
long microsecondsToCentimeters(long microseconds){
return microseconds / PULSE_TO_CM;
}
void loop() {
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_mode();
sleep_disable();
/*
* Measurement is done every 3rd wakeup, that gives more less 20Hz update rate (48ms)
*/
if (wakeCounter == 3) {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, PULSE_TIMEOUT);
if (duration > 0) {
i2c_regs[0] = STATUS_OK;
} else {
i2c_regs[0] = STATUS_OUT_OF_RANGE;
}
uint16_t cm = (uint16_t) microsecondsToCentimeters(duration);
#ifdef DEBUG
if (cm <10) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
#endif
i2c_regs[1] = cm >> 8;
i2c_regs[2] = cm & 0xFF;
wakeCounter = 0;
}
}