Azzera filtri
Azzera filtri

The code doesn't want to run properly and it gives me many errors, can someone fix it.

1 visualizzazione (ultimi 30 giorni)
Unrecognized function or variable 'R'.
Error in test>robot_kinematics (line 97)
v = R/2*(w_left+w_right);
Error in test (line 71)
xdot = robot_kinematics(x, w_left, w_right);
And here is the code:
% Robot Parameters
L = 0.2; % distance between the left and right wheels in meters
R = 0.05; % radius of the wheels in meters
% Motor Characteristics
Kv = 0.1; % motor velocity constant
Kt = 0.05; % motor torque constant
Rm = 3; % motor resistance
Lm = 0.1; % motor inductance
% Controller Parameters
Kp = 1; % proportional gain
% Line Sensor Parameters
sensor_distance = 0.1; % distance between the sensors in meters
sensor_offset = 0.05; % distance between the sensors and the center of the robot in meters
sensor_threshold = 0.5; % threshold for detecting the black line
% Initial Conditions
x0 = 0; % initial x position
y0 = 0; % initial y position
theta0 = 0; % initial orientation in radians
% State-Space Model
A = [0 0 0 0;
0 0 0 0;
0 0 0 0;
0 0 0 0];
B = [1 1 0 0;
0 0 1 1;
-1/L 1/L -1/L 1/L;
R/(2*L) R/(2*L) -R/(2*L) -R/(2*L)];
C = [1 0 0 0;
0 1 0 0;
0 0 1 0;
0 0 0 1];
D = zeros(4,4);
% Create State-Space Model
sys = ss(A,B,C,D);
% Design Controller
Q = diag([10 10 10 1]); % state cost matrix
R = 1; % input cost matrix
[K,S,e] = lqr(sys,Q,R);
controller = -K;
% Simulation Parameters
tspan = [0 10]; % simulation time interval
dt = 0.01; % simulation time step
% Simulation Loop
t = tspan(1);
x = [x0; y0; theta0; 0];
while t < tspan(2)
% Read Line Sensor
sensor_vals = read_line_sensor(x(1:2), x(3), sensor_distance, sensor_offset);
error_left = sensor_threshold - sensor_vals(1);
error_right = sensor_threshold - sensor_vals(2);
% Compute Control Input
u = controller * [error_left; error_right; 0; 0];
v_left = Kp * u(1);
v_right = Kp * u(2);
% Compute Motor Velocities
w_left = v_left / R;
w_right = v_right / R;
% Update State
xdot = robot_kinematics(x, w_left, w_right);
x = x + xdot * dt;
t = t + dt;
% Plot Robot
plot_robot(x(1), x(2), x(3), R);
drawnow;
end
function sensor_vals = read_line_sensor(pos, theta, sensor_distance, sensor_offset)
% Compute Sensor Positions
sensor1_pos = pos + sensor_offset * [cos(theta+pi/2); sin(theta+pi/2)];
sensor2_pos = pos + sensor_offset * [cos(theta-pi/2); sin(theta-pi/2)];
% Compute Line Sensor Values
sensor1_val = exp(-norm(sensor1_pos-[pos(1)+cos(theta); pos(2)+sin(theta)])^2 / sensor_distance^2);
sensor2_val = exp(-norm(sensor2_pos-[pos(1)+cos(theta); pos(2)+sin(theta)])^2 / sensor_distance^2);
% Return Sensor Values
sensor_vals = [sensor1_val; sensor2_val];
end
function xdot=robot_kinematics(~,w_left,w_right)
% Compute Forward Velocity
v = R/2*(w_left+w_right);
% Compute Angular Velocity
w = R/L*(w_right-w_left);
% Compute State Derivative
xdot=[v*cos(x(3));v*sin(x(3));w;0];
end

Risposte (1)

Alex Batts
Alex Batts il 4 Mag 2023
Hello,
First, when copying code into a question, please use the "insert a line of code" feature for readability purposes.
Now, to answer your question, you do not have "R" defined as an input variable to your function "robot_kinematics". You also do not have "L" or "x" defined as inputs to the function either. You will want to define the function like:
function xdot=robot_kinematics(x,w_left,w_right,R,L)
Additionally, you need to replace line 71 with:
xdot = robot_kinematics(x, w_left, w_right, R, L);
However, your code will still not run because the function "plot_robot" is not defined, unless you already have this function defined on your local machine.
Alex

Categorie

Scopri di più su Simscape Electrical in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by