How to Control of mobile robots using PID and state-space techniques. using matlab

Controlling mobile robots (e.g., differential drive, omnidirectional, or Ackermann-steering robots) using PID and state-space techniques in MATLAB involves modeling the robot dynamics, designing controllers, and simulating the closed-loop system. Below is a structured guide to implement these methods:


1. Robot Modeling

Kinematic Model (Differential Drive Robot)

For a differential drive robot with wheel radius r, wheel separation L, and wheel angular velocities ωL,ωR:

{x˙=r2(ωR+ωL)cos⁡θy˙=r2(ωR+ωL)sin⁡θθ˙=rL(ωR−ωL)

Define the state vector: q=[x,y,θ]T.

Dynamic Model

For state-space control, linearize the model around an operating point or use feedback linearization:

q˙=f(q,u),

where u=[ωL,ωR]T.


2. PID Control

Problem Setup

  • Goal: Track a reference trajectory qd(t)=[xd(t),yd(t),θd(t)]T.
  • Errore=qd−q.

PID Controller Design

For linear/angular velocity control:

matlab
Copy
% PID Gains
Kp = 2.0;  % Proportional
Ki = 0.1;  % Integral
Kd = 0.5;  % Derivative

% Initialize errors
e_prev = [0; 0; 0];
integral = [0; 0; 0];

% PID Control Law
function u = PIDController(e, dt)
    persistent integral e_prev;
    if isempty(integral), integral = [0; 0; 0]; end
    if isempty(e_prev), e_prev = [0; 0; 0]; end
    
    integral = integral + e * dt;
    derivative = (e - e_prev) / dt;
    u = Kp * e + Ki * integral + Kd * derivative;
    e_prev = e;
end

Simulation Example

matlab
Copy
% Simulate robot motion with PID
t = 0:0.1:10;
q = zeros(3, length(t)); % [x; y; theta]
q_d = [sin(t); cos(t); atan2(cos(t), -sin(t))]; % Circular trajectory

for i = 2:length(t)
    dt = t(i) - t(i-1);
    e = q_d(:,i) - q(:,i-1);
    u = PIDController(e, dt); % Compute control input (velocities)
    
    % Update robot state using kinematic model
    v = (u(1) + u(2)) / 2;  % Linear velocity
    w = (u(2) - u(1)) / L;  % Angular velocity
    q(:,i) = q(:,i-1) + dt * [v*cos(q(3,i-1)); v*sin(q(3,i-1)); w];
end

% Plot results
figure;
plot(q(1,:), q(2,:), 'b', q_d(1,:), q_d(2,:), 'r--');
legend('Actual Path', 'Desired Path');

3. State-Space Control

Linearized Model

For trajectory tracking, define error dynamics:

e˙=Ae+Bu,

where e=qd−q.

LQR Controller Design

Use MATLAB’s Control System Toolbox to compute optimal gains:

matlab
Copy
% Linearized state-space matrices (example for a point robot)
A = [0 0 -v_d*sin(theta_d);
     0 0  v_d*cos(theta_d);
     0 0  0];
B = [1 0;
     0 1;
     0 0]; 

% LQR Weighting Matrices
Q = diag([10, 10, 5]);  % State penalty
R = diag([0.1, 0.1]);    % Control penalty

% Compute LQR gain
[K, ~, ~] = lqr(A, B, Q, R);

State Feedback Control Law

matlab
Copy
% State feedback (assuming full-state measurement)
u = -K * e + feedforward_term;

Simulation Example

matlab
Copy
% Simulate with LQR
for i = 2:length(t)
    dt = t(i) - t(i-1);
    e = q_d(:,i) - q(:,i-1);
    u = -K * e; % LQR control input
    
    % Update robot state (using dynamic model)
    q_dot = A * q(:,i-1) + B * u;
    q(:,i) = q(:,i-1) + dt * q_dot;
end

4. Comparison of Techniques

Method Pros Cons
PID Simple tuning, robust to noise. Struggles with nonlinearities.
State-Space Handles MIMO systems, optimal. Requires accurate model, complex.

5. Advanced Topics

  1. Feedback Linearization:
    Transform nonlinear dynamics into a linear system for state-space control.

    matlab
    Copy
    % Example for differential drive robot
    v = input1; % Linear velocity
    w = input2; % Angular velocity
    u = [v; w]; % Transformed inputs
  2. Trajectory Generation:
    Use polynomial or spline trajectories for smooth reference paths.

    matlab
    Copy
    t_ref = 0:0.1:10;
    x_d = polyval(p, t_ref); % Polynomial trajectory
  3. Hardware Integration:
    Deploy controllers to ROS (Robot Operating System) using MATLAB’s ROS toolbox.

6. MATLAB Tools & Functions

  • Control System Toolbox: For LQR, pole placement (place), and system analysis.
  • Robotics System Toolbox: For ROS integration and sensor simulation.
  • Simulink: Visual modeling of control systems.

Example: Differential Drive Robot in Simulink

  1. Model kinematics using integrators and trigonometric blocks.
  2. Design PID controllers for v and ω.
  3. Use PID Tuner app for automatic gain adjustment.
  4. Simulate and visualize with Simulink 3D Animation.

7. Sample Results

  • PID Tracking:
    PID Tracking
  • LQR Performance:
    LQR

Code Resources

  1. MATLAB PID Tutorial
  2. LQR Control Design
  3. Mobile Robotics Simulation

This framework enables the design and simulation of controllers for mobile robots in MATLAB, bridging the gap between theory and real-world implementation.