dqrobotics / learning-dqrobotics-in-matlab

Other
21 stars 6 forks source link

[QUESTION] Implementing QP controller in MATLAB #1

Closed AzizAlsayed closed 1 year ago

AzizAlsayed commented 1 year ago

Acceptance of the Code of Conduct

Leave these in your question, read, and check them.


Please find attached the full code

clear all;
close all;
include_namespace_dq;

% Define the robot
kuka = KukaLwr4Robot.kinematics();

% Robot definition
%seven_dof_planar_robot = SevenDofPlanarRobotDH.kinematics();
% Solver definition
qp_solver = DQ_QuadprogSolver();

% Controller definition
translation_controller = DQ_ClassicQPController(kuka, qp_solver);
translation_controller.set_control_objective(ControlObjective.Translation);
translation_controller.set_gain(10);
translation_controller.set_damping(1);

% Joint limits
q_minus = -(pi/8)*ones(7,1);
q_plus  = (pi/8)*ones(7,1);

% Desired translation (pure quaternion)
td = 7*j_;
% Sampling time [s]
tau = 0.01;
% Simulation time [s]
final_time = 4;
% Initial joint values [rad]
q = zeros(7,1);
% Store the joint values
stored_q = [];

% Translation controller loop.
for time=0:tau:final_time

        % Define the linear inequality matrix and the linear inequality vector
    Wjl = [-eye(7); eye(7)];
    wjl = [-1*(q_minus-q); 1*(q_plus-q)];

     % Update the linear inequalities in the controller
    translation_controller.set_inequality_constraint(Wjl, wjl);

       % Get the next control signal [rad/s]
    u = translation_controller.compute_setpoint_control_signal(q,vec4(td));

    % Move the robot
    q = q + u*tau;

    % Store data
    stored_q = [stored_q q];

    % Plot
    % Plot the robot
    plot(kuka,q);
    title(['Translation control' ' time = ' num2str(time) 's out of ' num2str(final_time) 's'])
    % Plot the desired pose
    hold on
    plot3(td.q(2),td.q(3),td.q(4), 'ko');
    hold off
    % [For animations only]
    drawnow limitrate % [For animations only] Ask MATLAB to draw the plot now
end

for i=1:7
    subplot(3,3,i)
    plot(stored_q(i,:))
    hold on
    plot(repmat(q_minus(i),1,size(stored_q,2)),'k')
    plot(repmat(q_plus(i),1,size(stored_q,2)),'k')
    title(['q_{' num2str(i) '}'])
    ylim([-1 1])
    box off
    ylabel('[rad]')
    xlabel('Iteration')
end

Error

Environment:

AzizAlsayed commented 1 year ago

Hello,

I have updated MATLAB version and everything is working now.

kind regards, Aziz

mmmarinho commented 1 year ago

@AzizAlsayed Thanks for writing up the detailed issue and for the solution!