/Lucretia/src/Floodland/coreApps/FlSetMoverLimits.m

http://atf2flightsim.googlecode.com/ · Objective C · 158 lines · 146 code · 12 blank · 0 comment · 26 complexity · 552e5ae712b191168e9ef4a49e4f669f MD5 · raw file

  1. function [stat xlim ylim rotlim] = FlSetMoverLimits(igir)
  2. % FLSETMOVERLIMITS
  3. % Set dynamic high/low limits for given Mover (provide GIRDER index igir)
  4. global GIRDER FL BEAMLINE
  5. persistent camsettings
  6. stat{1}=1;
  7. % Mover CAM constants
  8. if isempty(camsettings)
  9. camsettings.quad.a = 0.145250;
  10. camsettings.quad.c = 0.123952;
  11. camsettings.quad.R = 0.031;
  12. camsettings.quad.L = 0.0015875;
  13. camsettings.quad.S1 = 0.034925;
  14. camsettings.quad.S2 = 0.2905;
  15. camsettings.quad.b = camsettings.quad.c + ...
  16. camsettings.quad.S1 - (sqrt(2)*camsettings.quad.R);
  17. camsettings.sext.a = 0.145250;
  18. camsettings.sext.c = 0.123952;
  19. camsettings.sext.R = 0.031;
  20. camsettings.sext.L = 0.0015875;
  21. camsettings.sext.S1 = 0.034925;
  22. camsettings.sext.S2 = 0.2905;
  23. camsettings.sext.b = camsettings.sext.c + ...
  24. camsettings.sext.S1 - (sqrt(2)*camsettings.sext.R);
  25. end
  26. % Argument check
  27. igir=round(igir);
  28. if igir<1 || igir>length(GIRDER) || ~isfield(GIRDER{igir},'Mover')
  29. stat{1}=-1;
  30. stat{2}='No MOVER on this GIRDER or invalid index';
  31. return
  32. end
  33. % Get Mover location
  34. xmov = GIRDER{igir}.MoverSetPt(1);
  35. ymov = GIRDER{igir}.MoverSetPt(2);
  36. rotmov = GIRDER{igir}.MoverSetPt(3);
  37. % betaminus = pi/4 - rotmov;
  38. % betaplus = pi/4 + rotmov;
  39. % Get magnet type on Mover
  40. camset=[];
  41. for ele=GIRDER{igir}.Element(1):GIRDER{igir}.Element(end)
  42. if strcmp(BEAMLINE{ele}.Class,'QUAD')
  43. camset=camsettings.quad;
  44. break
  45. elseif strcmp(BEAMLINE{ele}.Class,'SEXT')
  46. camset=camsettings.sext;
  47. break
  48. end
  49. end
  50. if isempty(camset)
  51. stat{1}=-1;
  52. stat{2}='Magnet type on GIRDER not QUAD or SEXT';
  53. return
  54. end
  55. % x_1 = xmov + (camset.a*cos(rotmov)) + (camset.b*sin(rotmov)) - camset.a;
  56. % y_1 = ymov - (camset.b*cos(rotmov)) + (camset.a*sin(rotmov)) + camset.c;
  57. % Compute x limits
  58. % x1max(1) = ((camset.L + y_1*cos(rotmov) - camset.c + camset.b) / sin(rotmov)) - camset.S2 ;
  59. % x1max(2) = ((camset.L - y_1*cos(betaminus) + camset.R) / sin(betaminus)) - camset.S1 ;
  60. % x1max(3) = ((camset.L + y_1*cos(betaplus) - camset.R) / sin(betaplus)) - camset.S1 ;
  61. % xmax = min(abs(x1max - camset.a*cos(rotmov) - camset.b*sin(rotmov) + camset.a));
  62. xlim=[-2.5e-3 2.5e-3];
  63. for xtry=0:-1e-5:-2.5e-3
  64. camangles=camcalc(xtry,ymov,rotmov,camset);
  65. if ~isreal(camangles) || any(abs(camangles)>pi/2)
  66. xlim(1)=xtry;
  67. break
  68. end
  69. end
  70. for xtry=0:1e-5:2.5e-3
  71. camangles=camcalc(xtry,ymov,rotmov,camset);
  72. if ~isreal(camangles) || any(abs(camangles)>pi/2)
  73. xlim(2)=xtry;
  74. break
  75. end
  76. end
  77. % Compute y limits
  78. % y1max(1) = ((x_1+camset.S2)*sin(rotmov) - camset.L + camset.c - camset.b) / cos(rotmov) ;
  79. % y1max(2) = (camset.L - (x_1+camset.S1)*sin(betaminus) + camset.R ) / cos(betaminus) ;
  80. % y1max(3) = ((x_1-camset.S1)*sin(betaplus) + camset.R - camset.L) / cos(betaplus) ;
  81. % ymax = min(abs(y1max + camset.b*cos(rotmov) - camset.a*sin(rotmov) - camset.c)) ;
  82. ylim=[-2e-3 2e-3];
  83. for ytry=0:-1e-5:-2e-3
  84. camangles=camcalc(xmov,ytry,rotmov,camset);
  85. if ~isreal(camangles) || any(abs(camangles)>pi/2)
  86. ylim(1)=ytry;
  87. break
  88. end
  89. end
  90. for ytry=0:1e-5:2e-3
  91. camangles=camcalc(xmov,ytry,rotmov,camset);
  92. if ~isreal(camangles) || any(abs(camangles)>pi/2)
  93. ylim(2)=ytry;
  94. break
  95. end
  96. end
  97. % Compute roll limits
  98. % rotmax(1) = abs(fminbnd(@(x) minth1(x,camset,xmov,ymov),-0.03,0.03,optimset('TolX',1e-6,'TolFun',1e-12,'Display','off')));
  99. % rotmax(2) = abs(fminbnd(@(x) minth2(x,camset,xmov,ymov),-0.03,0.03,optimset('TolX',1e-6,'TolFun',1e-12,'Display','off')));
  100. % rotmax(3) = abs(fminbnd(@(x) minth3(x,camset,xmov,ymov),-0.03,0.03,optimset('TolX',1e-6,'TolFun',1e-12,'Display','off')));
  101. % rotmax = min(rotmax) ;
  102. rotlim=[-10e-3 10e-3];
  103. for ttry=0:-10e-6:-10e-3
  104. camangles=camcalc(xmov,ymov,ttry,camset);
  105. if ~isreal(camangles) || any(abs(camangles)>pi/2)
  106. rotlim(1)=ttry;
  107. break
  108. end
  109. end
  110. for ttry=0:10e-6:10e-3
  111. camangles=camcalc(xmov,ymov,ttry,camset);
  112. if ~isreal(camangles) || any(abs(camangles)>pi/2)
  113. rotlim(2)=ttry;
  114. break
  115. end
  116. end
  117. % Set Limits
  118. FL.HwInfo.GIRDER(igir).low=[xlim(1) ylim(1) rotlim(1)];
  119. FL.HwInfo.GIRDER(igir).high=[xlim(2) ylim(2) rotlim(2)];
  120. % function chi2 = minth1(th,c,x,y)
  121. % x1 = x + (c.a*cos(th)) + (c.b*sin(th)) - c.a;
  122. % y1 = y - (c.b*cos(th)) + (c.a*sin(th)) + c.c;
  123. % chi2=(1/c.L) * ( (x1 + c.S2)*sin(th) - y1*cos(th) + c.c - c.b ) - 1 ;
  124. % chi2 = chi2^2;
  125. %
  126. % function chi2 = minth2(th,c,x,y)
  127. % x1 = x + (c.a*cos(th)) + (c.b*sin(th)) - c.a;
  128. % y1 = y - (c.b*cos(th)) + (c.a*sin(th)) + c.c;
  129. % betaminus = pi/4 - th;
  130. % chi2=(1/c.L) * ( (x1+c.S1)*sin(betaminus) + y1*cos(betaminus) - c.R ) - 1 ;
  131. % chi2=chi2^2;
  132. %
  133. % function chi2 = minth3(th,c,x,y)
  134. % x1 = x + (c.a*cos(th)) + (c.b*sin(th)) - c.a;
  135. % y1 = y - (c.b*cos(th)) + (c.a*sin(th)) + c.c;
  136. % betaplus = pi/4 + th;
  137. % chi2 = (1/c.L) * ( (x1 - c.S1)*sin(betaplus) - y1*cos(betaplus) + c.R ) - 1 ;
  138. % chi2=chi2^2;
  139. function camangles=camcalc(xmov,ymov,rotmov,camset)
  140. betaminus = pi/4 - rotmov;
  141. betaplus = pi/4 + rotmov;
  142. x_1 = xmov + (camset.a*cos(rotmov)) + (camset.b*sin(rotmov)) - camset.a;
  143. y_1 = ymov - (camset.b*cos(rotmov)) + (camset.a*sin(rotmov)) + camset.c;
  144. camangles(1) = rotmov - asin((1/camset.L) * ((x_1+camset.S2)*sin(rotmov) - y_1*cos(rotmov) + (camset.c - camset.b)));
  145. camangles(2) = rotmov - asin((1/camset.L) * ((x_1+camset.S1)*sin(betaminus) + y_1*cos(betaminus) - camset.R));
  146. camangles(3) = rotmov - asin((1/camset.L) * ((x_1-camset.S1)*sin(betaplus) - y_1*cos(betaplus) + camset.R));