28 #if defined(__linux__) || defined(__APPLE__)
32 #define STDIN_FILENO 0
33 #elif defined(_WIN32) || defined(_WIN64)
41 #include "../../include/mercury_sdk/mercury_sdk.h"
46 #define ADDR_MCY_TORQUE_ENABLE 0x30
47 #define ADDR_MCY_GOAL_POSITION 0x4e
48 #define ADDR_MCY_PRESENT_POSITION 0x5a
51 #define LEN_MCY_GOAL_POSITION 4
52 #define LEN_MCY_PRESENT_POSITION 4
55 #define BAUDRATE 1000000
56 #define DEVICENAME "/dev/ttyACM1"
59 #define TORQUE_ENABLE 1
60 #define TORQUE_DISABLE 0
61 #define MCY_MINIMUM_POSITION_VALUE -2000
62 #define MCY_MAXIMUM_POSITION_VALUE 2000
63 #define MCY_MOVING_STATUS_THRESHOLD 20
65 #define ESC_ASCII_VALUE 0x1b
69 #if defined(__linux__) || defined(__APPLE__)
70 struct termios oldt, newt;
72 tcgetattr(STDIN_FILENO, &oldt);
74 newt.c_lflag &= ~(ICANON | ECHO);
75 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
77 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
79 #elif defined(_WIN32) || defined(_WIN64)
86 #if defined(__linux__) || defined(__APPLE__)
87 struct termios oldt, newt;
91 tcgetattr(STDIN_FILENO, &oldt);
93 newt.c_lflag &= ~(ICANON | ECHO);
94 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
95 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
96 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
100 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
101 fcntl(STDIN_FILENO, F_SETFL, oldf);
110 #elif defined(_WIN32) || defined(_WIN64)
136 int32_t mcy_present_position;
139 std::vector<mcy_servo> mcy_servos{{1, 0}, {2, 0}};
143 bool mcy_addparam_result =
false;
144 bool mcy_getdata_result =
false;
147 uint8_t mcy_error = 0;
148 uint8_t param_goal_position[4];
153 printf(
"Succeeded to open the port!\n");
157 printf(
"Failed to open the port!\n");
158 printf(
"Press any key to terminate...\n");
169 printf(
"Succeeded to change the baudrate!\n");
173 printf(
"Failed to change the baudrate!\n");
174 printf(
"Press any key to terminate...\n");
179 printf(
"**** Synchronising Mercury servos ****\n");
181 for (mcy_servo servo : mcy_servos)
183 printf(
"Synchronising Mercury#%d\n", servo.id);
185 mcy_comm_result = packetHandler->
synchronise(portHandler, servo.id, &mcy_error);
188 printf(
"Mercury#%d: has failed to synchronise. Error: %s\n", servo.id, packetHandler->
getTxRxResult(mcy_comm_result));
192 printf(
"Mercury#%d is synchronised\n", servo.id);
196 printf(
"**** Mercury servos are synchronised ****\n");
198 printf(
"**** Connecting Mercury servos ****\n");
200 for (mcy_servo servo : mcy_servos)
205 printf(
"Mercury#%d: %s\n", servo.id, packetHandler->
getTxRxResult(mcy_comm_result));
207 else if (mcy_error != 0)
209 printf(
"Mercury#%d torque enabled failed: %s\n", servo.id, packetHandler->
getRxPacketError(mcy_error));
213 printf(
"Mercury#%d has been successfully connected \n", servo.id);
219 printf(
"**** Mercury servos are connected ****\n");
221 printf(
"Press any key to continue! (or press ESC to quit!)\n");
224 for (mcy_servo servo : mcy_servos)
227 mcy_addparam_result = groupSyncRead.
addParam(servo.id);
228 if (mcy_addparam_result !=
true)
230 fprintf(stderr,
"[ID:%03d] groupSyncRead addparam failed", servo.id);
239 #if defined(__linux__)
252 printf(
"Iteration:%03d\t\n", iteration);
260 for (mcy_servo servo : mcy_servos)
263 mcy_addparam_result = groupSyncWrite.
addParam(servo.id, param_goal_position);
264 if (mcy_addparam_result !=
true)
266 fprintf(stderr,
"[ID:%03d] groupSyncWrite addparam failed", servo.id);
272 mcy_comm_result = groupSyncWrite.
txPacket();
274 printf(
"%s\n", packetHandler->
getTxRxResult(mcy_comm_result));
281 #if defined(__linux__)
289 printf(
"%s\n", packetHandler->
getTxRxResult(mcy_comm_result));
293 for (mcy_servo servo : mcy_servos)
295 if (groupSyncRead.
getError(servo.id, &mcy_error))
296 printf(
"[ID:%03d] %s\n", servo.id, packetHandler->
getRxPacketError(mcy_error));
300 for (mcy_servo servo : mcy_servos)
306 if (mcy_getdata_result !=
true)
308 fprintf(stderr,
"[ID:%03d] groupSyncRead getdata failed", servo.id);
313 for (mcy_servo& servo : mcy_servos)
320 printf(
"[ID:%03d] GoalPos1:%03d PresPos:%03d", (
int)servo.id, mcy_goal_position[index], servo.mcy_present_position);
337 for (mcy_servo servo : mcy_servos)
342 printf(
"Mercury#%d: %s\n", servo.id, packetHandler->
getTxRxResult(mcy_comm_result));
344 else if (mcy_error != 0)
346 printf(
"Mercury#%d torque disable failed: %s\n", servo.id, packetHandler->
getRxPacketError(mcy_error));
350 printf(
"Mercury#%d has been successfully disconnected \n", servo.id);
The class for reading multiple Dynamixel data from same address with same length at once.
bool addParam(uint8_t id)
The function that adds id, start_address, data_length to the Sync Read list.
int txRxPacket()
The function that transmits and receives the packet which might be come from the Dynamixel.
bool getError(uint8_t id, uint8_t *error)
The function that gets the error which might be received by GroupSyncRead::rxPacket or GroupSyncRead:...
bool isAvailable(uint8_t id, uint16_t address, uint16_t data_length)
The function that checks whether there are available data which might be received by GroupSyncRead::r...
uint32_t getData(uint8_t id, uint16_t address, uint16_t data_length)
The function that gets the data which might be received by GroupSyncRead::rxPacket or GroupSyncRead::...
The class for writing multiple Dynamixel data from same address with same length at once.
void clearParam()
The function that clears the Sync Write list.
int txPacket()
The function that transmits the Sync Write instruction packet which might be constructed by GroupSync...
bool addParam(uint8_t id, uint8_t *data)
The function that adds id, start_address, data_length to the Sync Write list.
The class that inherits Protocol1PacketHandler class or Protocol2PacketHandler class.
virtual const char * getTxRxResult(int result)=0
The function that gets description of communication result.
virtual int write1ByteTxRx(PortHandler *port, uint8_t id, uint16_t address, uint8_t data, uint8_t *error=0)=0
The function that calls PacketHandler::writeTxRx() for writing 1 byte data and receives the packet @d...
virtual const char * getRxPacketError(uint8_t error)=0
The function that gets description of hardware error.
virtual int synchronise(PortHandler *port, uint8_t id, uint8_t *error=0)=0
The function that synchronises the servo @description This function will synchronise the target Mercu...
static PacketHandler * getPacketHandler()
The function that returns PacketHandler instance.
The class for port control that inherits PortHandlerLinux, PortHandlerWindows, PortHandlerMac,...
static PortHandler * getPortHandler(const char *port_name)
The function that gets PortHandler class inheritance @description The function gets class inheritance...
virtual bool openPort()=0
The function that opens the port @description The function calls PortHandlerLinux::setBaudRate() to o...
virtual bool setBaudRate(const int baudrate)=0
The function that sets baudrate into the port handler @description The function sets baudrate into th...
virtual void closePort()=0
The function that closes the port @description The function closes the port.
#define MCY_MAXIMUM_POSITION_VALUE
#define MCY_MOVING_STATUS_THRESHOLD
#define LEN_MCY_GOAL_POSITION
#define MCY_MINIMUM_POSITION_VALUE
#define LEN_MCY_PRESENT_POSITION
#define ADDR_MCY_TORQUE_ENABLE
#define ADDR_MCY_GOAL_POSITION
#define ADDR_MCY_PRESENT_POSITION