모터와 블루투스 모듈을 각각 테스트해보았으니
이번엔 로보티즈 모터를 블루투스 모듈로 제어해보려고 합니다.
회로를 다음과 같이 연결합니다.
회로를 구성한 후, 여태까지 각각 작업했던 코드(모터, 블루투스)를 다음과 같이 병합하여 작성하고 업로드를 실시합니다.
#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);
}
}
}
업로드가 완료된 후 앱과 아두이노를 연결했더니 잘 작동하네요.
'컴퓨터 > 아두이노' 카테고리의 다른 글
아두이노 우노를 활용한 드로잉 로봇 만들기 (0) | 2017.12.04 |
---|---|
아두이노 메가 2560을 활용한 블루투스 통신 테스트(LED제어) (0) | 2017.12.04 |
아두이노 메가 2560을 활용한 로보티즈 모터(AX-18A) 제어 (0) | 2017.11.30 |
아두이노 우노를 활용한 로보티즈 모터(AX-18A) 3축 제어 (0) | 2017.11.28 |
아두이노 메가 2560을 활용한 LED 제어 (0) | 2017.11.28 |