Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The given key was not present in the dictionary (probably solved?) #1347

Open
jhuber-de opened this issue Jan 4, 2016 · 3 comments
Open
Labels
documentation Change the Sphinx user documents. enhancement Something new (not a bug fix) is being requested Error Messages Feedback to the user explaining an error doesn't explain it properly.

Comments

@jhuber-de
Copy link

While trying to write an autopilot for launching a rocket I've got this error.
screenshot2

It appears while loading a custom lib written by me that contains all the functions of the script. Since there isn't a line of code that causes the error I'm not able to tell where it stoped working, The last thing I was working on was to regulate the throttle by locking at the TWR. --> function limitTWR and function startGravityturn.

Since this is one of my first scripts I probably did something horrible wrong and it's all my fault but as it tells me to show it to the dev's thats what I'm doing :)

Here are my two files that caused the Error:

launch003.ks.txt
lib_launch.ks.txt

and here is the output_log:

output_log.txt

Thanks for your help and greetings from Germany

@jhuber-de
Copy link
Author

I could narrow down the error.
I tried uncomment the limitTWR function and it didn't crashed.
After some further inspection I rewrote the function from scratch and then it worked then I compared the code and the only real difference was that the variable called limitTWR had a different name. So I changed the name of the varaible to something else and it worked quite nice.

This code caused errors

function limitTWR{
    PARAMETER imp_TWRLimit.

    SET currentThrottle TO getCurrentThrottle().
    SET currentTWR TO getCurrentTWR().
    SET limitTWR TO imp_TWRLimit.

    DECLARE newThrottle TO 0.

    PRINT "currentTWR: " + currentTWR.
    PRINT "limitTWR: " + limitTWR.

    IF currentTWR - limitTWR > 0.5{
        SET newThrottle TO currentThrottle - 0.1.
        PRINT "Throtteling down by 0.1".
    } ELSE IF currentTWR - limitTWR > 0.1{
        SET newThrottle TO currentThrottle - 0.01.
        PRINT "Throtteling down by 0.01".
    } ELSE {
        SET newThrottle TO currentThrottle + 0.05.
        PRINT "Throtteling up by 0.05".
    }
    LOCK THROTTLE TO newThrottle.
}

while this one worked

function limitTWR{
    PARAMETER imp_TWRLimit.

    SET currentThrottle TO getCurrentThrottle().    
    SET currentTWR TO getCurrentTWR().
    SET maxTWR TO imp_TWRLimit.

    IF currentTWR - maxTWR > 0.5 AND SHIP:CONTROL:PILOTMAINTHROTTLE > 0{
        SET newThrottle TO (currentThrottle - 0.1).
    } ELSE IF currentTWR - maxTWR > 0.1 AND SHIP:CONTROL:PILOTMAINTHROTTLE > 0{
        SET newThrottle TO (currentThrottle - 0.01).
    } ELSE IF currentTWR - maxTWR < 0 AND SHIP:CONTROL:PILOTMAINTHROTTLE < 1{
        SET newThrottle TO (currentThrottle + 0.05).
    }
    SET SHIP:CONTROL:PILOTMAINTHROTTLE TO newThrottle.
}

You see the only real diffenences are the name of the variable it's now called maxTWR instead of limitTWR and the use of the SET SHIP:CONTROL:PILOTMAINTHROTTLE TO newThrottle instead of LOCK THROTTLE TO newThrottle. The last had to be changed due to another strage behavior where the thrust jumps up and down like crazy but thats not important here.

If I had to guess what caused the crash I would say it was caused by the variable called like the function.

Here is the now working code:

launch_atlas2.1.ks.txt
lib_launch.ks.txt

@jhuber-de jhuber-de changed the title The given key was not present in the dictionary The given key was not present in the dictionary (probably solved?) Jan 7, 2016
@Dunbaratu
Copy link
Member

Yeah I think this is a duplicate of an existing issue, then.

@hvacengi
Copy link
Member

hvacengi commented Jan 8, 2016

I started doing a little debugging on this issue. It appears that you are correct, the root of the problem is that the function and the variable shared the same name. Under the hood, locks and functions share a naming structure ("$[name]*"). So kOS was trying to pull the "default" value of a limitTWR lock that didn't exist. When you create a lock, it adds an identifier like "limittwr`0-default".

So as you surmised, changing the name of the variable was the best fix to your problem. We could probably do something to improve the error message in this instance however to give you a chance at identifying the source of the issue instead of randomly changing things.

Two other comments before I finish:

First, you don't really need that extra variable in the function anyways because you can reference imp_TWRLimit. Even if you want to modify the value in the future, the parameter imp_TWRLimit is scoped as a local variable, so modifying it's value won't change the originating source that called the function.

Second, I am not a fan of repeatedly locking the throttle or steering. We're working on updating our tutorial so that it doesn't imply this is a good code practice. Every time you create a new steering or throttle lock, the underpinnings of kOS set up the user function that returns the set value, as well as toggling the control parameter "on". It isn't as huge of an issue with the throttle, but it will reset the steering's integral component every time. You might consider revising the function to return the new throttle value, and then in your main body

set idealTWR to 1.2.
lock throttle to limitTWR(idealTWR).
wait 5.
set idealTWR to 2.0.

or if you don't want to evaluate the whole function every single physics tick:

set idealTWR to 1.2.
set limitedThrottle to limitTWR(idealTWR).
lock throttle to limitedThrottle.
until false {
    set limitedThrottle to limitTWR(idealTWR).
    wait 1.
}

@hvacengi hvacengi added enhancement Something new (not a bug fix) is being requested documentation Change the Sphinx user documents. labels Jan 28, 2016
@hvacengi hvacengi added the Error Messages Feedback to the user explaining an error doesn't explain it properly. label Mar 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Change the Sphinx user documents. enhancement Something new (not a bug fix) is being requested Error Messages Feedback to the user explaining an error doesn't explain it properly.
Projects
None yet
Development

No branches or pull requests

3 participants