ATOMIC Motion ベースで GeekServo 9G 360° Motor-Orange を動かしたメモ

ATOMIC Motion ベースで GeekServo 9G 360° Motor-Orange を動かしたメモ

ATOMIC Motion ベースで GeekServo 9G 360° Motor-Orange を動かしたメモです。

背景

ATOMIC Motionベースで GeekServo 9G 360° Motor-Orange を動かしてみます。

今回使う GeekServo 9G 360° Motor-Orange はこちらです。

GeekServo 9G 360° Motor-Orange — スイッチサイエンス

ATOMIC Motionベースは v1.0 で試しています。

また、v1.2 も持っていて、同じくうまく動きました。仕様が同じなのありがたいです。

実際のプログラム

ATOMIC Motion のサンプル で setServoAngle の関数で動いたので、ボタンで停止・前進・後退で動くようにしました。

前進・後退といっても回転・逆回転するという感じです。このサンプルは 4 ch 全部同じように動きます。

#include <M5Unified.h>
#include "M5AtomicMotion.h"

// AtomicMotion の呼び出し
M5AtomicMotion AtomicMotion;

// モーションの状態を管理する変数
// 0: 停止, 1: 前進, 2: 後退
int motionState = 0;

void stopAllMotors()
{
  // 全チャンネルを停止
  for (int ch = 0; ch < 4; ch++)
  {
    AtomicMotion.setServoAngle(ch, 90);
    Serial.printf("Servo Channel %d: STOP (0)\n", ch);
  }
}

void moveForward()
{
  // 全チャンネルを前進
  for (int ch = 0; ch < 4; ch++)
  {
    AtomicMotion.setServoAngle(ch, 180);
    Serial.printf("Servo Channel %d: FORWARD (180)\n", ch);
  }
}

void moveBackward()
{
  // 全チャンネルを後退
  for (int ch = 0; ch < 4; ch++)
  {
    AtomicMotion.setServoAngle(ch, 0);
    Serial.printf("Servo Channel %d: BACKWARD (90)\n", ch);
  }
}

// 個別チャンネル制御用の関数(必要に応じて使用)
void setChannelAngle(int channel, int angle)
{
  if (channel >= 0 && channel < 4)
  {
    AtomicMotion.setServoAngle(channel, angle);
    Serial.printf("Servo Channel %d: %d degrees\n", channel, angle);
  }
}

void handleButtonPress()
{
  motionState = (motionState + 1) % 3; // 0→1→2→0のループ

  M5.Display.clear();
  M5.Display.setCursor(0, 0);
  M5.Display.startWrite();

  switch (motionState)
  {
  case 0:
    // 停止
    stopAllMotors();
    M5.Display.setTextColor(RED);
    M5.Display.println("STOP");
    M5.Display.setTextColor(WHITE);
    M5.Display.println("All motors");
    M5.Display.println("stopped");
    Serial.println("Motion: STOP");
    break;

  case 1:
    // 前進
    moveForward();
    M5.Display.setTextColor(GREEN);
    M5.Display.println("FORWARD");
    M5.Display.setTextColor(WHITE);
    M5.Display.println("All motors");
    M5.Display.println("forward");
    Serial.println("Motion: FORWARD");
    break;

  case 2:
    // 後退
    moveBackward();
    M5.Display.setTextColor(BLUE);
    M5.Display.println("BACKWARD");
    M5.Display.setTextColor(WHITE);
    M5.Display.println("All motors");
    M5.Display.println("backward");
    Serial.println("Motion: BACKWARD");
    break;
  }

  M5.Display.endWrite();
}

void setup()
{

  // M5Stack初期設定用の構造体を代入
  auto cfg = M5.config();

  // M5デバイスの初期化
  M5.begin(cfg);

  M5.Display.setTextColor(GREEN);
  M5.Display.setTextSize(2);
  M5.Display.setCursor(0, 0);
  M5.Display.startWrite();
  M5.Display.println("Atomic Init");
  M5.Display.endWrite();

  // AtomicMotionの初期化
  while (!AtomicMotion.begin(&Wire, M5_ATOMIC_MOTION_I2C_ADDR, 38, 39, 100000))
  {
    M5.Display.clear();
    M5.Display.setCursor(0, 0);
    M5.Display.startWrite();
    M5.Display.println("Init Fail");
    M5.Display.endWrite();
    Serial.println("Atomic Motion begin failed");
    delay(1000);
  }

  // 初期状態で全チャンネルを停止
  stopAllMotors();

  M5.Display.clear();
  M5.Display.setCursor(0, 0);
  M5.Display.startWrite();
  M5.Display.println("Ready");
  M5.Display.println("Press Display:");
  M5.Display.println("Stop->Forward");
  M5.Display.println("->Backward");
  M5.Display.endWrite();
  Serial.println("Atomic Motion Ready");
}

void loop()
{
  M5.update();

  // ボタンが押されたかチェック
  if (M5.BtnA.wasPressed())
  {
    handleButtonPress();
  }

  delay(10);
}

角度からの実際の動き

今回は最大速度で回転・逆回転してますが、こまごまパラメータをチェックして、だいたいこのように動きました。

  • 回転
    • 0 が最大速度 82 に近づくほど減速
  • 停止
    • 82 - 98
  • 逆回転
    • 98 から始まり 180 が最大速度

実際に詳細を作るときは、これをもとにいろいろやっている予定です!