Electric Motor Limiter

Discussion in 'Programming' started by ChickenFeline0, Oct 14, 2021.

  1. ChickenFeline0

    ChickenFeline0
    Expand Collapse

    Joined:
    Jan 28, 2021
    Messages:
    9
    I just posted this in mod support, but I realize it might fit better here.

    I have a bit of an interesting challenge for anyone experienced in Lua. I have an electric car mod for the modern ETKs, which can be found on my page if you are interested. My current goal for that mod is to create a system where the drive mode will dictate how much power it has. I have figured out where to put the limiting number (such as .5 for 50% power), but I am stuck on how to get the drive mode jbeam to communicate with my custom controller, shiftLogic-electricMotorCustom.lua. I have so far tried heavily modeling it after the two-step system, but that doesn't seem to work. If anybody has any advice, please let me know. I have attached the WIP mod (consider it a beta ;) ). I have done my best to add comments to the program, but I can't seem to figure that out. Currently, I only have the modified drive mode files for the K-series, so keep that in mind if you open the mod. I would greatly appreciate any insight you might have on how I can fix this problem.
     
    • Like Like x 1
  2. ChickenFeline0

    ChickenFeline0
    Expand Collapse

    Joined:
    Jan 28, 2021
    Messages:
    9
    UPDATE: I went through and found a lot of my mistakes. I am uploading the new file in this reply. The vehicle now drives when the new electronics and the limited motor are on it, but it still won't limit the motor power.
     

    Attached Files:

  3. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,319
    The easiest way I can think of to limit the motor would be to reset the electrics.values.throttle value to something less than 1 (0.9 for example), everytime the input is over that (also I do not think you'd have to make a shiftlogic file if you do this)
    Example of this idea for 'updateGFX':
    Code:
    if electrics.values.throttle > 0.9 then
           electrics.values.throttle = 0.9
    end
    You'd obviously have to implement the checks for the drive mode and make the code act accordingly.

    There's definitely a better way of doing this lol
    --- Post updated ---
    Or you can try modifying the torque curve a little like the Volkswagen Beetle 'Herbie' mod, however I don't know how you'd do that off the top of my head.
     
  4. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,056
    Drive modes can change the turbocharger boost, maybe you could somehow try to turbocharge the electric motor and have 0 boost by default and some small boost on sport drive mode? Idk
     
    • Agree Agree x 1
  5. SKB

    SKB
    Expand Collapse

    Joined:
    Apr 22, 2017
    Messages:
    1,964
    gives a whole new meaning for Turbo and Turbo S for the Taycan lol
     
  6. ChickenFeline0

    ChickenFeline0
    Expand Collapse

    Joined:
    Jan 28, 2021
    Messages:
    9
    I thought about doing it this way. Unfortunately, it seems that the turbocharger just tells the game a boost level, which the internal combustion lua then translates it into a power increase. It doesn't seem to work with electric. thank you for the suggestion, though.
    --- Post updated ---
    this is a great idea, but it doesn't quite fit my desired outcome. I am hoping to linearly scale the power. for example, if the throttle input from the user is at 20%, and the scale is 50%, then the actual input to the motor will be 10%. As for the herbie mod, I will definitely look into this to see if I can find any useful information in it's code. Thanks for the suggestions.
     
  7. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Hah ! " throttle input = throttle input / 2 " :D
     
  8. default0.0player

    default0.0player
    Expand Collapse

    Joined:
    Nov 30, 2018
    Messages:
    1,925
    You can dig into this mod to find out how to adjust power on the fly.
     
  9. ChickenFeline0

    ChickenFeline0
    Expand Collapse

    Joined:
    Jan 28, 2021
    Messages:
    9
    this is a great idea, but it doesn't quite fit my desired outcome. I am hoping to linearly scale the power. for example, if the throttle input from the user is at 20%, and the scale is 50%, then the actual input to the motor will be 10%. As for the herbie mod, I will definitely look into this to see if I can find any us
    thats about where I am at with it. Except, instead of 2, I want a number that changes depending on the drive mode. My current plan is 50% power with comfort mode, 75% with sport, and 100% with ttSport/sport+, 2WD, and TC/ESC off.
     
  10. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    That's what I meant :)
    (putting each statement in each gear mode)
     
  11. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    Hey, what you want is quite easy to do once you have the actual power change figured out (which seems to be case according to what you wrote).
    In order to hook a controller into drivemodes, it needs a new public method:
    Code:
    M.setParameters = setParameters
    
    Once you implement the actual method similar to this:
    Code:
    local function setParameters(parameters)
      if parameters.isEnabled ~= nil then
        setTwoStep(parameters.isEnabled)
      end
      if parameters.launchRPM then
        setTwoStepRPM(parameters.launchRPM)
      end
    end
    You can start sending drivemode values to this controller and act upon them.

    Above code is from twoStepLaunch.lua, I hope that should get you on the right path :)
     
    • Like Like x 2
  12. ChickenFeline0

    ChickenFeline0
    Expand Collapse

    Joined:
    Jan 28, 2021
    Messages:
    9
    that is about the point I got to. I also added the following
    Code:
    local limit = 100
    
    I also added this based on the two step program, per your advice. I had actually done this before, but I redid it to make sure that it was correct.
    Code:
    local function setParameters(parameters)
      if parameters.limit then
        setlimit(parameters.limit)
      end
    end
    I now get a shared functions error every time I load in the custom driving electronics. I have attached the latest version below if you would like to take a look. again, I only have the custom drive mode files for the K-Series.
     

    Attached Files:

    #12 ChickenFeline0, Oct 20, 2021
    Last edited: Oct 20, 2021
  13. ChickenFeline0

    ChickenFeline0
    Expand Collapse

    Joined:
    Jan 28, 2021
    Messages:
    9
    I would like to thank everybody who has made a suggestion. I eventually did figure it out. I had to go a level deeper to the powertrain control file rather than just the shiflogic file. It is a little jank, but it functions perfectly. You can get it as soon as it is approved here. Again, thank you for your help!
     
  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