cl-ballistics.lisp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ;;;; cl-ballistics.lisp
  2. (in-package #:cl-ballistics)
  3. ;;; Constant definitions
  4. (defconstant +gravity+ -32.194)
  5. (defconstant +standard-pressure+ 29.92)
  6. (defconstant +to-radians+ (/ pi 180))
  7. (defconstant +to-degrees+ (/ 180 pi))
  8. (defconstant +standard-altitude+ 0)
  9. (defconstant +standard-temperature+ 59)
  10. (defconstant +standard-humidity+ 1)
  11. ;;; Alist of Drag Functions
  12. (defconstant +drag-functions+
  13. '((:g1 . "Ingalls, flatbase")
  14. (:g2 . "Aberdeen \"J\" Projectile")
  15. (:g3 . "")
  16. (:g4 . "")
  17. (:g5 . "Short, 7.5° boat-tail")
  18. (:g6 . "Flatbase, 6 Calibers long")
  19. (:g7 . "Long 7.5° boat-tail")
  20. (:g8 . "Flatbase, 10 Calibers long")))
  21. ;;; Solution class
  22. (defclass ballistic-solution ()
  23. ())
  24. ;;; Atmospheric Functions
  25. (defun calc-fp (pressure)
  26. (declare (real pressure))
  27. (/ (- pressure +standard-pressure+) +standard-pressure+))
  28. (defun calc-fr (temperature pressure relative-humidity)
  29. (declare (real temperature pressure relative-humidity))
  30. (let ((vpw (- (* 4e-6 (expt temperature 3))
  31. (+ (* 4e-4 (expt temperature 2))
  32. (* 0.0234 temperature))
  33. 0.2517)))
  34. (* 0.995
  35. (/ pressure (* (- pressure 0.3783)
  36. relative-humidity
  37. vpw)))))
  38. (defun calc-ft (temperature altitude)
  39. (declare (real temperature altitude))
  40. (let ((tstd (+ (* -0.0036 altitude) 59)))
  41. (/ (- temperature tstd)
  42. (- 459.6 tstd))))
  43. (defun calc-fa (altitude)
  44. (declare (real altitude))
  45. (/ 1 (+ (* -4e-15 (expt altitude 3))
  46. (* 4e-10 (expt altitude 2))
  47. (* -3e-5 altitude)
  48. 1)))
  49. (defun atmospheric-correction (drag-coefficient &optional (altitude +standard-altitude+) (pressure +standard-pressure+) (temperature +standard-temperature+) (relative-humidity +standard-humidity+))
  50. (declare (real drag-coefficient altitude pressure temperature relative-humidity))
  51. (let* ((altitude-correction (calc-fa altitude))
  52. (temperature-correction (calc-ft temperature altitude))
  53. (humidity-correction (calc-fr temperature pressure relative-humidity))
  54. (pressure-correction (calc-fp pressure))
  55. (correction-factor (* altitude-correction
  56. (- (+ 1 temperature-correction)
  57. pressure-correction)
  58. humidity-correction)))
  59. (* drag-coefficient correction-factor)))
  60. (export 'atmospheric-correction)
  61. ;;; G-function retardation
  62. ;; (defun retard (drag-function drag-coefficient velocity)
  63. ;; (declare (integer drag-function)
  64. ;; (real drag-coefficient velocity))
  65. ;; (let (())
  66. ;; ))
  67. ;;; Angular conversion functions
  68. (defun degrees-to-moa (degrees)
  69. "Converts degrees to Minutes of Angle."
  70. (declare (real degrees))
  71. (* degrees 60))
  72. (defun degrees-to-radians (degrees)
  73. "Converts Degrees to Radians"
  74. (declare (real degrees))
  75. (* degrees +to-radians+))
  76. (defun moa-to-degrees (moa)
  77. "Converts Minutes of Angle to Degrees"
  78. (declare (real moa))
  79. (/ moa 60))
  80. (defun moa-to-radians (moa)
  81. "Converts Minutes of Angle to Radians"
  82. (declare (real moa))
  83. (degrees-to-radians (moa-to-degrees moa)))
  84. (defun radians-to-degrees (radians)
  85. "Converts Radians to Degrees"
  86. (declare (real radians))
  87. (* radians +to-degrees+))
  88. (defun radians-to-moa (radians)
  89. "Converts Radians to Minutes of Angle"
  90. (declare (real radians))
  91. (degrees-to-moa (radians-to-degrees radians)))
  92. (export '(degrees-to-moa
  93. degrees-to-radians
  94. moa-to-degrees
  95. moa-to-radians
  96. radians-to-degrees
  97. radians-to-moa))
  98. ;;; Windage functions
  99. (defun windage (wind-speed initial-velocity range time)
  100. "Calculates the windage correction, in inches, to achive zero on
  101. target at given range."
  102. (declare (real wind-speed initial-velocity range time))
  103. (let ((wind-speed-inches-per-second (* wind-speed 17.60)))
  104. (* wind-speed-inches-per-second (/ (- time range) initial-velocity))))
  105. (defun head-wind (wind-speed wind-angle)
  106. "Calculates the Headwind component in miles per hour"
  107. (declare (real wind-speed wind-angle))
  108. (* (cos (degrees-to-radians wind-angle))
  109. wind-speed))
  110. (defun cross-wind (wind-speed wind-angle)
  111. "Calculates the crosswind components in miles per hour"
  112. (* (sin (degrees-to-radians wind-angle))
  113. wind-speed))
  114. (export '(windage
  115. head-wind
  116. cross-wind))
  117. ;;; Bore angle functions
  118. (defun zero-angle (drag-function ballistic-coefficient initial-velocity sight-height zero-range y-intercept)
  119. "Calculates the angle of the bore, relative to the sighting system in degrees."
  120. (declare (keyword drag-function)
  121. (real ballistic-coefficient
  122. initial-velocity
  123. sight-height
  124. zero-range
  125. y-intercept))
  126. (let ((nit 0)
  127. (dt (/ 1 initial-velocity))
  128. (y (/ sight-height 12))
  129. (x 0)
  130. (da 0)
  131. (v 0) (vx 0) (vy 0)
  132. (vx1 0) (vy1 0)
  133. (dv 0) (dvx 0) (dvy 0)
  134. (gx 0) (gy 0)
  135. (angle 0)
  136. (quit 0))
  137. (declare (real nit
  138. dt
  139. y
  140. x
  141. da
  142. v vx vy
  143. vx1 vy1
  144. dv dvx dvy
  145. gx gy
  146. angle)
  147. (integer quit))
  148. (loop for angle = (+ angle da)
  149. if (= quit 1)
  150. return (radians-to-degrees angle)
  151. else
  152. )))
  153. ;;; Solving
  154. (defun solve-all (drag-function drag-coefficient initial-velocity sight-height shooting-angle zero-angle wind-speed wind-angle)
  155. "Generates a ballistic solutions table."
  156. (declare (keyword drag-function)
  157. (real drag-coefficient
  158. initial-velocity
  159. sight-height
  160. shooting-angle
  161. zero-angle
  162. wind-speed
  163. wind-angle)))
  164. ;;; Retrieving Data
  165. (defun get-range (solution yardage)
  166. "Retrieve range in yards.")
  167. (defun get-path (solution yardage)
  168. "Retrieves projectile pathin inches, relative to the line of sight at yardage.")
  169. (defun get-moa (solution yardage)
  170. "Retrieves the estimated elevation correction for zero at specified range.
  171. Very useful for \"click charts\" and similar.")
  172. (defun get-time (solution yardage)
  173. "Retrieves the time of flight to this range.")
  174. (defun get-windage (solution yardage)
  175. "Retrives the windage correction, in inches, required to achieve
  176. zero at this range.")
  177. (defun get-windage-moa (solution yardage)
  178. "Retrieves windage correction in MOA to achieve zero at this range.")
  179. (defun get-velocity (solution yardage)
  180. "Retrieves velocity of projectile at specified yardage.")
  181. (defun get-velocity-bore (solution yardage)
  182. "Retrives projectile's velocity in the direction of the bore.")
  183. (defun get-velocity-perpendicular (solution yardage)
  184. "Retireves the velocity of the projectile perpendicular to the
  185. direction of the bore.")