アマゾンなどで安く売られていたので思わずポチッちゃったけど出力信号が微弱なためオペアンプで増幅する必要があったりノイズが比較的多かったりして想定外に扱いが難しい。
公開されていたオペアンプ回路(数万倍)を試してみたらコンパレータによりデジタル信号に変換しようとしても出力が安定しなくて感度を高くするために際どいレベルで閾値を設定するほど誤動作が激しくなり使い物にならなくなってしまう。
もっと簡単な回路でと思いついたのはゲイン・アンプ内蔵のADCを使うという方法。以前に調べていて入手も容易なADS1115でリファレンス電圧+-256mVを選択すれば約8uV単位で計測できるので試してみる価値ありとやってみたら検出はできるもののノイズ等の影響により不安定っぽくてやはりうまくいかない...
ふとFFTという言葉を思い出した。まだ使ってみたこともないのだが公開されているライブラリもあるしドップラーセンサーは周波数出力だしFFTは周波数検出だから相性ばっちりなのかなと試してみたら検出はできるもののやはりセンサー出力のドリフトやノイズ等の影響を受けてしまい感度を上げようとするといまいち判定が難しい。そこでFFTの結果をローパスフィルターにかけて平均化した値とFFTの結果との差分によるレベル判定を試してみたところなんとなくいい感じになった。ローパスフィルターのおかげでセンサー出力のドリフトは自動調整されるのであとはノイズの問題のみだが、なぜかサンプリング数が128のときが感度とノイズが両立できて一番良いようだ。
実験では10m程度離れても真正面のゆっくり移動する人間なら辛うじて検出できたけど遠くになるに従って検出可能な移動速度が遅くなるような気がするのはなぜだろう...
ちなみに【だるまさんがころんだ】で検出されないよう移動するのは至難の業かも。(笑)
【修正】
2024-01-29
複数のローパスフィルターを使いノイズの影響が少なくなるように改良。かなり安定するようになったが感度は落ちたかも...このへんは要調整かな。
【サンプル・スケッチ (XAIO ESP32C3)】
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 |
#include <Wire.h> #include <arduinoFFT.h> #include "ads1115.h" #define LED 8 #define PEEK_THRESHOLD 30 #define SAMPLE_FREQ 860 #define SAMPLE_COUNT 128 double vReal[SAMPLE_COUNT]; double vImag[SAMPLE_COUNT]; arduinoFFT fft(vReal, vImag, SAMPLE_COUNT, SAMPLE_FREQ); ADS1115<> adc; void led_off(void) { digitalWrite(LED, HIGH); } void led_blink(void) { digitalWrite(LED, !digitalRead(LED)); } void setup(void) { /* Setup LED Port */ pinMode(LED, OUTPUT); /* Setup I2C (for ADC) */ Wire.setClock(400000); Wire.begin(); /* Setup ADC */ adc.mux(ADS1115_MUX_AIN0_GND); adc.pga(ADS1115_PGA_0256V); adc.dr(ADS1115_DR_860SPS); adc.begin(true); } void loop(void) { static uint count; static double f, v, s, b, d; switch (adc.handle()) { case ADS1115_STATUS_READY: vReal[count] = adc.result(); vImag[count] = 0; if (++count < SAMPLE_COUNT) break; count = 0; fft.Windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD); fft.Compute(FFT_FORWARD); fft.ComplexToMagnitude(); fft.MajorPeak(&f, &v); b += (v - b) * 5 / 100; s += (v - s) * 50 / 100; d += (s - b - d) * 50 / 100; if (abs(d) <= PEEK_THRESHOLD) led_off(); else led_blink(); break; default: break; } } |
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 |
/* ADS1115.h - ADC Library for ADS1115 Copyright (c) 2020 Sasapea's Lab. All right 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef __ADS1115_H #define __ADS1115_H #include <stdint.h> #include <stdbool.h> #ifdef ARDUINO #include <Arduino.h> #else #define makeWord(h, l) (((uint16_t)(h) << 8) | (uint8_t)(l)) #define lowByte(w) (uint8_t)(w) #define highByte(w) (uint8_t)((w) >> 8) #endif #define ADS1115_RETRY_INTERVAL 1000000 #define ADS1115_DEVICE_ADDRESS 0x48 #define ADS1115_RESOLUSION 16 typedef enum { ADS1115_STATUS_NO_DEVICE, ADS1115_STATUS_IDLE, ADS1115_STATUS_BUSY, ADS1115_STATUS_READY, } ADS1115_STATUS; typedef enum { ADS1115_REG_CONVERSION, ADS1115_REG_CONFIG, ADS1115_REG_LO_THRESH, ADS1115_REG_HI_THRESH, } ADS1115_REG; typedef enum { ADS1115_OS_NONE, ADS1115_OS_CONVERSION, } ADS1115_OS; typedef enum { ADS1115_MUX_AIN0_AIN1, ADS1115_MUX_AIN0_AIN3, ADS1115_MUX_AIN1_AIN3, ADS1115_MUX_AIN2_AIN3, ADS1115_MUX_AIN0_GND, ADS1115_MUX_AIN1_GND, ADS1115_MUX_AIN2_GND, ADS1115_MUX_AIN3_GND, } ADS1115_MUX; typedef enum { ADS1115_PGA_6144V, ADS1115_PGA_4096V, ADS1115_PGA_2048V, ADS1115_PGA_1024V, ADS1115_PGA_0512V, ADS1115_PGA_0256V, } ADS1115_PGA; #define ADS1115_REFERENCES \ { 6144000, 4096000, 2048000, 1024000, 512000, 256000 } typedef enum { ADS1115_MODE_CONTINUOUS, ADS1115_MODE_POWER_DOWN, } ADS1115_MODE; typedef enum { ADS1115_DR_8SPS, ADS1115_DR_16SPS, ADS1115_DR_32SPS, ADS1115_DR_64SPS, ADS1115_DR_128SPS, ADS1115_DR_250SPS, ADS1115_DR_475SPS, ADS1115_DR_860SPS, } ADS1115_DR; #define ADS1115_CONVERSION_TIMES \ { 1000007 / 8, 1000015 / 16, 1000031 / 32, 1000063 / 64, \ 1000127 / 128, 1000249 / 250, 1000474 / 475, 1000859 / 860 } typedef enum { ADS1115_COMP_MODE_TRADITIONAL, ADS1115_COMP_MODE_WINDOW, } ADS1115_COMP_MODE; typedef enum { ADS1115_COMP_POL_ACTIVE_LOW, ADS1115_COMP_POL_ACTIVE_HIGH, } ADS1115_COMP_POL; typedef enum { ADS1115_COMP_LAT_DISABLE, ADS1115_COMP_LAT_ENABLE, } ADS1115_COMP_LAT; typedef enum { ADS1115_COMP_QUE_ONE, ADS1115_COMP_QUE_TWO, ADS1115_COMP_QUE_FOUR, ADS1115_COMP_QUE_DISABLE, } ADS1115_COMP_QUE; typedef union { struct { ADS1115_COMP_QUE comp_que :2; ADS1115_COMP_LAT comp_lat :1; ADS1115_COMP_POL comp_pol :1; ADS1115_COMP_MODE comp_mode:1; ADS1115_DR dr :3; ADS1115_MODE mode :1; ADS1115_PGA pga :3; ADS1115_MUX mux :3; ADS1115_OS os :1; } reg; uint16_t val; } ADS1115_CONFIG; template<typename WIRE = TwoWire, WIRE& WOBJ = Wire> class ADS1115 { private: uint8_t _addr; int32_t _value; unsigned long _start; ADS1115_CONFIG _config; ADS1115_CONFIG _active; ADS1115_STATUS _status; uint8_t write_reg(ADS1115_REG reg, uint16_t data) { WOBJ.beginTransmission(_addr); WOBJ.write(reg); WOBJ.write(highByte(data)); WOBJ.write(lowByte(data)); return WOBJ.endTransmission(); } uint8_t read_reg(ADS1115_REG reg, uint16_t *data) { WOBJ.beginTransmission(_addr); WOBJ.write(reg); uint8_t rv = WOBJ.endTransmission(false); if (rv == 0) { if (WOBJ.requestFrom(_addr, sizeof(*data)) == sizeof(*data)) { uint8_t high = WOBJ.read(); uint8_t low = WOBJ.read(); *data = makeWord(high, low); } else rv = 4; } return rv; } ADS1115_STATUS start(void) { uint8_t rv = write_reg(ADS1115_REG_CONFIG, _config.val); if (rv) _status = ADS1115_STATUS_NO_DEVICE; else { _status = ADS1115_STATUS_BUSY; _active = _config; } _start = micros(); return _status; } ADS1115_STATUS fetch(void) { uint16_t val; _start = micros(); uint8_t rv = read_reg(ADS1115_REG_CONVERSION, &val); if (rv) return _status = ADS1115_STATUS_NO_DEVICE; if (_active.reg.mode == ADS1115_MODE_POWER_DOWN) _status = ADS1115_STATUS_IDLE; else _status = ADS1115_STATUS_BUSY; if (_active.reg.mux < ADS1115_MUX_AIN0_GND) _value = (int16_t)val; else _value = val; return ADS1115_STATUS_READY; } uint32_t conversion_time(void) { static const uint32_t CONVERSION_TIMES[] = ADS1115_CONVERSION_TIMES; return CONVERSION_TIMES[_active.reg.dr]; } public: ADS1115(uint8_t addr = ADS1115_DEVICE_ADDRESS) : _addr(addr) , _value(0) , _start(0) , _status(ADS1115_STATUS_NO_DEVICE) { const ADS1115_CONFIG _DEFAULT = { ADS1115_COMP_QUE_DISABLE, ADS1115_COMP_LAT_DISABLE, ADS1115_COMP_POL_ACTIVE_LOW, ADS1115_COMP_MODE_TRADITIONAL, ADS1115_DR_128SPS, ADS1115_MODE_POWER_DOWN, ADS1115_PGA_2048V, ADS1115_MUX_AIN0_AIN1, ADS1115_OS_CONVERSION, }; _config = _active = _DEFAULT; } void mux(ADS1115_MUX value) { _config.reg.mux = value; } void pga(ADS1115_PGA value) { _config.reg.pga = value; } void dr(ADS1115_DR value) { _config.reg.dr = value; } void comp_mode(ADS1115_COMP_MODE value) { _config.reg.comp_mode = value; } void comp_pol(ADS1115_COMP_POL value) { _config.reg.comp_pol = value; } void comp_lat(ADS1115_COMP_LAT value) { _config.reg.comp_lat = value; } void comp_que(ADS1115_COMP_QUE value) { _config.reg.comp_que = value; } uint8_t low_threshold(uint16_t value) { return write_reg(ADS1115_REG_LO_THRESH, value); } uint8_t high_threshold(uint16_t value) { return write_reg(ADS1115_REG_HI_THRESH, value); } uint8_t resolution(void) { return ADS1115_RESOLUSION; } uint32_t reference(void) { static const uint32_t REFERENCES[] = ADS1115_REFERENCES; return REFERENCES[_active.reg.pga]; } int32_t result(void) { return _value; } int32_t voltage(void) { return (int32_t)((int64_t)result() * reference() / 32768); } void begin(bool continuous = false) { if (!continuous) _start = micros() - ADS1115_RETRY_INTERVAL; else { _config.reg.mode = ADS1115_MODE_CONTINUOUS; _config.reg.os = ADS1115_OS_NONE; start(); } } bool restart(void) { if ((_config.val == _active.val) || (_config.reg.mode == ADS1115_MODE_POWER_DOWN)) return true; unsigned long t = (_status == ADS1115_STATUS_BUSY ? conversion_time() : 0); if (!start()) return false; if (t) { delayMicroseconds(t); _start = micros(); } return true; } bool conversion(void) { unsigned long t = 0; if (_status == ADS1115_STATUS_BUSY) t = micros() - _start; else if (start() == ADS1115_STATUS_NO_DEVICE) return false; if (t < conversion_time()) delayMicroseconds(conversion_time() - t); return fetch() == ADS1115_STATUS_READY; } ADS1115_STATUS handle(void) { unsigned long now = micros(); switch (_status) { default: case ADS1115_STATUS_NO_DEVICE: if (now - _start < ADS1115_RETRY_INTERVAL) break; /* Falls through. */ case ADS1115_STATUS_IDLE: start(); break; case ADS1115_STATUS_BUSY: if (now - _start < conversion_time()) break; return fetch(); } return _status; } }; #endif |