annotate scripts/quaternion/demoquat.m @ 6447:3f79532415b5

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