Preston Ernst
/
mirror_actuator_VT
-data logging revision
Mirror_Kinematic.cpp@2:92c25cb669f4, 2021-08-24 (annotated)
- Committer:
- ernstpre
- Date:
- Tue Aug 24 08:51:13 2021 +0000
- Revision:
- 2:92c25cb669f4
- Parent:
- 0:d2e117716219
Publish Commit 24/8/21
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altb2 | 0:d2e117716219 | 1 | #include "Mirror_Kinematic.h" |
altb2 | 0:d2e117716219 | 2 | // THis class describes the mirror kinematic. |
altb2 | 0:d2e117716219 | 3 | Mirror_Kinematic::Mirror_Kinematic(void) |
altb2 | 0:d2e117716219 | 4 | { |
altb2 | 0:d2e117716219 | 5 | screen_h = 102; |
altb2 | 0:d2e117716219 | 6 | screen_d = 4; |
altb2 | 0:d2e117716219 | 7 | dist_L = 16; |
altb2 | 0:d2e117716219 | 8 | inc_offset[0] = inc_offset[1] = 0; |
altb2 | 0:d2e117716219 | 9 | inc_additional_offset[0] = inc_additional_offset[1] = 0; |
altb2 | 0:d2e117716219 | 10 | mot_inc_to_rad = (3.141592653589793 * 2.0) / 4000.0; |
altb2 | 0:d2e117716219 | 11 | mot_rad_to_inc = 4000.0/(3.141592653589793 * 2.0); |
altb2 | 0:d2e117716219 | 12 | n = 1.585; // index of refraction, Brechungsindex |
altb2 | 0:d2e117716219 | 13 | old_phi[0]=0.0; |
altb2 | 0:d2e117716219 | 14 | old_phi[1]=0.0; |
altb2 | 0:d2e117716219 | 15 | trafo_is_on = false; |
altb2 | 0:d2e117716219 | 16 | external_control = false; |
altb2 | 0:d2e117716219 | 17 | controller_is_on = true; |
altb2 | 0:d2e117716219 | 18 | } |
altb2 | 0:d2e117716219 | 19 | |
altb2 | 0:d2e117716219 | 20 | void Mirror_Kinematic::set_offsets(int16_t o1,int16_t o2) |
altb2 | 0:d2e117716219 | 21 | { |
altb2 | 0:d2e117716219 | 22 | inc_offset[0] = o1; |
altb2 | 0:d2e117716219 | 23 | inc_offset[1] = o2; |
altb2 | 0:d2e117716219 | 24 | } |
altb2 | 0:d2e117716219 | 25 | void Mirror_Kinematic::set_additional_offsets(int16_t o1,int16_t o2) |
altb2 | 0:d2e117716219 | 26 | { |
altb2 | 0:d2e117716219 | 27 | inc_additional_offset[0] = o1; |
altb2 | 0:d2e117716219 | 28 | inc_additional_offset[1] = o2; |
altb2 | 0:d2e117716219 | 29 | } |
altb2 | 0:d2e117716219 | 30 | void Mirror_Kinematic::add_additional_offsets(int16_t o1,int16_t o2) |
altb2 | 0:d2e117716219 | 31 | { |
altb2 | 0:d2e117716219 | 32 | inc_additional_offset[0] += o1; |
altb2 | 0:d2e117716219 | 33 | inc_additional_offset[1] += o2; |
altb2 | 0:d2e117716219 | 34 | } |
altb2 | 0:d2e117716219 | 35 | int16_t Mirror_Kinematic::get_additional_offsets(uint8_t axis) |
altb2 | 0:d2e117716219 | 36 | { |
altb2 | 0:d2e117716219 | 37 | if(axis>1) |
altb2 | 0:d2e117716219 | 38 | return 0; |
altb2 | 0:d2e117716219 | 39 | else |
altb2 | 0:d2e117716219 | 40 | return inc_additional_offset[axis]; |
altb2 | 0:d2e117716219 | 41 | } |
altb2 | 0:d2e117716219 | 42 | // this is the transformation, transforming phi to xy values |
altb2 | 0:d2e117716219 | 43 | bool Mirror_Kinematic::P2X(float *P, float *X) |
altb2 | 0:d2e117716219 | 44 | { |
altb2 | 0:d2e117716219 | 45 | // calculation time 5.7usec on F446RE |
altb2 | 0:d2e117716219 | 46 | float c1 = cosf(P[0]); |
altb2 | 0:d2e117716219 | 47 | float c2 = cosf(P[1]); |
altb2 | 0:d2e117716219 | 48 | float s1 = sinf(P[0]); |
altb2 | 0:d2e117716219 | 49 | float s2 = -sinf(2.0f*P[1]); // alwayx sin(2*phi2) is needed |
altb2 | 0:d2e117716219 | 50 | // and: reverse the phi2 values! |
altb2 | 0:d2e117716219 | 51 | float n1x=c1; // normal vector of 2nd laser beam (from mirror 1 -> mirror 2) |
altb2 | 0:d2e117716219 | 52 | float n1y=c1*s1; |
altb2 | 0:d2e117716219 | 53 | float n1z=s1*s1; |
altb2 | 0:d2e117716219 | 54 | float sq2=sqrt(.5); |
altb2 | 0:d2e117716219 | 55 | // i.e.: cos(2*x) = 2*cos(x)^2-1 |
altb2 | 0:d2e117716219 | 56 | //float n2x = n1z*cosf(2.0f*P[1]) + n1x*sinf(2.0f*P[1]); |
altb2 | 0:d2e117716219 | 57 | float n2x = n1z*(2.0f*c2*c2-1.0f) + n1x*s2; // normal vector of 3rd laser beam (mirror 2 -> screen) |
altb2 | 0:d2e117716219 | 58 | float n2y = n1y; |
altb2 | 0:d2e117716219 | 59 | float a1 = sq2*c2 - sq2*s1; |
altb2 | 0:d2e117716219 | 60 | float a2 = sq2*c2 + sq2*s1; |
altb2 | 0:d2e117716219 | 61 | float dum1 = n1x * a1 - n1z*a2; |
altb2 | 0:d2e117716219 | 62 | float n2z = n1x*(2.0f*c2*c2-1.0f) - n1z*s2; |
altb2 | 0:d2e117716219 | 63 | if(dum1*n2z == 0) |
altb2 | 0:d2e117716219 | 64 | return false; |
altb2 | 0:d2e117716219 | 65 | float dad = dist_L * a1/dum1; |
altb2 | 0:d2e117716219 | 66 | float Q2x = dad * n1x - dist_L; |
altb2 | 0:d2e117716219 | 67 | float Q2y = dad * n1y; |
altb2 | 0:d2e117716219 | 68 | float Q2z = dad * n1z; |
altb2 | 0:d2e117716219 | 69 | float x = atanf(n2x/n2z)/n; |
altb2 | 0:d2e117716219 | 70 | float y = atanf(n2y/n2z)/n; |
altb2 | 0:d2e117716219 | 71 | float dx = screen_d * x/sqrt(1-x*x); |
altb2 | 0:d2e117716219 | 72 | float dy = screen_d * y/sqrt(1-y*y); |
altb2 | 0:d2e117716219 | 73 | X[0] = Q2x + (n2x*(screen_h - Q2z))/n2z - dx; |
altb2 | 0:d2e117716219 | 74 | X[1] = Q2y + (n2y*(screen_h - Q2z))/n2z - dy; |
altb2 | 0:d2e117716219 | 75 | |
altb2 | 0:d2e117716219 | 76 | return true; |
altb2 | 0:d2e117716219 | 77 | } |
altb2 | 0:d2e117716219 | 78 | // this is the trafo from xy -> phi1,phi2, not possible in an analytical form, use |
altb2 | 0:d2e117716219 | 79 | // GN-iteration with a constant Jacobian |
altb2 | 0:d2e117716219 | 80 | bool Mirror_Kinematic::X2P(float *X, float *P) |
altb2 | 0:d2e117716219 | 81 | { |
altb2 | 0:d2e117716219 | 82 | float J12 = 0.0090517133f; |
altb2 | 0:d2e117716219 | 83 | float J21 = -0.0052923231f; // on motor angle co-ordinates, we have a negative sensitivity |
altb2 | 0:d2e117716219 | 84 | float Xn[2]; |
altb2 | 0:d2e117716219 | 85 | float dx = 1e4; |
altb2 | 0:d2e117716219 | 86 | float dy = 1e4; |
altb2 | 0:d2e117716219 | 87 | P[0] = old_phi[0]; |
altb2 | 0:d2e117716219 | 88 | P[1] = old_phi[1]; |
altb2 | 0:d2e117716219 | 89 | uint8_t k = 0; |
altb2 | 0:d2e117716219 | 90 | do |
altb2 | 0:d2e117716219 | 91 | { |
altb2 | 0:d2e117716219 | 92 | if( !P2X(P,Xn)) |
altb2 | 0:d2e117716219 | 93 | return false; |
altb2 | 0:d2e117716219 | 94 | dx = Xn[0]-X[0]; |
altb2 | 0:d2e117716219 | 95 | dy = Xn[1]-X[1]; |
altb2 | 0:d2e117716219 | 96 | P[0] -= J12 * dy; |
altb2 | 0:d2e117716219 | 97 | P[1] -= J21 * dx; |
altb2 | 0:d2e117716219 | 98 | } |
altb2 | 0:d2e117716219 | 99 | while((dx*dx+dy*dy) > 1e-3 && ++k<20); |
altb2 | 0:d2e117716219 | 100 | data.num_it = k; |
altb2 | 0:d2e117716219 | 101 | old_phi[0] = P[0]; |
altb2 | 0:d2e117716219 | 102 | old_phi[1] = P[1]; |
altb2 | 0:d2e117716219 | 103 | return true; |
altb2 | 0:d2e117716219 | 104 | } |