PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qdq2av_c.c

https://github.com/mattbornski/spice
C | 717 lines | 16 code | 16 blank | 685 comment | 0 complexity | 2e74d4f82e7b2f3cac6d9eb4f36e30b6 MD5 | raw file
  1. /*
  2. -Procedure qdq2av_c (Quaternion and quaternion derivative to a.v.)
  3. -Abstract
  4. Derive angular velocity from a unit quaternion and its derivative
  5. with respect to time.
  6. -Disclaimer
  7. THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE
  8. CALIFORNIA INSTITUTE OF TECHNOLOGY (CALTECH) UNDER A U.S.
  9. GOVERNMENT CONTRACT WITH THE NATIONAL AERONAUTICS AND SPACE
  10. ADMINISTRATION (NASA). THE SOFTWARE IS TECHNOLOGY AND SOFTWARE
  11. PUBLICLY AVAILABLE UNDER U.S. EXPORT LAWS AND IS PROVIDED "AS-IS"
  12. TO THE RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY
  13. WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A
  14. PARTICULAR USE OR PURPOSE (AS SET FORTH IN UNITED STATES UCC
  15. SECTIONS 2312-2313) OR FOR ANY PURPOSE WHATSOEVER, FOR THE
  16. SOFTWARE AND RELATED MATERIALS, HOWEVER USED.
  17. IN NO EVENT SHALL CALTECH, ITS JET PROPULSION LABORATORY, OR NASA
  18. BE LIABLE FOR ANY DAMAGES AND/OR COSTS, INCLUDING, BUT NOT
  19. LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS,
  21. REGARDLESS OF WHETHER CALTECH, JPL, OR NASA BE ADVISED, HAVE
  22. REASON TO KNOW, OR, IN FACT, SHALL KNOW OF THE POSSIBILITY.
  23. RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF
  24. THE SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY
  25. CALTECH AND NASA FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE
  26. ACTIONS OF RECIPIENT IN THE USE OF THE SOFTWARE.
  27. -Required_Reading
  28. ROTATION
  29. -Keywords
  30. MATH
  31. POINTING
  32. ROTATION
  33. */
  34. #include "SpiceUsr.h"
  35. #undef qdq2av_c
  36. void qdq2av_c ( ConstSpiceDouble q [4],
  37. ConstSpiceDouble dq [4],
  38. SpiceDouble av [3] )
  39. /*
  40. -Brief_I/O
  41. VARIABLE I/O DESCRIPTION
  42. -------- --- --------------------------------------------------
  43. q I Unit SPICE quaternion.
  44. dq I Derivative of `q' with respect to time.
  45. av O Angular velocity defined by `q' and `dq'.
  46. -Detailed_Input
  47. q is a unit length 4-vector representing a SPICE-style
  48. quaternion. See the discussion of "Quaternion Styles"
  49. in the Particulars section below.
  50. Note that multiple styles of quaternions are in use.
  51. This routine will not work properly if the input
  52. quaternions do not conform to the SPICE convention.
  53. See the Particulars section for details.
  54. dq is a 4-vector representing the derivative of `q' with
  55. respect to time.
  56. -Detailed_Output
  57. av is 3-vector representing the angular velocity defined
  58. by `q' and `dq', that is, the angular velocity of the
  59. frame defined by the rotation matrix associated with
  60. `q'. This rotation matrix can be obtained via the
  61. CSPICE routine q2m_c; see the Particulars section for
  62. the explicit matrix entries.
  63. `av' is the vector (imaginary) part of the quaternion
  64. product
  65. *
  66. -2 * q * dq
  67. This angular velocity is the same vector that could
  68. be obtained (much less efficiently ) by mapping `q'
  69. and `dq' to the corresponding C-matrix `r' and its
  70. derivative `dr', then calling the CSPICE routine
  71. xf2rav_c.
  72. `av' has units of
  73. radians / T
  74. where
  75. 1 / T
  76. is the unit associated with `dq'.
  77. -Parameters
  78. None.
  79. -Exceptions
  80. Error free.
  81. 1) A unitized version of input quaternion is used in the
  82. computation. No attempt is made to diagnose an invalid
  83. input quaternion.
  84. -Files
  85. None.
  86. -Particulars
  87. Quaternion Styles
  88. -----------------
  89. There are different "styles" of quaternions used in
  90. science and engineering applications. Quaternion styles
  91. are characterized by
  92. - The order of quaternion elements
  93. - The quaternion multiplication formula
  94. - The convention for associating quaternions
  95. with rotation matrices
  96. Two of the commonly used styles are
  97. - "SPICE"
  98. > Invented by Sir William Rowan Hamilton
  99. > Frequently used in mathematics and physics textbooks
  100. - "Engineering"
  101. > Widely used in aerospace engineering applications
  102. CSPICE function interfaces ALWAYS use SPICE quaternions.
  103. Quaternions of any other style must be converted to SPICE
  104. quaternions before they are passed to CSPICE functions.
  105. Relationship between SPICE and Engineering Quaternions
  106. ------------------------------------------------------
  107. Let M be a rotation matrix such that for any vector V,
  108. M*V
  109. is the result of rotating V by theta radians in the
  110. counterclockwise direction about unit rotation axis vector A.
  111. Then the SPICE quaternions representing M are
  112. (+/-) ( cos(theta/2),
  113. sin(theta/2) A(1),
  114. sin(theta/2) A(2),
  115. sin(theta/2) A(3) )
  116. while the engineering quaternions representing M are
  117. (+/-) ( -sin(theta/2) A(1),
  118. -sin(theta/2) A(2),
  119. -sin(theta/2) A(3),
  120. cos(theta/2) )
  121. For both styles of quaternions, if a quaternion q represents
  122. a rotation matrix M, then -q represents M as well.
  123. Given an engineering quaternion
  124. QENG = ( q0, q1, q2, q3 )
  125. the equivalent SPICE quaternion is
  126. QSPICE = ( q3, -q0, -q1, -q2 )
  127. Associating SPICE Quaternions with Rotation Matrices
  128. ----------------------------------------------------
  129. Let FROM and TO be two right-handed reference frames, for
  130. example, an inertial frame and a spacecraft-fixed frame. Let the
  131. symbols
  132. V , V
  133. FROM TO
  134. denote, respectively, an arbitrary vector expressed relative to
  135. the FROM and TO frames. Let M denote the transformation matrix
  136. that transforms vectors from frame FROM to frame TO; then
  137. V = M * V
  138. TO FROM
  139. where the expression on the right hand side represents left
  140. multiplication of the vector by the matrix.
  141. Then if the unit-length SPICE quaternion q represents M, where
  142. q = (q0, q1, q2, q3)
  143. the elements of M are derived from the elements of q as follows:
  144. +- -+
  145. | 2 2 |
  146. | 1 - 2*( q2 + q3 ) 2*(q1*q2 - q0*q3) 2*(q1*q3 + q0*q2) |
  147. | |
  148. | |
  149. | 2 2 |
  150. M = | 2*(q1*q2 + q0*q3) 1 - 2*( q1 + q3 ) 2*(q2*q3 - q0*q1) |
  151. | |
  152. | |
  153. | 2 2 |
  154. | 2*(q1*q3 - q0*q2) 2*(q2*q3 + q0*q1) 1 - 2*( q1 + q2 ) |
  155. | |
  156. +- -+
  157. Note that substituting the elements of -q for those of q in the
  158. right hand side leaves each element of M unchanged; this shows
  159. that if a quaternion q represents a matrix M, then so does the
  160. quaternion -q.
  161. To map the rotation matrix M to a unit quaternion, we start by
  162. decomposing the rotation matrix as a sum of symmetric
  163. and skew-symmetric parts:
  164. 2
  165. M = [ I + (1-cos(theta)) OMEGA ] + [ sin(theta) OMEGA ]
  166. symmetric skew-symmetric
  167. OMEGA is a skew-symmetric matrix of the form
  168. +- -+
  169. | 0 -n3 n2 |
  170. | |
  171. OMEGA = | n3 0 -n1 |
  172. | |
  173. | -n2 n1 0 |
  174. +- -+
  175. The vector N of matrix entries (n1, n2, n3) is the rotation axis
  176. of M and theta is M's rotation angle. Note that N and theta
  177. are not unique.
  178. Let
  179. C = cos(theta/2)
  180. S = sin(theta/2)
  181. Then the unit quaternions Q corresponding to M are
  182. Q = +/- ( C, S*n1, S*n2, S*n3 )
  183. The mappings between quaternions and the corresponding rotations
  184. are carried out by the CSPICE routines
  185. q2m_c {quaternion to matrix}
  186. m2q_c {matrix to quaternion}
  187. m2q_c always returns a quaternion with scalar part greater than
  188. or equal to zero.
  189. SPICE Quaternion Multiplication Formula
  190. ---------------------------------------
  191. Given a SPICE quaternion
  192. Q = ( q0, q1, q2, q3 )
  193. corresponding to rotation axis A and angle theta as above, we can
  194. represent Q using "scalar + vector" notation as follows:
  195. s = q0 = cos(theta/2)
  196. v = ( q1, q2, q3 ) = sin(theta/2) * A
  197. Q = s + v
  198. Let Q1 and Q2 be SPICE quaternions with respective scalar
  199. and vector parts s1, s2 and v1, v2:
  200. Q1 = s1 + v1
  201. Q2 = s2 + v2
  202. We represent the dot product of v1 and v2 by
  203. <v1, v2>
  204. and the cross product of v1 and v2 by
  205. v1 x v2
  206. Then the SPICE quaternion product is
  207. Q1*Q2 = s1*s2 - <v1,v2> + s1*v2 + s2*v1 + (v1 x v2)
  208. If Q1 and Q2 represent the rotation matrices M1 and M2
  209. respectively, then the quaternion product
  210. Q1*Q2
  211. represents the matrix product
  212. M1*M2
  213. About this routine
  214. ==================
  215. Given a time-dependent SPICE quaternion representing the
  216. attitude of an object, we can obtain the object's angular
  217. velocity AV in terms of the quaternion Q and its derivative
  218. with respect to time DQ:
  219. *
  220. AV = Im ( -2 * Q * DQ ) (1)
  221. That is, AV is the vector (imaginary) part of the product
  222. on the right hand side (RHS) of equation (1). The scalar part
  223. of the RHS is zero.
  224. We'll now provide an explanation of formula (1). For any
  225. time-dependent rotation, the associated angular velocity at a
  226. given time is a function of the rotation and its derivative at
  227. that time. This fact enables us to extend a proof for a limited
  228. subset of rotations to *all* rotations: if we find a formula
  229. that, for any rotation in our subset, gives us the angular
  230. velocity as a function of the rotation and its derivative, then
  231. that formula must be true for all rotations.
  232. We start out by considering the set of rotation matrices
  233. R(t) = M(t)C (2)
  234. where C is a constant rotation matrix and M(t) represents a
  235. matrix that "rotates" with constant, unit magnitude angular
  236. velocity and that is equal to the identity matrix at t = 0.
  237. For future reference, we'll consider C to represent a coordinate
  238. transformation from frame F1 to frame F2. We'll call F1 the
  239. "base frame" of C. We'll let AVF2 be the angular velocity of
  240. M(t) relative to F2 and AVF1 be the same angular velocity
  241. relative to F1.
  242. Referring to the axis-and-angle decomposition of M(t)
  243. 2
  244. M(t) = I + sin(t)OMEGA + (1-cos(t))OMEGA (3)
  245. (see the Rotation Required Reading for a derivation) we
  246. have
  247. d(M(t))|
  248. -------| = OMEGA (4)
  249. dt |t=0
  250. Then the derivative of R(t) at t = 0 is given by
  251. d(R(t))|
  252. -------| = OMEGA * C (5)
  253. dt |t=0
  254. The rotation axis A associated with OMEGA is defined by (6)
  255. A(1) = - OMEGA(2,3)
  256. A(2) = OMEGA(1,3)
  257. A(3) = - OMEGA(1,2)
  258. Since the coordinate system rotation M(t) rotates vectors about A
  259. through angle t radians at time t, the angular velocity AVF2 of
  260. M(t) is actually given by
  261. AVF2 = - A (7)
  262. This angular velocity is represented relative to the image
  263. frame F2 associated with the coordinate transformation C.
  264. Now, let's proceed to the angular velocity formula for
  265. quaternions.
  266. To avoid some verbiage, we'll freely use 3-vectors to represent
  267. the corresponding pure imaginary quaternions.
  268. Letting QR(t), QM(t), and QC be quaternions representing the
  269. time-dependent matrices R(t), M(t) and C respectively, where
  270. QM(t) is selected to be a differentiable function of t in a
  271. neighborhood of t = 0, the quaternion representing R(t) is
  272. QR(t) = QM(t) * QC (8)
  273. Differentiating with respect to t, then evaluating derivatives
  274. at t = 0, we have
  275. d(QR(t))| d(QM(t))|
  276. --------| = --------| * QC (9)
  277. dt |t=0 dt |t=0
  278. Since QM(t) represents a rotation having axis A and rotation
  279. angle t, then (according to the relationship between SPICE
  280. quaternions and rotations set out in the Rotation Required
  281. Reading), we see QM(t) must be the quaternion (represented as the
  282. sum of scalar and vector parts):
  283. cos(t/2) + sin(t/2) * A (10)
  284. where A is the rotation axis corresponding to the matrix
  285. OMEGA introduced in equation (3). By inspection
  286. d(QM(t))|
  287. --------| = 1/2 * A (11)
  288. dt |t=0
  289. which is a quaternion with scalar part zero. This allows us to
  290. rewrite the quaternion derivative
  291. d(QR(t))|
  292. --------| = 1/2 * A * QC (12)
  293. dt |t=0
  294. or for short,
  295. DQ = 1/2 * A * QC (13)
  296. Since from (7) we know the angular velocity AVF2 of the frame
  297. associated with QM(t) is the negative of the rotation axis
  298. defined by (3), we have
  299. DQ = - 1/2 * AVF2 * QC (14)
  300. Since
  301. AVF2 = C * AVF1 (15)
  302. we can apply the quaternion transformation formula
  303. (from the Rotation Required Reading)
  304. *
  305. AVF2 = QC * AVF1 * QC (16)
  306. Now we re-write (15) as
  307. *
  308. DQ = - 1/2 * ( QC * AVF1 * QC ) * QC
  309. = - 1/2 * QC * AVF1 (17)
  310. Then the angular velocity vector AVF1 is given by
  311. *
  312. AVF1 = -2 * QC * DQ (18)
  313. The relation (18) has now been demonstrated for quaternions
  314. having constant, unit magnitude angular velocity. But since
  315. all time-dependent quaternions having value QC and derivative
  316. DQ at a given time t have the same angular velocity at time t,
  317. that angular velocity must be AVF1.
  318. -Examples
  319. The following test program creates a quaternion and quaternion
  320. derivative from a known rotation matrix and angular velocity
  321. vector. The angular velocity is recovered from the quaternion
  322. and quaternion derivative by calling qdq2av_c and by an
  323. alternate method; the results are displayed for comparison.
  324. #include <stdio.h>
  325. #include "SpiceUsr.h"
  326. #include "SpiceZfc.h"
  327. int main()
  328. {
  329. /.
  330. Local constants
  331. ./
  332. /.
  333. Local variables
  334. ./
  335. SpiceDouble angle [3];
  336. SpiceDouble av [3];
  337. SpiceDouble avx [3];
  338. SpiceDouble dm [3][3];
  339. SpiceDouble dq [4];
  340. SpiceDouble expav [3];
  341. SpiceDouble m [3][3];
  342. SpiceDouble mout [3][3];
  343. SpiceDouble q [4];
  344. SpiceDouble qav [4];
  345. SpiceDouble xtrans [6][6];
  346. SpiceInt i;
  347. /.
  348. Pick some Euler angles and form a rotation matrix.
  349. ./
  350. angle[0] = -20.0 * rpd_c();
  351. angle[1] = 50.0 * rpd_c();
  352. angle[2] = -60.0 * rpd_c();
  353. eul2m_c ( angle[2], angle[1], angle[0], 3, 1, 3, m );
  354. m2q_c ( m, q );
  355. /.
  356. Choose an angular velocity vector.
  357. ./
  358. expav[0] = 1.0;
  359. expav[1] = 2.0;
  360. expav[2] = 3.0;
  361. /.
  362. Form the quaternion derivative.
  363. ./
  364. qav[0] = 0.0;
  365. vequ_c ( expav, qav+1 );
  366. qxq_c ( q, qav, dq );
  367. vsclg_c ( -0.5, dq, 4, dq );
  368. /.
  369. Recover angular velocity from `q' and `dq' using qdq2av_c.
  370. ./
  371. qdq2av_c ( q, dq, av );
  372. /.
  373. Now we'll obtain the angular velocity from `q' and
  374. `dq' by an alternate method.
  375. Convert `q' back to a rotation matrix.
  376. ./
  377. q2m_c ( q, m );
  378. /.
  379. Convert `q' and `dq' to a rotation derivative matrix. This
  380. somewhat messy procedure is based on differentiating the
  381. formula for deriving a rotation from a quaternion, then
  382. substituting components of `q' and `dq' into the derivative
  383. formula.
  384. ./
  385. dm[0][0] = -4.0 * ( q[2]*dq[2] + q[3]*dq[3] );
  386. dm[0][1] = 2.0 * ( q[1]*dq[2] + q[2]*dq[1]
  387. - q[0]*dq[3] - q[3]*dq[0] );
  388. dm[0][2] = 2.0 * ( q[1]*dq[3] + q[3]*dq[1]
  389. + q[0]*dq[2] + q[2]*dq[0] );
  390. dm[1][0] = 2.0 * ( q[1]*dq[2] + q[2]*dq[1]
  391. + q[0]*dq[3] + q[3]*dq[0] );
  392. dm[1][1] = -4.0 * ( q[1]*dq[1] + q[3]*dq[3] );
  393. dm[1][2] = 2.0 * ( q[2]*dq[3] + q[3]*dq[2]
  394. - q[0]*dq[1] - q[1]*dq[0] );
  395. dm[2][0] = 2.0 * ( q[3]*dq[1] + q[1]*dq[3]
  396. - q[0]*dq[2] - q[2]*dq[0] );
  397. dm[2][1] = 2.0 * ( q[2]*dq[3] + q[3]*dq[2]
  398. + q[0]*dq[1] + q[1]*dq[0] );
  399. dm[2][2] = -4.0 * ( q[1]*dq[1] + q[2]*dq[2] );
  400. /.
  401. Form the state transformation matrix corresponding to `m'
  402. and `dm'.
  403. ./
  404. /.
  405. Upper left block:
  406. ./
  407. for ( i = 0; i < 3; i++ )
  408. {
  409. vequ_c ( m[i], xtrans[i] );
  410. }
  411. /.
  412. Upper right block:
  413. ./
  414. for ( i = 0; i < 3; i++ )
  415. {
  416. vpack_c ( 0.0, 0.0, 0.0, xtrans[i]+3 );
  417. }
  418. /.
  419. Lower left block:
  420. ./
  421. for ( i = 0; i < 3; i++ )
  422. {
  423. vequ_c ( dm[i], xtrans[3+i] );
  424. }
  425. /.
  426. Lower right block:
  427. ./
  428. for ( i = 0; i < 3; i++ )
  429. {
  430. vequ_c ( m[i], xtrans[3+i]+3 );
  431. }
  432. /.
  433. Now use xf2rav_c to produce the expected angular velocity.
  434. ./
  435. xf2rav_c ( xtrans, mout, avx );
  436. /.
  437. The results should match to nearly full double precision.
  438. ./
  439. printf ( "Original angular velocity: \n"
  440. " %24.16e, %24.16e, %24.16e \n"
  441. "qdq2av_c's angular velocity: \n"
  442. " %24.16e, %24.16e, %24.16e \n"
  443. "xf2rav's angular velocity: \n"
  444. " %24.16e, %24.16e, %24.16e \n",
  445. expav[0], expav[1], expav[2],
  446. av [0], av [1], av [2],
  447. avx [0], avx [1], avx [2] );
  448. return ( 0 );
  449. }
  450. -Restrictions
  451. None.
  452. -Literature_References
  453. None.
  454. -Author_and_Institution
  455. N.J. Bachman (JPL)
  456. -Version
  457. -CSPICE Version 1.0.1, 27-FEB-2008 (NJB)
  458. Updated header; added information about SPICE
  459. quaternion conventions.
  460. -CSPICE Version 1.0.0, 31-OCT-2005 (NJB)
  461. -Index_Entries
  462. angular velocity from quaternion and derivative
  463. -&
  464. */
  465. { /* Begin qdq2av_c */
  466. /*
  467. Local variables
  468. */
  469. SpiceDouble qhat [4];
  470. SpiceDouble qstar [4];
  471. SpiceDouble qtemp [4];
  472. /*
  473. This routine is error free.
  474. */
  475. /*
  476. Get a unitized copy of the input quaternion.
  477. */
  478. vhatg_c ( q, 4, qhat );
  479. /*
  480. Get the conjugate `qstar' of `qhat'.
  481. */
  482. qstar[0] = qhat[0];
  483. vminus_c ( qhat+1, qstar+1 );
  484. /*
  485. Compute the angular velocity via the relationship
  486. *
  487. av = -2 * q * dq
  488. */
  489. qxq_c ( qstar, dq, qtemp );
  490. vequ_c ( qtemp+1, av );
  491. vscl_c ( -2.0, av, av );
  492. } /* End qdq2av_c */