Solved LUA Torque vectoring with dual electric motor

Discussion in 'Mod Support' started by Antonomasia, May 8, 2024.

  1. Antonomasia

    Antonomasia
    Expand Collapse

    Joined:
    May 8, 2024
    Messages:
    3
    Hi everyone, I am currently developing a Formula SAE mod for my team.
    The vehicle has two independent electric motors, respectively, driving the left and right rear wheels through a chain.

    We would like to implement a torque vectoring system for the car that takes various inputs from the car sensors and manages the power of each motor accordingly.
    I checked other electric vehicles in the game, and the closest one is the E-SBR, which uses brakes to keep the car stable.
    The problem is that even though I found the LUA script that does this, I was not able to comprehend what anything does in that file.

    The powertrain setup for each motor is quite simple: motor -> torsionReactor -> shaft -> wheel

    I would love for someone to guide me through the basics of LUA scripting for BeamNG so that I can at least try to make something work.

    --- EDIT ---

    I reached out to Zeit (if you know the tag pls tag him) on discord and he gave me this really simple but perfect solution:
    I can control the motors separately by using a separate custom throttle value for each of them.
    This is the code for a very basic and pretty useless torque vectoring:

    Code:
    local M = {}
    
    M.type = "auxilliary"
    M.relevantDevice = nil
    
    local abs = math.abs
    
    local function init(jbeamData)
       -- adding custom values for separate throttle
        electrics.values.throttle_left = 0
        electrics.values.throttle_right = 0
    end
    
    local function updateGFX()
        -- grab data
        local throttle = electrics.values.throttle or 0
        local brake = electrics.values.brake or 0
        local wheelSpeed = electrics.values.wheelSpeed or 0
        local wheelSpeed = electrics.values.wheelSpeed or 0
        local steer = electrics.values.steering or 0-- positive is right turn
        
        -- here you can control how to apply thtottle for each motor
        if throttle > 0 then
            electrics.values.throttle_left = abs(throttle - steer/360) + 0.1
            electrics.values.throttle_right = abs(throttle + steer/360) + 0.1
        else
            electrics.values.throttle_left = 0
            electrics.values.throttle_right = 0
        end
        
        --log("D", "Torque Vectoring", string.format("Left torque: %.2f ~~~ Right torque: %.2f", electrics.values.throttle_left, electrics.values.throttle_right))
    end
    
    M.init = init
    M.updateGFX = updateGFX
    
    return M
    I than had to "tell" to each motor which input to take as his throttle, I did it by adding the "electricsThrottleName" variable to the motor parameters. This is how:

    Code:
        "leftMotor":{
            "torque":[
                ["rpm", "torque"],
                [0, 50],
                [300, 56],
                [6500, 56],
                [8000, 40],
            ],
            "electricsThrottleName": "throttle_left", // throttle for leftMotor is called "throttle_left"
            "maxRPM":8000,
            "maxRegenPower":150,
            "maxRegenTorque":100,
            "inertia":0.10,
            "friction":2,
            "dynamicFriction":0.0005,
            "electricalEfficiency":0.96,
            "torqueReactionNodes:":["rlm1","rlm5","rlm8"],
            "energyStorage": "mainBattery",
            "electricsThrottleFactorName": "throttleFactorRear",
    
            //node beam interface
            "waterDamage": {"[engineGroup]:":["rear_left_motor_waterdamage"]},
            "engineBlock": {"[engineGroup]:":["rear_left_motor_block"]},
            "breakTriggerBeam":"rear_left_motor",
            "uiName":"Rear Left Motor",
    
            "soundConfig": "soundConfigRear",
        },
        "rightMotor":{
            "torque":[
                ["rpm", "torque"],
                [0, 50],
                [300, 56],
                [6500, 56],
                [8000, 40],
            ],
            "electricsThrottleName": "throttle_right", // throttle for rightMotor is called "throttle_right"
            "maxRPM":8000,
            "maxRegenPower":150,
            "maxRegenTorque":100,
            "inertia":0.10,
            "friction":2,
            "dynamicFriction":0.0005,
            "electricalEfficiency":0.96,
            "torqueReactionNodes:":["rrm5","rrm1","rrm4"],
            "energyStorage": "mainBattery",
            "electricsThrottleFactorName": "throttleFactorRear",
    
            //node beam interface
            "waterDamage": {"[engineGroup]:":["rear_right_motor_waterdamage"]},
            "engineBlock": {"[engineGroup]:":["rear_right_motor_block"]},
            "breakTriggerBeam":"rear_right_motor",
            "uiName":"Rear Right Motor",
    
            "soundConfig": "soundConfigRear",
        },
    
     
    #1 Antonomasia, May 8, 2024
    Last edited: May 24, 2024
  2. SineMatic

    SineMatic
    Expand Collapse

    Joined:
    Nov 17, 2022
    Messages:
    62
    Some time ago I came across this big thread about making two separate powertrains: https://www.beamng.com/threads/how-to-create-seperate-inputs-for-twin-engine-car.70076/, hope you'll find it helpful!
     
  3. Antonomasia

    Antonomasia
    Expand Collapse

    Joined:
    May 8, 2024
    Messages:
    3
    Thank you, I very much appreciate the reply. I sorted it out thanks to a guy on discord which gave me a relly simple but effective way to do what I wanted. I''l update the question now and mark it as solved
     
  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