|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright 2016 ROBOTIS CO., LTD. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*******************************************************************************/ |
| 16 | + |
| 17 | +#include <Dynamixel2Arduino.h> |
| 18 | + |
| 19 | +// Please modify it to suit your hardware. |
| 20 | +#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_MEGA2560) // When using DynamixelShield |
| 21 | + #include <SoftwareSerial.h> |
| 22 | + SoftwareSerial soft_serial(7, 8); // DYNAMIXELShield UART RX/TX |
| 23 | + #define DXL_SERIAL Serial |
| 24 | + #define DEBUG_SERIAL soft_serial |
| 25 | + const uint8_t DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN |
| 26 | +#elif ARDUINO_OpenCM904 // Official ROBOTIS board with DXL circuit. |
| 27 | + #define DXL_SERIAL Serial3 //OpenCM9.04 EXP Board's DXL port Serial. (Serial1 for the DXL port on the OpenCM 9.04 board) |
| 28 | + #define DEBUG_SERIAL Serial |
| 29 | + const uint8_t DXL_DIR_PIN = 22; //OpenCM9.04 EXP Board's DIR PIN. (28 for the DXL port on the OpenCM 9.04 board) |
| 30 | +#elif ARDUINO_OpenCR // Official ROBOTIS board with DXL circuit. |
| 31 | + // For OpenCR, there is a DXL Power Enable pin, so you must initialize and control it. |
| 32 | + // Reference link : https://github.com/ROBOTIS-GIT/OpenCR/blob/master/arduino/opencr_arduino/opencr/libraries/DynamixelSDK/src/dynamixel_sdk/port_handler_arduino.cpp#L78 |
| 33 | + #define DXL_SERIAL Serial3 |
| 34 | + #define DEBUG_SERIAL Serial |
| 35 | + const uint8_t DXL_DIR_PIN = 84; //OpenCM9.04 EXP Board's DIR PIN. (To use the DXL port on the OpenCM 9.04 board, you must use 28 for DIR PIN.) |
| 36 | +#else // When using DynamixelShield |
| 37 | + #define DXL_SERIAL Serial1 |
| 38 | + #define DEBUG_SERIAL Serial |
| 39 | + const uint8_t DXL_DIR_PIN = 2; // DYNAMIXEL Shield DIR PIN |
| 40 | +#endif |
| 41 | + |
| 42 | +class NewSerialPortHandler : public DYNAMIXEL::SerialPortHandler |
| 43 | +{ |
| 44 | + public: |
| 45 | + NewSerialPortHandler(HardwareSerial& port, const int dir_pin = -1) |
| 46 | + : SerialPortHandler(port, dir_pin), port_(port), dir_pin_(dir_pin) |
| 47 | + {} |
| 48 | + |
| 49 | + virtual size_t write(uint8_t c) override |
| 50 | + { |
| 51 | + size_t ret = 0; |
| 52 | + digitalWrite(DXL_DIR_PIN, HIGH); |
| 53 | + while(digitalRead(DXL_DIR_PIN) != HIGH); |
| 54 | + |
| 55 | + ret = port_.write(c); |
| 56 | + |
| 57 | + port_.flush(); |
| 58 | + digitalWrite(DXL_DIR_PIN, LOW); |
| 59 | + while(digitalRead(DXL_DIR_PIN) != LOW); |
| 60 | + |
| 61 | + return ret; |
| 62 | + } |
| 63 | + |
| 64 | + virtual size_t write(uint8_t *buf, size_t len) override |
| 65 | + { |
| 66 | + size_t ret; |
| 67 | + digitalWrite(DXL_DIR_PIN, HIGH); |
| 68 | + while(digitalRead(DXL_DIR_PIN) != HIGH); |
| 69 | + |
| 70 | + ret = port_.write(buf, len); |
| 71 | + |
| 72 | + port_.flush(); |
| 73 | + digitalWrite(DXL_DIR_PIN, LOW); |
| 74 | + while(digitalRead(DXL_DIR_PIN) != LOW); |
| 75 | + |
| 76 | + return ret; |
| 77 | + } |
| 78 | + |
| 79 | + private: |
| 80 | + HardwareSerial& port_; |
| 81 | + const int dir_pin_; |
| 82 | +}; |
| 83 | + |
| 84 | +const uint8_t DXL_ID = 1; |
| 85 | +const float DXL_PROTOCOL_VERSION = 2.0; |
| 86 | + |
| 87 | +Dynamixel2Arduino dxl; |
| 88 | +NewSerialPortHandler dxl_port(DXL_SERIAL, DXL_DIR_PIN); |
| 89 | + |
| 90 | + |
| 91 | +void setup() { |
| 92 | + // put your setup code here, to run once: |
| 93 | + |
| 94 | + // Use Serial to debug. |
| 95 | + DEBUG_SERIAL.begin(115200); |
| 96 | + |
| 97 | + // Set Port instance |
| 98 | + dxl.setPort(dxl_port); |
| 99 | + // Set Port baudrate to 57600bps. This has to match with DYNAMIXEL baudrate. |
| 100 | + dxl.begin(57600); |
| 101 | + // Set Port Protocol Version. This has to match with DYNAMIXEL protocol version. |
| 102 | + dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION); |
| 103 | +} |
| 104 | + |
| 105 | +void loop() { |
| 106 | + // put your main code here, to run repeatedly: |
| 107 | + |
| 108 | + DEBUG_SERIAL.print("PROTOCOL "); |
| 109 | + DEBUG_SERIAL.print(DXL_PROTOCOL_VERSION, 1); |
| 110 | + DEBUG_SERIAL.print(", ID "); |
| 111 | + DEBUG_SERIAL.print(DXL_ID); |
| 112 | + DEBUG_SERIAL.print(": "); |
| 113 | + if(dxl.ping(DXL_ID) == true){ |
| 114 | + DEBUG_SERIAL.print("ping succeeded!"); |
| 115 | + DEBUG_SERIAL.print(", Model Number: "); |
| 116 | + DEBUG_SERIAL.println(dxl.getModelNumber(DXL_ID)); |
| 117 | + }else{ |
| 118 | + DEBUG_SERIAL.println("ping failed!"); |
| 119 | + } |
| 120 | + delay(500); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | + |
0 commit comments