2022-05-04
Raspberry Piのターミナルを起動し、つぎのコマンドで必要なソフトを取得、展開、インストールします。この作業には時間がかかる場合があります。
$ sudo wget https://www.pololu.com/file/0J1501/pololu-jrk-g2-1.4.0-linux-rpi.tar.xz $ tar -xvf pololu-jrk-g2-1.4.0-linux-rpi.tar.xz $ cd ./pololu-jrk-g2-1.4.0-linux-rpi $ sudo ./install.sh $ pip install JrkG2modulePi
wgetで取得している、Raspberry Pi用のURLがバージョンアップで変わるときがあります。最新のライブラリはPololuユーザガイドから確認してください。
この時にJrk G2 Configuration Utilityもインストールされるので、つぎのコマンドでGUIを起動できます。
$ jrk2gui
USBコネクタでPololu JrkとRaspberry Piを接続します。
次に、Jrk G2 Configuration Utilityを起動し、「Input」タブを開き、「Serial Interface」の「USB dual port」を選択し、右下の「Apply settings」を選択して設定を適用します。
最後に以下のコマンドでUSBポート名を確認します。
$ jrk2cmd --cmd-port
先ほど確認したポート名をport_name = “ポート名”として書き換えてください。
import serial from enum import IntEnum import ctypes from JrkG2modulePi import JrkG2 # you can run "jrk2cmd --cmd-port" to get the right name to use here. # コマンド"jrk2cmd --cmd-port"を実行することでポート名を確認できます。 # Linux USB example: "/dev/ttyACM0" # macOS USB example: "/dev/cu.usbmodem001234562" # Windows example: "COM6" port_name = "/dev/ttyACM0" baud_rate = 9600 device_number = None port = serial.Serial(port_name, baud_rate, timeout=0.1, write_timeout=0.1) jrk = JrkG2.JrkG2Serial(port, device_number) feedback = jrk.getFeedback() print("feedback : ", feedback) target = jrk.getTarget() print("target : ", target) new_target = 2248 if target < 2048 else 1848 print("new target : ", new_target) jrk.setTarget(new_target)
つぎのように接続します。
Pololu Jrk | RaspberryPi |
---|---|
SDA | GPIO2(SDA) |
SCL | GPIO3(SCL) |
GND | GND |
Rapberry Piのピン配置はつぎのコマンドで確認できます。
$ sudo pinout
つぎのコマンドでPololu JrkのI2Cアドレスを確認します。
$ i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- 0b -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
先ほど確認したアドレスが0x0bでなかった場合は変数addressを書き換えてください。
from smbus2 import SMBus, i2c_msg from enum import IntEnum import ctypes from JrkG2modulePi import JrkG2 # you can run "i2cdetect -l" to get the right number to use here. bus = SMBus(1) # and you can run "i2cdetect -y 1" # you can find the right address to use here. # Normaly, this address is 0x0B address = 0x0B jrk = JrkG2.JrkG2I2C(bus, address) feedback = jrk.getFeedback() print("feedback : ", feedback) target = jrk.getTarget() print("target : ", target) new_target = 2248 if target < 2048 else 1848 print("new target : ", new_target) jrk.setTarget(new_target)