# $env:BLINKA_FT232H=1
import time
import datetime
import board
import busio
import adafruit_mpu6050
# i2cインスタンスを作成する
i2c = busio.I2C(board.SCL, board.SDA)
# mpu6050をi2cで使用するインスタンスを作成する
mpu1 = adafruit_mpu6050.MPU6050(i2c, address=0x68)
mpu2 = adafruit_mpu6050.MPU6050(i2c, address=0x69)
print('Temperature degC, Acceleration m/s2')
print('')
# 初期時刻の設定
init_time = datetime.datetime.now()
# 初期時間の設定
time_cnt = 0.0
# 測定値を出力する周期
interval = 0.2
while True:
# センサー1 address=0x68 温度T1, 加速度 ax, ay, azを取得
T1 = mpu1.temperature
ax1 = mpu1.acceleration[0]
ay1 = mpu1.acceleration[1]
az1 = mpu1.acceleration[2]
a_total1 = (ax1 ** 2 +ay1 ** 2 + az1 ** 2) ** (1/3) # 異常値の指標として
# センサー2 address=0x68 温度T1, 加速度 ax, ay, azを取得
T2 = mpu2.temperature
ax2 = mpu2.acceleration[0]
ay2 = mpu2.acceleration[1]
az2 = mpu2.acceleration[2]
a_total2 = (ax2 ** 2 +ay2 ** 2 + az2 ** 2) ** (1/3) # 異常値の指標として
# センサー1 address=0x68 異常値のしきい値設定
if a_total1 > 6 :
status1 = ' ==== ! ! ALERT ! ! sensor1 ==== '
elif a_total1 > 5:
status1 = ' = CAUTION sensor1 ='
else:
status1 = ''
# センサー2 address=0x69 異常値のしきい値設定
if a_total2 > 6 :
status2 = ' ==== ! ! ALERT ! ! sensor2 ==== '
elif a_total2 > 5:
status2 = ' = CAUTION sensor2 ='
else:
status2 = ''
# 現在時刻
current_time = datetime.datetime.now() - init_time
current_time = current_time.seconds + current_time.microseconds/1000_000
# 現在時刻が測定値出力周期を超えたら測定値を出力する
if current_time >= time_cnt:
# センサー1 address=0x68
print(str(datetime.datetime.now()) + ' ' + '{:.3f}'.format(current_time) +
" T1:{:.2f}, ax1:{:.2f}, ay1:{:.2f}, az1:{:.2f}, a_total1:{:.2f}, status1:{} "
.format(T1, ax1, ay1, az1, a_total1, status1))
# センサー2 address=0x69
print(str(datetime.datetime.now()) + ' ' + '{:.3f}'.format(current_time) +
" T2:{:.2f}, ax2:{:.2f}, ay2:{:.2f}, az2:{:.2f}, a_total2:{:.2f}, status2:{} "
.format(T2, ax2, ay2, az2, a_total2, status2))
print('')
# 次回測定値出力のために判定タイミングtime_cntを更新
time_cnt += interval
# ジャイロ:角速度
# print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s"%(mpu.gyro))