フィルターのクリア

Getting errors in the simulation code for the obstacle avoiding robot using fuzzy logics

2 ビュー (過去 30 日間)
Hi! I created fuzzy logic controller for a object avoiding robot using Sugeno method. Now I need a simple simulate for that FLC. I tried for hours and I'm still getting errors.
I have attached the FLC and the code here.
In the code I gave the starting point at (0,0) and the goal (100,100). And I also wanted to save the trace which the robot goes.
Thank you very much for the help. I really need it at this moment.))
% Initialize the environment and figure
figure;
axis([0 120 0 120]); % Define the XY plane size
hold on;
% Define the robot as a small colored solid circle
robot = viscircles([0, 0], 2, 'EdgeColor', 'b', 'LineWidth', 2);
% Manually place obstacles as solid colored squares
obstacles = [
% Define obstacle coordinates as [x, y, width, height]
% You can add more obstacles here or modify as needed
[40, 30, 10, 10]; % Example obstacle 1
[60, 70, 15, 15]; % Example obstacle 2
];
% Load Fuzzy Logic Controller from a FIS file
flc = readfis('Fuzzy_HW_Sugeno.fis');
% Robot parameters
robotPosition = [0, 0];
goal = [100, 100];
robotPath = (robotPosition); % Initialize the robot path with the starting position
% Main simulation loop
while norm(robotPosition - goal) > 2 % Adjust the termination condition
% Calculate front, left, and right obstacle distances (you need to implement this)
% For example, you can use sensors or logic to determine obstacle distances
% Use FLC to calculate left and right motor speeds
fuzzyInputs = [Front_OD, Left_OD, Right_OD]; % Replace with actual values
fuzzyOutputs = evalfis(fuzzyInputs, flc);
Left_MS = fuzzyOutputs(1);
Right_MS = fuzzyOutputs(2);
% Update the robot's position based on the motor speeds
delta_speed = Left_MS - Right_MS;
radius = 10;
delta_theta = delta_speed / radius;
robotPosition = robotPosition + [radius * (Right_MS + Left_MS) * cos(delta_theta / 2), ...
radius * (Right_MS + Left_MS) * sin(delta_theta / 2)];
% Check for collisions with obstacles and adjust the robot's trajectory
for i = 1:length(obstacles)
if isCollision(robotPosition, obstacles(i, :))
% Implement collision avoidance logic here
end
end
% Update the plot to reflect the robot's new position and shadow path
set(robot, 'Position', [robotPosition, 2]);
robotPath = [robotPath; robotPosition]; % Append to the robot path
% Plot the robot path
plot(robotPath(:, 1), robotPath(:, 2), 'k.');
pause(0.1); % Adjust the simulation speed
end
% End of the simulation
% Cleanup and visualization
delete(robot);
delete(obstacles);
% Function to check for collision with an obstacle
function collision = isCollision(robotPosition, obstacle)
x = robotPosition(1);
y = robotPosition(2);
obstacle_x = obstacle(1);
obstacle_y = obstacle(2);
width = obstacle(3);
height = obstacle(4);
collision = (x >= obstacle_x && x <= obstacle_x + width && y >= obstacle_y && y <= obstacle_y + height);
end
  2 件のコメント
Janidu
Janidu 2023 年 11 月 7 日
編集済み: Janidu 2023 年 11 月 7 日
Now I created the environment which I need.
Now I need to create the main simulation loop, which I'm struggling. I cannot understand how to implement FLC rules to the matlab code.
% Initialize the environment and figure
figure;
axis([0 120 0 120]); % Define the XY plane size
hold on;
% Define the robot as a small colored solid circle
robot = viscircles([3, 3], 1, 'EdgeColor', 'b', 'LineWidth', 8);
% Manually place obstacles as solid colored squares.
obstacles =[
% Define obstacle coordinates as [x, y, width, height]
rectangle('Position',[50 50 10 10],'EdgeColor','k','FaceColor',[0 .5 .5]);
rectangle('Position',[75 80 20 15],'EdgeColor','k','FaceColor',[0 .5 .5]);
rectangle('Position',[25 15 5 15],'EdgeColor','k','FaceColor',[0 .5 .5]);
];
% Load Fuzzy Logic Controller from a FIS file
flc = readfis('Fuzzy_HW_Sugeno.fis');
% Robot parameters
robotPosition = [3, 3];
goal = viscircles([100, 100], 1, 'EdgeColor', 'y', 'LineWidth', 8);
robotPath = (robotPosition); % Initialize the robot path with the starting position
%{
% Main simulation loop
while norm(robotPosition - goal) > 2 % Adjust the termination condition
% Calculate front, left, and right obstacle distances (you need to implement this)
% For example, you can use sensors or logic to determine obstacle distances
% Use FLC to calculate left and right motor speeds
fuzzyInputs = [Front_OD, Left_OD, Right_OD]; % Replace with actual values
fuzzyOutputs = evalfis(fuzzyInputs, flc);
Left_MS = fuzzyOutputs(1);
Right_MS = fuzzyOutputs(2);
% Update the robot's position based on the motor speeds
delta_speed = Left_MS - Right_MS;
radius = 10;
delta_theta = delta_speed / radius;
robotPosition = robotPosition + [radius * (Right_MS + Left_MS) * cos(delta_theta / 2), ...
radius * (Right_MS + Left_MS) * sin(delta_theta / 2)];
% Check for collisions with obstacles and adjust the robot's trajectory
for i = 1:length(obstacles)
if isCollision(robotPosition, obstacles(i, :))
% Implement collision avoidance logic here
end
end
% Update the plot to reflect the robot's new position and shadow path
set(robot, 'Position', [robotPosition, 2]);
robotPath = [robotPath; robotPosition]; % Append to the robot path
% Plot the robot path
plot(robotPath(:, 1), robotPath(:, 2), 'k.');
pause(0.1); % Adjust the simulation speed
end
% End of the simulation
% Cleanup and visualization
delete(robot);
delete(obstacles);
% Function to check for collision with an obstacle
function collision = isCollision(robotPosition, obstacle)
x = robotPosition(1);
y = robotPosition(2);
obstacle_x = obstacle(1);
obstacle_y = obstacle(2);
width = obstacle(3);
height = obstacle(4);
collision = (x >= obstacle_x && x <= obstacle_x + width && y >= obstacle_y && y <= obstacle_y + height);
end
%}
Janidu
Janidu 2023 年 11 月 14 日
any idea please?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2023 年 11 月 14 日
for i = 1:length(obstacles)
length(obstacles) is defined as:
temp = size(obstacles);
if any(temp == 0)
Length is 0
else
Length is max(temp)
end
For a 2 x 4 array, size would be [2 4] and none of those are 0, so length would be max([2 4]) which would be 4.
You then try to index up to row #4 of your array that only has 2 rows.
You should almost never use length() with something that might not be a simple empty array or else a vector.
  2 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 14 日
Change
fuzzyOutputs = evalfis(fuzzyInputs, flc);
to
fuzzyOutputs = evalfis(flc, fuzzyInputs);
Janidu
Janidu 2023 年 11 月 16 日
Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFuzzy Inference System Modeling についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by