logFile = fopen('data_log.csv', 'w');
fprintf(logFile, 'Time,Disturbance1,Disturbance2,Disturbance3,RollAngle,PitchAngle,CentroidX,CentroidY,Servo1,Servo2,Servo3\n');
stateLogFile = fopen('state_log.csv', 'w');
fprintf(stateLogFile, 'Time,Phi,Theta,X_Offset,Y_Offset,Zp,Gamma\n');
cap = webcam('Adesso CyberTrack H3');
cap.ExposureMode = 'manual';
cap.Exposure = exposureTime;
disp('Warning: Unable to set exposure time for the camera.');
blobAnalyzer = vision.BlobAnalysis(...
'MinimumBlobArea', 200, ...
'MaximumBlobArea', 50000, ...
'BoundingBoxOutputPort', true, ...
'CentroidOutputPort', true, ...
hFig = figure('DoubleBuffer', 'on');
hAxes = axes('Parent', hFig);
arduino_board = arduino(port, board, 'Libraries', 'Servo');
servo_motor = servo(arduino_board, 'D9','MinPulseDuration', 500e-6, 'MaxPulseDuration', 2500e-6);
writePosition(servo_motor, 0.333);
servo_motor1 = servo(arduino_board, 'D10','MinPulseDuration', 500e-6, 'MaxPulseDuration', 2500e-6);
writePosition(servo_motor1, 0.333);
servo_motor2 = servo(arduino_board, 'D11','MinPulseDuration', 500e-6, 'MaxPulseDuration', 2500e-6);
writePosition(servo_motor2, 0.333);
[Zp,alpha] = forwardkinfunc(A,B,C);
undistortedFrame = undistortImage(frame, cameraParams2);
undistortedFrame = imresize(undistortedFrame, 0.2);
[frameHeight, frameWidth, ~] = size(undistortedFrame);
centerX = frameWidth / 2;
centerY = frameHeight / 2;
I_gray = rgb2gray(undistortedFrame);
I_contrast = imadjust(I_gray);
[Centers, Radii] = imfindcircles(I_contrast, [1 5], 'ObjectPolarity', 'dark', 'Sensitivity', 0.9);
disp('No circles detected');
disp(['Detected ', num2str(size(Centers, 1)), ' circles']);
imshow(undistortedFrame, 'Parent', hAxes);
viscircles(hAxes, Centers, Radii, 'EdgeColor', 'r');
plot(hAxes, centerX, centerY, 'm.', 'MarkerSize', 20);
disp(['Line fit for X (Y): slope = ', num2str(pX(1))]);
disp(['Line fit for Y (X): slope = ', num2str(pY(1))]);
Theta = -atan2(slopeX, 1);
disp(['Roll Angle: ', num2str(Theta)]);
disp(['Pitch Angle: ', num2str(Phi)]);
plot(hAxes, [0, size(I_contrast, 2)], [mean(yof), mean(yof)], 'g--', 'LineWidth', 2);
plot(hAxes, [mean(x), mean(x)], [0, size(I_contrast, 1)], 'y--', 'LineWidth', 2);
yFitLineX = polyval(pX, [0, size(I_contrast, 1)]);
plot(hAxes, yFitLineX, [0, size(I_contrast, 1)], 'b-', 'LineWidth', 2);
xFitLineY = polyval(pY, [0, size(I_contrast, 2)]);
plot(hAxes, [0, size(I_contrast, 2)], xFitLineY, 'r-', 'LineWidth', 2);
title(hAxes, sprintf('Roll: %.2f°, Pitch: %.2f°', Theta, Phi));
title(hAxes, 'Not enough circles detected');
hsvFrame = rgb2hsv(undistortedFrame);
sliderBW = ((hsvFrame(:,:,1) >= channel1Min) | (hsvFrame(:,:,1) <= channel1Max)) & ...
(hsvFrame(:,:,2) >= channel2Min) & (hsvFrame(:,:,2) <= channel2Max) & ...
(hsvFrame(:,:,3) >= channel3Min) & (hsvFrame(:,:,3) <= channel3Max);
BW = imfill(BW, 'holes');
BW = bwareaopen(BW, 200);
BW = imopen(BW, strel('disk', 5));
BW = imclose(BW, strel('disk', 20));
[area,centroid,bbox] = step(blobAnalyzer,BW);
rectangle('Position', bbox, 'EdgeColor', 'green', 'LineWidth', 2);
plot(hAxes, x_p, y_p, 'ro', 'MarkerSize', 10);
[xof1, yof1] = camera_base(xc,yc)
elapsed_time = toc(start);
Kp_x = 10; Ki_x = 0; Kd_x = 0
Kp_y = 10; Ki_y = 0; Kd_y = 0;
Kp_phi = 80; Ki_phi = 0; Kd_phi = 0.5;
Kp_theta =80; Ki_theta =0; Kd_theta = 0.5;
derivative_errortheta = 0
error_x= setpoint_x - xof1;
integral_errorx = integral_errorx + error_x * Ts;
derivative_errorx = (error_x - previous_errorx);
filte_est = (o*prev_filtest)+(1-o)*derivative_errorx
derivative = filte_est/Ts;
prev_filtest = filte_est;
control_signalx = Kp_x * error_x + Ki_x * integral_errorx + Kd_x * derivative;
previous_errorx = error_x;
theta_coupling_effect = K_xr * error_x;
error_y= setpoint_y - yof1;
integral_errory = integral_errory + error_y * Ts;
derivative_errory = (error_y - previous_errory)
filte_esty = (o*prev_filtesty)+(1-o)*derivative_errory
derivativey = filte_esty/Ts;
prev_filtesty = filte_esty;
control_signaly = Kp_y * error_y + Ki_y * integral_errory + Kd_y * derivativey;
previous_errory = error_y;
phi_coupling_effect = K_yp * error_y;
theta_desired =(K_yr * error_y) + phi_coupling_effect;
phi_desired = (K_xp * error_x) + theta_coupling_effect;
error_phi= phi_desired - Phi;
integral_errorphi = integral_errorphi + error_phi * Ts;
derivative_errorphi = (error_phi - previous_errorphi)
filte_estp = (o*prev_filtestp)+(1-o)*derivative_errorphi
derivativep = filte_estp/Ts;
prev_filtestp = filte_estp;
control_signalphi = Kp_phi * error_phi + Ki_phi * integral_errorphi + Kd_phi * derivativep;
previous_errorphi = error_phi;
Phi1 = (Phi + control_signalphi * Ts);
Phi1 = max(-0.6, min(Phi1, 0.6));
error_theta= theta_desired - Theta;
integral_errortheta = integral_errortheta + error_theta * Ts;
derivative_errortheta = (error_theta - previous_errortheta)
filte_estth = (o*prev_filtestth)+(1-o)*derivative_errortheta;
derivativeth = filte_estth/Ts;
prev_filtestth = filte_estth;
control_signaltheta = Kp_theta * error_theta + Ki_theta * integral_errortheta + Kd_theta * derivativeth;
previous_errortheta = error_theta;
Theta1 = (Theta + control_signaltheta * Ts);
Theta1 = max(-0.6, min(Theta1, 0.6));
xof1 = xof1+(control_signalx*Ts);
yof1 = yof1+(control_signaly*Ts)
[a11f,a22f,a33f] = inversekin(xof1,yof1,Phi1,Theta1,Zp,alpha)
a11 = max(42, min(a11, 48));
a22 = max(42, min(a22, 48));
a33 = max(42, min(a33, 48));
currentTime = datestr(now, 'HH:MM:SS.FFF');
fprintf(logFile, '%s,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.4f,%.4f,%.4f\n', ...
currentTime, dist_1, dist_2, dist_3, Phi, Theta, xof1, yof1,a11, a22, a33);
writePosition(servo_motor,a)
writePosition(servo_motor1,b)
writePosition(servo_motor2,c)
if (xof1 <=0.5 && xof1>=-0.5)&&(yof1 <= 1.5 &&yof1>=-1.5)
writePosition(servo_motor,0.3300)
writePosition(servo_motor1,0.3300)
writePosition(servo_motor2,0.3300)
clear arduino_board servo_motor servo_motor1 servo_motor2 s
disp('System is Stable');
function norm_position = normalize(angle_deg)
angle_deg = angle_deg/1.5;
angle_deg = 90 - angle_deg;
angle_deg = angle_deg - (angle_deg/100);
norm_position = angle_deg/180;