1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5AtomによるRoller485の制御

Posted at

はじめに

本記事では、M5Atomを用いてUnit-Roller485を制御する。
(本記事で使用しているM5Atomは「M5Atom-Matrix」である。)

目的

M5Atomをコントローラーとして使用し、I2C通信を用いてRoller485の制御を行う。

準備物

○ハードウェア系

  • PC(Windows10)
  • M5Atom
  • Unit-Roller485
  • USB Type-Cケーブル(PC-M5Atom接続用)

○ソフトウェア系

  • Arduino IDE
    →インストールについては、こちらを参照とする。

接続準備

事前準備として、

  • Roller485の通信設定をI2C通信に変更する。
  • I2C通信時のRoller485のアドレスを確認する。
    →筆者環境については、「0x40」を使用。
    ※上記2項目の確認についてはRoller485電源投入時に、背面にあるボタンを押下しながら電源を投入することで、設定画面を確認することができる。(確認画面を以下に示す。)
  • M5AtomのI2C通信用ピン(SDASCL)が何番かを確認する。
     →「SDA:26・SCL:32」をそれぞれ使用する。

実行スクリプト

Roller485 通信プロトコル(I2C通信用)を参考にコマンドを送信する。

動作としては、

  • LED下スイッチによる動作切替
     →・短押し:ON/OFFの切替
     ・長押し:CW/CCWの切替
  • ダイヤルによるモータ回転速度の調整・方向変更(エンコーダ値使用)
    となる。

本記事で実際に使用したソースコードを以下に示す。
※ボードは「M5Atom」を選択する。

m5atom.ino
// ----------------------------------------------------------------------------------------------------
// ライブラリの読込
#include "M5Atom.h"
#include <Wire.h>

// ----------------------------------------------------------------------------------------------------
// マクロの定義

// ON/OFF
#define ON  true
#define OFF false
#define CW  true
#define CCW false

// I2C通信
#define ROLLER_ADDRESS 0x40  // Unit-Roller485 I2Cアドレス
#define I2C_SDA        26    // M5Atom I2C通信ポート(SDA)
#define I2C_SCL        32    // M5Atom I2C通信ポート(SCL)

// ----------------------------------------------------------------------------------------------------
// グローバル変数・定数の定義

// スイッチ状態
bool motor_sw_state  = OFF;
bool rotate_sw_state = CW;

// モータ回転速度
int32_t  motor_speed    = 100;
uint8_t  motor_bytes[4] = {0, 0, 0, 0};
uint32_t press_time     = 2000;   // 長押し検知時間: 2000[msec]
bool     long_press     = false;  // 長押し検知フラグ


// ----------------------------------------------------------------------------------------------------
// 関数の定義

// int32 --> byteに変換(Roller485-Unit用)
void cvtInt2Bytes(void) {
  uint32_t remainder = motor_speed * 100;  // コマンド変換用にx100
  // char speed_str[4];  // デバッグ用

  for(int i = 0; i < 4; i++) {
    if (rotate_sw_state) {
      motor_bytes[i] = remainder % 256;  // CW
    }
    else {
      motor_bytes[i] = ~(remainder % 256);  // CCW
    }
    remainder /= 256;
  }
}

// M5Atom LED点灯色変更
void controlLED(){
  if (motor_sw_state) {
    if (rotate_sw_state) {
      // LEDを緑色で点灯
      M5.dis.fillpix(0x00ff00);
    }
    else{
      // LEDを青色で点灯
      M5.dis.fillpix(0x0000ff);
    }
  }
  else{
    // LEDを赤色で点灯
    M5.dis.fillpix(0xff0000);
  }
}

// モータON/OFF切替
void motorSW() {
  uint8_t on_command[2]  = {0x00, 0x01};  // モータONコマンド
  uint8_t off_command[2] = {0x00, 0x00};  // モータOFFコマンド

  uint8_t length = 2;  // コマンド長

  if (motor_sw_state) {
    // ON --> OFF
    motor_sw_state = OFF;
    // コマンド送信
    sendCommand(off_command, length);
  }
  else {
    // OFF --> ON
    motor_sw_state = ON;
    // コマンド送信
    sendCommand(on_command, length);
  }
}

// 回転方向切替
void rotateSW() {
  if (rotate_sw_state) {
    // CW --> CCW
    rotate_sw_state = CCW;
  }
  else {
    // CCW --> CW
    rotate_sw_state = CW;
  }
}

// 速度
void controlSpeed() {
  uint8_t command[5] = {0x40, motor_bytes[0], motor_bytes[1], motor_bytes[2], motor_bytes[3]};  // 速度設定コマンド
  uint8_t length = 5;  // コマンド長

  // コマンドの送信
  sendCommand(command, length);
}

// コマンド送信
void sendCommand(uint8_t* command, uint8_t length) {
  Wire.beginTransmission(ROLLER_ADDRESS);  // ポート オープン
  for (int i = 0; i < length; i++) {
    Wire.write(command[i]);  // コマンド送信(I2C)
  }
  Wire.endTransmission(true);  // ポート クローズ
}

// ----------------------------------------------------------------------------------------------------
// メイン処理

void setup() {
  uint8_t off_command[2] = {0x00, 0x00};  // モータOFFコマンド
  uint8_t length = 2;                     // コマンド長
  motor_sw_state = OFF;                   // モータOFF

  // I2C通信 初期化
  Wire.begin(I2C_SDA, I2C_SCL);
  // M5Atom 初期化
  M5.begin(true, true, true);
  // LED表示変更
  controlLED();
}

void loop() {
  M5.update();  // 状態更新

  // LED下スイッチ長押し時
  if (M5.Btn.pressedFor(press_time)){
    // 長押し後の状態ではない場合
    if (long_press == false) {
      // CW/CCWの切替
      rotateSW();
      // モータ回転速度の設定
      cvtInt2Bytes();
      controlSpeed();
      // LED表示変更
      controlLED();

      long_press = true;  // 長押し後状態に
    }
  }
  
  // LED下スイッチ押下時
  if (M5.Btn.wasReleased()) {
    // 長押し後の状態の場合
    if (long_press) {
      long_press = false;  // 状態のリセット
    }
    // 短押し時
    else{
      // ON/OFFの切替
      motorSW();
      // モータ回転速度の設定
      if (motor_sw_state){
        cvtInt2Bytes();
        controlSpeed();
      }
      // LED表示変更
      controlLED();
    }
  }
}

実行結果

実行結果の動画を以下に示す。
動画では、

  1. 起動(LED:赤色)
  2. M5Atom LED下ボタンを短押し
    →モータON(CW、LED:青色)
  3. M5Atom LED下ボタンを長押し
    →モータ回転:CCWに切替(LED:青色)
  4. M5Atom LED下ボタンを長押し
    →モータ回転:CWに切替(LED:緑色)
  5. M5Atom LED下ボタンを短押し
    →モータOFF(LED:赤色)
    の順番で操作を実施している。

上記より、M5Atomをコントローラーとして使用し、I2C通信を用いてRoller485の制御をすることができた。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?