I'm struggling with the formatting of an IF statement in the Value Change script of a tag in Vision. If I comment out my "if" statements and make one of the gain paths fixed, the readBlocking and writeBlocking works fine and "ScriptValue" displays the correct value of my if conditions.
I'm sure this is something basic, Python isn't my strong suit.
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
value = currentValue
if value == 0:
gainpaths = ["[~]PGainTunedSlow","[~]IGainTunedSlow","[~]DGainTunedSlow"]
elif value == 1:
gainpaths = ["[~]PGainTunedMed","[~]IGainTunedMed","[~]DGainTunedMed"]
elif value == 2:
gainpaths = ["[~]PGainTunedFast","[~]IGainTunedFast","[~]DGainTunedFast"]
gainvalues = system.tag.readBlocking(gainpaths)
writevalue = [value,gainvalues[0],gainvalues[1],gainvalues[2]]
writepath = ["[~]Scriptvalue","[~]PGain","[~]IGain","[~]DGain"]
system.tag.writeBlocking(writepath,writevalue)
currentValue is a qualified value object, not the raw value of the tag. Use:
value = currentValue.value
Note that readBlocking() delivers a list of qualified values, not raw values, but the rest of your script works because writeBlocking() also accepts qualified values.
For what it's worth, I would write this script like this:
gainTypes = ['TunedSlow','TunedMed','TunedFast']
#limits currentValue between 0 and the length of gainTypes.
#Other logic can be used.
value = max(0, min(currentValue.value,len(gainTypes)-1)
gainPaths = ['[~]' + gain + gainTypes[value] for gain in ('PGain','IGain','DGain']
writeValues = [value] + [qv.value for qv in system.tag.readBlocking(gainPaths)]
system.tag.writeBlocking(['[~]ScriptValue','[~]PGain','[~]IGain','[~]DGain'],writeValues)