Use the following template to help create your post:
-
What model of servo are you using?
MX-28(2.0)AT -
Describe your control environment. This includes the controller or interface, and any power source.
U2D2 controller with 3 motors connected in daisy chain powered by 12V SMPS. -
Specify the operating mode for applicable models, and any firmware settings you are using.
Position control mode, firmware 44. -
Include pictures if possible.
-
Include a full description of the issue.
clc;
clear all;
close all ;
lib_name = ââ;
if strcmp(computer, âPCWINâ)
lib_name = âdxl_x86_câ;
elseif strcmp(computer, âPCWIN64â)
lib_name = âdxl_x64_câ;
elseif strcmp(computer, âGLNX86â)
lib_name = âlibdxl_x86_câ;
elseif strcmp(computer, âGLNXA64â)
lib_name = âlibdxl_x64_câ;
elseif strcmp(computer, âMACI64â)
lib_name = âlibdxl_mac_câ;
end
% Load Libraries
if ~libisloaded(lib_name)
[notfound, warnings] = loadlibrary(lib_name, âdynamixel_sdk.hâ, âaddheaderâ, âport_handler.hâ, âaddheaderâ, âpacket_handler.hâ, âaddheaderâ, âgroup_sync_write.hâ, âaddheaderâ, âgroup_sync_read.hâ);
end
% Control table address
ADDR_PRO_TORQUE_ENABLE = 64; % Control table address is different in Dynamixel model
ADDR_OPERATING_MODE = 11;
ADDR_PRO_PROFILE_VELOCITY = 112;
ADDR_PRO_GOAL_POSITION = 116;
ADDR_PRO_PRESENT_POSITION = 132;
% Data Byte Length
LEN_PRO_GOAL_POSITION = 4;
LEN_PRO_PRESENT_POSITION = 4;
% Protocol version
PROTOCOL_VERSION = 2.0; % See which protocol version is used in the Dynamixel
% Default setting
DXL1_ID = 1; % Dynamixel#1 ID: 1
DXL2_ID = 2; % Dynamixel#2 ID: 2
DXL3_ID = 3;
BAUDRATE = 57600;
DEVICENAME = âCOM3â; % Check which port is being used on your controller
% ex) Windows: âCOM1â Linux: â/dev/ttyUSB0â Mac: â/dev/tty.usbserial-*â
TORQUE_ENABLE = 1; % Value for enabling the torque
TORQUE_DISABLE = 0; % Value for disabling the torque
EXT_POSITION_CONTROL_MODE = 4;
DXL_MINIMUM_POSITION_VALUE = -100; % Dynamixel will rotate between this value
DXL_MAXIMUM_POSITION_VALUE = 5000; % and this value (note that the Dynamixel would not move when the position value is out of movable range. Check e-manual about the range of the Dynamixel you use.)
DXL_MOVING_STATUS_THRESHOLD = 20; % Dynamixel moving status threshold
ESC_CHARACTER = âeâ; % Key for escaping loop
COMM_SUCCESS = 0; % Communication Success result value
COMM_TX_FAIL = -1001; % Communication Tx Failed
% Initialize PortHandler Structs
% Set the port path
% Get methods and members of PortHandlerLinux or PortHandlerWindows
port_num = portHandler(DEVICENAME);
% Initialize PacketHandler Structs
packetHandler();
% Initialize Groupsyncwrite Structs
groupwrite_num = groupSyncWrite(port_num, PROTOCOL_VERSION, ADDR_PRO_GOAL_POSITION, LEN_PRO_GOAL_POSITION);
% Initialize Groupsyncread Structs for Present Position
groupread_num = groupSyncRead(port_num, PROTOCOL_VERSION, ADDR_PRO_PRESENT_POSITION, LEN_PRO_PRESENT_POSITION);
% Initialize Groupsyncread Structs for Present Position
% groupwriteVel_num = groupSyncWrite(port_num, PROTOCOL_VERSION, ADDR_PRO_PROFILE_VELOCITY, LEN_PRO_PRESENT_POSITION);
% index = 1;
dxl_comm_result = COMM_TX_FAIL; % Communication result
dxl_addparam_result = false; % AddParam result
% dxl_goal_position = [DXL_MINIMUM_POSITION_VALUE DXL_MAXIMUM_POSITION_VALUE]; % Goal position
dxl_error = 0; % Dynamixel error
% dxl1_present_position = 0; % Present position
% dxl2_present_position = 0;
% Open port
if (openPort(port_num))
fprintf(âSucceeded to open the port!\nâ);
else
unloadlibrary(lib_name);
fprintf(âFailed to open the port!\nâ);
input(âPress any key to terminateâŚ\nâ);
return;
end
% Set port baudrate
if (setBaudRate(port_num, BAUDRATE))
fprintf(âSucceeded to change the baudrate!\nâ);
else
unloadlibrary(lib_name);
fprintf(âFailed to change the baudrate!\nâ);
input(âPress any key to terminateâŚ\nâ);
return;
end
% Enable Dynamixel#1 Torque
write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_ENABLE);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf(â%s\nâ, getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
elseif dxl_error ~= 0
fprintf(â%s\nâ, getRxPacketError(PROTOCOL_VERSION, dxl_error));
else
fprintf(âDynamixel #%d has been successfully connected \nâ, DXL1_ID);
end
% Enable Dynamixel#2 Torque
write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_ENABLE);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf(â%s\nâ, getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
elseif dxl_error ~= 0
fprintf(â%s\nâ, getRxPacketError(PROTOCOL_VERSION, dxl_error));
else
fprintf(âDynamixel #%d has been successfully connected \nâ, DXL2_ID);
end
% Enable Dynamixel#3 Torque
write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_ENABLE);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf(â%s\nâ, getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
elseif dxl_error ~= 0
fprintf(â%s\nâ, getRxPacketError(PROTOCOL_VERSION, dxl_error));
else
fprintf(âDynamixel #%d has been successfully connected \nâ, DXL3_ID);
end
% Add parameter storage for Dynamixel#1 present position value
dxl_addparam_result = groupSyncReadAddParam(groupread_num, DXL1_ID);
if dxl_addparam_result ~= true
fprintf(â[ID:%03d] groupSyncRead addparam failedâ, DXL1_ID);
return;
end
% Add parameter storage for Dynamixel#2 present position value
dxl_addparam_result = groupSyncReadAddParam(groupread_num, DXL2_ID);
if dxl_addparam_result ~= true
fprintf(â[ID:%03d] groupSyncRead addparam failedâ, DXL2_ID);
return;
end
% Add parameter storage for Dynamixel#3 present position value
dxl_addparam_result = groupSyncReadAddParam(groupread_num, DXL3_ID);
if dxl_addparam_result ~= true
fprintf(â[ID:%03d] groupSyncRead addparam failedâ, DXL3_ID);
return;
end
d1 = ();
d2 = ();
d3 = ();
T = 1 ;
f = 1/T ;
j = 0;
alpha_old = 0;
phi_old = 0 ;
itr1 = 0 ;
itr2 = 0 ;
itr3 = 0 ;
phase_diff1 = 0 ;
phase_diff2 = 0 ;
phase_diff3 = 0 ;
for t = 0:0.005:T
j = j +1;
time(j) = t;
alpha = -140/(2 tanh(2.7)) * tanh(2.7 sin(2 pi f t));
phi = -300/(2 asin(0.8)) * asin(0.8* cos(2 pi f*t)) ;
sweep(j) = phi ;
if t<=T/2
alpha_new = alpha;
pitch(j) = alpha ;
if abs(alpha_new-alpha_old) <= 0.0879 && j>1
itr1 = itr1+1;
if itr1==1
t_dash = time(j);
end
if t == t_dash
phase_diff1 = alpha - phi ;
end
pitch(j) = phi + phase_diff1 ;
kite = pitch(j);
end
if abs(alpha_new-alpha_old) > 0.0879 && t>T/4
itr2 = itr2 + 1;
if itr2 == 1
t_dash2 = time(j);
phase_diff2 = kite - alpha ;
end
pitch(j) = alpha + phase_diff2;
end
alpha_old = alpha_new;
pitch(1) = alpha;
end
k = j ;
if t>T/2
alpha_new = alpha;
pitch(k) = alpha+phase_diff2;
if abs(alpha_new-alpha_old) <= 0.0879
itr3 = itr3+1;
if itr3==1
t_dash3 = time(k);
phase_diff3 = phi-pitch(k) ;
end
pitch(k) = phi - phase_diff3 ;
end
if t > (3*T/4) && abs(alpha_new-alpha_old) > 0.0879
pitch (k) = alpha ;
end
alpha_old = alpha_new;
end
end
p1 = 900 ;
p2 = 2000 ;
p3 = 2000 ;
Q = () ;
pause(1);
for i = 1 : 3
for n = 1 : length(pitch)
%motor1
q1 = pitch(1,n)/0.0879 ;
Q(1,n) = q1 ;
m_pos(1) = p1 + Q(1,n);
M1(n) = m_pos(1);
%motor2
q1 = sweep(1,n)/0.0879 ;
Q(2,n) = q1 ;
m_pos(2) = p2 + Q(2,n);
M2(n) = m_pos(2);
%motor3
q3 = sweep(1,n)/0.0879 ;
Q(3,n) = q3 ;
m_pos(3) = p3 + Q(3,n);
M3(n) = m_pos(3);
% Add Dynamixel#1 goal position value to the Syncwrite storage
dxl_addparam_result = groupSyncWriteAddParam(groupwrite_num, DXL1_ID, typecast(int32(m_pos(1)), 'uint32'), LEN_PRO_GOAL_POSITION);
if dxl_addparam_result ~= true
fprintf('[ID:%03d] groupSyncWrite addparam failed', DXL1_ID);
return;
end
% Add Dynamixel#2 goal position value to the Syncwrite parameter storage
dxl_addparam_result = groupSyncWriteAddParam(groupwrite_num, DXL2_ID, typecast(int32(m_pos(2)), 'uint32'), LEN_PRO_GOAL_POSITION);
if dxl_addparam_result ~= true
fprintf('[ID:%03d] groupSyncWrite addparam failed', DXL2_ID);
return;
end
% Add Dynamixel#3 goal position value to the Syncwrite parameter storage
dxl_addparam_result = groupSyncWriteAddParam(groupwrite_num, DXL3_ID, typecast(int32(m_pos(3)), 'uint32'), LEN_PRO_GOAL_POSITION);
if dxl_addparam_result ~= true
fprintf('[ID:%03d] groupSyncWrite addparam failed', DXL3_ID);
return;
end
% Syncwrite goal position
groupSyncWriteTxPacket(groupwrite_num);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf('%s\n', getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
end
% Clear syncwrite parameter storage
groupSyncWriteClearParam(groupwrite_num);
% Syncread present position
groupSyncReadTxRxPacket(groupread_num);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf('%s\n', getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
end
% Get Dynamixel#1 present position value
dxl1_present_position = groupSyncReadGetData(groupread_num, DXL1_ID, ADDR_PRO_PRESENT_POSITION, LEN_PRO_PRESENT_POSITION);
% Get Dynamixel#2 present position value
dxl2_present_position = groupSyncReadGetData(groupread_num, DXL2_ID, ADDR_PRO_PRESENT_POSITION, LEN_PRO_PRESENT_POSITION);
% Get Dynamixel#3 present position value
dxl3_present_position = groupSyncReadGetData(groupread_num, DXL3_ID, ADDR_PRO_PRESENT_POSITION, LEN_PRO_PRESENT_POSITION);
d1(n) = dxl1_present_position;%*0.0879;
d2(n) = dxl2_present_position;%*0.0879;
d3(n) = dxl3_present_position;%*0.0879;
end
end
% Disable Dynamixel#1 Torque
write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf(â%s\nâ, getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
elseif dxl_error ~= 0
fprintf(â%s\nâ, getRxPacketError(PROTOCOL_VERSION, dxl_error));
end
% Disable Dynamixel#2 Torque
write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf(â%s\nâ, getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
elseif dxl_error ~= 0
fprintf(â%s\nâ, getRxPacketError(PROTOCOL_VERSION, dxl_error));
end
% Disable Dynamixel#3 Torque
write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_PRO_TORQUE_ENABLE, TORQUE_DISABLE);
dxl_comm_result = getLastTxRxResult(port_num, PROTOCOL_VERSION);
dxl_error = getLastRxPacketError(port_num, PROTOCOL_VERSION);
if dxl_comm_result ~= COMM_SUCCESS
fprintf(â%s\nâ, getTxRxResult(PROTOCOL_VERSION, dxl_comm_result));
elseif dxl_error ~= 0
fprintf(â%s\nâ, getRxPacketError(PROTOCOL_VERSION, dxl_error));
end
% Close port
closePort(port_num);
% Unload Library
unloadlibrary(lib_name);
figure(1)
movegui(âeastâ)
plot(time,M1,â-â,time,d1,âxâ,time,M2,â.â,time,d2,âsâ,time,M3,â-.â,time,d3,âoâ)
ylabel(âInput Positionsâ)
xlabel(âTime in Secondâ)
legend(âDesired-Output-M1â,âMotor-Output-M1â,âDesired-Output-M2â,âMotor-Output-M2â,âDesired-Output-M3â,âMotor-Output-M3â)
where,
M1,M2,M3 are desired positions to motors
d1,d2,d3 are output positions from motors