距離測定の仕組み
M5Stackから信号を出力すると超音波センサから超音波が送信されます。送信してから超音波が反射してくるまでの時間を計測しています。計測した時間は超音波が往復するのにかかった時間なので、これを2分の1にして音速340m/sをかけると物体までの距離を求めることができます。
動作の流れ
ソースコードはGithubをご覧ください。 大まかな流れは以下の通りです。
- M5StackからPWMを出力してサーボを指定した角度に動かします。
- 超音波センサに信号を出力し、超音波センサから超音波が送信されます。
- 超音波が反射してくるまでの時間を計測して、距離を計算します。同時に、M5StackのLCDにスキャンしている角度の線を描画します。
- 求めた距離とサーボの角度から三角関数を用いてLCDに描画する座標を計算して表示します。
Mechanism of measurement
When M5Stack outputs a trigger signal, ultrasound sensor transmits ultrasound. M5Stack measures time until ultrasound reflects. M5Stack calculates the distance by the time and sonic.
Sequence of operation
- M5Stack controls angle of servo motor.
- M5Stack outputs a trigger signal to ultrasound sensor. Then the sensor transmits ultrasound.
- M5Stack measures time until ultrasound reflects and calculates the distance. The LCD displaies a line links angle of servo motor.
- M5Stack calculates the coordinate and draws pixels in the LCD.
すごくcoolですね!