Here we show how to extend the previous example (see ExampleApp.h and FactoryInstance.cc) by application modules coded in Python. We will turn off the C++-coded SetpointRamp
module by configuration and add a replacement written in Python.
Currently, it is still necessary to define at least the main Application as C++ class, so it is not possible to write ApplicationCore servers only in Python. If that is the goal, consider using the GenericDeviceServer [https://github.com/ChimeraTK/GenericDeviceServer] as starting point to add your Python code as configuration files.
Changed/added files
config to load python modules
Note the <module name="PythonModules"> section below
<configuration>
<module name="Configuration">
<variable name="enableSetpointRamping" type="boolean" value="false"/>
<variable name="heaterMode" type="uint8" value="2"/>
</module>
<module name="PythonModules">
<module name="UserAppModules">
<variable name="path" type="string" value="userAppModules" />
</module>
</module>
<module name="Information">
<variable name="ovenName" type="string" value="Cookie Oven 42"/>
</module>
<module name="Timer">
<variable name="period" type="uint32" value="500"/>
</module>
<module name="Application">
<variable name="configPatchVersion" type="int32" value="67"/>
</module>
</configuration>
python code
Save the Python code as userAppModes.py and put it next to the config file.
import sys
import os
import os.path
import traceback
import PyApplicationCore as ac
def __init__(self, owner):
super().__init__(owner, "SetpointRamp", "Slow ramping of temperator setpoint")
self.operatorSetpoint = ac.ScalarPollInput(ac.DataType.float32, self, "operatorSetpoint", "degC", "description")
self.ctrl = ac.VariableGroup(self, "/ControlUnit/Controller", "")
self.ctrl.actualSetpoint = ac.ScalarOutput(ac.DataType.float32, self.ctrl, "temperatureSetpoint", "degC", "...")
self.trigger = ac.ScalarPushInput(ac.DataType.uint64, self, "/Timer/tick", "", "...")
maxStep = 1
while True:
self.ctrl.actualSetpoint.set( self.ctrl.actualSetpoint + max( min(self.operatorSetpoint - self.ctrl.actualSetpoint, maxStep), -maxStep) )
self.writeAll()
self.readAll()