Roller485 Lite買った -2-

 ライブラリはこれ。Arduinoのライブラリにも入っていないようだ。これは、売る気がないwww

  m5stack/M5Unit-Roller

  https://github.com/m5stack/M5Unit-Roller

 なんか制御できることが多すぎるかな。表示とかエンコーダーモードとか、LEDとか、回ればいいというニーズには多すぎる機能かも。回してて気持ちはいいから、エンコーダーモードは単独で面白いかもしれない。STM32を直接プログラムできるとか、上級者向き。

 今回は、とりあえず指定のスピードで一方向に回ればいいからな... デモなので回転数を変えてみたり、回転方向を変えてみたりもいいかもだけど。デモだと8時間くらいは動作しっぱなしになるから、負荷とか過熱も気になるところ。

 ライブラリをロードして、I2Cで制御するオブジェクトを作っておく。PID制御用の変数(符号なし32ビット整数)と、LEDのRGB値の変数(符号なし8ビット整数)を定義する。とりあえず、LEDのほうはいらない。LEDはよくある0-255での制御になっている。

#include "unit_rolleri2c.hpp"
UnitRollerI2C RollerI2C;
uint32_t p, i, d;
uint8_t r, g, b;

 初期化は、setup()の中で以下だけ。

 I2C通信 (Wire) を利用し、デバイスのアドレス 0x64 で初期化。GPIO 21 (SDA) と GPIO 22 (SCL) を使用する宣言。通信速度を 400kHz に設定する宣言。

    RollerI2C.begin(&Wire, 0x64, 21, 22, 400000);

 あとはloop()の中で制御を書く。

 CPlus2のGroveは、G32,33なのだよね。出力は5Vなので、そのまま動かせそう。CPlus2はLLMモジュールで出払っているので、CPlusのほうでやるか。こちらは内臓電池を抜いてあるもの。

 ライブラリマネージャーにはドライバもサンプルも存在しなかったので、Githubからzipを落としてきてコピる。zipを展開したファイル名に-mainが付いているので、これを削除してコピーする。

 library.propertiesがあるので、これでArduinoIDEから認識される。この情報にはライセンスはないのね。サンプルコードはMITライセンスになっている。

name=M5UnitRoller
version=0.0.1
author=M5Stack
maintainer=M5Stack
sentence=Library for M5Stack M5Unit Roller
paragraph=M5Stack, M5UnitRoller, See more on http://M5Stack.com
category=Display
url=https://github.com/m5stack/M5Unit-Roller.git
architectures=esp32

 srcにソースコード、examplesにサンプルコード、という感じ。

 コピー先は、「~/Documents/Arduino/libraries/」。

% cp -a ~/Downloads/M5Unit-Roller ~/Documents/Arduino/libraries/
% ls ~/Documents/Arduino/libraries/ |grep Roller
M5Unit-Roller

 ArduinoIDEを再起動すると、ライブラリが認識されているので、サンプル例に表示される。命名ルール的には、「name=M5UnitRoller」はハイフン入ってないとダメだね。

 I2Cには、2つのサンプルが入っている。何をするものかのドキュメントもないのだよねぇ...

 encoder_calibrationは、エンコーダーのキャリブレーション(校正)を行い、その後に速度制御を開始するスケッチ。機能は以下。

  ・Roller485が正常に接続されているか確認

  ・10秒待機後、エンコーダーキャリブレーションを実施

  ・キャリブレーション完了後、速度 240000 でモーターを回転させる

 キャリブレーションは、モーター内部のマイコンで勝手にやって、結果も内部のマイコンに自動保存しているようなので、特にすることはない。動かすだけのスケッチになる。

 修正は、CPlusに合わせて、I2C接続ピンの番号を変更するくらい。モーターが回りっぱなしになるのが気に掛かるので、少し動かしたら停止を追加するか。

/*
 *SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 *
 *SPDX-License-Identifier: MIT
 */
#include "unit_rolleri2c.hpp"
#include <M5Unified.h>
#define ROLLER_CALIBRATION_DELAY 10000
UnitRollerI2C RollerI2C;  // Create a UNIT_ROLLERI2C object
uint8_t is_roller_valid             = 0;
uint8_t is_roller_calibrated        = 0;
uint32_t roller_start_delay_counter = 0;
void setup()
{
    M5.begin();
    // if (RollerI2C.begin(&Wire, 0x64, 21, 22, 400000)) {
    if (RollerI2C.begin(&Wire, 0x64, 32, 33, 400000)) { // CPlus
        is_roller_valid            = 1;
        roller_start_delay_counter = millis();
    }
}
void loop()
{
    if (is_roller_valid) {
        if (millis() - roller_start_delay_counter < ROLLER_CALIBRATION_DELAY) {
            printf("Calibration will start after %dS\n",
                   (roller_start_delay_counter - (millis() - roller_start_delay_counter)) / 1000);
        } else {
            if (!is_roller_calibrated) {
                printf("Start encoder calibration\n");
                RollerI2C.setOutput(0);
                delay(100);
                RollerI2C.startAngleCal();
                delay(100);
                printf("Calibrationing...\n");
                while (RollerI2C.getCalBusyStatus()) {
                    printf("Calibrationing...\n");
                }
                RollerI2C.updateAngleCal();
                printf("Encoder calibration done\n");
                delay(500);
                RollerI2C.setOutput(0);
                RollerI2C.setMode(ROLLER_MODE_SPEED);
                RollerI2C.setSpeed(240000);
                RollerI2C.setSpeedMaxCurrent(100000);
                RollerI2C.setOutput(1);
                is_roller_calibrated = 1;
                delay(5000);
                RollerI2C.setOutput(0); // モーターを停止
                printf("Calibration finished!\n");
            }
        }
    } else {
        printf("No roller485 dectected\n");
    }
}

 Serial.print()していないのに、printfでシリアルに出力に出るのは、M5Unified ライブラリが printf の標準出力を Serial に設定しているからのよう。こういう基本と違う動きは、余計なお節介のような気が...

 なんだこれ...

spi_flash: Detected size(4096k) smaller than the size in the binary image header(8192k). Probe failed.
14:22:18.363 ->
14:22:18.363 -> assert failed: do_core_init startup.c:350 (flash_ret == ESP_OK)
14:22:18.363 ->
14:22:18.363 ->
14:22:18.363 -> Backtrace: 0x40082a8d:0x3ffe3ab0 0x4008abb5:0x3ffe3ad0 0x4008ff6a:0x3ffe3af0 0x400ec97a:0x3ffe3c20 0x40082dea:0x3ffe3c50 0x400794fe:0x3ffe3c90 |<-CORRUPTED
14:22:18.363 ->
14:22:18.363 ->
14:22:18.363 ->
14:22:18.363 ->
14:22:18.363 -> ELF file SHA256: 3c28fd8ea70bb033
14:22:18.363 ->
14:22:18.363 -> E (188) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0x7bd5c66f instead of 0x0
14:22:18.393 -> E (197) esp_core_dump_elf: Elf write init failed!
14:22:18.393 -> E (201) esp_core_dump_common: Core dump write failed with error=-1
14:22:18.393 -> Rebooting...
14:22:18.576 -> E (166) spi_flash: Detected size(4096k) smaller than the size in the binary image header(8192k). Probe failed.
14:22:18.576 ->
14:22:18.576 -> assert failed: do_core_init startup.c:350 (flash_ret == ESP_OK)
14:22:18.576 ->
14:22:18.576 ->
14:22:18.576 -> Backtrace: 0x40082a8d:0x3ffe3ab0 0x4008abb5:0x3ffe3ad0 0x4008ff6a:0x3ffe3af0 0x400ec97a:0x3ffe3c20 0x40082dea:0x3ffe3c50 0x400794fe:0x3ffe3c90 |<-CORRUPTED
14:25:05.774 -> ELF file SHA256: 3c28fd8ea70bb033
14:25:05.774 ->
14:25:05.774 -> E (188) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0x7bd5c66f instead of 0x0
14:25:05.774 -> E (197) esp_core_dump_elf: Elf write init failed!
14:25:05.774 -> E (201) esp_core_dump_common: Core dump write failed with error=-1
14:25:05.774 -> Rebooting...

 CPlus2の設定で書き込んでたわw

 しかし、CPlusで書き込んで試しても動かない。しまいには、シリアルにこんな表示が...

17:32:59.709 -> No roller485 dectected

 Groveのハーネスを変更してみたり、PCから電源を取らずにACから取ってみたりしたけど改善しない... なんだろ。

 そのうち、CPlusに書き込みしようとしてもエラーになるようになった。

 なんか、どうしようもないな。

 CPlus死んだかな。

 なんかPMICの変なバージョンだったから、もう寿命ということで... 無念。

      100円投げ銭  https://kinneko.booth.pm/items/1963720


オリジナル投稿:
Roller485 Lite買った -2-|kinneko|pixivFANBOX
https://kinneko.fanbox.cc/posts/9388145