Advertisement
Guest User

DCS Lua for engine vibration

a guest
Jul 5th, 2023
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.77 KB | Gaming | 0 0
  1. --[[
  2.  
  3. DCS LUA SCRIPT FOR FLYPT MOVER 3.5.2
  4. ====================================
  5. Version from 27 08 2021
  6. This file should be placed in the folder "C:\Users\YOUR USERNAME\Saved Games\DCS\Scripts\Hooks".
  7. If the "Hooks" folder doen's exist, create it.
  8.  
  9. ]]--
  10.  
  11. local FlyPT_Mover_Callbacks = {}
  12.  
  13. function FlyPT_Mover_Callbacks.onSimulationStart()
  14.  
  15.     log.write('FlyPT_Mover', log.INFO, "Starting data export")
  16.     package.path = package.path..";.\\LuaSocket\\?.lua"
  17.     package.cpath = package.cpath..";.\\LuaSocket\\?.dll"
  18.     socket = require("socket")
  19.     DCSClient = socket.udp()
  20.     DCSClient:settimeout(0)
  21.     DCSIP = "127.0.0.1"
  22.     DCSPort = 4124 -- If changed, the source should match this value
  23.     DCSClient:setpeername(DCSIP, DCSPort)
  24.        
  25. end
  26.  
  27. function FlyPT_Mover_Callbacks.onSimulationFrame()
  28.  
  29.     local acceleration = Export.LoGetAccelerationUnits()  
  30.     local speed = Export.LoGetVectorVelocity()
  31.     local pitch, roll, yaw = Export.LoGetADIPitchBankYaw()
  32.     local rotationSpeed = Export.LoGetAngularVelocity()
  33.     local altitude = Export.LoGetAltitudeAboveGroundLevel()
  34.     local o = Export.LoGetSelfData()
  35.     --[[
  36.     LatLongAlt.Lat -- Latitude in degress
  37.     LatLongAlt.Long -- Longitude in degress
  38.     LatLongAlt.Alt -- Altitude in meters MSL
  39.     Heading -- Heading in radians
  40.     Pitch -- Pitch in radians
  41.     Bank -- Bank in radians
  42.     ]]--
  43.    
  44.     local mechInfo=Export.LoGetMechInfo()
  45.     --[[
  46.     gear          = {status,value,main = {left = {rod},right = {rod},nose =  {rod}}}
  47.     flaps         = {status,value}  
  48.     speedbrakes   = {status,value}
  49.     refuelingboom = {status,value}
  50.     airintake     = {status,value}
  51.     noseflap      = {status,value}
  52.     parachute     = {status,value}
  53.     wheelbrakes   = {status,value}
  54.     hook          = {status,value}
  55.     wing          = {status,value}
  56.     canopy        = {status,value}
  57.     controlsurfaces = {elevator = {left,right},eleron = {left,right},rudder = {left,right}}
  58.     ]]--
  59.    
  60.     local alarm = Export.LoGetMCPState()
  61.     --[[
  62.         returned table keys for LoGetMCPState():
  63.         "LeftEngineFailure"
  64.         "RightEngineFailure"
  65.         "HydraulicsFailure"
  66.         "ACSFailure"
  67.         "AutopilotFailure"
  68.         "AutopilotOn"
  69.         "MasterWarning"
  70.         "LeftTailPlaneFailure"
  71.         "RightTailPlaneFailure"
  72.         "LeftAileronFailure"
  73.         "RightAileronFailure"
  74.         "CanopyOpen"
  75.         "CannonFailure"
  76.         "StallSignalization"
  77.         "LeftMainPumpFailure"
  78.         "RightMainPumpFailure"
  79.         "LeftWingPumpFailure"
  80.         "RightWingPumpFailure"
  81.         "RadarFailure"
  82.         "EOSFailure"
  83.         "MLWSFailure"
  84.         "RWSFailure"
  85.         "ECMFailure"
  86.         "GearFailure"
  87.         "MFDFailure"
  88.         "HUDFailure"
  89.         "HelmetFailure"
  90.         "FuelTankDamage"
  91.     ]]--
  92.     local stall = 0
  93.     for k,v in pairs(alarm) do
  94.         if k == "StallSignalization" then
  95.             if v == true then
  96.                 stall = 1
  97.             end
  98.         end
  99.     end
  100.  
  101.     -- local engine = LoGetEngineInfo()
  102.     --[[
  103.     RPM = {left, right},(%)
  104.     Temperature = { left, right}, (Celcium degrees)
  105.     HydraulicPressure = {left ,right},kg per square centimeter
  106.     FuelConsumption   = {left ,right},kg per sec
  107.     fuel_internal      -- fuel quantity internal tanks  kg
  108.     fuel_external      -- fuel quantity external tanks  kg 
  109.     ]]--
  110.    
  111.     -- Simulation of engine vibration
  112.    
  113.     local engine        = Export.LoGetEngineInfo()
  114.     local RPM_pct       = engine.RPM.left / 100 -- RPM in DCS is reported in percentage
  115.     local rotor_max_rpm = 324                   -- This is the maximum rotor speed for the UH-1: https://en.wikipedia.org/wiki/Bell_UH-1_Iroquois.
  116.     local freq_max      = rotor_max_rpm / 60    -- Convert into Hz
  117.     local ts            = socket.gettime()      -- Timestamp in seconds (up to ms resolution)
  118.    
  119.     local vibration_freq    = RPM_pct * freq_max
  120.     local vibration_val     = math.sin( 2 * math.pi * vibration_freq * ts )
  121.    
  122.     -- The FlyPT Mover uses Z for vertical and Y to the front
  123.     -- That's the opposite in DCS
  124.     -- Values sent in one string, separated by spaces  
  125.     socket.try(DCSClient:send(
  126.     --               00   01   02   03   04   05   06   07   08   09   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24
  127.     string.format("%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f",
  128.     acceleration.x,                             -- 00 = Lateral acceleration (G)
  129.     acceleration.z,                             -- 01 = Lateral acceleration (G)
  130.     acceleration.y,                             -- 02 = Vertical acceleration (G)
  131.     speed.x,                                    -- 03 = Lateral speed (m/s)
  132.     speed.z,                                    -- 04 = Longitudinal speed (m/s)
  133.     speed.y,                                    -- 05 = Vertical speed (m/s)
  134.     rotationSpeed.y,                            -- 06 = Rotation speed around y (yaw in rad/s)
  135.     rotationSpeed.x,                            -- 07 = Rotation speed around x (roll in rad/s)
  136.     rotationSpeed.z,                            -- 08 = Rotation speed around z (pitch in rad/s)
  137.     o.Heading,                                  -- 09 = Yaw position (rad)
  138.     o.Bank,                                     -- 10 = Roll position (rad)
  139.     o.Pitch,                                    -- 11 = Pitch position (rad)
  140.     Export.LoGetTrueAirSpeed(),                 -- 12 = Air speed (m/s)
  141.     Export.LoGetAircraftDrawArgumentValue(1),   -- 13 = Front/Rear landing gear (0 to 1)?
  142.     Export.LoGetAircraftDrawArgumentValue(2),   -- 14 = Turning landing gear (0 to 1)?
  143.     Export.LoGetAircraftDrawArgumentValue(4),   -- 15 = Left landing gear (0 to 1)?
  144.     Export.LoGetAircraftDrawArgumentValue(6),   -- 16 = Right landing gear (0 to 1)?
  145.     Export.LoGetAltitudeAboveGroundLevel(),     -- 17 = Vertical position relative to ground (m)
  146.     mechInfo.flaps.value,                       -- 18 = Flaps amount (%)
  147.     mechInfo.gear.value,                        -- 19 = Delployed landing gear (%)
  148.     mechInfo.speedbrakes.value,                 -- 20 = Speed brakes (%)
  149.     mechInfo.canopy.value,                      -- 21 = Canopy open (%)
  150.     vibration_val,                              -- 22 = Stall alarm (0 or 1)
  151.     Export.LoGetAngleOfAttack(),                -- 23 = Angle of attack
  152.     Export.LoGetModelTime()                     -- 24 = Time in seconds
  153.     )))
  154. end
  155.  
  156. function FlyPT_Mover_Callbacks.onSimulationStop()
  157.     log.write('FlyPT_Mover', log.INFO, "Data export stopped")
  158.     if DCSClient then
  159.         DCSClient:close()
  160.     end
  161. end
  162.  
  163. DCS.setUserCallbacks(FlyPT_Mover_Callbacks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement