-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegway_lqr_v3.m
143 lines (116 loc) · 2.95 KB
/
segway_lqr_v3.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%% LQR IMPLEMENTATION FOR 2-WHEELED SEGWAY MODEL
% JEREMY ENGELS FOR UCLA MAE 162D
clc; close all; clear;
%% set sim times
ts = 1; % ms
T = 15000; % ms
N = T/ts;
%% Getting motor and sensor labels
motor_R = wb_robot_get_device('motor_R');
motor_L = wb_robot_get_device('motor_L');
robot = wb_supervisor_node_get_from_def('Robot'); % for ground truth access
%% library initialization
t = linspace(1,T,N);
U = zeros(2,N);
E = zeros(4,N);
POSITION = zeros(3,N);
VELOCITY = zeros(3,N);
GYRO = zeros(3,N);
IMU = zeros(3,N);
V = zeros(1,N);
%% sensor initialization
gyro = wb_robot_get_device('gyro');
imu = wb_robot_get_device('imu');
wb_gyro_enable(gyro,ts);
wb_inertial_unit_enable(imu,ts);
%% controller setup
% initial torque inputs
U(:,1) = [0; 0];
% reference velocities
rOmega = 1;
rV = 1;
% controller gain matrix
% GET THIS MATRIX BY RUNNING lqrcontroller_v2.m
K = [1; 1]*[-349.6918 352.9846 -583.604 -438.7241];
K(2,2) = -K(2,2);
i = 1;
while wb_robot_step(ts) ~= -1
% send torque info to motors
wb_motor_set_torque(motor_L, U(2,i)); % rad/s
wb_motor_set_torque(motor_R, U(1,i)); % rad/s
% get ground truth data
position = wb_supervisor_node_get_position(robot);
velocity = wb_supervisor_node_get_velocity(robot);
POSITION(:,i) = position;
VELOCITY(:,i) = velocity;
% get sensor data
GYRO(:,i) = wb_gyro_get_values(gyro);
IMU(:,i) = wb_inertial_unit_get_roll_pitch_yaw(imu);
% organize data into my variables
Omega = GYRO(2,i);
phi = -IMU(1,i);
phidot = -GYRO(1,i);
% build error vector
ephi = -phi - .0132; % might be minus bias
ephidot = - phidot;
V(i) = sqrt(VELOCITY(1,i)^2 + VELOCITY(3,i)^2);
eV = rV - V(i);
eOmega = rOmega - Omega;
e = [eV eOmega ephi ephidot]';
E(:,i) = e;
% run controller
U(:,i+1) = -K*e;
% end condition
i = i + 1;
if i*ts >= T
wb_robot_step(ts) = -1;
end
end
% opens workspace in matlab
desktop;
keyboard;
%% plots
V = sqrt(VELOCITY(1,:).^2 + VELOCITY(3,:).^2);
% figure
% plot(t,-IMU(1,:),t,IMU(3,:))
% legend('phi','theta')
% title('angle')
figure
plot(t,-U(1,:),t,-U(2,:))
legend('right','left')
title('torque')
figure
plot(t,E)
legend('v','Omega','phi','phidot')
title('error')
figure
plot(t,V,t,GYRO(2,:),t,-IMU(1,:),t,-GYRO(1,:))
legend('V','Omega','phi','phidot')
title('outputs')
%%
COLORS = get(gca,'colororder');
plotdefaults(14,1,1.2,'northeast');
Omega = GYRO(2,:);
phi = -IMU(1,:);
phidot = -GYRO(1,:);
tsec = t/1000;
xrange = [0 10];
figure(1)
subplot(1,2,1)
plot(tsec,V,tsec,Omega,tsec,phi,tsec,phidot)
hold on
plot(tsec,ones(1,length(tsec)),'k--','color',COLORS(2,:))
hold off
legend('$V$','$\Omega$','$\phi$','$\dot\phi$')
xlim(xrange)
xlabel('Time, $t$ [s]')
ylabel('State Outputs')
subplot(1,2,2)
plot(tsec,-U(1,:),tsec,-U(2,:))
legend('$\tau_R$','$\tau_L$')
xlabel('Time, $t$ [s]')
ylabel('Torque Commands [N-m]')
xlim(xrange)
figure(1)
tightfig;
saveas(gcf,'Vel_Om_Step_Both.pdf')