Advertisement
Guest User

Telescience v5 source

a guest
Nov 6th, 2014
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Telescience2
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. calibrations = new List<CalibrationData>();
  19. offsets = new List<Offsets>();
  20. resetOffsets();
  21. updateCalibrationDisplay();
  22. }
  23.  
  24. private List<CalibrationData> calibrations;
  25. private List<Offsets> offsets;
  26.  
  27. //Constants
  28. private const int POW_MIN_OFF = -4;
  29. private const int POW_MAX_OFF = 0;
  30. private const int HA_MIN_OFF = -10;
  31. private const int HA_MAX_OFF = 10;
  32. private const int VA_MIN_OFF = 0;
  33. private const int VA_MAX_OFF = 0;
  34.  
  35. private const double G = 10.0;
  36.  
  37. private class CalibrationData
  38. {
  39. public int power;
  40. public double horAngle;
  41. public double verAngle;
  42. public int destX;
  43. public int destY;
  44.  
  45. public CalibrationData(int power, double horAngle, double verAngle, int destX, int destY)
  46. {
  47. this.power = power;
  48. this.horAngle = horAngle;
  49. this.verAngle = verAngle;
  50. this.destX = destX;
  51. this.destY = destY;
  52. }
  53. }
  54.  
  55. private class Offsets
  56. {
  57. public int HAOff;
  58. public int VAOff;
  59. public int PowOff;
  60.  
  61. public Offsets(int HAOff, int VAOff, int PowOff)
  62. {
  63. this.HAOff = HAOff;
  64. this.VAOff = VAOff;
  65. this.PowOff = PowOff;
  66. }
  67. }
  68.  
  69. private double mod(double a, double b)
  70. {
  71. return a - b * Math.Floor(a / b);
  72. }
  73.  
  74. private double toScientificDegree(double angle)
  75. {
  76. return mod(360.0 - angle + 90.0, 360.0);
  77. }
  78.  
  79. private double fromScientificDegree(double angle)
  80. {
  81. return mod(360.0 - angle + 90.0, 360.0);
  82. }
  83.  
  84. private double toRadian(double angle)
  85. {
  86. return angle * (Math.PI / 180.0);
  87. }
  88.  
  89. private double fromRadian(double angle)
  90. {
  91. return angle * (180.0 / Math.PI);
  92. }
  93.  
  94. private void resetOffsets()
  95. {
  96. offsets.Clear();
  97. for (int ha = HA_MIN_OFF; ha <= HA_MAX_OFF; ++ha)
  98. {
  99. for (int va = VA_MIN_OFF; va <= VA_MAX_OFF; ++va)
  100. {
  101. for (int pow = POW_MIN_OFF; pow <= POW_MAX_OFF; ++pow)
  102. {
  103. offsets.Add(new Offsets(ha, va, pow));
  104. }
  105. }
  106. }
  107. }
  108.  
  109. private void updateCalibrationDisplay()
  110. {
  111. possibleOffsetsDisplay.Text = offsets.Count.ToString();
  112. if (offsets.Count > 1)
  113. cVerdictDisplay.Text = "Not Accurate Yet";
  114. if (offsets.Count == 1)
  115. {
  116. cVerdictDisplay.Text = "Accurate!";
  117. HAOffsetIn.Text = offsets[0].HAOff.ToString();
  118. POWOffsetIn.Text = offsets[0].PowOff.ToString();
  119. }
  120. if (offsets.Count < 1)
  121. cVerdictDisplay.Text = "Impossible, Reset";
  122. }
  123.  
  124. //Checks the destination for the given settings
  125. //CD has the entered values
  126. //Returns True if it matches, False if not
  127. private bool checkDestination(CalibrationData cd, int sourceX, int sourceY, int HAOff, int VAOff, int PowOff)
  128. {
  129. //Get the actual HAngle
  130. double actHDegree = mod(cd.horAngle + (double)HAOff, 360.0);
  131.  
  132. //Convert it to scientific radian
  133. double actHAngle = toRadian(toScientificDegree(actHDegree));
  134.  
  135. //Get the actual VAngle
  136. double actVDegree = cd.verAngle + (double)VAOff;
  137. if (actVDegree < 1.0) actVDegree = 1.0;
  138. if (actVDegree > 90.0) actVDegree = 90.0;
  139. double actVAngle = toRadian(actVDegree);
  140.  
  141. //Get the actual velocity
  142. double actVelocity = (double)cd.power + (double)PowOff;
  143.  
  144. //Compute the range
  145. double range = actVelocity * actVelocity * Math.Sin(2.0 * actVAngle) / G;
  146.  
  147. //Compute DX and DY
  148. double deltaX = range * Math.Cos(actHAngle);
  149. double deltaY = range * Math.Sin(actHAngle);
  150.  
  151. //Compute destX and destY
  152. int destX = (int)Math.Round(sourceX + deltaX, 0);
  153. int destY = (int)Math.Round(sourceY + deltaY, 0);
  154.  
  155. //Final check
  156. return destX == cd.destX && destY == cd.destY;
  157. }
  158.  
  159. private void cAddButton_Click(object sender, EventArgs e)
  160. {
  161. //Get data
  162. int power;
  163. double horAngle;
  164. double verAngle;
  165. int destX;
  166. int destY;
  167. int sourceX;
  168. int sourceY;
  169. try
  170. {
  171. power = int.Parse(cPowerInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  172. horAngle = double.Parse(cHorAngleInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  173. verAngle = double.Parse(cVerAngleInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  174. destX = int.Parse(cResXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  175. destY = int.Parse(cResYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  176. sourceX = int.Parse(telepadXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  177. sourceY = int.Parse(telepadYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  178. }
  179. catch (System.FormatException exception)
  180. {
  181. statusDisplay.Text = "wrong entry format";
  182. return;
  183. }
  184.  
  185. //Add new thing
  186. CalibrationData cd = new CalibrationData(power, horAngle, verAngle, destX, destY);
  187. calibrations.Add(cd);
  188.  
  189. //Cull the offsets
  190. offsets.RemoveAll(off => !checkDestination(cd, sourceX, sourceY, off.HAOff, off.VAOff, off.PowOff));
  191. offsets.TrimExcess();
  192. updateCalibrationDisplay();
  193. }
  194.  
  195. private void cResetButton_Click(object sender, EventArgs e)
  196. {
  197. resetOffsets();
  198. updateCalibrationDisplay();
  199. }
  200.  
  201. private void computeButton_Click(object sender, EventArgs e)
  202. {
  203. //Check offsets
  204. if (offsets.Count != 1)
  205. {
  206. destVerdict.Text = "not accurate yet";
  207. statusDisplay.Text = "Likely inaccurate!";
  208. }
  209. Offsets off = offsets[0];
  210.  
  211. //Get data
  212. int power;
  213. int destX;
  214. int destY;
  215. int sourceX;
  216. int sourceY;
  217.  
  218. try
  219. {
  220. power = int.Parse(destPowInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  221. destX = int.Parse(destXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  222. destY = int.Parse(destYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  223. sourceX = int.Parse(telepadXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  224. sourceY = int.Parse(telepadYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
  225. }
  226. catch (System.FormatException exception)
  227. {
  228. statusDisplay.Text = "wrong entry format";
  229. destVerdict.Text = "wrong entry format";
  230. return;
  231. }
  232.  
  233. //DeltaX/Y
  234. double deltaX = (double)destX - (double)sourceX;
  235. double deltaY = (double)destY - (double)sourceY;
  236.  
  237. //Compute the distance we need
  238. double dist = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  239.  
  240. //Get the actual power
  241. double actPower = (double)power + (double)off.PowOff;
  242.  
  243. //Get maximum distance
  244. double maxRange = actPower * actPower * Math.Sin(2 * toRadian(45.0)) / G;
  245.  
  246. //Check
  247. if (dist > maxRange)
  248. {
  249. destVerdict.Text = "out of range!";
  250. return;
  251. }
  252.  
  253. //Compute the angle we want
  254. double desHAngle = mod(Math.Atan2(deltaY, deltaX), Math.PI * 2.0);
  255.  
  256. //Turn it into a bearing
  257. double desBearing = fromScientificDegree(fromRadian(desHAngle));
  258.  
  259. //Round it
  260. double actBearing = Math.Round(desBearing, 2);
  261.  
  262. //Modify it with the offset
  263. actBearing -= (double)off.HAOff;
  264.  
  265. //Compute the vertical angle we want
  266. double desVAngle = 0.5 * Math.Asin(G * dist / (actPower * actPower));
  267.  
  268. //Round it as well
  269. double actElevation = Math.Round(fromRadian(desVAngle), 1);
  270.  
  271. //Modify it with the offset
  272. actElevation -= (double)off.VAOff;
  273.  
  274. //Done, print the result
  275. destVerdict.Text = "Bearing: " + actBearing.ToString() + " Elevation: " + actElevation.ToString();
  276. }
  277.  
  278. private void aboutButton_Click(object sender, EventArgs e)
  279. {
  280. statusDisplay.Text = "Braincake made this! Read the readme!";
  281. }
  282.  
  283. private void ApplyOffsetButton_Click(object sender, EventArgs e)
  284. {
  285. //Get data
  286. int haoff;
  287. int powoff;
  288.  
  289. try
  290. {
  291. haoff = int.Parse(HAOffsetIn.Text, System.Globalization.CultureInfo.InvariantCulture);
  292. powoff = int.Parse(POWOffsetIn.Text, System.Globalization.CultureInfo.InvariantCulture);
  293. }
  294. catch (System.FormatException exception)
  295. {
  296. statusDisplay.Text = "wrong entry format";
  297. return;
  298. }
  299.  
  300. //Check
  301. if (haoff < HA_MIN_OFF || haoff > HA_MAX_OFF || powoff < POW_MIN_OFF || powoff > POW_MAX_OFF)
  302. {
  303. statusDisplay.Text = "Offsets out of range!";
  304. return;
  305. }
  306.  
  307. //Apply
  308. offsets.Clear();
  309. Offsets newoff = new Offsets(haoff, VA_MIN_OFF, powoff);
  310. offsets.Add(newoff);
  311.  
  312. //Update status
  313. updateCalibrationDisplay();
  314. cVerdictDisplay.Text = "Offsets applied!";
  315. }
  316. }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement