본문 바로가기
정나우/코드

Opencr로 다이나믹셀(Dynamixel) 제어하기

by 정_나우 2022. 11. 7.

Opencr과 다이나믹셀을 연결해서 다이나믹셀을 움직여보자.

 


1. 다이나믹셀 ID, bps 설정

 

다이나믹셀을 처음 샀으면 기존에 설정되있는 값들을 바꿔줘야 한다.

 

그러기 위해서는 다이나믹셀 위자드를 이용해야 한다.

 

https://emanual.robotis.com/docs/kr/software/dynamixel/dynamixel_wizard2/

 

ROBOTIS e-Manual

 

emanual.robotis.com

다이나믹셀 위자드 2.0을 깐 뒤

 

아래 그림과 같이 U2D2, SMPS2Dynamixel, 12V 5A SMPS을 이용해서 다이나믹셀과 노트북을 연결해준다.

 

그리고 검색해서 다이나믹셀을 찾는다. (보통 57600 bps, ID:1로 설정되어 있는 거 같음)

검색 조건을 조절해서 범위를 좁히면 빠르게 검색이 가능하다.

그리고 다이나믹셀의 설정 값을 자신의 상황에 맞게 바꿔준다. (필자는 115200bps, 다이나믹셀이 두 개여서 ID:1,2로 설정)

 


2. Arduino IDE

 

Software

Open-source electronic prototyping platform enabling users to create interactive electronic objects.

www.arduino.cc

아두이노 IDE를 설치해줍니다.

 

그리고 OpenCR 보드 매니저와 다이나믹셀 라이브러리를 다운받아 줍니다.

 

 

기존의 Arduino IDE를 사용하시는 분은 이 유튜브를 보고 따라하면 되고, Arduino IDE 2.0.1을 설치한 분은 

 

 

이렇게 보드와 라이브러리를 설치해줍니다.


3. Code

 

컴퓨터, OpenCR, 다이나믹셀을 다음과 같이 연결합니다.

Dynamixel2Arudino 라이브러리에 있는 예제를 참고해서 시리얼 모니터에 입력하는 값에 따라 모터가 움직일 수 있도록 코드를 작성합니다.

 

#include <Dynamixel2Arduino.h>

#if defined(ARDUINO_OpenCR)
#define DXL_SERIAL   Serial3
#define DEBUG_SERIAL Serial
const uint8_t DXL_DIR_PIN = 84; // OpenCR Board's DIR PIN.
#endif

const uint8_t DXL_ID_1 = 1;
const uint8_t DXL_ID_2 = 2;
const float DXL_PROTOCOL_VERSION = 1.0;

Dynamixel2Arduino dxl(DXL_SERIAL, DXL_DIR_PIN);

using namespace ControlTableItem;

void setup() {
  // put your setup code here, to run once:

  // Use UART port of DYNAMIXEL Shield to debug.
  DEBUG_SERIAL.begin(115200);

  // Set Port baudrate to 57600bps. This has to match with DYNAMIXEL baudrate.
  dxl.begin(115200);
  // Set Port Protocol Version. This has to match with DYNAMIXEL protocol version.
  dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
  // Get DYNAMIXEL information
  dxl.ping(DXL_ID_1);

  // Turn off torque when configuring items in EEPROM area
  dxl.torqueOff(DXL_ID_1);
  dxl.setOperatingMode(DXL_ID_1, OP_VELOCITY);
  dxl.torqueOn(DXL_ID_1);

  dxl.ping(DXL_ID_2);

  // Turn off torque when configuring items in EEPROM area
  dxl.torqueOff(DXL_ID_2);
  dxl.setOperatingMode(DXL_ID_2, OP_VELOCITY);
  dxl.torqueOn(DXL_ID_2);
}

void loop()
{
  char input=' '; 
  if (Serial.available()) {
    input = Serial.read();
  }

  if(input=='a') dxl.setGoalVelocity(DXL_ID_1, 1523);
  if(input=='d') dxl.setGoalVelocity(DXL_ID_1, 500);
  if(input=='w') dxl.setGoalVelocity(DXL_ID_2, 1523);
  if(input=='s') dxl.setGoalVelocity(DXL_ID_2, 500);
  if(input=='q') dxl.setGoalVelocity(DXL_ID_1, 0);
  if(input=='q') dxl.setGoalVelocity(DXL_ID_2, 0);
}

 

void loop() 윗 부분은 ID와 bps만 자신이 설정한 환경에 맞게 바꿔주면 되고

 

밑 부분은 다음과 같은 양식으로 자신의 조건에 맞게 다이나믹셀을 제어해주면 됩니다.

 

(필자는 지금 wheel 모드로 사용 중)

 

코드 작성을 완료하면 port와 board를 알맞게 설정을 해주고 업로드를 하면 됩니다.

 

(업로드를 할 때는 SMPS 전원이 굳이 필요하지는 않지만 모터 제어 시 필요함.)

 

 

 

댓글