コンテンツにスキップ

3 軸加速度センサ AE-KXTC9-2050

KXTC9-2050 使用 3 軸加速度センサモジュール: 計測器・センサ・ロガー 秋月電子通商-電子部品・ネット通販

どの軸がどの向きかは基板上に描かれている。

Raspberry Pi Pico W

電圧値を取得する

ピン番号 名称 機能 備考
1 V+ 電源入力 3.3V
2 NC - 使用しないので開放しておく
3 ST セルフテスト 使用時 Low らしい。GND に繋げば OK
4 EN イネーブル High 時動作、Low 時パワーダウンらしい。3.3V に繋げば OK
5 X X 軸出力 -
6 Y Y 軸出力 -
7 Z Z 軸出力 -
8 GND - -
main.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import time
import board
import analogio

asensor_x = analogio.AnalogIn(board.GP26_A0)
asensor_y = analogio.AnalogIn(board.GP27_A1)
asensor_z = analogio.AnalogIn(board.GP28_A2)


while True:
    ax = asensor_x.value * 3.3 / 65536 - 1.88
    ay = asensor_y.value * 3.3 / 65536 - 1.35
    az = asensor_z.value * 3.3 / 65536 - 2.2

    print(ax, ay, az)
    time.sleep(1 / 60)
出力例
1
2
3
4
5
6
7
8
-0.120027 0.210118 0.120866
-0.119221 0.211729 0.122477
-0.120833 0.207701 0.121672
-0.120027 0.210118 0.122477
-0.122444 0.206895 0.124894
-0.121638 0.205284 0.123282
-0.124055 0.206089 0.128117
-0.120027 0.205284 0.121672

バイアス電圧が乗っているので注意すること。

バイアス電圧を除く

起動後 2 秒間サンプリングしたときの中央値をバイアス電圧として差を取る。

 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
import time

import analogio
import board

asensor_x = analogio.AnalogIn(board.GP26_A0)
asensor_y = analogio.AnalogIn(board.GP27_A1)
asensor_z = analogio.AnalogIn(board.GP28_A2)


def get_bias() -> None:
    ax = []
    ay = []
    az = []
    count = 120
    for i in range(count):
        ax.append(asensor_x.value)
        ay.append(asensor_y.value)
        az.append(asensor_z.value)
        time.sleep(1 / 60)
    print(ax)
    j = int(count / 2)
    return [sorted(ax)[j], sorted(ay)[j], sorted(az)[j]]


def main() -> None:
    bias = get_bias()
    while True:
        ax = (asensor_x.value - bias[0]) * 3.3 / 65536
        ay = (asensor_y.value - bias[1]) * 3.3 / 65536
        az = (asensor_z.value - bias[2]) * 3.3 / 65536
        print(ax, ay, az)
        time.sleep(1 / 60)


if __name__ == "__main__":
    main()

x 軸方向に手で降ってみた様子。上手く取得できていそう。

クラス化

 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
import time

import analogio
import board


class AeKxtC92050:
    def __init__(self, pin: board.Pin, bias_buffer_length=120, threshold=1000) -> None:
        self.__device = analogio.AnalogIn(pin)
        self.__bias_buffer_length = bias_buffer_length
        self.__bias_buffer = [self.__device.value for _ in range(self.__bias_buffer_length)]
        self.__bias_buffer_index = 0
        self.__bias = self.__bias_buffer[0]
        self.__threshold = threshold
        self.__a = [0, 0]
        self.__v = [0, 0]

    def update(self, delta_t: float) -> None:
        # 値を取得する
        value = self.__device.value

        # 加速度を求める
        self.__a[1] = value - self.__bias
        if abs(self.__a[1]) >= self.__threshold:
            self.__v[1] = (self.__a[1] + self.__a[0]) * delta_t / 2 + self.__v[0]
        else:
            # 閾値未満は0とみなす
            self.__a[1] = 0
            self.__v[1] = 0

        # 値を保存する
        self.__a[0] = self.__a[1]
        self.__v[0] = self.__v[1]

        # バイアスを更新する
        self.__bias_buffer[self.__bias_buffer_index] = value
        if self.__bias_buffer_index >= self.__bias_buffer_length - 1:
            self.__bias_buffer_index = 0
        else:
            self.__bias_buffer_index += 1
        self.__bias = sorted(self.__bias_buffer)[int(self.__bias_buffer_length / 2)]

    @property
    def acceleration(self) -> int:
        return self.__a[1]

    @property
    def velocity(self) -> float:
        return self.__v[1]


DELTA_T = 1 / 60


def main() -> None:
    ax = AeKxtC92050(board.GP26_A0)

    while True:
        # Xの計算
        ax.update(DELTA_T)

        print(ax.acceleration, ax.velocity)

        time.sleep(DELTA_T)


if __name__ == "__main__":
    main()