--- demoquat.m.orig 2011-01-05 16:23:40.000000000 -0500 +++ demoquat.m 2011-01-05 16:30:16.000000000 -0500 @@ -47,7 +47,7 @@ help quaternion prompt cmd = "q = quaternion(1,2,3,4)"; - run_cmd + run_cmd (cmd) disp("This format stores the i,j,k parts of the quaternion first;") disp("the real part is stored last.") prompt @@ -61,26 +61,26 @@ disp("Quaternions as rotations: unit quaternion to represent") disp("rotation of 45 degrees about the vector [1 1 1]") cmd = "degrees = pi/180; q1 = quaternion([1 1 1],45*degrees)"; - run_cmd + run_cmd (cmd) prompt cmd = "real_q = cos(45*degrees/2)"; - run_cmd + run_cmd (cmd) printf("The real part of the quaternion q(4) is cos(theta/2).\n----\n\n"); cmd = "imag_q = sin(45*degrees/2)*[1 1 1]/norm([1 1 1])" - run_cmd + run_cmd (cmd) disp("The imaginary part of the quaternion is sin(theta/2)*unit vector"); disp("The constructed quaternion is a unit quaternion."); prompt disp("Can also extract both forms of the quaternion:") disp("Vector/angle form of 1i + 2j + 3k + 4:") cmd = "[vv,th] = quaternion(q)"; - run_cmd + run_cmd (cmd) cmd = "vv_norm = norm(vv)"; - run_cmd + run_cmd (cmd) disp("Returns the eigenaxis as a 3-d unit vector"); disp("Check values: ") cmd = "th_deg = th*180/pi"; - run_cmd + run_cmd (cmd) disp("") disp("This concludes the quaternion construction/extraction demo."); prompt @@ -88,24 +88,24 @@ case(2) printf("Simple quaternion functions\n"); cmd = "help qconj"; - run_cmd + run_cmd (cmd) cmd = "degrees = pi/180; q1 = quaternion([1 1 1],45*degrees)"; - run_cmd + run_cmd (cmd) cmd = "q2 = qconj(q1)"; - run_cmd + run_cmd (cmd) disp("The conjugate changes the sign of the complex part of the") printf("quaternion.\n\n"); prompt printf("\n\n\nMultiplication of quaternions:\n"); cmd = "help qmult"; - run_cmd + run_cmd (cmd) cmd = "help qinv" - run_cmd + run_cmd (cmd) disp("Inverse quaternion: q*qi = qi*q = 1:") cmd = "q1i = qinv(q1)"; - run_cmd + run_cmd (cmd) cmd = "one = qmult(q1,q1i)"; - run_cmd + run_cmd (cmd) printf("Conclusion of simple quaternion functions"); prompt @@ -132,16 +132,16 @@ disp(cmd) ; eval(cmd); printf("\n\nRotation of a vector by 90 degrees about the reference x axis\n"); cmd="qx = quaternion([1 0 0], pi/2);"; - run_cmd + run_cmd (cmd) printf("\n\nSequence of three rotations: 90 degrees about x, then 90 degrees\n"); disp("about y, then 90 degrees about z (all axes specified in the reference frame):"); qchk = qmult(qz,qmult(qy,qx)); cmd = "[vv,th] = quaternion(qchk), th_deg = th*180/pi"; - run_cmd + run_cmd (cmd) disp("The sequence of the three rotations above is equivalent to a single rotation") disp("of 90 degrees about the y axis. Check:"); cmd = "err = norm(qchk - qy)"; - run_cmd + run_cmd (cmd) disp("Transformation of a quaternion by a quaternion:") disp("The three quaternions above were rotations specified about") @@ -149,7 +149,7 @@ disp("eigenaxis of a rotation in a different frame (e.g., when computing"); disp("the transformation rotation in terms of the Euler angles yaw-pitch-roll)."); cmd = "help qtrans"; - run_cmd + run_cmd (cmd) disp("") disp("NOTE: If the passive convention is used, then the above"); disp("formula changes to v = qinv(q)*v*q instead of ") @@ -162,41 +162,41 @@ disp(" the Frame 2 y axis (quaternion qy). The equivalent rotation") disp(" in the reference frame is:") cmd = "q_eq = qtrans(qy,qx); [vv,th] = quaternion(q_eq)"; - run_cmd + run_cmd (cmd) disp("The rotation is equivalent to rotating about the reference z axis") disp("by 90 degrees (quaternion qz)") prompt disp("Transformation of a vector by a quaternion"); cmd = "help qtransv"; - run_cmd + run_cmd (cmd) disp("NOTE: the above formula changes if the passive quaternion ") disp("is used; the cross product term is subtracted instead of added."); prompt disp("Example: rotate the vector [1,1,1] by 90 degrees about the y axis"); cmd = "vec_r = qtransv([1,1,1],qy)"; - run_cmd + run_cmd (cmd) prompt disp("Equivalently, one may multiply by qtransvmat:") cmd = "help qtransvmat"; - run_cmd + run_cmd (cmd) disp("NOTE: the passive quaternion convention would use the transpose") disp("(inverse) of the orthogonal matrix returned by qtransvmat."); prompt cmd = "vec_r_2 = qtransvmat(qy)*[1;1;1]; vec_err = norm(vec_r - vec_r_2)"; - run_cmd + run_cmd (cmd) disp("") disp("The last transformation function is the derivative of a quaternion") disp("Given rotation rates about the reference x, y, and z axes."); cmd = "help qderivmat"; - run_cmd + run_cmd (cmd) disp("") disp("Example:") disp("Frame is rotating about the z axis at 1 rad/s") cmd = "Omega = [0,0,1]; Dmat = qderivmat(Omega)"; - run_cmd + run_cmd (cmd) disp("Notice that Dmat is skew symmetric, as it should be.") disp("expm(Dmat*t) is orthogonal, so that unit quaternions remain") disp("unit quaternions as the rotating frame precesses."); @@ -257,3 +257,31 @@ endwhile endfunction + +function run_cmd (cmd) + disp (["Command: ", cmd]) + puts ("Press a key to execute command"); + fflush (stdout); + kbhit (); + disp (" executing"); + fflush (stdout); + evalin ("caller", cmd); + disp ("---") + disp (" ") +endfunction + +function prompt (str) + + if (nargin > 1) + print_usage (); + elseif (nargin == 0) + str = "\n ---- Press a key to continue ---"; + elseif (! ischar (str)) + error ("prompt: input must be a string"); + endif + + disp (str); + fflush (stdout); + kbhit (); + +endfunction