0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

M5StackのAtomMotionをM5Unifiedと共に使うClass

Posted at

はじめに

M5StackのAtomMotionを、いつも利用しているM5Unifiedと連携して使いやすくしたライブラリを作りました。Unit Encoder用のClassを作った話と動機は同じです。

メンバ関数はM5Stack公式のスケッチ例にあるAtomMotion.(cpp|h)を継承しています。

ライブラリ共有

GitHubにリポジトリを作りました。lib/AtomMotion_Class/AtomMotion_Class.cppが該当します。platformioのプロジェクトの形になってます。

初期化

特に宣言はありません。M5Stack系・M5Stick系はサイド、M5Atom系は底のピンのI2Cにつながるものとして初期化されます。M5Unifiedによって自動判別で適切に設定されます。
変更する場合は、コンストラクタの引数で指定できます。

main.cpp
#include <M5Unified.h>
#include <AtomMotion_Class.hpp>

m5::AtomMotion Motion;

void setup() {
  auto cfg = M5.config();
  M5.begin(cfg);

  Motion.SetServoAngle(1, 90);
}

メンバ関数

コンストラクタ

  AtomMotion(std::uint8_t i2c_addr = DEFAULT_ADDRESS, std::uint32_t freq = 400000, I2C_Class* i2c = &In_I2C);

パラメータ

  • i2c_addr : アドレス値。AtomMotionは0x38です。
  • freq : クロック数
  • i2c : I2C_ClassでSCL/SDAの番号を設定しています。

SetServoAngle

  std::uint8_t SetServoAngle(std::uint8_t Servo_CH, std::uint8_t angle);

サーボモーターを指定した角度にします。Servo Kit 180‘ を使用することが前提となっていると思います。angleの値とサーボモーターの角は機種ごとに確認したほうが良いです。

パラメータ

  • Servo_CH : サーボ番号(1~4)
  • angle : 角度(0~180)

戻り値

  • 0 : 成功
  • 1 : 引数が範囲外

SetServoPulse

  std::uint8_t SetServoPulse(std::uint8_t Servo_CH, std::uint16_t width);

サーボモーターのパルス幅を設定します。1500が中央値です。360度連続回転サーボを使う場合、1500で停止するようにプラスドライバーを使ってモーター本体を調整しましょう。

パラメータ

  • Servo_CH : サーボ番号(1~4)
  • width : パルス幅(500~2500)

戻り値

  • 0 : 成功
  • 1 : 引数が範囲外

SetMotorSpeed

  std::uint8_t SetMotorSpeed(std::uint8_t Motor_CH, std::int8_t speed);

Atom Motionの2ポートあるブラシモーターの速度を設定します。0で停止、プラス・マイナスで回転方向が変わります。

パラメータ

  • Servo_CH : モーター番号(1~2)
  • speed : -127~127

戻り値

  • 0 : 成功
  • 1 : 引数が範囲外

ReadServoAngle

  std::uint8_t ReadServoAngle(std::uint8_t Servo_CH);

サーボモーターの角度を取得します。

パラメータ

  • Servo_CH : サーボ番号(1~4)

戻り値

  • 角度

ReadServoPulse

  std::uint16_t ReadServoPulse(std::uint8_t Servo_CH);

サーボモーターのパルス幅を取得します。

パラメータ

  • Servo_CH : サーボ番号(1~4)

戻り値

  • パルス幅

ReadMotorSpeed

  std::int8_t ReadMotorSpeed(std::uint8_t Motor_CH);

ブラシモーターの速度を取得します。

パラメータ

  • Servo_CH : ブラシモーター番号(1~2)

戻り値

  • 速度
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?