質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

Q&A

2回答

3437閲覧

Arduino言語によるロータリーエンコーダ付きモーターの制御方法

kujir

総合スコア0

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

0グッド

0クリップ

投稿2021/11/05 08:17

Arduinoにおいてロータリーエンコーダ付きのモータを二つ位相が合うように回転させようと考えています.

プログラムを組んだのですが,PWMで制御はできるのですが,ロータリーエンコーダからのカウンタ値を読み取ることができません.

よろしくお願いします.

#include <Rotary.h>

Rotary r1 = Rotary(2, 3); //モーター①用エンコーダー
Rotary r2 = Rotary(4, 5); //モーター②用エンコーダー

//モーター①用エンコーダー入力カウンター
int motor1_count = 0;
//モーター②用エンコーダー入力カウンター
int motor2_count = 0;

//モーター①用PWM信号
int motor1_pwm=0;
//モーター②用PWM信号
int motor2_pwm=0;

//モーター①出力ピン
const int motor1_B_IA = 10; //正転
const int motor1_B_IB = 11; //逆転

//モーター②出力ピン
const int motor2_A_IA = 9; //正転
const int motor2_A_IB = 6; //逆転

//モーター①駆動トリガー
int motor1_on = 1;
//モーター②駆動トリガー
int motor2_on = 1;

//モーター正転・逆転を決定する変数
int motor1_LR = 0; //モーター①:初期値は正転
int motor2_LR = 0; //モーター②:初期値は正転

//モーター用カウンター値表示用変数
String motor1_monitor="";
String motor2_monitor="";

void setup() {
Serial.begin(9600);
//モーター①用エンコーダー入力割り込み
attachInterrupt(digitalPinToInterrupt(2), motor1, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), motor1, CHANGE);

//モーター②用エンコーダー入力割り込み
attachInterrupt(digitalPinToInterrupt(4), motor2, CHANGE);
attachInterrupt(digitalPinToInterrupt(5), motor2, CHANGE);

//モーター①②出力ピン準備
pinMode(motor1_B_IA,OUTPUT); //モーター①
pinMode(motor1_B_IB,OUTPUT); //モーター①
pinMode(motor2_A_IA,OUTPUT); //モーター②
pinMode(motor2_A_IB,OUTPUT); //モーター②

}

void loop() {

//モーター①ON・OFF制御
if (motor1_on == 1){
motor1_pwm = 50;
} else if (motor1_on == 0){
motor1_pwm = 0;
}

//モーター②ON・OFF制御
if (motor2_on == 1){
motor2_pwm = 50;
} else if (motor2_on == 0){
motor2_pwm = 0;
}

//モーター①駆動ON:正転・逆転制御
if (motor1_LR == 0){
analogWrite(motor1_B_IA,motor1_pwm); //正転
}else if(motor2_LR == 1){
analogWrite(motor1_B_IB,motor1_pwm); //逆転
}

//モーター②駆動ON:正転・逆転制御
if (motor2_LR == 0){
analogWrite(motor2_A_IA,motor2_pwm); //正転
}else if(motor2_LR == 1){
analogWrite(motor2_A_IB,motor2_pwm); //逆転
}

}

void motor1() {
//エンコーダー状態入力
unsigned char result1 = r1.process();

if (result1 == DIR_NONE) {
// do nothing
}
else if (result1 == DIR_CW) {
motor1_count++;
motor1_monitor = "Motor1【Right】:Count = " + String(motor1_count);
Serial.println(motor1_monitor);
}
else if (result1 == DIR_CCW) {
motor1_count++;
motor1_monitor = "Motor1【Left】:Count = " + String(motor1_count);
Serial.println(motor1_monitor);
}

if (motor1_count > 50){
motor1_on = 0;
motor1_count = 0;
motor1_monitor = "Motor1 CountMAX:Return to zero:Count = " + String(motor1_count);
Serial.println(motor1_monitor);
}
}

void motor2() {
//エンコーダー状態入力
unsigned char result2 = r2.process();

if (result2 == DIR_NONE) {
// do nothing
}
else if (result2 == DIR_CW) {
motor2_count++;
motor2_monitor = "Motor2【Right】:Count = " + String(motor2_count);
Serial.println(motor2_monitor);
}
else if (result2 == DIR_CCW) {
motor2_count++;
motor2_monitor = "Motor2【Left】:Count = " + String(motor2_count);
Serial.println(motor2_monitor);
}

if (motor2_count > 50){
motor2_on = 0;
motor2_count = 0;
motor2_monitor = "Motor2 CountMAX:Return to zero:Count = " + String(motor2_count);
Serial.println(motor2_monitor);
}

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2021/11/05 08:35

このままではコードが読みづらいので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
y_waiwai

2021/11/05 08:37

で、シリアルの出力がどうなるのか提示して下さい
guest

回答2

0

「よろしくお願いします」って、何をお願いされるのでしょうか。

お使いのロータリーエンコーダが単純な2相のパルス出力のものなら、「カウンタ値」なんていうものはありませんし、同期したいのならパルスの位相差が問題で「カウンタ値」の出る幕はないのでは?
具体的にどんな部品/ライブラリを使ってなにをするのか、どういう結果が得られれば満足なのか、問題点はなにか、そして回答に何を求めているのかちゃんと説明してください。

投稿2021/11/05 14:28

thkana

総合スコア7610

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

Arduino UNO で使える 外部割り込みピンは、(2),(3) のみです (4),(5) は使えません

割込みピン確認 プログラム

void intPin(unsigned char num ){ char dat; Serial.print(" digitalPinToInterrupt("); Serial.print( num ); Serial.print(") = "); dat = digitalPinToInterrupt( num ); Serial.print( dat , DEC ); if ( dat == -1 ){ Serial.print(" no support "); } Serial.println(); } void setup() { Serial.begin(9600); intPin( 2 ); intPin( 3 ); intPin( 4 ); intPin( 5 ); } void loop() { }

実行結果

digitalPinToInterrupt(2) = 0

digitalPinToInterrupt(3) = 1
digitalPinToInterrupt(4) = -1 no support
digitalPinToInterrupt(5) = -1 no support

PinChangeInterruptライブラリ を 使うと Arduino UNO 全てのピンで外部割込みが使用可能です
PinChangeInterrupt

投稿2021/11/05 12:33

koujikuu

総合スコア401

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問