Getting errors in the simulation code for the obstacle avoiding robot using fuzzy logics
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Robotics System Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!