メインコンテンツまでスキップ

XIAO Robot Kit 入門ガイド

はじめに​

XIAO Robot Kit は、Seeed Studio XIAO ESP32-S3 Plus を中心に構成されたモジュール式ロボティクス開発キットです。キャリアボードには、小型移動ロボット用の電子回路がすでに搭載されています:Wi-Fi と Bluetooth LE を備えた ESP32-S3 コントローラ、クアドラチャエンコーダ付き N20 ギアドモーター 2 個、4 チャンネルのサーボ、6 軸 IMU、およびバッテリーマネジメントです。キットには、Feetech FT1190M メタルギアサーボ 2 個と、後から接続するための Grove コネクタが追加されています。筐体はあなた自身で設計・3D プリントしてください — 駆動用ハードウェアはすでにボード上に揃っています。

仕様​

コンポーネント仕様詳細
XIAO Robot BoardメインコントローラSeeed Studio XIAO ESP32-S3 Plus
プロセッサESP32-S3R8、Xtensa LX7 デュアルコア 32-bit プロセッサ、最大 240 MHz
PSRAM8 MB
フラッシュ16 MB
無線接続2.4 GHz Wi-Fi (802.11 b/g/n)、Bluetooth LE 5.0
モータードライバ2 × GC9112 H ブリッジドライバ
DC モーターチャンネル2 × 独立した 5V DC モーターチャンネル
モーターインターフェース2 × 2 ピン標準 DC モーターポート
2 × 6 ピンエンコーダモーターポート
エンコーダフィードバック2 × クアドラチャエンコーダインターフェース (A/B、3.3V ロジック)
PWM サーボインターフェース4 × PWM サーボポート (5V 電源、3.3V 信号)
オンボード IMULSM6DS3TR-C 6 軸 IMU (3 軸加速度センサ + 3 軸ジャイロスコープ)
Grove 拡張2 × I²C
1 × UART
1 × RGB
USB インターフェース2 × USB-C (メインボードおよび XIAO)
電源入力5V USB-C / 7.4V (2S) LiPo バッテリー
バッテリーコネクタJST-PH 2.0、2 ピン
バッテリー充電メイン USB-C 経由のオンボードバッテリー充電
電源管理統合 DC-DC 変換および電源ソース管理
電源スイッチ1 × ON/OFF スライドスイッチ
オンボードボタン1 × ユーザーボタン
1 × リセットボタン
ステータスインジケータ1 × 電源 LED
1 × 充電 LED
開発環境Arduino / ESP-IDF
寸法65 × 72 × 10 mm
N20 エンコーダモーターモータータイプクアドラチャエンコーダ付きブラシ付き DC ギアドモーター
動作電圧2.5–5.5 V DC
ギア比101:1
無負荷回転数170 RPM ±10%
無負荷電流≤90 mA
停止トルク0.4 kgf·cm
停止電流≤1.3 A
起動電圧≤2.0 V
出力シャフトØ3 mm D シャフト、長さ 8 mm
接続6 ピンモーター & エンコーダハーネス
重量約 14.8 g
Feetech FT1190M サーボモデルカスタマイズ版 FT1190M (FT-1190-C016)
モーター & ギアコアレスモーター、メタルギア
入力電圧3.3–7.4 V DC
制御方式PWM
パルス幅範囲500–2500 μs
ニュートラル位置1500 μs
停止トルク2.8 kgf·cm @ 4.8V;3.5 kgf·cm @ 6V
定格トルク0.9 kgf·cm @ 4.8V;1.15 kgf·cm @ 6V
無負荷速度0.125 s/60° @ 4.8V;0.10 s/60° @ 6V
無負荷電流≤150 mA @ 4.8V;≤170 mA @ 6V
定格電流250 mA @ 4.8V;300 mA @ 6V
停止電流900 mA @ 4.8V;1100 mA @ 6V
コネクタPHR-4P (Signal / NC / VCC / GND)
ケーブル長150 ± 5 mm
寸法23.2 × 12.1 × 25.25 mm
重量13 ± 2 g
ホイール駆動ホイールØ34 × 6.5 mm、3 mm D シャフトマウント
ボールキャスター25 × 25 × 13 mm (L × W × H)、スチールボール付き

Note: XIAO Robot Board 上のサーボポートは 5V 電源を供給します。モーターおよびサーボの性能は、電源電圧と機械的負荷によって変化します。

特長​

  • オールインワンロボットコントローラ

  • フィードバックで動かし、自由にアクション。

  • より多くセンシングし、より多く作る。

  • ロボットの電源は、あなたのスタイルで。

  • 設計段階からつながるように設計

  • コードからモーションへ

ハードウェア概要​

XIAO Robot Board​

モーターおよびサーボコネクタ、IMU、2 つの USB-C ポート、電源スイッチ、バッテリーコネクタはすべてこのボード上にあります。以降のセクションでも同じ名称を使用します。

ヒント

モーターとサーボは大きな電流を消費します。コンピュータの USB ポートだけでは電力が不足する場合があるため、サンプル 3 と 4 では バッテリーコネクタ からキットに給電してください。

オンボード周辺機能のピンマッピング​

XIAO ESP32-S3 Plus モジュールがキットの中核を成しています。以下の表は、各オンボードハードウェア機能がどの専用ピンに対応しているかを示します。

Dxx is a silkscreen marking, not a GPIO number

A Dxx marking is not a GPIO number. For example, D11 maps to GPIO38.

機能ピンGPIO備考
SERVO3D0GPIO1サーボ 3
M1 PWM1D1GPIO2モーター 1 駆動
M1 PWM2D2GPIO3モーター 1 駆動
M1 Encoder AD3GPIO4モーター 1 エンコーダ A
I²C SDAD4GPIO5オンボード IMU
I²C SCLD5GPIO6オンボード IMU
UART TXD6GPIO43Grove UART TX
UART RXD7GPIO44Grove UART RX
SERVO4D8GPIO7サーボ 4
SERVO2D9GPIO8サーボ 2
M2 Encoder BD10GPIO9モーター 2 エンコーダ B
KEYD11GPIO38ユーザーボタン
IMU Heater PWMD12GPIO39IMU 横のヒーター抵抗を駆動
RGBD13GPIO40Grove RGB / デジタル
M1 Encoder BD14GPIO41モーター 1 エンコーダ B
SERVO1D15GPIO42サーボ 1
ADCD16GPIO10オンボード ADC
M2 Encoder AD17GPIO13モーター 2 エンコーダ A
M2 PWM2D18GPIO12モーター 2 駆動
M2 PWM1D19GPIO11モーター 2 駆動

セットアップ​

ハードウェアの準備​

ステップ 1. コンピュータに接続する

方法 1​

USB-C データケーブル を使用して、XIAO ESP32-S3 Plus 上の USB-C ポートをコンピュータに接続し、そのうえでキット底面の USB-C / バッテリーポートには、モバイルバッテリーやバッテリーなどの別電源から給電します。

方法 2​

USB-C データケーブルを使用して、キットを下側の USB-C ポート経由でコンピュータに接続し、XIAO ESP32-S3 Plus 上の USB-C ポートは接続しないままにします。電源スイッチを ON にスライドします。注意:この方法では、コンピュータ側の USB-C ポートがセルフパワーのドックまたはハブに接続されており、十分な供給電流が得られる必要があります。

データケーブルを使用してください

充電専用ケーブルの場合、COM ポートがまったく表示されなかったり、デバイスがまったく反応しません。まずはデータケーブルに交換してください。

ソフトウェアの準備​

XIAO ESP32-S3 Plus や Arduino IDE を使うのが初めてですか? ここで一旦止まり、まずボードの Getting Started guide を読んでください。Arduino IDE のインストール、ESP32 ボードパッケージの追加、そして正しいポートで XIAO_ESP32S3_PLUS を選択する手順が説明されています。

Tools > Port にあなたのボードが表示されるようになったら、ここに戻ってきてください。このページのすべてのサンプルは同じ方法で書き込みます。

例 1: ボタン + Hello World シリアル出力​

このキットの「hello world」であり、ツールチェーンが動作していることを最も手早く確認できるサンプルです。1 秒ごとに hello world N を出力し、オンボードの KEY ボタンが押された回数をカウントします。配線は一切不要です。

setup() はシリアルポートを開き、KEY ピンを INPUT_PULLUP に設定するので、ピンは何もしていないときは HIGH を読み取り、ボタンが押されている間だけ LOW になります。loop() は delay(1000) を一切呼び出さず、millis() を次の締め切りと比較することで、出力とボタンチェックが並行して動作し続けます。新しいレベルは 10 ms 維持された後にのみカウントされるため、1 回の物理的な押下が 1 行の出力に対応します。

ステップ 1. スケッチを作成する

Arduino IDE で File > New Sketch を開き、テンプレートコードを削除して、以下のスケッチを貼り付けます:

Full code
/* Example 1: prints "hello world N" once per second,
* and counts presses of the on-board KEY button.
*
* Nothing to wire: the KEY button and the USB serial port are on the board. */
#include <Arduino.h>

#define PIN_BUTTON 38 /* silkscreen KEY = GPIO38 */

void setup()
{
Serial.begin(115200); /* must match the Serial Monitor baud rate */
delay(200); /* give the port a moment to come up */

/* KEY shorts to ground when pressed, so the internal pull-up holds the
* pin HIGH at rest and lets it drop to LOW while the button is held. */
pinMode(PIN_BUTTON, INPUT_PULLUP);

Serial.println("01 button + hello world");
Serial.println("press the on-board key to count");
}

void loop()
{
uint32_t now = millis();

/* ---- print one line every 1000 ms, without blocking the loop ---- */
static uint32_t nextHello = 0; /* when the next line is due */
static uint32_t n = 0; /* how many lines printed so far */
if ((int32_t)(now - nextHello) >= 0) {
nextHello = now + 1000;
Serial.print("hello world ");
Serial.println(++n);
}

/* ---- watch the button ---- */
static bool lastRaw = HIGH; /* last level seen on the pin */
static bool lastStable = HIGH; /* last level we accepted */
static uint32_t lastChange = 0; /* when that level last moved */
static uint32_t presses = 0;

bool cur = (digitalRead(PIN_BUTTON) == LOW); /* true = pressed */
if (cur != lastRaw) { lastRaw = cur; lastChange = now; }

/* A new level counts only after it has held for 10 ms, so one press
* becomes one line instead of a burst of them. */
if (now - lastChange >= 10 && cur != lastStable) {
lastStable = cur;
if (lastStable) { /* count on press */
presses++;
Serial.print("button pressed = ");
Serial.println(presses);
}
}

delay(10); /* keeps the loop light for everything else */
}

ステップ 2. スケッチを書き込む

Tools > Board が XIAO_ESP32S3_PLUS であり、Tools > Port にあなたのボードが表示されていることを確認し、Upload(→)をクリックします。

ステップ 3. 結果を確認する

Serial Monitor(右上の虫眼鏡アイコン)を開き、ボーレートを 115200 に設定します。1 秒ごとに 1 行の hello world N が表示されるはずです。次に、オンボードの KEY ボタンを数回押してみてください。押すたびに button pressed = N という行が追加されます:

01 button + hello world
press the on-board key to count
hello world 1
hello world 2
button pressed = 1

次に Serial Monitor を見てみると、同じ実行結果はこのように表示されます:

ヒント

Serial Monitor が空のままだったり、文字化けしたテキストが表示される場合は、選択されているポートと 115200 のボーレート設定を確認してください。— FAQs を参照してください。

例 2: IMU 6 軸生データ + 温度​

オンボードの LSM6DS3TR-C 6 軸 IMU を I2C 経由で読み取り、7 つすべてのチャンネルを 20 Hz で出力します。加速度は g、角速度は dps、ダイ温度は °C で表示されます。

このチップは D4/D5 上の 2 線式バスに接続されているため、スケッチに必要なのは 3 つの小さなヘルパーだけです。wr() は 1 つのレジスタに書き込み、rd() はそこからバーストでバイトを読み出し、i16() は 2 つのリトルエンディアンバイトを結合して符号付き 16 ビットサンプルにします。setup() はまず WHO_AM_I を読み取ります。誤った応答はバスの問題を意味し、その場合スケッチはゼロを延々とストリーミングする代わりに IMU init failed を出力して停止します。その後、出力レートとフルスケールレンジを設定します。各ループでは、温度、ジャイロ、加速度を 1 回の 14 バイトバーストで読み取り、その設定から決まる感度でカウント値をスケーリングします。

ステップ 1. スケッチを作成する

新しいスケッチ(File > New Sketch)を作成し、以下のフルコードを貼り付けます:

Full code
/* Example 2: streams 6-axis acceleration (g), angular rate (dps)
* and temperature (C) from the LSM6DS3TR-C IMU over I2C.
*
* The IMU is on the board: SDA = D4, SCL = D5, address 0x6A. */
#include <Arduino.h>
#include <Wire.h>
#include <stdlib.h>

#define ADDR 0x6A /* I2C address of the on-board IMU */
#define PIN_SDA 5 /* silkscreen D4 */
#define PIN_SCL 6 /* silkscreen D5 */

/* Registers used below, straight out of the LSM6DS3TR-C datasheet. */
#define WHO_AM_I 0x0F /* identity register, always reads 0x6A */
#define CTRL1_XL 0x10 /* accelerometer: output rate + full scale */
#define CTRL2_G 0x11 /* gyroscope: output rate + full scale */
#define CTRL3_C 0x12 /* block data update + register auto-increment */
#define OUT_TEMP 0x20 /* first of the 14 output bytes read here */

/* Write one 8-bit register. */
static void wr(uint8_t r, uint8_t v) {
Wire.beginTransmission(ADDR); Wire.write(r); Wire.write(v); Wire.endTransmission(true);
}

/* Read n bytes starting at register r; false = the bus did not answer. */
static bool rd(uint8_t r, uint8_t *b, uint8_t n) {
Wire.beginTransmission(ADDR); Wire.write(r);
if (Wire.endTransmission(false)) return false;
if (Wire.requestFrom((int)ADDR, (int)n) != n) return false;
for (uint8_t i = 0; i < n; i++) b[i] = Wire.read();
return true;
}

/* Join two little-endian bytes into one signed 16-bit sample. */
static int16_t i16(const uint8_t *p) { return (int16_t)(p[0] | p[1] << 8); }

/* Print one "tag=value" field: value right-aligned in w characters,
* d digits after the decimal point. */
static void p(const char *tag, float v, int w, int d) {
char s[10];
Serial.print(tag);
Serial.print(dtostrf(v, w, d, s));
Serial.print(' ');
}

void setup() {
Serial.begin(115200);
delay(200);

Wire.begin(PIN_SDA, PIN_SCL); /* start the I2C bus on D4 / D5 */
Wire.setClock(400000); /* 400 kHz fast mode, plenty for this */

/* Read the identity register first: a wrong answer means the bus or the
* address is wrong, so stop with a clear message instead of streaming
* meaningless zeros forever. */
uint8_t id = 0;
if (!rd(WHO_AM_I, &id, 1) || id != 0x6A) {
Serial.println("IMU init failed");
while (1) delay(1000);
}

wr(CTRL1_XL, 0x40); /* accel 104 Hz, +/-2 g -> 0.061 mg/LSB */
wr(CTRL2_G, 0x4C); /* gyro 104 Hz, +/-2000 dps -> 0.070 dps/LSB */
wr(CTRL3_C, 0x44); /* block data update + register auto-increment */
Serial.println("IMU ready (ax/ay/az in g, gx/gy/gz in dps, t in C)");
}

void loop() {
/* One burst covers temperature, gyro and accelerometer: 14 bytes. */
uint8_t b[14];
if (!rd(OUT_TEMP, b, 14)) { delay(50); return; }

/* Convert the raw counts with the sensitivity set in setup(). */
float t = 25.0f + i16(&b[0]) / 256.0f;
float gx = i16(&b[2]) * 0.070f;
float gy = i16(&b[4]) * 0.070f;
float gz = i16(&b[6]) * 0.070f;
float ax = i16(&b[8]) * 0.000061f;
float ay = i16(&b[10]) * 0.000061f;
float az = i16(&b[12]) * 0.000061f;

/* All seven channels on one line, so a plotter sees one sample per row. */
p("ax=", ax, 6, 3);
p("ay=", ay, 6, 3);
p("az=", az, 6, 3);
p("gx=", gx, 8, 2);
p("gy=", gy, 8, 2);
p("gz=", gz, 8, 2);
p("t=", t, 6, 2);
Serial.println("C");

delay(50); /* 50 ms + the read time is about 20 Hz */
}

ステップ 2. スケッチを書き込む

例 1 と同様に書き込みます。起動時にスケッチは IMU の WHO_AM_I レジスタをチェックし、チップが応答しない場合は IMU init failed を出力します。応答があれば IMU ready が表示されます。

ステップ 3. 結果を確認する

ボードを机の上に平らに置くと、シリアルモニタには 20 Hz で安定したデータが流れ続けます。次の点を確認してみてください:

  • 平らで静止しているとき: az ≈ 1.0(重力は下向き)、ax ≈ ay ≈ 0、3 つのジャイロ軸はすべて 0 付近になります。
  • ボードを傾ける: 回転させると ax / ay が変化します — これらは重力の向きを測定しています。
  • ボードを振る: 動かすとジャイロの値がスパイクします。
IMU ready  (ax/ay/az in g, gx/gy/gz in dps, t in C)
ax= 0.012 ay= 0.008 az= 0.995 gx= 0.12 gy= -0.03 gz= 0.05 t=25.31 C

ここでシリアルモニタを見てみると、同じ実行結果は次のように表示されます:

例 3: サーボ最小構成の例​

SERVO1 の位置サーボを 50 Hz の PWM 信号で駆動し、そのパルス幅を 500 µs から 2500 µs の間でスイープさせます — 4 秒で 1 サイクルです。

ホビー用サーボは角度ではなくパルス幅を受け取ります。servoPulseUs() は -1.0 から +1.0 の位置を 500–2500 µs に変換し、その範囲外の値はクランプします。そして servoWrite() は、そのパルス幅を、20 ms 周期・14 ビット分解能で LEDC ペリフェラルが必要とするデューティ比に変換します。loop() は経過時間の sinf() を与えるので、ホーンはジャンプせずに滑らかに動きます。

ハードウェアのセットアップ​

付属のサーボとケーブルを、ボード上の SERVO1 コネクタに接続するだけです。

ステップ 1. スケッチを作成する

新しいスケッチを作成し、以下の完全なコードを貼り付けます:

Full code
/* Example 3: sweeps a position servo smoothly across its full range
* (one sine cycle every 4 s) on servo port SERVO1.
*
* Plug the servo cable into SERVO1; nothing else to wire. */
#include <Arduino.h>
#include <math.h>

#define PIN_SERVO 42 /* silkscreen SERVO1 = GPIO42 */

#define PWM_FREQ 50 /* hobby servos expect 50 Hz, i.e. 20 ms */
#define PWM_RES_BITS 14 /* 16384 steps across one period */
#define PWM_PERIOD_US 20000 /* 1 / 50 Hz, in microseconds */

#define SERVO_PULSE_CENTER 1500 /* center position */
#define SERVO_PULSE_MIN 500 /* full travel one way */
#define SERVO_PULSE_MAX 2500 /* full travel the other way */

#define SWEEP_PERIOD 4000 /* one complete sweep, milliseconds */
#define TWO_PI 6.2831853f

/* Map a position between -1.0 and +1.0 to a pulse width in microseconds.
* Anything outside that range is clamped, so a bad number cannot drive the
* servo past its mechanical stop. */
static float servoPulseUs(float normPos) {
if (normPos < -1.0f) normPos = -1.0f;
if (normPos > 1.0f) normPos = 1.0f;
return (normPos < 0.0f)
? (float)SERVO_PULSE_CENTER + normPos * (float)(SERVO_PULSE_CENTER - SERVO_PULSE_MIN)
: (float)SERVO_PULSE_CENTER + normPos * (float)(SERVO_PULSE_MAX - SERVO_PULSE_CENTER);
}

/* Move the servo: -1.0 = one end, 0.0 = center, +1.0 = the other end.
* The pulse width becomes a duty cycle out of 2^14 steps. */
static void servoWrite(float normPos) {
float us = servoPulseUs(normPos);
uint32_t duty = (uint32_t)(us * (1u << PWM_RES_BITS) / (float)PWM_PERIOD_US + 0.5f);
ledcWrite(PIN_SERVO, duty);
}

void setup() {
Serial.begin(115200);
delay(200);

Serial.println("servo - full-range sweep (SERVO1)");
Serial.println("WARN: servos on Motor_5V rail - use battery / external 5V");

/* Park the pin low first, then hand it to the LEDC peripheral at 50 Hz
* with 14-bit resolution. */
pinMode(PIN_SERVO, OUTPUT); digitalWrite(PIN_SERVO, LOW);
ledcAttach(PIN_SERVO, PWM_FREQ, PWM_RES_BITS);
servoWrite(0.0f); /* start from the center position */
}

void loop() {
static unsigned long tPrint = 0;
uint32_t now = millis();

/* sinf() of the elapsed time gives a smooth -1 .. +1 over SWEEP_PERIOD;
* the modulo keeps the argument small so precision never degrades. */
float normPos = sinf(TWO_PI * (float)(now % SWEEP_PERIOD) / (float)SWEEP_PERIOD);
servoWrite(normPos);

/* Print 10 times a second: enough to follow, not enough to flood. */
if (now - tPrint >= 100) {
tPrint = now;
Serial.print("normPos="); Serial.print(normPos, 2);
Serial.print(" us="); Serial.println(servoPulseUs(normPos), 0);
}

delay(20);
}

ステップ 2. スケッチを書き込む

これまでと同様に書き込みます。起動時には、スイープを開始する前にサーボはセンターに指令されます(normPos = 0, 1500 µs)。

ステップ 3. 結果を観察する

電源投入後、ホーンはまず中央に移動し、その後スイープを開始します。シリアルモニタには 100 ms ごとに 1 行出力されます:normPos= は -1.00 から 1.00 の間の指令位置、us= はそれに対応するマイクロ秒単位のパルス幅です。ホーンの動きと数値を一緒に観察してみてください — 下の 2 つの図のように、両方が同期して上下します。

ここでシリアルモニタを見てみると、同じ実行結果は次のように表示されます:

パルス幅のキャリブレーション

3 つのマクロ SERVO_PULSE_MIN / CENTER / MAX は、正規化された位置をパルス幅にマッピングします。サーボが可動範囲の端でブザー音を立てる場合は、MIN / MAX を狭めてください — 機械的なリミットはサーボのモデルによって異なり、リンケージにも依存します。

例 4: エンコーダ付きモータ最小構成の例​

M1 ギアドモータをサイン波状のデューティ比で回転させ、そのクアドラチャエンコーダを読み取ることで、シリアルモニタに指令デューティ、エンコーダカウント、および測定された速度を表示できるようにします。

motorSet() は -100 から +100 を受け取り、その絶対値を 2 本の方向ピンの一方に出力します。エンコーダは割り込み内でデコードされます:onEncoderChange() は両チャネルを読み取り、16 エントリのクアドラチャテーブルから -1、0、または +1 を加算します。一方 readEnc() は、32 ビット読み出しが分割されないように割り込みを無効にしてから合計値をコピーします。速度は、2 回の出力の間に増えたカウント数を、ホイール 1 回転あたりの 4848 カウントで割ることで求めます。

ハードウェアのセットアップ​

付属のモータとケーブルを エンコーダモータコネクタ に接続するだけです。

ステップ 1. スケッチを作成する

新しいスケッチを作成し、以下の完全なコードを貼り付けます:

Full code
/* Example 4: drives the M1 gear motor with a sine-shaped duty cycle
* (one cycle every 4 s) and prints encoder count and speed (rpm).
*
* Plug the motor cable into the M1 connector; nothing else to wire. */
#include <Arduino.h>
#include <math.h>

#define PIN_M1_IN1 2 /* silkscreen M1 PWM1, D1 */
#define PIN_M1_IN2 3 /* silkscreen M1 PWM2, D2 */
#define PIN_ENC_A 4 /* silkscreen M1 Encoder A, D3 */
#define PIN_ENC_B 41 /* silkscreen M1 Encoder B, D14 */

#define DRIVE_PERIOD 4000 /* one drive cycle, milliseconds */
#define TWO_PI 6.2831853f
#define DEAD_ZONE 15 /* duty below this cannot move the gearbox */

/* 12 pulses per motor revolution x 4 (quadrature x4) x 101 (gear ratio). */
const uint32_t kCountsPerRev = 4848;

/* Quadrature table: index = (previous state << 2) | new state, where a state
* is (A << 1) | B. The value is the direction of that one step: -1, 0 or +1. */
static const int8_t kQuad[16] = {
0, 1, -1, 0,
-1, 0, 0, 1,
1, 0, 0, -1,
0, -1, 1, 0
};
static volatile int32_t encCount = 0; /* written by the ISR, read by loop */
static volatile uint8_t encState = 0; /* previous A/B level */

/* Runs on every edge of A or B and adds -1, 0 or +1 to the count. */
void IRAM_ATTR onEncoderChange() {
uint8_t a = (uint8_t)digitalRead(PIN_ENC_A);
uint8_t b = (uint8_t)digitalRead(PIN_ENC_B);
uint8_t next = (uint8_t)((a << 1) | b);
encCount += kQuad[(encState << 2) | next];
encState = next;
}

/* Copy the count with interrupts off, so a 32-bit read cannot be torn. */
static int32_t readEnc() {
noInterrupts();
int32_t c = encCount;
interrupts();
return c;
}

/* Apply a duty cycle from -100 (reverse) to +100 (forward); 0 = coast.
* The magnitude goes to one direction pin, the other one stays at zero. */
static void motorSet(int duty) {
if (duty > 100) duty = 100;
if (duty < -100) duty = -100;
if (duty > -DEAD_ZONE && duty < DEAD_ZONE) duty = 0;

uint32_t d = (uint32_t)((duty < 0 ? -duty : duty) * 1023 / 100); /* 10-bit */
if (duty > 0) { ledcWrite(PIN_M1_IN1, d); ledcWrite(PIN_M1_IN2, 0); }
else if (duty < 0) { ledcWrite(PIN_M1_IN1, 0); ledcWrite(PIN_M1_IN2, d); }
else { ledcWrite(PIN_M1_IN1, 0); ledcWrite(PIN_M1_IN2, 0); }
}

void setup() {
Serial.begin(115200);
delay(200);

Serial.println("encoder motor - auto duty");

/* Both direction pins low = motor stopped. */
pinMode(PIN_M1_IN1, OUTPUT); digitalWrite(PIN_M1_IN1, LOW);
pinMode(PIN_M1_IN2, OUTPUT); digitalWrite(PIN_M1_IN2, LOW);

/* Encoder channels use the internal pull-ups and interrupt on any edge. */
pinMode(PIN_ENC_A, INPUT_PULLUP);
pinMode(PIN_ENC_B, INPUT_PULLUP);
encState = (uint8_t)((digitalRead(PIN_ENC_A) << 1) | digitalRead(PIN_ENC_B));
attachInterrupt(digitalPinToInterrupt(PIN_ENC_A), onEncoderChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_ENC_B), onEncoderChange, CHANGE);

/* 20 kHz PWM sits above the audible range, so the motor does not whine. */
ledcAttach(PIN_M1_IN1, 20000, 10);
ledcAttach(PIN_M1_IN2, 20000, 10);
motorSet(0);
}

void loop() {
static unsigned long tPrint = 0;
static int32_t lastCount = 0;
uint32_t now = millis();

/* The sine takes the duty from 0 up to 100, back through 0 down to -100:
* the wheel speeds up, stops and reverses with no abrupt step. */
int duty = (int)(100.0f * sinf(TWO_PI * (float)(now % DRIVE_PERIOD) / (float)DRIVE_PERIOD));
motorSet(duty);

/* Every 200 ms: turn the counts gained since the last print into rpm. */
if (now - tPrint >= 200) {
int32_t c = readEnc();
float dt = (now - tPrint) / 1000.0f;
int32_t d = c - lastCount;
lastCount = c;
tPrint = now;
float rpm = dt > 0.0f ? (float)d * 60.0f / ((float)kCountsPerRev * dt) : 0.0f;

Serial.print("duty="); Serial.print(duty);
Serial.print(" count="); Serial.print(c);
Serial.print(" rpm="); Serial.println(rpm, 1);
}

delay(5);
}

ステップ 2. スケッチを書き込む

先ほどと同じように書き込みます。モーターは、サイン波の最初の立ち上がり 1/4 周期でデューティ比が上がるまで静止したままです。

ステップ 3. 結果を観察する

ホイールはサイン波に合わせて加速し、停止し、反転します。シリアルモニタには 200 ms ごとに 1 行が表示されます。duty= は指令値、count= はエンコーダの累積値、rpm= はそこから算出された回転速度です。count はホイールが両方向に回転するにつれて増減し、rpm は duty と一緒に符号が変わります。下の 2 つの図と見比べてください。

次に シリアルモニタ を見てみましょう。同じ実行結果はこのように表示されます:

1 回転あたりのカウント数

kCountsPerRev = 4848 = 12 PPR × 4(クアドラチャ ×4 デコード)× 101(ギア比)です。ギアモーターを交換した場合は、この定数を更新して rpm の表示が正しいままになるようにしてください。

例 5: スマホもロボットになれる​

Honor Robot Phone は AI の頭脳で、XIAO Robot Kit はボディです。2 つを組み合わせるとコラボレーション仕様のローバーになります。スマホが判断し、キットが動きます。

8 月 12 日に広州で行われた Honor Robot Phone の発表会では、このローバーがステージ上で手を振り、ダンスし、走行し、駐車しました。この設計はオープンなので、再現したり、リミックスしたり、さらに発展させたりできます。

両者をつないでいるのは Bluetooth コマンドセットです。スマホは話し言葉の文をファームウェアコマンドに変換し、1 つのキャラクタリスティック経由で送信します。これは robot> シェルが受け付けるコマンドと同じなので、新しい挙動のために再フラッシュする必要はありません。

詳しいストーリーは Seeed Studio の投稿をお読みください:Seeed Studio put the Honor Robot Phone in motion。

使い方​

  1. ボード側では、examples/xiao_robot_kit_ble_skill/ をそのままフラッシュします。これは ESP-IDF プロジェクトです。Application, Application 2 にインストール方法、ターゲット、フラッシュ手順が説明されています。
  2. スマホ側では、YOYO エージェントストアからベーススキルをインストールします。中国語キーワード 机甲小车 で検索してください。接続されると車が応答し、この部分には開発者アカウントは不要です。
  3. 人に話しかけるのと同じように YOYO に話しかけます。"cheer up"、"happy dance" などです。YOYO が自動的にインテントを解釈し、車が動き始めます。

自分のバージョンを公開するには、Honor developer platform にサインインし、Management Center → Ecosystem Services → Agent Services を開きます。次に My Skills → Import Skill を選び、ZIP をアップロードして、Private Release でレビューと公開を行います。

注記

メニュー名や Bluetooth 機能は Honor 独自のガイドに従っています。ラベルが変更された場合は、the skill integration guide を確認してください。

よくある質問(FAQ)​

Q1: ボードを接続しても COM ポートが表示されません​

A: 充電専用の USB ケーブルを使っている可能性が高いです。データ通信対応ケーブルに交換してください。それでもポートが表示されない場合は、PC の別の USB ポートを試してください。

Q2: アップロードに失敗し、コンソールが Connecting..... を繰り返します​

A: XIAO 上の BOOT ボタンを押し続けたまま RST をタップし、BOOT を離してボードを強制的にブートローダーモードに入れてから、もう一度アップロードを開始してください。

Q3: シリアルモニタに文字化けした文字が表示されます​

A: サンプルは 115200 ボーで動作します。シリアルモニタ右下で選択されているボーレートが一致していることを確認してください。

Q4: サーボやモーターがビクッと動いてボードがリセットされます​

A: USB 電源だけで動かしていると、Motor_5V レールの電力が不足します。バッテリーコネクタにリチウム電池を接続するか、外部 5 V 電源を使用してください。詳細は「ハードウェア概要」の電源に関する注意を参照してください。

Q5: 例 4 でホイールが回っているのに rpm=0.0 と表示されます​

A: ケーブルがソケットに奥まで差し込まれ、ロックされていることを確認してください。それでも rpm が 0 のままの場合は、一度も落としたりぶつけたりしていないモーターと交換してください。モーター内部のエンコーダが損傷していると、カウントが報告されなくなります。

リソース​

ハードウェア設計

機械設計

Loading Comments...