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

조회 수: 11 (최근 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
%}

댓글을 달려면 로그인하십시오.

채택된 답변

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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fuzzy Logic Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by