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

아두이노 메가 2560을 활용한 로보티즈 모터(AX-18A) 제어

by 로봇과학자 2017. 11. 30.

 

이전글에서 아두이노 우노를 통한 로보티즈 모터(AX-18A) 제어 방법을 소개했었습니다.(http://hyungc.tistory.com/2)

이번엔 아두이노 메가 2560을 사용하여 로보티즈 모터(AX-18A) 제어 해보려 합니다.

우노에서 작업한 코드를 아두이노 메가에 2560에 업로드를 했으나 모터가 움직이질 않네요.

 

왜 안되는지 이유를 찾아봅니다.

먼저, 아두이노 메가 2560은 우노와 달리 시리얼 통신을 할 수 있는 RX, TX포트가 4개나 있습니다.

 

 

모터의 Data라인을 RX1, TX1에 연결하여 코드를 업로드하니 당연히 작동하지 않습니다. (RX0, TX0에 연결하였으면 기존 코드로도 작동했을겁니다.)

 

그럼 RX1, TX1에 연결하여 모터를 제어하려면 어떻게 해야하나 살펴봅니다.

그 방법은 생각보다 쉽네요.

 

기존의 코드에서 HardwareDynamixelInterface interface(Serial); 이 부분을

HardwareDynamixelInterface interface(Serial1); 이렇게 바꿔주면 되네요.

 

// Test motor control for arduino Mega 2560

#include "DynamixelMotor.h"

 

// id of the motor
const uint8_t id=1;

// speed, between 0 and 1023
int16_t speed=512;

// communication baudrate
const long unsigned int baudrate = 1000000;

 

// hardware serial without tristate buffer
// see blink_led example, and adapt to your configuration

HardwareDynamixelInterface interface(Serial1);

DynamixelMotor motor(interface, id);

 

void setup()
{
  interface.begin(baudrate);
  delay(100);
 
  // check if we can communicate with the motor
  // if not, we turn the led on and stop here
  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);
}

 

void loop()
{
  // go to middle position
  motor.goalPosition(512);
  delay(500);

 

  // move 45° CCW
  motor.goalPosition(666);
  delay(500);

 

  // go to middle position
  motor.goalPosition(512);
  delay(500);

 

  // move 45° CW
  motor.goalPosition(358);
  delay(500);
}

다시 살펴보면,

RX0, TX0 은 Serial

RX1, TX1 은 Serial1

RX2, TX2 은 Serial2

RX3, TX3 은 Serial3

을 사용하여 통신할 수 있습니다.

 

이렇게 해결하니 아두이노 메가 2560에서도 로보티즈 모터를 제어할 수가 있네요.