Menu

NetworkViewer:ScriptEngine

Back to the NetworkViewer Page

ScriptEngine Plugin Home Page

Author(s)

  • Dominic Létourneau

Version

  • 1.0, Alpha

Code

General Information

Qt 4.7 as a javascript engine called QScriptEngine that is well integrated with Qt. QScriptEngine allows an easy interface with C++ objects and offers great flexibility. Using javascript, we can then interact with the variables on the CAN bus. Function generators, variable conversions and complex logic can then be implemented in javascript for testing before the complete implementation in the embedded microcontroller. Test scripts can also be created to interact with multiple modules at once. The complete documentation of the QScriptEngine is available on the Qt/Nokia web site.

Specialized javascript functions and variables are available on the main context for easy access to NetworkModule, NetworkVariable C++ objects. Next sections describe the built-in variables and functions available.

Variables

TABLE 1 - ScriptEngine - Variables

Variable Description

time
Elapsed time since we started the script (seconds)

period
The selected period for the script execution. 0 = single shot.

cycle
The cycle counter [0..N]

modules[id]
Map of CAN modules/nodes on the bus. It is a NetworkModule (QObject). Each module has the following functions :

  • Q_INVOKABLE int getNumVariable();
  • Q_INVOKABLE ModuleVariable* getVariable(int index);
  • id is the module ID, not an array index
  • You can also access modules like this :
  • Variables can be accessed with their names :

modules[id].variables[j]
Variable array.It is a mapping of javascript objects and ModuleVariable (QObject). Each variable has the following functions :

  • Q_INVOKABLE void setUserValue(QVariant value);
  • Q_INVOKABLE QVariant getValue() const;
  • Q_INVOKABLE VARIABLE_TYPE getType() const;
  • Q_INVOKABLE VARIABLE_MEMORY_TYPE getMemType() const;
  • Q_INVOKABLE QString getName() const;
  • Q_INVOKABLE unsigned int getOffset() const;
  • Q_INVOKABLE QString getDescription() const;
  • Q_INVOKABLE unsigned long getVersion() const;
  • Q_INVOKABLE int getDeviceID() const;
  • Q_INVOKABLE int getLength() const;

Built-in Functions

TABLE 2 - ScriptEngine - Functions.

Function Description

print(...);
Redirection to the graphical console.

* Ex : print("Hellow World!");

addToPlot(module_id,variable_id);
Add a variable to the scope.

  • module_id = id of the module
  • variable_id = index of the variable
  • Ex : addToPlot(1,"SetPoint");

  • createPseudoModule(module_id);

  • addPseudoModule(module_id);
    Virtual module creation. module_id must be > 255 to avoid confusion with physical modules;

    • Ex : createPseudoModule(5000);

addScriptVariable(module_id,"Variable Name", "Variable Description");
add a variable to a module, can be a physical or pseudo module.

  • Ex: addScriptVariable(5000,"Sinus","Sinus value");

stop();
Stop the execution of the script (within the script).

  • Ex: stop();

(BETA) addSliderControl(module_id,variable_id,min_value,max_value);
create a slider to control a variable with min and max value.

  • Ex: addSliderControl(500,0,0,100);

Script Examples

Sinus Generator

if (cycle == 0)
{
    addPseudoModule(500);
    addScriptVariable(500,"Sinus1","Sinus1 value");
    addToPlot(500,modules[500].Sinus1.index);

    addScriptVariable(500,"Sinus2","Sinus2 value");
    addToPlot(500,modules[500].Sinus2.index);


    addScriptVariable(500,"Sinus3","Sinus3 value");
    addToPlot(500,modules[500].Sinus3.index);

    freq = 0.1;
    amplitude = 100;
}

SinValue1 = amplitude * Math.sin(2 * Math.PI * freq * time);
SinValue2 = 2 * amplitude * Math.sin(4 * Math.PI * freq * time);
SinValue3 = 3 * amplitude * Math.sin(6 * Math.PI * freq * time);


modules[500].Sinus1.setUserValue(SinValue1);
modules[500].Sinus2.setUserValue(SinValue2);
modules[500].Sinus3.setUserValue(SinValue3);
true;

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.