본문 바로가기
컴퓨터/아두이노

아두이노 우노 활용한 로보티즈 모터(AX-18A)의 블루투스 제어

by 로봇과학자 2017. 12. 4.

모터와 블루투스 모듈을 각각 테스트해보았으니

이번엔 로보티즈 모터를 블루투스 모듈로 제어해보려고 합니다.

 

회로를 다음과 같이 연결합니다.

 

 

회로를 구성한 후, 여태까지 각각 작업했던 코드(모터, 블루투스)를 다음과 같이 병합하여 작성하고 업로드를 실시합니다.

 

#include "SoftwareSerial.h"
#include "DynamixelMotor.h"

 

// defin Rx, Tx number
int Tx=11;  
int Rx=10;  

// define id of the motor
const uint8_t id=1;
// define speed, between 0 and 1023
int16_t speed=512;
// define communication baudrate
const long unsigned int baudrate = 1000000;

 

SoftwareSerial BT(Tx, Rx);

HardwareDynamixelInterface interface(Serial);
DynamixelMotor motor(interface, id);

 

void setup() 
{
  interface.begin(baudrate);
  delay(100);
 
  uint8_t status=motor.init();
 
  if(status!=DYN_STATUS_OK)
  {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    while(1);
  }
  motor.enableTorque(); 

 

  // set to joint mode, with a 180° angle range
  // see robotis doc to compute angle values
  motor.jointMode(204, 820);
  motor.speed(speed);
 
  BT.begin(9600);
  BT.println("Conneted up to Arduino");
}

 

char master_input;
void loop()
{
  if (BT.available())
  {
    master_input=(BT.read());
    if (master_input=='1')
    {
      motor.goalPosition(258);
      BT.println("Move Right");
      delay(500);
    }
    if (master_input=='2')
    {
      motor.goalPosition(512);
      BT.println("Move Center");
      delay(500);
     
    }
    if (master_input=='3')
    {
      motor.goalPosition(666);
      BT.println("Move Left");
      delay(500);
    }
  }
}

 

업로드가 완료된 후 앱과 아두이노를 연결했더니 잘 작동하네요.