'---------------------------------------------------------------------------
' crane jib, hydraulic force            FreeBASIC
'   https://www.physicsforums.com/threads/crane-boom-piston-force.980118/
' The hinge pin between horizontal boom and jib is defined as the origin.
'   Reverse the diagram, so boom is fixed on LHS, with jib starting on the RHS.
'---------------------------------------------------------------------------
'       Jib angle      Rod pin pos'n    Hydraulic ram   Tween    Moment    Ram axial
'       deg   rad       x       y       length  angle     deg    jib N.m   force kN
'       0.0   0.000    0.500   0.000    1.700     0.0      0.0     825.0  -9999.999
'     -10.0  -0.175    0.492  -0.087    1.695    -2.9     -7.1     812.5     13.215
'     -20.0  -0.349    0.470  -0.171    1.679    -5.8    -14.2     775.2      6.341
'     -30.0  -0.524    0.433  -0.250    1.652    -8.7    -21.3     714.5      3.934
'     -40.0  -0.698    0.383  -0.321    1.615   -11.5    -28.5     632.0      2.647
'     -50.0  -0.873    0.321  -0.383    1.569   -14.1    -35.9     530.3      1.810
'     -60.0  -1.047    0.250  -0.433    1.513   -16.6    -43.4     412.5      1.201
'     -70.0  -1.222    0.171  -0.470    1.449   -18.9    -51.1     282.2      0.725
'     -80.0  -1.396    0.087  -0.492    1.378   -20.9    -59.1     143.3      0.334
'     -90.0  -1.571    0.000  -0.500    1.300   -22.6    -67.4       0.0      0.000
'    -100.0  -1.745   -0.087  -0.492    1.217   -23.9    -76.1    -143.3     -0.295
'    -110.0  -1.920   -0.171  -0.470    1.131   -24.5    -85.5    -282.2     -0.566
'    -120.0  -2.094   -0.250  -0.433    1.044   -24.5    -95.5    -412.5     -0.829
'    -130.0  -2.269   -0.321  -0.383    0.958   -23.6   -106.4    -530.3     -1.106
'    -140.0  -2.443   -0.383  -0.321    0.878   -21.5   -118.5    -632.0     -1.439
'    -150.0  -2.618   -0.433  -0.250    0.807   -18.1   -131.9    -714.5     -1.921
'    -160.0  -2.793   -0.470  -0.171    0.750   -13.2   -146.8    -775.2     -2.833
'    -170.0  -2.967   -0.492  -0.087    0.713    -7.0   -163.0    -812.5     -5.559
'    -180.0  -3.142   -0.500  -0.000    0.700    -0.0   -180.0    -825.0  -9999.999
'---------------------------------------------------------------------------
' set parameters here
Dim As Double rod_pin, cyl_pin, jib_end, jib_load
rod_pin = 0.5   ' length from hinge along jib to rod_pin, metres
cyl_pin = -1.2  ' length from hinge along boom to cyl_pin, metres
jib_end = 5.5   ' length of jib, metres
jib_load = 150. ' jib end-load, newtons

'---------------------------------------------------------------------------
Const As Double Pi = 4 * Atn( 1 )

Type v2d    ' a point on the x, y plane, or a vector in two dimensions
    As Double x, y  ' is realy a complex number ( x + j y )
End Type

'----------------------------------------------------------------------
' table heading
Print             "   Jib angle      Rod pin pos'n    Hydraulic ram   Tween    Moment    Ram axial"
Print             "   deg   rad       x       y       length  angle     deg    jib N.m   force kN "
Dim As String f = "####.#  ##.###   ##.###  ##.###   ##.###  ####.#   ####.#  ######.#  #####.###"

'---------------------------------------------------------------------------
' sweep the jib angle, theta, with negative theta being a depression
For deg As Double = -0 To -180 Step -10     ' sweep jib angle in degrees
    Dim As Double theta = deg * Pi / 180    ' convert deg to radians
    
    Dim As v2d jib  ' jib direction, unit vector
    jib.x = Cos( theta )
    jib.y = Sin( theta )
    
    Dim As v2d cyl  ' position of hydraulic cylinder-end eye or pin
    cyl.x = cyl_pin ' cyl, is attached to boom
    cyl.y = 0.0
    
    Dim As v2d rod  ' position of hydraulic rod_end pin on jib
    rod.x = rod_pin * jib.x
    rod.y = rod_pin * jib.y
    
    Dim As v2d ram  ' hydraulic ram, becomes ram direction vector
    ram.x = rod.x - cyl.x   ' translate cylinder end to origin without rotation
    ram.y = rod.y - cyl.y
    Dim As Double r = Sqr( ram.x * ram.x + ram.y * ram.y )  ' hydraulic ram length
    ram.x = ram.x / r ' normalise the cylinder vector length to unity
    ram.y = ram.y / r ' ? - conjugate cylinder direction to later unwind jib angle
    
    Dim As Double rangle = Atan2( ram.y, ram.x )    ' ram angle
    rangle = rangle * 180 / Pi    ' convert to degrees
    
    ' cosine of angle between cylinder and jib is important to hydraulic force
    ram.y = -ram.y  ' conjugate of ram angle will untwist jib by ram direction
    'complex multiply rotates jib by ram    
    Dim As v2d da   ' difference angle vector = JIB * Conj( RAM )
    da.x = ram.x * jib.x - ram.y * jib.y  ' this term is not needed
    da.y = ram.x * jib.y + ram.y * jib.x  ' this is cosine of angle difference
    
    Dim As Double tween =  Atan2( da.y, da.x )    ' angle between jib and ram
    tween = tween * 180 / Pi    ' convert to degrees
    
    ' moment of jib end force, still need to add weight of the jib to moment
    Dim As Double mj = jib.x * jib_end * jib_load  ' units are N.m
    
    Dim As Double hf = -mj / ( da.y * rod_pin )  ' hydraulic force
    If Abs( hf ) > 1e9 Then hf = Sgn( hf ) * 9999999
    
    ' report
    Print Using f; deg; theta; rod.x; rod.y; r; rangle; tween; mj; hf / 1000
    
Next deg

'---------------------------------------------------------------------------
Sleep
