2021年の1月にRaspberry Piシリーズに
Raspberry Pi Pico
Raspberry Pi Picoは、
Raspberry Pi Picoのマイコンボードとしての特徴のひとつが余計な専用ツールが不要なことです。実行イメージのコンパイルは一般的なGCCコンパイラーでできますし、
必要な機材は次のとおりです。
- Raspberry Pi Pico本体
- USB 2.
0 Micro-Bのプラグが付いたUSBケーブル [3] - 上記を接続するPC
なおPicoはピン有り・
PCもUSB給電ができ、
上記を用意したら最初にサンプルプログラムを動かしましょう。正式な由来は不明なのですが、
購入直後はおそらく、
$ sudo dmesg (中略) [ 753.792803] usb 1-1: new full-speed USB device number 8 using xhci_hcd [ 753.942280] usb 1-1: New USB device found, idVendor=2e8a, idProduct=0003, bcdDevice= 1.00 [ 753.942293] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [ 753.942299] usb 1-1: Product: RP2 Boot [ 753.942303] usb 1-1: Manufacturer: Raspberry Pi [ 753.942307] usb 1-1: SerialNumber: E0C912D24340 [ 753.975352] usb-storage 1-1:1.0: USB Mass Storage device detected [ 753.975457] scsi host0: usb-storage 1-1:1.0 [ 753.975571] usbcore: registered new interface driver usb-storage [ 753.977555] usbcore: registered new interface driver uas [ 754.981713] scsi 0:0:0:0: Direct-Access RPI RP2 1 PQ: 0 ANSI: 2 [ 754.982348] sd 0:0:0:0: Attached scsi generic sg0 type 0 [ 754.982926] sd 0:0:0:0: [sda] 262144 512-byte logical blocks: (134 MB/128 MiB) [ 754.983245] sd 0:0:0:0: [sda] Write Protect is off [ 754.983254] sd 0:0:0:0: [sda] Mode Sense: 03 00 00 00 [ 754.983561] sd 0:0:0:0: [sda] No Caching mode page found [ 754.983574] sd 0:0:0:0: [sda] Assuming drive cache: write through [ 755.005984] sda: sda1 [ 755.037747] sd 0:0:0:0: [sda] Attached SCSI removable disk $ lsusb | grep Rasp Bus 001 Device 008: ID 2e8a:0003 Raspberry Pi RP2 Boot
もし認識しないようなら、
ちなみにディレクトリの中身を見るとINDEX.
」INFO_
」?version=シリアルナンバー
」
shibata@nuc:~$ cat /media/shibata/RPI-RP2/INFO_UF2.TXT UF2 Bootloader v1.0 Model: Raspberry Pi RP2 Board-ID: RPI-RP2
どちらも気にすることはないでしょう。
デスクトップ版のUbuntuなら、
$ wget https://rptl.io/pico-blink -O blink.uf2 $ cp blink.uf2 /media/shibata/RPI-RP2/
これだけでPicoは自動的に再起動し、
ここではCLIからコピーしましたが、
SDKのインストールのサンプルのビルド
これだけだと味気ないので、
最初にビルドに必要なパッケージ一式をインストールしておきます。今回はUbuntu 20.
$ sudo apt install gcc cmake gcc-arm-none-eabi \ libnewlib-arm-none-eabi build-essential
Picoのドキュメントだと
次のpico-sdkとpico-examplesのコードをそれぞれcloneします。場所はどこでもかまいません。また、
$ git clone -b master https://github.com/raspberrypi/pico-sdk.git $ cd pico-sdk $ git submodule update --init $ cd .. $ git clone -b master https://github.com/raspberrypi/pico-examples.git
pico-sdkはPico用のファームウェアを構築する際に必要なコード一式とそれをビルドするためのcmakeファイルから成り立っています。よっていわゆる
言い換えるとファームウェアのビルド時にpico-sdkのパスを参照できるようにしておかなくてはなりません。一番手っ取り早いのは環境変数PICO_
を指定しておくことです。pico-sdkのclone先を固定化できるのであれば、~/.bashrc
」
$ export PICO_SDK_PATH=$PWD/pico-sdk $ ls -1 $PICO_SDK_PATH CMakeLists.txt LICENSE.TXT README.md cmake docs external lib pico_sdk_init.cmake pico_sdk_version.cmake src test tools
次にPico用ファームウェアのサンプルコードをビルドします。サンプルページのREADME.
先ほど利用したblink.
$ cd pico-examples $ mkdir build $ cd build $ cmake .. Using PICO_SDK_PATH from environment ('/home/ubuntu/pico-sdk') PICO_SDK_PATH is /home/ubuntu/pico-sdk Defaulting PICO_PLATFORM to rp2040 since not specified. Defaulting PICO platform compiler to pico_arm_gcc since not specified. -- Defaulting build type to 'Release' since not specified. PICO compiler is pico_arm_gcc -- The C compiler identification is GNU 9.2.1 -- The CXX compiler identification is GNU 9.2.1 -- The ASM compiler identification is GNU -- Found assembler: /usr/bin/arm-none-eabi-gcc Defaulting PICO target board to pico since not specified. Using board configuration from /home/ubuntu/pico-sdk/src/boards/include/boards/pico.h -- Found Python3: /usr/bin/python3.8 (found version "3.8.10") found components: Interpreter TinyUSB available at /home/ubuntu/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; adding USB support. -- Configuring done -- Generating done -- Build files have been written to: /home/ubuntu/pico-examples/build $ cd blink $ make -j $(nproc) Scanning dependencies of target ELF2UF2Build Scanning dependencies of target bs2_default [ 0%] Creating directories for 'ELF2UF2Build' [ 0%] Building ASM object pico-sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj [ 0%] Linking ASM executable bs2_default.elf (中略) [100%] Linking CXX executable blink.elf [100%] Built target blink
上記手順通り実施すれば現在のディレクトリpico-examples/
)
ちなみにpico-sdkでそのまま作ったUF2ファイルなら、
USBシリアルコンソールを使ってみる
これだけだと最初の結果と同じなので味気ないですね。せっかくなのでPicoからなにか表示させてみましょう。Picoには複数のUARTピンがあります。さらにFlash書き込み・
もちろんこれも、hello_
がそれです。ちなみにhello_
を使えば、
$ cd ../hello_world/usb/ $ make -j $(nproc) (中略) [ 50%] Building C object hello_world/usb/CMakeFiles/hello_usb.dir/home/ubuntu/pico-sdk/src/rp2_common/pico_stdio/stdio.c.obj [ 50%] Building C object hello_world/usb/CMakeFiles/hello_usb.dir/home/ubuntu/pico-sdk/src/rp2_common/pico_stdio_usb/reset_interface.c.obj [ 50%] Building C object hello_world/usb/CMakeFiles/hello_usb.dir/home/ubuntu/pico-sdk/src/rp2_common/pico_stdio_usb/stdio_usb.c.obj [ 50%] Building C object hello_world/usb/CMakeFiles/hello_usb.dir/home/ubuntu/pico-sdk/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c.obj [ 50%] Building C object hello_world/usb/CMakeFiles/hello_usb.dir/home/ubuntu/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c.obj (中略) [100%] Linking CXX executable hello_usb.elf [100%] Built target hello_usb
pico-sdkのほうでstdio_
やtinyusb
もセットでビルドされていることがわかりますね。実際、
作成されたhello_
をUSBマスストレージ経由でPicoに書き込んでください。自動的に再起動し、
PicoにUSBシリアルコンソールとして接続しましょう。hello_
が動いている場合、/dev/
が作成されます。このデバイスはudevルールによって、
$ sudo usermod -a -G dialout $(whoami)
dialoutグループへの参加は一度ログインしなおすことで、id
コマンドで確認してみてください。ちなみにdialoutグループに追加したあと、newgrp
コマンドを実行すると、
$ newgrp dialout
/dev/
へのアクセス権限が得られたので、
$ sudo apt install picocom $ picocom /dev/ttyACM0 -b 115200 picocom v3.1 (中略) Type [C-a] [C-h] to see available commands Terminal ready Hello, world! Hello, world! (picocomを終了するためにはCtrl-a Ctrl-xを入力) Terminating... Thanks for using picocom
ひたすら
Picoを姿勢測定デバイスとして動かす
既存のRaspberry Piと比べるとPicoは、
すでにPico向けのさまざまなオプションボードが登場しています。そこで今回は最後にWaveshareのセンサーデバイスであるPico-10DOF-IMUを使ってみましょう。これはそれぞれ3軸の加速度センサー・
もちろんPico用のサンプルコードもあるので、
$ sudo apt install unzip $ wget https://www.waveshare.com/w/upload/3/3d/Pico-10dof-imu.zip $ unzip Pico-10dof-imu.zip
サンプルコードはC言語向けとMicroPython向けが用意されています。Picoは公式にMicroPythonもサポートしており、
$ cd pico-10dof-imu/c/build/ $ cmake .. Using PICO_SDK_PATH from environment ('/home/ubuntu/pico-sdk') PICO_SDK_PATH is /home/ubuntu/pico-sdk Defaulting PICO_PLATFORM to rp2040 since not specified. Defaulting PICO platform compiler to pico_arm_gcc since not specified. -- Defaulting build type to 'Release' since not specified. PICO compiler is pico_arm_gcc -- The C compiler identification is GNU 9.2.1 -- The CXX compiler identification is GNU 9.2.1 -- The ASM compiler identification is GNU -- Found assembler: /usr/bin/arm-none-eabi-gcc Defaulting PICO target board to pico since not specified. Using board configuration from /home/ubuntu/pico-sdk/src/boards/include/boards/pico.h -- Found Python3: /usr/bin/python3.8 (found version "3.8.10") found components: Interpreter TinyUSB available at /home/ubuntu/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; adding USB support. -- Configuring done -- Generating done -- Build files have been written to: /home/ubuntu/pico-10dof-imu/c/build $ make -j $(nproc) (中略) [100%] Linking CXX executable imu.elf [100%] Built target imu
このあたりはpico-examplesと手順は同じですね。最終的にimu.
」
このファームウェアはUSBシリアルとUART0の両方に出力する設定になっています。具体的にはpico-10dof-imu/
」
pico_enable_stdio_usb(imu 1) pico_enable_stdio_uart(imu 1)
そこで/dev/
にpicocomで接続してみます。
$ picocom /dev/ttyACM0 -b 115200 picocom v3.1 (中略) /-------------------------------------------------------------/ Roll: 2.62 Pitch: -1.49 Yaw: -158.66 Pressure = 999.34 hPa , Temperature = 31.46 °C /-------------------------------------------------------------/ Roll: 2.57 Pitch: -1.44 Yaw: -158.57 Pressure = 999.33 hPa , Temperature = 31.45 °C
100ms周期で、
ロール
実際に上記の出力を眺めながら個々の軸を回転させるとイメージがつかめると思います。
「pico-10dof-imu/
」
printf("\r\n /-------------------------------------------------------------/ \r\n"); printf("\r\n Roll: %.2f Pitch: %.2f Yaw: %.2f \r\n",stAngles.fRoll, stAngles.fPitch, stAngles.fYaw); printf("Pressure = %6.2f hPa , Temperature = %6.2f °C\r\n", PRESS_DATA, TEMP_DATA); //printf("\r\n Acceleration: X: %d Y: %d Z: %d \r\n",stAccelRawData.s16X, stAccelRawData.s16Y, stAccelRawData.s16Z); //printf("\r\n Gyroscope: X: %d Y: %d Z: %d \r\n",stGyroRawData.s16X, stGyroRawData.s16Y, stGyroRawData.s16Z); //printf("\r\n Magnetic: X: %d Y: %d Z: %d \r\n",stMagnRawData.s16X, stMagnRawData.s16Y, stMagnRawData.s16Z); sleep_ms(100);
これをコメントアウトを外して//
を削除して)、
Roll: 1.45 Pitch: 0.38 Yaw: -174.00 Pressure = 1004.02 hPa , Temperature = 26.06 °C Acceleration: X: -234 Y: -198 Z: 16970 Gyroscope: X: 8 Y: 2 Z: -9 Magnetic: X: 97 Y: -41 Z: -504
Pioc-10DOF-IMUのコードは本当にサンプルと言うべきシンプルなものなので、