Difference between revisions of "HANtune/Scripting"
(→Signals and Parameters) |
(→Scripted Signals) |
||
Line 82: | Line 82: | ||
[[File:Scripted_Elements2.png|Scripted Elements]] | [[File:Scripted_Elements2.png|Scripted Elements]] | ||
− | Setting | + | Setting the attributes of a signal: |
<nowiki>mySignal.minimum = 0 | <nowiki>mySignal.minimum = 0 |
Revision as of 08:57, 17 January 2018
HANtune offers the ability to run your own scripts by integrating a Jython interpreter. Jython is a Java implementation of a Python interpreter, which means that it runs inside the Java Virtual Machine (JVM). This makes it very easy to interface with HANtune and enables us to automate its functionality. Jython currently corresponds to CPython 2.7 and can run any pure Python module, but does not support the use of native libraries (CPython extension modules written in C). However, this has been made available in HANtune by integrating JyNI, but is currently still limited.
Contents
Scripts
Adding a script to a project
To add a script to your project right click the 'Scripts folder' in the 'Project Data' side panel and click 'Add Script'. Browse to the right directory, select your script and click 'Add Script'.
Starting a script
To start a script right click on it in the 'Project Data' side panel and click 'Start'
Alternatively, you can start a script by first dragging it from the 'Project Data' side panel to the layout and clicking the newly created button.
To indicate that a script is running, its icon in the 'Project Data' side panel will change its appears.
Stopping a script
To stop a script right click on it in the 'Project Data' side panel and click 'Stop'.
Alternatively, if you have added a button to your layout you can stop a script by click it.
Startup scripts
If you want to expand the default behavior of HANtune or one of its projects, you can use startup scripts. Inside the HANtune directory you will find a file called Startup.py. This file is executed each time you start HANtune. For example, if you want to let the Jython interpreter know where to look for modules, you put the following lines into Startup.py.
import sys sys.path.append('C:\Users\User\Scripts')
Note: the path used is just an example and must be replaced by an actual location on your hard drive.
Console and Interactive Interpreter
HANtune integrates a console to which all scripts can print their output. In addition it serves as an interactive interpreter into which you can enter code directly. The >>>
prompt indicates the interpreter is ready to receive commands.
These can be simple expressions or multi line statements.
1+1
Running a script from the console:
execfile('C:\PathToMyScript\MyScript.py')
Or if the path to the script is known by the interpreter, you can import it as a module:
import MyScript
Sometimes the interpreter requires more input, in which case the prompt will look as following ...
.
Multi line statements:
for x in range(0,100): print x
Signals and Parameters
Scripted Signals
Creating a new signal:
mySignal = createSignal('MySignal')
After creating the signal, it will be shown in the 'Scripted Elements' side panel.
Setting the attributes of a signal:
mySignal.minimum = 0 mySignal.maximm = 255
Tip: enter dir(mySignal)
into the console for a full list of attributes.
Triggers
Sometimes you want to perform an action when a event occurs. For example, you might want to log a signal when it exceeds a certain value. You could do this by polling, checking the signal at a fixed interval and performing the action if needed. But this is not very efficient and it would be better if we could only check the signal when it changes value. Thats where triggers come in.
The following example creates a new signal called 'mySignal'. Then it defines a function that will print 'mySignal out of bounds' if the value of 'mySignal' is greater than '50'. Finally it creates the trigger by calling the 'createTrigger' function and passing in the signal and function as arguments.
mySignal = createSignal('MySignal') def myTriggerFunction(): if (mySignal.value > 50): print 'mySignal out of bounds' createTrigger(mySignal, myTriggerFunction)
Tip: to run the example, first copy and paste the above code into HANtune's console and press enter. Then type mySignal.value = 51
and press enter again to see the trigger in action.
The following example shows how to react to value changes of another signal. The trigger function will be called whenever 'canSignal' changes value and will set the value of 'mySignal' accordingly.
mySignal = createSignal('MySignal') canSignal = getSignal('CanSignal') def myTriggerFunction(): mySignal.value = canSignal.value * 2 createTrigger(canSignal, myTriggerFunction)
Note: currently HANtune only offers the ability to trigger on value changes of a signal or parameter.