Control Algorithms using In-Game Scripting Mod

#1
I've been trying a simple on-off control algorithm which uses spaar's mod.





Code:
-- import unity engine refference and declare variables
import ('UnityEngine')
setPoint = 325.0 -- the angle at which the lever should bounce about
tilt = 0.0 -- the speed of the wheel (which is in automatic mode)
er = 0.0 -- the difference(called error in control theory) between the actual angle of the lever and the desired one (setPoint)

simStart = function ()
-- initialize variables at start of simulation
setPoint = 325.0
tilt = 0.0
er = 0.0
end

frame = function ()

-- first introduce controls for the setPoint so that it is adjustable during simulation
if (Input.GetKey(KeyCode.I)) then
setPoint = setPoint + 1
end
if (Input.GetKey(KeyCode.K)) then
setPoint = setPoint - 1
end

-- now set the starting block to act as a gyroscope sensor
gyro = GameObject.Find ('bgeL0')
coord = gyro.transform.eulerAngles

er = coord[2] - setPoint -- estimate error

if (er < 0.0) then tilt = 0.1 else tilt = 0.0 end -- if the error is below 0 then apply torque to the wheel
-- log the relevant variables
Debug.Log (coord[2])
Debug.Log (setPoint)
Debug.Log (er)
Debug.Log (tilt)
 
end
The script is working fine as the debugging log shows, but the wheel does not respond to changes in the "tilt" variable regardless of whether it's in automatic mode or not. Suggestions?


Attached Files
control1.bsg
 

spaar

Active Member
#2
I think I know what the problem is: Currently, you can only use variables starting with 'v' for controlling blocks. Try renaming tilt to vTilt instead. This is to prevent collisions with other global lua variables, of which a few exist, but perhaps I should have made this clearer in the mod description. In the future, there will also be proper error-checking (probably starting with the next version) so that this doesn't just silently fail.

Also, your idea is pretty cool, I'm looking forward to what evolves out of this.
 
#3
It works. :)


Code:
-- import unity engine refference and declare variables
import ('UnityEngine')
setPoint = 350.0 -- the angle at which the lever should bounce about
vTilt = 0.0 -- the speed of the wheel (which is in automatic mode)
er = 0.0 -- the difference(called error in control theory) between the actual angle of the lever and the desired one (setPoint)

simStart = function ()
-- initialize variables at start of simulation
setPoint = 350.0
vTilt = 0.0
er = 0.0
end

frame = function ()

-- first introduce controls for the setPoint so that it is adjustable during simulation
if (Input.GetKey(KeyCode.I)) then
setPoint = setPoint + 1
end
if (Input.GetKey(KeyCode.K)) then
setPoint = setPoint - 1
end

-- now set the starting block to act as a gyroscope sensor
gyro = GameObject.Find ('bgeL0')
coord = gyro.transform.eulerAngles

er = coord[2] - setPoint -- estimate error

if (er < 0.0 and coord[2] > 100) then vTilt = 1 else vTilt = 0.0 end -- if the error is below 0 then apply torque to the wheel
. The second condition ensures that the wheel doesn't keep spinning if it bounces too high.
-- log the relevant variables
Debug.Log (coord[2])
Debug.Log (setPoint)
Debug.Log (er)
Debug.Log (vTilt)
 
end
Attached Files
control1.bsg
 

ITR

l̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ֍̫̜̥̭͖̱̟̟͉͙̜̰ͅl̺̤͈̘̰̺͉̳͉̖̝̱̻̠̦͈ͅ
Staff member
#4
If you had access to OnGUI, maybe it would look a little better? (The log I mean)
 
#5

So I finally got down to implementing a PID algorithm. It's working fine but the implementation is not polished to perfection yet.

BTW I tuned the PID parameters by hand with no measurement and calculation. Fine tuning is more of an art form so it's going to take a lot of time and I am much more willing to make a flying machine and then tune the algorithm according to its size and weight.

On a side note, the way I calculate the delta time needed for integration and differentiation works surprisingly well. Using the slider for slow mo in the game increases the sampling rate of the calculations. This is perfect for the cases where the wheels should compensate rapidly and avoids glitching.

If there is interest I will make a tutorial or more like a set of tutorials on control theory with working examples. The final goal is a self-stabilizing quad.

Thanks to everyone who gave me advice. :)

Download the code.

Yeah, the bsg got screwed.
 
#9
Imagine a self stabilizing flying machine which automatically shoots at targets. This could open a whole new space of Besiege modding. Not modding the actual game but modding the machines behavior.
 
#10
I did think about automatic aiming and shooting but I will get to it at a later stage if ever. Something like full auto all level completer. :|
 
Top