Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

SetValueFloat does not work as advertised, V2!

Discussion in 'Programming Questions and Suggestions' started by TangoFoxtrot, Jan 16, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    if (RotorChange < 0) {
    if (Input < lowerLimit) {
    Rotor.SetValueFloat("LowerLimit", lowerLimit);
    }
    else {
    Rotor.SetValueFloat("LowerLimit", Input);
    }
    if (RotorSpeed > 0) {
    RotorReverse.Apply(Rotor);
    }
    }

    This script works.

    else if (RotorChange > 0) {
    if (Input > upperLimit) {
    Rotor.SetValueFloat("UpperLimit", upperLimit);
    }
    else {
    Rotor.SetValueFloat("UpperLimit", Input);
    }
    if (RotorSpeed < 0) {
    RotorReverse.Apply(Rotor);
    }
    }

    This, despite being essentially the same code, doesn't. The target rotor reverses as needed, but the UpperLimit doesn't get set.
    Am I doing something wrong, or is it a bug with SetValueFloat?
    Copied over from bug reports.
     
  2. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Is this the right forum for this?
     
    Last edited by a moderator: Jan 16, 2015
  3. hellokeith Apprentice Engineer

    Messages:
    335
    I ran my http://forums.keenswh.com/post/fun-with-getproperties-7251794?pid=1285802929 GetProperties script on a rotor. Are you setting the values within the min/max correctly?

    [​IMG]
     
  4. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Yes, I think so.
    At least it should be. What the code is supposed to do is detect whether the Input(desired heading)is higher than the Output(the actual heading), and if so to set the UpperLimit to the input(unless it's over an upper limit specified in the code, in which case it sets UpperLimit to that). That's what's it's not doing.
    I haven't tried any values for Input higher than 200, so I don't think it's that.

    The odd thing is that the code for when the Input is lower than the Output is almost exactly the same(just switched around the operators and entered "LowerLimit" as needed) and it works just fine.

    If you want to fiddle with this, it's on the Steam Workshop under the name RotorControlMethod.(Although the stupid degree symbol might not carry over.)
     
    Last edited by a moderator: Jan 16, 2015
  5. pipakin Apprentice Engineer

    Messages:
    182
    My off the wall guess is that the value must be less than (not less than or equal to) the upper limit, while with lower limit it just must be greater than or equal to.

    Might be worth subtracting a small value (like 0.0001) and seeing if you can set it to that.
     
    Last edited by a moderator: Jan 16, 2015
  6. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    ...Value of what?
     
  7. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Tried subtracting 1 from the Input, is that what you meant?
    It didn't work.
     
  8. pipakin Apprentice Engineer

    Messages:
    182
    Instead of setting it to the upper limit, try setting it to the upper limit - some small value.
     
  9. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    That's not how SetValueFloat works.

    See, the input is what I want the UpperLimit to be. Rotor.SetValueFloat("UpperLimit", Input) sets the value of UpperLimit(which for some strange reason is represented by its label in a string) to whatever the input is.

    So theoretically, if Input=130(and is higher than the Output, which is where the rotor's at currently), then after running this the UpperLimit should be set to 130 degrees. Of course, this is apparently not the case.
     
    Last edited by a moderator: Jan 16, 2015
  10. pipakin Apprentice Engineer

    Messages:
    182
    Sorry, totally didn't read correctly. Three beers in is not a good time to offer advice. I thought the upper limit you were reffering to was the property's upper limit. I don't see any obvious problems with the code as is.
     
  11. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Me neither, and I'm sober(and maybe new to coding, but eh.)
     
  12. pipakin Apprentice Engineer

    Messages:
    182
    Would you be willing to share the whole code? It may be somewhere else that the problem lies.

    Another thing to try would be sprinkling some debug output in there (update an antenna's name or something).
     
  13. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Here's the full code:

    public void RotorControl (string RotorTarget, float upperLimit, float lowerLimit, float Input) {
    var Rotor = GridTerminalSystem.GetBlockWithName(RotorTarget) as IMyMotorStator;
    var RotorReverse = Rotor.GetActionWithName("Reverse");
    float RotorSpeed = Rotor.Velocity;

    string OutputUnfiltered = Rotor.DetailedInfo;
    string OutputFilter1 = OutputUnfiltered.Replace("Current angle: ", "");
    //If the script has an exception involving input string not having the right format, replace the question mark symbol in thi
    //in this next line with the degree symbol (That's CTRL 0176 for windows.)
    string OutputFilter2 = OutputFilter1.Replace("°", "");
    float RotorOutput = float.Parse(OutputFilter2);

    float RotorChange = Input-RotorOutput;

    if (RotorChange < 0) {
    if (Input < lowerLimit) {
    Rotor.SetValueFloat("LowerLimit", lowerLimit);
    }
    else {
    Rotor.SetValueFloat("LowerLimit", Input);
    }
    if (RotorSpeed > 0) {
    RotorReverse.Apply(Rotor);
    }
    }
    else if (RotorChange > 0) {
    if (Input > upperLimit) {
    Rotor.SetValueFloat("UpperLimit", upperLimit);
    }
    else {
    Rotor.SetValueFloat("UpperLimit", Input);
    }
    if (RotorSpeed < 0) {
    RotorReverse.Apply(Rotor);
    }
    }
    }

    I haven't put any debug output in, and I'm not sure where to start.
     
  14. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    I just set up some debug output, and I found some weird stuff.

    This is my debug code:
    Debug.SetCustomName("UpperLimit "+ Rotor.UpperLimit + " RotorChange " +
    RotorChange + " LowerLimit " + Rotor.LowerLimit);

    And it outputs this:
    UpperLimit 1.919862 RotorChange -100 LowerLimit 0.1745329

    For reference, the rotor says it's Lower limit is 10 degrees and it's Upper limit is 110 degrees.

    The heck? RotorChange works as advertised, but Rotor.UpperLimit and Rotor.LowerLimit output aren't even closely related to the rotor's page. What the heck is going on?
     
  15. Digi Senior Engineer

    Messages:
    2,393
    Just a small side note... you don't need the exact degree character, you can just remove 1 character at the end... and you don't even need the replace method :p

    Code:
    int index = Rotor.DetailedInfo.LastIndexOf(':') + 1;
    string output = Rotor.DetailedInfo.Substring(index, Rotor.DetailedInfo.Length - index - 1).Trim();
    This also doesn't rely on the language being english, it does rely on the format to be "&lt;any string here>:&lt;what you get>&lt;extra char here>"
     
  16. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    More results from weirdo land:
    UpperLimit 1.919862 RotorChange 80 LowerLimit 0.1745329

    I ran the code with a different input, so the limits should have changed. According to this, they haven't.
    What the everliving hell?
     
    Last edited by a moderator: Jan 16, 2015
  17. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    That could work...except that piece of code won't get rid of the degree symbol at the end.
    I could probably make it work though.
     
  18. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Applying that code(along with some needed modifications) actually seems to have solved my problem. Thanks!
     
  19. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    False alarm, it didn't. Bloody heck.
     
  20. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    By the way, does anyone know how to update a script that's already been published?
     
  21. Digi Senior Engineer

    Messages:
    2,393
    It does, that's the whole point of it.
    Code:
        static void Main()
        {
            string info = "Current Whatever: 75.1*";
            
            int index = info.LastIndexOf(':') + 1;
            string output = info.Substring(index, info.Length - index - 1).Trim();
            
            Console.WriteLine("input = '" + info + "'");
            Console.WriteLine("output = '" + output + "'");
            Console.ReadKey();
        }
    
    Code:
    input = 'Whatever: 75.2*'
    output = '75.2'
    Replace from editor to update your local copy then publish it again, which will replace the workshop one.


    EDIT:
    I tested some things... it seems that the rotor itself is broken with the limits.
    Do it without coding, limit the rotor between 30 and 45 (or any numbers you want) and set the velocity to something positive, you'll see it goes way beyond 45 then if you reverse it goes way below 30 (or whatever numbers you had). It has like 10deg error range.
     
    Last edited by a moderator: Jan 16, 2015
  22. Sirhan Blixt Apprentice Engineer

    Messages:
    467
    Yeah, there are definitely some issues with rotors that you cannot blame on your code.
     
  23. kor kilden Trainee Engineer

    Messages:
    29
    Deecrease torque and the rotor will get closer to the limit.
     
  24. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Wait, it does?
    Crap, I'm an idiot.
     
  25. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    It actually works if you set braking torque to max and lower the torque.
     
  26. TangoFoxtrot Apprentice Engineer

    Messages:
    112
    Went back to testing again, after adding the code from Digi.

    I don't know if it did this before, but I added if(RotorChange != 0){} around the main part of the code, and now it works correctly so long as you exceed the rotor's current upper limit every time you go clockwise.
    Odd.
     
    Last edited by a moderator: Jan 16, 2015
Thread Status:
This last post in this thread was made more than 31 days old.