%% UPPER ARM MOVEMENT SIMULATION
%% This file runs the Simulink model 'MAF_UpperArmPID.slx that demonstrates activation of the biceps
%% and triceps brachii muscles (under the direction of FES stimulation), which causes the forearm to move downwards.
%% It includes a PID controller to manage the FES stimulation.
%% The program creates plots of the arm movement, muscle forces and the resultant angle of the elbow in Radians.
%% CLEAR THE WORKSPACE
%% To ensure that no previous variables are present.
clear all
close all
clc
%% SYSTEM SET-UP
%% Firstly, the Arm parameters and Biceps and Triceps muscle activation transfer functions are loaded:-
load ArmParameters.mat
Inertia = 0.3; %needs checking...
hLAD_BI = tf(1,[1/dyn_wn_BI^2 2/dyn_wn_BI 1]); %BICEPS linear activation dynamics
hLAD_TR = tf(1,[1/dyn_wn_TR^2 2/dyn_wn_TR 1]); %TRICEPS linear activation dynamics
hIRC_BI = myfun_ID([a1_BI,a2_BI,a3_BI],0:1:300); %BICEPS isometric recruitment curve
hIRC_TR = myfun_ID([a1_TR,a2_TR,a3_TR],0:1:300); %TRICEPS isometric recruitment curve
%% Theta_in values (the model range of input 'Set-points') to move the arm from 30Degrees to 180Degrees.
%%Theta_in = 1:3.14
%% INITIAL CONDITIONS
%% The arm movement has been set with a starting point of 30Degrees (0.5 Radians) as in practice, the
%% human arm tends to only get this close physically, when the biceps muscle is fully activated.
%% The input signal for the simulink model is set (in the Theta_in block) for moving the arm in a downwards
%% action from 30Degrees (0.5Radians) to 180 Degrees (3.14 Radians) in steps of 1Degree (0.017 radians).
%% Set the range of movement and time:-
Time = 10;
Ts = 1/50;
t = 0:Ts:Time;
%% Create the reference
Theta_min = 0.15; Theta_max = 3.14; Theta_step = 2*(Theta_max - Theta_min)/(length(t));
Theta_in = [Theta_min:Theta_step:Theta_max (Theta_max-Theta_step):-Theta_step:Theta_min];
%figure; plot(t,Theta_in); xlabel('Time, s'); ylabel('Angle, rad'); title('reference')
%%Steps = (Theta_Max - Theta_Min)/Time
%% ACTIVATE THE SIMULINK MODEL
%% Store the input (Set-point) and output (Elbow Angle) values so that later we can plot them.
%% (This example includes a PID controller)
% for i = 1:10
sim('MAF_UpperArm_PID_Degrees')
figure; subplot(211);
plot(t,Theta_in,'k',t,Theta_out','b',t,PID_out,'m'); legend('reference','theta_output','PID out');
xlabel('Time, s'); ylabel('Angle, rad'); title('output')
subplot(212);
plot(t,u_Biceps,'r',t,u_Triceps,'b'); xlabel('Time, s'); ylabel('Stim, microsecs'); legend('Biceps','Triceps');
% storagevec(i,:) = Theta_out;
% end