2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

9軸センサー使って移動距離を測る

Posted at

#概要
BMX055という9軸センサーを使って値をとって、加速度から速度を計算して移動距離を取ろうとしてみました。
BMX055から加速度センサーを取得する際に参考にしたサイトこちらです。
https://labo-kajisaki.blogspot.com/2019/03/2169.html?m=1

#使用ソフト

  • uPyCraft

#移動距離計算

import urequests
import ujson
from machine import Pin, I2C
from bma2x2 import BMA2X2
from bmg160 import BMG160
from bmm050 import BMM050
import time

i2cBMX055 = I2C(scl=Pin(22), sda=Pin(21))

print('I2C setup')

accl=BMA2X2(i2cBMX055, 0x19)
gyro=BMG160(i2cBMX055, 0x69)
mag=BMM050(i2cBMX055, 0x13)

t = 0.064
count = 1
vx2 = 0
distx = 0
disty = 0
count_result = 0

def vx(ax):
  conv_ax = convx(ax)
  vx = conv_ax*t
  return vx

def convx(ax):
  return ax * 0.00981

def vy(ay):
  conv_ay = convy(ay)
  vy = conv_ay*t
  return vy

def convy(ay):
  return ay * 0.00981


while count <= 300:
  a = BMA2X2.xyz(accl)
  ax = a[0]
  ay = a[1]
  az = a[2]

#X軸方向
  # G ➡ 加速度
  conv_ax = convx(ax)
  # 加速度➡速度(一回目のループ)
  if count_result == 0:
    vx2 = conv_ax * t

    #速度➡距離(初速なし)
    dist2x = conv_ax * t ** 2 / 2

  # 加速度➡速度(初速あり(2回目以降))
  if count_result == 1:
    vx2 = vx2 + conv_ax * t

    #速度➡距離(初速あり(2回目以降))
    dist2_1x = vx2 * t
    dist2_2x = conv_ax * t ** 2 / 2
    dist2x = dist2_1x + dist2_2x

#Y軸方向
  # G ➡ 加速度
  conv_ay = convy(ay)
  # 加速度➡速度(一回目のループ)
  if count_result == 0:
    vy2 = conv_ay * t

  #速度➡距離(初速なし)
    dist2y = conv_ay * t ** 2 / 2

  # 加速度➡速度(初速あり(2回目以降))
  if count_result == 1:
    vy2 = vy2 + conv_ay * t

    #速度➡距離(初速あり(2回目以降))
    dist2_1y = vy2 * t
    dist2_2y = conv_ay * t ** 2 / 2
    dist2y = dist2_1y + dist2_2y
    
  count_result = 1

  distx += dist2x
  disty += dist2y


  if count % 16 == 0:
    print('x >>> ', distx, '(m)')
    print('y >>> ', disty, '(m)')
    print('----')

  count += 1
  time.sleep(t)

データシートを読んだところ加速度は0.064秒ごとに値をとるらしく、それらを物理の式にあてはめて移動距離を測ろうとしています。それをX軸方向・Y軸方向で求め、最初の位置からどのくらい移動したかを計測しています。

#おわりに
結構無理やり物理の式を使っているので、雑な計測結果になってしまいました。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?