Vehicle lua - data from Beamng engine (streams)

Discussion in 'Programming' started by mjc506, Jun 4, 2015.

  1. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    So, we can use some data from the engine in our vehicle lua code, but I'm struggling to get some data I need.

    It appears the data is available to the UI apps through a stream, but I can't access it with the vehicle lua?

    Is there a way for the vehicle lua to access the streamed data? (ideally without modifying any of the core files)

    Specifically, i'm after the vehicle roll (sensors.roll) but I imagine any advice will be global.
     
  2. tdev

    tdev
    Expand Collapse
    Developer
    BeamNG Team

    Joined:
    Aug 3, 2012
    Messages:
    3,031
    What do you want to achieve? We can help you maybe if we know the whole thing of what you want to get working :)
     
  3. Dummiesman

    Dummiesman
    Expand Collapse

    Joined:
    Sep 17, 2013
    Messages:
    4,685
    Look at the code for roll in guistreams, it can be applied to local vehicle lua :)
     
  4. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    That's classified:p

    Basically, I'm trying to create a motorcycle. Or rather, I've created a motorcycle, and I want it to stay upright.

    If I can read the vehicle roll and speed, I can calculate a required steering input to tilt the vehicle towards the target roll (set by steering_input) but I can't seem to read roll at the moment.


    I can already get it sort-of working at very high speeds by having the steering hydros acting backwards and having a low spring rate, but this method of control requires high gyroscopic forces, so isn't ideal.

    Dummiesman, thanks, hadn't thought of looking there. I'll give that a go this evening :)

    - - - Updated - - -

    Ok, here's where you can laugh at my noobiness...

    guistreams.lua contains the following segment:
    Code:
        if M.sensors>0 then
            local dirVector = obj:getDirectionVector()
            local dirVectorUp = obj:getDirectionVectorUp()
            local position = obj:getPosition()
            sensorvalues = {
                gx = sensors.gx,
                gy = sensors.gy,
                gz = sensors.gz,
                gx2 = sensors.gx2,
                gy2 = sensors.gy2,
                gz2 = sensors.gz2,
                gxMax = sensors.gxMax,
                gxMin = sensors.gxMin,
                gyMax = sensors.gyMax,
                gyMin = sensors.gyMin,
                gzMax = sensors.gzMax,
                gzMin = sensors.gzMin,
                ffb = hydros.lastFFBforce,
                ffb2 = hydros.lastFFBforce2,
                position = {
                    x = position.x,
                    y = position.y,
                    z = position.z
                },
                roll = dirVectorUp.x * -dirVector.y + dirVectorUp.y * dirVector.x,
                pitch = dirVector.z,
                yaw = obj:getDirection(),
                gravity = Settings.gravity
            }
            gui.send('sensors' , sensorvalues )
        end
    and I've stuck the following in my vehicle lua file:
    Code:
    local function updateGFX(dt)
        local dirVector = obj:getDirectionVector()
        local dirVectorUp = obj:getDirectionVectorUp()
        --local position = obj:getPosition()
        --local roll = dirVectorUp.x * -dirVector.y + dirVectorUp.y * dirVector.x,
        --local pitch = dirVector.z,
        --local yaw = obj:getDirection(),
        if electrics.values['wheelspeed'] < speedThreshold then
            electrics.values['leansteer'] = 0
        else
            electrics.values['leansteer'] = (Kp * roll) / electrics.values['wheelspeed']
        end
    end
    The above runs fine (but does nothing), but as soon as I uncomment the 'local roll' line, the game freezes and the console fills up with the following error message which confuses me:
    Code:
    libbeamng.object|[string "lua/vehicle/main.lua"]:25: attempt to index global value "drivetrain" (a nil value)
    I imagine I'm committing some lua faux pas...
     
    #4 mjc506, Jun 4, 2015
    Last edited: Jun 4, 2015
  5. CarlosAir

    CarlosAir
    Expand Collapse

    Joined:
    Nov 16, 2013
    Messages:
    336
    Remove the comma at the end of the "local roll" line.
    ???
    Profit

    :D

    Idk, works for me :p.
    Remove the comma too from the yaw and pitch lines if they're going to be used. They were needed originally as the lines were in some sort of array or something, but now they are not needed.

    Report here if it still doesn't work. Apart from that, you seem to be going on the right track:p.
     
  6. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    doh, getting confused with jbeam and lua...

    Awesome, thank you very much, that bit is now working.

    Give me a few days to write a better algorithm and tune the variables are I should (hopefully) have something basic to release!
     
  7. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    I've noticed that this kind of error
    Code:
    libbeamng.object|[string "lua/vehicle/main.lua"]:25: attempt to index global value "drivetrain" (a nil value)
    often simply means: Syntax Error! :D Make sure to check your syntax if you encounter it again.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice