COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
playground.cpp File Reference
#include <iostream>
#include <iomanip>
#include "math/mathlib.h"
#include "support/stringlib.h"
#include "device/cpu/devicecpu.h"
#include "support/elapsedtime.h"
Include dependency graph for playground.cpp:

Functions

void compute_quaternion_from_vectors ()
 
void torque_mtr ()
 
int main ()
 
void test ()
 

Function Documentation

void compute_quaternion_from_vectors ( )
63  {
64  DCM dcm;
65 
66  // define basis 1
67  basisOrthonormal frame_inertial;
68  frame_inertial.i = cv_unitx();
69  frame_inertial.j = cv_unity();
70  frame_inertial.k = cv_unitz();
71 
72  // define basis 2
73  // target frame wrt basis1 frame inertial
74  basisOrthonormal frame_target_wrt_inertial;
75 
76  // // rotate 90 deg
77  // frame_target_wrt_inertial.i = {0,1,0};
78  // frame_target_wrt_inertial.j = {-1,0,0};
79  // frame_target_wrt_inertial.k = {0,0,1};
80 
81  // // rotate 180 deg
82  // frame_target_wrt_inertial.i = {-1,0,0};
83  // frame_target_wrt_inertial.j = {0,-1,0};
84  // frame_target_wrt_inertial.k = {0,0,1};
85 
86  // // rotate 270 deg, q = [0,0,-0.707107, 0.707107]
87  // frame_target_wrt_inertial.i = {0,-1,0};
88  // frame_target_wrt_inertial.j = {1,0,0};
89  // frame_target_wrt_inertial.k = {0,0,1};
90 
91  // rotate x down a bit [1,0,0]->[1,0,-1] [0 -0.382683 0 0.92388]
92  frame_target_wrt_inertial.i = {1,0,1};
93  frame_target_wrt_inertial.j = {0,1,0};
94  frame_target_wrt_inertial.k = {-1,0,1};
95 
96  // rotate x up a bit [1,0,0]->[1,0,-1] [0 -0.382683 0 0.92388]
97  frame_target_wrt_inertial.i = {1,0,-1};
98  frame_target_wrt_inertial.j = {0,1,0};
99  frame_target_wrt_inertial.k = {1,0,1};
100 
101  // rotate y up a bit [1,0,0]->[1,0,-1] [0 -0.382683 0 0.92388]
102  frame_target_wrt_inertial.i = {1,0,0};
103  frame_target_wrt_inertial.j = {0,1,1};
104  frame_target_wrt_inertial.k = {0,-1,1};
105 
106  // this rotation matrix represents an active rotation (fixed axis, vector rotates)
107  // the vector is represented in the inertial frame
108  // the vector will rotate counterclockwise around the axis
109  cmatrix R_target_from_inertial = dcm.base1_from_base2(frame_inertial, frame_target_wrt_inertial);
110 
111  // cout << R_target_from_inertial << endl;
112 
113  // test vector rotation
114  // a x-vector of the inertial frame (1,0,0) will be rotated to the x-vector of the target frame (1,1,0)
115  cvector test_vector = cv_mmult(R_target_from_inertial, {1,0,0});
116  cout << "test_vector " << test_vector << endl;
117 
118  // quaternion that represents the rotation between the body frame and the inertial frame
119  quaternion q_target_wrt_inertial = q_dcm2quaternion_cm(R_target_from_inertial);
120  cout << "q_target_wrt_inertial: ";
121  cout << q_target_wrt_inertial.d.x << " ";
122  cout << q_target_wrt_inertial.d.y << " ";
123  cout << q_target_wrt_inertial.d.z << " ";
124  cout << q_target_wrt_inertial.w << " " << endl;
125 
126 
127  // load the Quaternion namespace
128  using Cosmos::Math::Quaternion;
129 
130  // The above math can be simplified by just calling this function
131  Quaternion q;
132  // // rotate 90 deg (i -> j) = [0 0 0.707107 0.707107]
133  // q.fromTwoVectors({1,0,0},{0,1,0});
134 
135  // // rotate 180 deg (i -> -i) = [0 0 1 0]
136  // q.fromTwoVectors({1,0,0},{-1,0,0});
137 
138  // // rotate 270 deg (i -> -j) = [0,0,-0.707107, 0.707107]
139  // q.fromTwoVectors({1,0,0},{0,-1,0});
140 
141  // rotate x down a bit [1,0,0]->[1,0,-1] [0 -0.382683 0 0.92388]
142  q.fromTwoVectors({1,0,0},{1,0,1});
143 
144  // rotate x up a bit [1,0,0]->[1,0,-1] [0 0.382683 0 0.92388]
145  q.fromTwoVectors({1,0,0},{1,0,-1});
146 
147  // rotate y up a bit [0,1,0]->[0,1,1] [0 0.382683 0 0.92388]
148  q.fromTwoVectors({0,1,0},{0,1,1});
149 
150 
151  cout << "q: " << q << endl;
152 
153  Quaternion q1(0.017,-0.022,-0.004, 1.000);
154  cout << q1.toEuler()*(180./PI) << endl;
155 
156 
157  // Matrix3f m;
158  // m = AngleAxisf(45*PI/180., Vector3f::UnitZ());
159 
160  // cout << m << endl;
161 
162  // Quaterniond q;
163  // Vector3d a,b;
164  // a << 1,0,0;
165  // b << 1,1,0;
166  // //Quaterniond q1 = q.setFromTwoVectors(a,b);
167 
168  // Quaterniond q1 = q.FromTwoVectors(a,b);
169  // cout << q1.x() << "," << q1.y() << "," << q1.z() << "," << q1.w() << endl;
170 
171  // q.setFromTwoVectors(a,b);
172  // cout << q.x() << "," << q.y() << "," << q.z() << "," << q.w() << endl;
173 
174  //
175  //q_rotation_45deg_around_z;
176 
177  // I want: the quaternion that represents the target frame wrt the inertial frame
178  // this is equivalent of a quaternion that represents the rotation of 45 deg around z axis of the inertial frame
179 
180  // using COSMOS::Math::Vector;
181  // Vector a, b;
182  // a = {123,2,3};
183 
184  // cout << a.x << endl;
185  // cout << a.y << endl;
186  // cout << a.z << endl;
187 
188  // for (int i= 0; i<3; i++){
189  // cout << a.at(i) << endl;
190  // }
191 
192 }
double y
Y value.
Definition: vector.h:114
Orthonormal basis.
Definition: rotation.h:49
cvector k
Definition: rotation.h:54
cvector cv_unitx()
Unit x vector.
Definition: vector.cpp:422
3x3 element cartesian matrix
Definition: matrix.h:96
quaternion q_dcm2quaternion_cm(cmatrix dcm)
Direction Cosine Matrix to Quaternion.
Definition: rotation.cpp:100
cvector d
Orientation.
Definition: vector.h:405
Quaternion, scalar last, using x, y, z.
Definition: vector.h:402
double x
X value.
Definition: vector.h:112
const double PI
PI.
Definition: math/constants.h:12
Definition: rotation.h:60
cvector i
Definition: rotation.h:52
double w
Rotation.
Definition: vector.h:407
cvector cv_unitz()
Unit z vector.
Definition: vector.cpp:442
cvector cv_unity()
Unit y vector.
Definition: vector.cpp:432
double z
Z value.
Definition: vector.h:116
cmatrix base1_from_base2(basisOrthonormal base1, basisOrthonormal base2)
Definition: rotation.cpp:310
cvector j
Definition: rotation.h:53
cvector cv_mmult(cmatrix m, cvector v)
Multiply cartesian vector by cartesian matrix.
Definition: matrix.cpp:80
3 element cartesian vector
Definition: vector.h:107
void torque_mtr ( )
46 {
47  // define torque desired
48  Vector torqueDesired = {0,0,2};
49 
50  // define magnetic Field
51  Vector magField = {0,2,1};
52 
53  // compute moment for torque desired
54  Vector momentDesired = 1./(magField.norm()*magField.norm()) * magField.cross(torqueDesired) ;
55 
56  Vector torqueReal = momentDesired.cross(magField);
57 
58  cout << "torqueDesired = " << torqueDesired << endl;
59  cout << "momentDesired = " << momentDesired << endl;
60  cout << "torqueReal = " << torqueReal << endl;
61 }
double norm()
Norm.
Definition: vector.cpp:1735
Vector cross(Vector b)
Cross product.
Definition: vector.cpp:1667
Vector Class.
Definition: vector.h:672
int main ( )
25 {
26  // use this program to test your code
27  cout << "COSMOS Playground" << endl;
28 
30 
31  while (1) {
32  cout << et.lap() << endl;
33  COSMOS_SLEEP(1.);
34  }
35 
36  // compute_quaternion_from_vectors();
37 
38  // torque_mtr();
39 
40  return 0;
41 
42 }
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
double lap()
Lap Time.
Definition: elapsedtime.cpp:145
Definition: elapsedtime.h:62
void test ( )
195  {
196 
197  // cout << 100 << '\n';
198  // cout.width(10);
199  // cout << 100 << '\n';
200  // //cout.fill('x');
201  // cout.width(15);
202  // cout << left << 100 << '\n';
203 
204  // cout << "[PD] ";
205 
206 
207  // quaternion q;
208  // rvector v;
209  // v = rv_one();
210 
211  // q = q_eye();
212  // cout << "q:" << q << endl;
213  // q = {{-1.,0,0},1};
214  // cout << "q:" << q << endl;
215 
216 
217  // v = {1.23,1,3};
218  // cout << "v:" << v << endl;
219  // v = {-1.23,-10,3};
220  // cout << "v:" << v << endl;
221 
222  // quaternion q_error, q_error_last, q_derror;
223  // double dt = 1.58805e-005;
224 
225  // q_error = {0.0129237,-0.0372017,-0.648016,0.760608};
226  // q_error_last = {0.00867779,-0.030221,-0.654894,0.755066};
227  // q_derror = q_smult( 1.0/dt , q_sub(q_error, q_error_last));
228 
229  // //cout << q_derror << endl;
230 
231  // //
232  // DCM dcm;
233 
234  // // define body frame base
235  // basisOrthonormal frame_body;
236  // frame_body.i = cv_unitx();
237  // frame_body.j = cv_unity();
238  // frame_body.k = cv_unitz();
239 
240  // // define IMU frame
241  // // IMU+X is aligned with BODY+Y
242  // // IMU+Y is aligned with BODY+X
243  // // IMU+Z is aligned with BODY-Z
244  // basisOrthonormal frame_imu;
245  // frame_imu.i = {0,1,0};
246  // frame_imu.j = {1,0,0};
247  // frame_imu.k = {0,0,-1};
248 
249  // // get the DCM to convert IMU vector into body vector
250  // cmatrix R_body_from_imu = dcm.base1_from_base2(frame_body,frame_imu);
251 
252  // quaternion test_q = q_dcm2quaternion_cm(R_body_from_imu);
253 
254  // //cout << R_body_from_imu << endl;
255  // cout << test_q << endl;
256 
257  // //rvector sourcea = {frame_imu.i.x, frame_imu.i.y, frame_imu.i};
258  // uvector sourcea, sourceb, sourcec;
259  // sourcea.c = frame_imu.i;
260  // sourceb.c = frame_imu.j;
261  // sourcec.c = frame_imu.k;
262 
263  // uvector targeta, targetb;
264  // targeta.c = frame_body.i;
265  // targetb.c = frame_body.j;
266 
267  // quaternion tq = q_transform_for(sourcea.r, sourceb.r, targeta.r, targetb.r);
268  // cmatrix R = cm_quaternion2dcm(tq);
269 
270  // cout << tq << endl;
271 
272  // // test
273 
274  // uvector test_imu_x, test_imu_y, test_imu_z;
275  // test_imu_x.c = cv_mmult(R_body_from_imu, frame_imu.i);
276  // test_imu_y.c = cv_mmult(R_body_from_imu, frame_imu.j);
277  // test_imu_z.c = cv_mmult(R_body_from_imu, frame_imu.k);
278  // cout << test_imu_x.c << endl;
279  // cout << test_imu_y.c << endl;
280  // cout << test_imu_z.c << endl;
281 
282  // test_imu_x.r = transform_q(tq, sourcea.r);
283  // test_imu_y.r = transform_q(tq, sourceb.r);
284  // test_imu_z.r = transform_q(tq, sourcec.r);
285  // cout << test_imu_x.r << endl;
286  // cout << test_imu_y.r << endl;
287  // cout << test_imu_z.r << endl;
288 
289  // // ------------------------------------------
290  // // testing * operator in rvector
291  // rvector v, b;
292  // v = {1,2,3};
293 
294  // b = v * 3;
295 
296  // cout << "b=" << b << endl;
297 
298  //-119.568,-195.145,-138.744,-1397.701
299  //70.4439,-80.4836,222.288,183.266
300  //267.364,-439.573,433.141,348.974
301 
302  // ------------------------------------------
303  // define ST frame wrt BODY frame
304  // TODO: must check with Lance
305  // ST+X (direction of connector) is aligned with BODY-X (opposite of + direction)
306  // ST+Y is triad between X and Z
307  // ST+Z (direction of outward boresight) is aligned with BODY-Z (opposite of + direction)
308 
309  // DCM dcm;
310 
311  // // define body frame base
312  // basisOrthonormal frame_body_wrt_body;
313  // frame_body_wrt_body.i = cv_unitx();
314  // frame_body_wrt_body.j = cv_unity();
315  // frame_body_wrt_body.k = cv_unitz();
316 
317  // // define ST frame base wrt to the body frame
318  // basisOrthonormal frame_st_wrt_body;
319  // frame_st_wrt_body.i = {-1,0,0};
320  // frame_st_wrt_body.j = {0,1,0};
321  // frame_st_wrt_body.k = {0,0,-1};
322 
323  // // get the DCM to represent a vector in the ST frame on the body frame
324  // // This is a passive rotation
325  // // Ex: The vector (1,0,0) in ST frame is (-1,0,0) in body frame
326  // cmatrix R_body_from_st_frame = dcm.base1_from_base2(frame_body_wrt_body, frame_st_wrt_body);
327 
328  // //cout << R_body_from_st_frame << endl;
329  // // test vector rotation
330  // cvector test_vector = cv_mmult(R_body_from_st_frame, {1,0,0});
331  // //cout << "test_vector1" << test_vector << endl;
332 
333  // // quaternion that represents the rotation between the st frame and the body frame
334  // quaternion q_rotation_body_from_st_frame = q_dcm2quaternion_cm(R_body_from_st_frame);
335  // cout << "q_rotation_body_from_st_frame:" << q_rotation_body_from_st_frame << endl;
336 
337  // quaternion q_transform_body_from_st_frame = q_transform_for(rv_unitx(), rv_unitz(), {-1,0,0}, {0,0,-1});
338  // cout << "q_transform_body_from_st_frame:" << q_transform_body_from_st_frame << endl;
339 
340  // // quaternion given by star tracker (in st frame)
341 
342  // // co-aligned with inertial frame
343  // quaternion q_st = {{0,0,0},1};
344 
345  // // st frame rotated +45 deg around z
346  // q_st = q_change_around_z( DEG2RAD(45) ); // this is actually a rotation of the object!!!
347  // q_st = q_change_around_cv({1,2,3},DEG2RAD(45));
348 
349  // cout << "q_st:::::" << q_st << endl;
350 
351  // cmatrix R_inertial_from_st = cm_quaternion2dcm(q_st);
352  // //test_vector = cv_mmult(R_inertial_from_st, {1,0,0});
353  // //cout << "test_vector:" << test_vector << endl;
354 
355  // cout << "q_st:" << q_st << endl;
356 
357  // quaternion q_inertial_from_body = q_mult(q_st, q_conjugate(q_rotation_body_from_st_frame));
358 
359  // cout << "q_inertial_from_body (1):" << q_inertial_from_body << endl;
360 
361  // //cout << q_body_from_st_frame << endl;
362  // //cout << q_body_from_st_data << endl;
363 
364  // // now test the conversion of a vector in the ST frame to body frame
365  // // testing
366  // cmatrix R_inertial_from_body = cm_quaternion2dcm(q_inertial_from_body);
367  // // testing with conjugate because cm_quaternion2dcm is not proper???
368  // R_inertial_from_body = cm_quaternion2dcm(q_conjugate(q_inertial_from_body));
369  // //cout << R_inertial_from_body << endl;
370  // test_vector = cv_mmult(R_inertial_from_body, {1,0,0});
371  // cout << "test_vector:" << test_vector << endl;
372 
373  // // confirm
374  // R_inertial_from_body = cm_mmult(R_inertial_from_st, cm_transpose(R_body_from_st_frame));
375 
376  // //cout << R_inertial_from_body << endl;
377 
378  // test_vector = cv_mmult(R_inertial_from_body, {1,0,0});
379 
380  // //cout << test_vector << endl;
381 
382  // // convert the omega given in the st frame to the body frame
383  // rvector omega_st_wrt_eci_in_st_frame = {-1.4,-1.34,-4};
384  // rvector omega_st_wrt_eci_in_body_frame = rv_mmult(rm_from_cm(R_body_from_st_frame), omega_st_wrt_eci_in_st_frame);
385 
386  // //cout << omega_st_wrt_eci_in_body_frame << endl;
387 
388  // // calculated from
389  // // calc_transform 1 0 0 0 0 1 -1 0 0 0 0 -1
390  // q_rotation_body_from_st_frame = {{0,1,0},0};
391  // omega_st_wrt_eci_in_body_frame = transform_q(q_rotation_body_from_st_frame, omega_st_wrt_eci_in_st_frame);
392 
393  // //cout << omega_st_wrt_eci_in_body_frame << endl;
394 
395  // /// testing quaterion conversion
396  // q_inertial_from_body = q_conjugate(q_mult(q_rotation_body_from_st_frame,q_conjugate(q_st)));
397 
398  // cout << "q_inertial_from_body (2):" << q_inertial_from_body << endl;
399 
400  // // testing the vectors now
401  // R_inertial_from_body = cm_quaternion2dcm(q_conjugate(q_inertial_from_body));
402  // test_vector = cv_mmult(R_inertial_from_body, {1,0,0});
403  // cout << "test_vector:" << test_vector << endl;
404 }