annotate scripts/quaternion/demoquat.m @ 3383:ebf69eb3d07d

[project @ 1999-12-15 21:27:56 by jwe]
author jwe
date Wed, 15 Dec 1999 21:28:01 +0000
parents 41602f25d19f
children e098ebb77023
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3224
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
1
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
2 # Thanks to Mr. Charles Hall, Dr. Don Krupp and Dr. Larry Mullins at
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
3 # NASA's Marshall Space Flight Center for notes and instruction in
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
4 # use and conventions with quaternions. - A. S. Hodel
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
5
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
6 function opt = demoquat()
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
7
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
8 opt = 0;
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
9 quitopt = 5;
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
10 while(opt != quitopt)
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
11 opt = menu("Quaternion function demo (c) 1998 A. S. Hodel, a.s.hodel@eng.auburn.edu",
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
12 "quaternion construction/data extraction",
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
13 "simple quaternion functions",
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
14 "transformation functions",
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
15 "body-inertial frame demo",
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
16 "Quit");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
17
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
18 switch(opt)
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
19 case(1),
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
20 printf("Quaternion construction/data extraction\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
21 help quaternion
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
22 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
23 cmd = "q = quaternion(1,2,3,4)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
24 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
25 disp("This format stores the i,j,k parts of the quaternion first;")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
26 disp("the real part is stored last.")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
27 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
28 disp(" ")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
29 disp("i, j, and k are all square roots of -1; however they do not")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
30 disp("commute under multiplication (discussed further with the function")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
31 disp("qmult). Therefore quaternions do not commute under multiplcation:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
32 disp(" q1*q2 != q2*q1 (usually)")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
33 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
34
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
35 disp("Quaternions as rotations: unit quaternion to represent")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
36 disp("rotation of 45 degrees about the vector [1 1 1]")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
37 cmd = "degrees = pi/180; q1 = quaternion([1 1 1],45*degrees)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
38 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
39 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
40 cmd = "real_q = cos(45*degrees/2)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
41 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
42 printf("The real part of the quaternion q(4) is cos(theta/2).\n----\n\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
43 cmd = "imag_q = sin(45*degrees/2)*[1 1 1]/norm([1 1 1])"
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
44 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
45 disp("The imaginary part of the quaternion is sin(theta/2)*unit vector");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
46 disp("The constructed quaternion is a unit quaternion.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
47 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
48 disp("Can also extract both forms of the quaternion:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
49 disp("Vector/angle form of 1i + 2j + 3k + 4:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
50 cmd = "[vv,th] = quaternion(q)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
51 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
52 cmd = "vv_norm = norm(vv)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
53 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
54 disp("Returns the eigenaxis as a 3-d unit vector");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
55 disp("Check values: ")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
56 cmd = "th_deg = th*180/pi";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
57 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
58 disp("")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
59 disp("This concludes the quaternion construction/extraction demo.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
60 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
61
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
62 case(2),
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
63 printf("Simple quaternion functions\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
64 cmd = "help qconj";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
65 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
66 cmd = "degrees = pi/180; q1 = quaternion([1 1 1],45*degrees)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
67 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
68 cmd = "q2 = qconj(q1)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
69 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
70 disp("The conjugate changes the sign of the complex part of the")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
71 printf("quaternion.\n\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
72 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
73 printf("\n\n\nMultiplication of quaternions:\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
74 cmd = "help qmult";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
75 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
76 cmd = "help qinv"
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
77 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
78 disp("Inverse quaternion: q*qi = qi*q = 1:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
79 cmd = "q1i = qinv(q1)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
80 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
81 cmd = "one = qmult(q1,q1i)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
82 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
83
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
84 printf("Conclusion of simple quaternion functions");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
85 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
86 case(3),
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
87 printf("Transformation functions\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
88 disp("A problem with the discussion of coordinate transformations is that");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
89 disp("one must be clear on what is being transformed: does a rotation of");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
90 disp("theta degrees mean that you're rotating the VECTOR by theta degrees,");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
91 disp("also called the 'active convention,' or does it mean that you rotate ");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
92 disp("the COORDINATE FRAME by theta degrees, also called the 'passive convention,' ");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
93 disp("which is equivalent to rotating the VECTOR by (-theta) degrees. The");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
94 disp("functions in this demo use the active convention. I'll point out where");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
95 disp("this changes the code as the demo runs.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
96 disp(" -- The author");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
97 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
98 printf("\n\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
99 disp("Sequences of rotations:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
100 printf("\n\nRotation of a vector by 90 degrees about the reference z axis\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
101 cmd = "qz = quaternion([0 0 1], pi/2);";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
102 disp(cmd) ; eval(cmd);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
103 printf("\n\nRotation of a vector by 90 degrees about the reference y axis\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
104 cmd="qy = quaternion([0 1 0], pi/2);";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
105 disp(cmd) ; eval(cmd);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
106 printf("\n\nRotation of a vector by 90 degrees about the reference x axis\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
107 cmd="qx = quaternion([1 0 0], pi/2);";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
108 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
109 printf("\n\nSequence of three rotations: 90 degrees about x, then 90 degrees\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
110 disp("about y, then 90 degrees about z (all axes specified in the reference frame):");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
111 qchk = qmult(qz,qmult(qy,qx));
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
112 cmd = "[vv,th] = quaternion(qchk), th_deg = th*180/pi";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
113 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
114 disp("The sequence of the three rotations above is equivalent to a single rotation")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
115 disp("of 90 degrees about the y axis. Check:");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
116 cmd = "err = norm(qchk - qy)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
117 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
118
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
119 disp("Transformation of a quaternion by a quaternion:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
120 disp("The three quaternions above were rotations specified about")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
121 disp("a single reference frame. It is often convenient to specify the");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
122 disp("eigenaxis of a rotation in a different frame (e.g., when computing");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
123 disp("the transformation rotation in terms of the Euler angles yaw-pitch-roll).");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
124 cmd = "help qtrans";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
125 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
126 disp("")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
127 disp("NOTE: If the passive convention is used, then the above");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
128 disp("formula changes to v = qinv(q)*v*q instead of ")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
129 disp("v = q*v*qinv(q).")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
130 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
131 disp("")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
132 disp("Example: Vectors in Frame 2 are obtained by rotating them from ")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
133 disp(" from Frame 1 by 90 degrees about the x axis (quaternion qx)")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
134 disp(" A quaternion in Frame 2 rotates a vector by 90 degrees about")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
135 disp(" the Frame 2 y axis (quaternion qy). The equivalent rotation")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
136 disp(" in the reference frame is:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
137 cmd = "q_eq = qtrans(qy,qx); [vv,th] = quaternion(q_eq)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
138 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
139 disp("The rotation is equivalent to rotating about the reference z axis")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
140 disp("by 90 degrees (quaternion qz)")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
141 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
142
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
143 disp("Transformation of a vector by a quaternion");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
144 cmd = "help qtransv";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
145 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
146
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
147 disp("NOTE: the above formula changes if the passive quaternion ")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
148 disp("is used; the cross product term is subtracted instead of added.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
149 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
150 disp("Example: rotate the vector [1,1,1] by 90 degrees about the y axis");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
151 cmd = "vec_r = qtransv([1,1,1],qy)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
152 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
153 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
154 disp("Equivalently, one may multiply by qtransvmat:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
155 cmd = "help qtransvmat";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
156 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
157 disp("NOTE: the passive quaternion convention would use the transpose")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
158 disp("(inverse) of the orthogonal matrix returned by qtransvmat.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
159 prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
160 cmd = "vec_r_2 = qtransvmat(qy)*[1;1;1]; vec_err = norm(vec_r - vec_r_2)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
161 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
162
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
163 disp("")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
164 disp("The last transformation function is the derivative of a quaternion")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
165 disp("Given rotation rates about the reference x, y, and z axes.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
166 cmd = "help qderivmat";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
167 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
168 disp("")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
169 disp("Example:")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
170 disp("Frame is rotating about the z axis at 1 rad/s")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
171 cmd = "Omega = [0,0,1]; Dmat = qderivmat(Omega)";
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
172 run_cmd
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
173 disp("Notice that Dmat is skew symmetric, as it should be.")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
174 disp("expm(Dmat*t) is orthogonal, so that unit quaternions remain")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
175 disp("unit quaternions as the rotating frame precesses.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
176 disp(" ")
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
177 disp("This concludes the transformation demo.");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
178 prompt;
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
179 case(4),
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
180 printf("Body-inertial frame demo: Look at the source code for\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
181 printf("demoquat.m and qcoordinate_plot.m to see how it's done.\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
182
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
183 # i,j,k units
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
184 iv = quaternion(1,0,0,0); jv = quaternion(0,1,0,0);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
185 kv = quaternion(0,0,1,0);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
186
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
187 # construct quaternion to desired view.
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
188 degrees = pi/180; daz = 45*degrees; del = -30*degrees;
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
189 qazimuth = quaternion([0,0,1],daz);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
190 qelevation = quaternion([cos(daz),sin(daz),0],del);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
191 qview = qmult(qelevation,qazimuth);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
192
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
193 # inertial frame i, j, k axes.
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
194 iif = iv; jf = qtrans(jv,iv); kf = qtrans(kv,iv);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
195
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
196 # rotation steps
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
197 th = 0:5:20; ov = ones(size(th)); myth = [th,max(th)*ov ; 0*ov,th];
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
198
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
199 # construct yaw-pitch-roll cartoon
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
200 for kk=1:length(myth(1,:))
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
201 figure(kk)
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
202 thy = myth(1,kk);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
203 thp = myth(2,kk);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
204
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
205 qyaw = quaternion([0,0,1],thy*pi/180);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
206 [jvy,th] = quaternion(qtrans(jf,qyaw));
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
207 qpitch = quaternion(jvy(1:3),thp*pi/180);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
208 qb = qmult(qpitch, qyaw);
3235
41602f25d19f [project @ 1999-03-05 07:14:08 by jwe]
jwe
parents: 3224
diff changeset
209 qi = quaternion([1, 0, 0],180*degrees);
3224
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
210
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
211 printf("yaw=%8.4f, pitch=%8.4f, \n qbi = (%8.4f)i + (%8.4e)j + (%8.4f)k + (%8.4f)\n",thy,thp, ...
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
212 qb(1), qb(2), qb(3), qb(4));
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
213 [vv,th] = quaternion(qb);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
214 printf(" = (vector) = [%8.4f %8.4f %8.4f], th=%5.2f deg\n", ...
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
215 vv(1), vv(2), vv(3), th*180/pi);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
216
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
217 qb = qmult(qb,qi);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
218 title(sprintf("yaw=%5.2f deg, pitch=%5.2f deg",thy,thp))
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
219 qcoordinate_plot(qi,qb,qview);
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
220 # uncomment the next four lines to get eps output
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
221 #gset terminal postscript eps
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
222 #eval(sprintf("gset output 'fig%d.eps'",kk));
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
223 #replot
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
224 #gset terminal x11
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
225 #prompt
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
226 endfor
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
227 case(quitopt),
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
228 printf("Exiting quaternion demo\n");
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
229 otherwise,
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
230 error(sprintf("Illegal option %f",opt));
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
231 endswitch
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
232 endwhile
1e7532b9173b [project @ 1998-12-04 22:08:23 by jwe]
jwe
parents:
diff changeset
233 endfunction