Nivekk / KOS

Fully programmable autopilot mod for KSP.
Other
80 stars 30 forks source link

lock keyword does not work properly for variables #168

Closed civilwargeeky closed 10 years ago

civilwargeeky commented 11 years ago

So I picked up KOS again the other day after not having tried it since around initial release. I wanted to make a simple hover program, and I have since made a basic setup. However, it seems that my lock statements are not being updated (at least inside an until loop). If I have:

  lock var to alt
  until controlVariable {
    print alt
    //do other stuff... like ascending
  }

Then it prints "0" It does the same thing if I attempt to put the lock statement inside the loop. However, if I use a "set" statement outside the loop, and then another "set" statement inside the loop to repeatedly set variables, it works properly. I don't think is intended.

Full Code for Reference:

set ground to Altitude.
set flux to 5.
set targetAlt to ground + 50.
set startTarget to targetAlt - flux.
set endTarget to targetAlt + flux.
set maxThrottle to 0.5.
set startTime to Time.
set timer to 30.

lock currAlt to Altitude - startTarget.
lock elapsedTime to Time - startTime.
lock neededForce to mass * 9.81 * 907.185. //Mass in tons times conversion times gravity
lock neededPercent to neededForce / (MaxThrust * 1000). //Percent Engine thrust in newtons

clearScreen.

until Time > (startTime + timer) {

  print "Percent " + neededPercent at (0,0).
  print "CurrAlt " + currAlt at (0,1).
  print "Altitude " + Altitude at (0,2).
  print "Ground " + ground at (0,3).
  print "Target Alt " + targetAlt at (0,4).
  print "Time " + elapsedTime at (0,5).

  if currAlt > flux {
    lock throttle to neededPercent - 0.05.
  }.
  if currAlt < flux {
    lock throttle to neededPercent + 0.1.
  }.
}.
Dunbaratu commented 11 years ago

This isn't related at all to the problem you're describing but when I tried following your code to see what the problem is I noticed this in passing: Your conversion factor of 907.185 seems to be assuming that mass is given in Imperial tons. It's not. It's given in metric tons. This could cause problems for you once the lock issue is fixed.

Dunbaratu commented 11 years ago

Strangely it does seem to work right in this example:

set b to 0.
lock a to b.
until b = 5 {
  set b to b + 1.
  print "a=" + a + " b=" + b .
}.

Which gives this output as expected:

a=1 b=1
a=2 b=2
a=3 b=3

Where it seems to fail is when using one of the built-in keywords like ALTITUDE, MAXTRHUST, and so on. It seems like the update works as long as it's based on a user variable not a keyword.

civilwargeeky commented 11 years ago

Hmmm. That is somewhat odd. Do you know whether or not this is intended behavior? If its not, I think that this issue should be labeled as a bug (If that's in my power, I'm not sure how to label it...). Regardless, thank you for the help, and until this is fixed (if unintended), I suppose I will be able to just set "base" variables in my loop, and the other variables should update off of that. Thanks.

Also, thanks for the tip with imperial vs metric tons. All this time I had been thinking about it wrong :)

Nivekk commented 11 years ago

I've done some overhauling to the expression system which I believe will solve this.

I tested it by locking a variable to sessiontime + 4 and then checking that variable inside an until loop. It checked out.

If you're able to compile from source, give the latest push a try.

civilwargeeky commented 10 years ago

Cool! Thank you. I'm unable ( or unwilling to go through the trouble of ) compiling this, but will be glad to use it when you next update.