Arduino to Modbus, HELP!

I need help with a project I am working on, I am very new to Ignition and python, We have a gauge that needs to be aligned with respect to a string, the gauge outputs its position with respect to the string as center(Y-axis) and power(X-axis). the center(Y-axis) is pretty straight forward, it returns a positive value if the gauge is too far back and a negative value I the gauge is too far forward. unfortunately for the power(X-axis) the gauge only returns positive values as the output signal is a square function.
I am having trouble with two things, one is how to get values from an expression tag into an OPC tag, I am using a SQL query to pull averages of the power(x-axis) and center(y-axis) values from the local database and I want to save it into the OPC tag I created to send it to the Arduino.

2nd problem.
I am having trouble writing the logic to flip the direction in power(x-axis) if the power values get bigger over time. I wanted to write something that will create like a moving average and compare it to the current value and maybe then move the gauge and then waits to check if the power(x-value) got smaller, else it flips the direction of the motor and moves the gauge the other way.

for my setup, I am using an Arduino UNO connected to ignition through Modbus. and the Arduino is connected to two stepper motors

any help would be much appreciated

(forgive my terrible code)

def tensiongaugemovement():
	        import time
			import threading
            import system
            import sys
			
			
			# adding Tag paths
			averagepowervalue = "Tension Interface/Status/Average Tension Gauge Power Reading"
			averagecentervalue = "Tension Interface/Status/Average Tension Gauge Fiber Center"
			center_gauge = "Tension_Arduino/Center_Gauge" # this is the center value going to the arduino
			power_gauge = "Tension_Arduino/Power_Gauge"	  # this is the power value going to the arduino 
			center_potentiometer = "Tension_Arduino/Center Potentiometer" # potentiometer value coming from the center
			power_potentiometer = "Tension_Arduino/Power Potentiometer"	# potentiometer value coming from the power
			center = "Tension_Arduino/Center" # current center value, need to pull this data from the local database, average of last 5 mins
			power = "Tension_Arduino/Power"   # same for power
			linespeed = "Line Heater Controllers/Linespeed 1"    #pull in current linespeed
			
			
			
			if (linespeed > 10):
							
					center_gauge = center 		# send center values to arduino		

					for counter in range(5, 0 , -1):
							average = [long1, long2, long3, long5, long5]
							if (x < len(average)):
									average[x] = averagepowervalue
									x += 1
							else: 
								power_average = sum(average)/len(average)
								power_gauge = power_average
								x = 0
								counter = 5
							time.sleep(1000)		
					
					

					
					if(power_average - power < 0):
							power_gauge = -power_gauge;                     #flip direction of the motor 
					
					else
					# do nothing

Thank you

Gijo

Hey, You will need to call a system.tag.readAll

tags = ["Tags/T1", "Tags/T2", "Tags/T3", "Tags/T4", "Tags/T5"]
values = system.tag.readAll(tags)

Similarly a system.tag.writeAll() to write data

tags = ["Tags/T1", "Tags/T2", "Tags/T3", "Tags/T4", "Tags/T5"]
values = [2, 4, 8, 16, 32]
values = system.tag.writeAll(tags,values)

Not too sure what the Long1, Long2 etc are doing. But I will encourage that this code run on a gateway event to ensure that is runs all the time and not on a client/session level

Hello Miyanalu,

Thank you for taking the time to help me out.

will system.tag.writeAll() be able to write each tag with five values? I wanted to take a long term average of the power value and then make the motors move. so like how can I make the script wait for the next updated reading?

Thank you

Gijo

call system.tag.readBlocking(). This can be seen in this manual link.

https://docs.inductiveautomation.com/display/DOC80/system.tag.readBlocking

No problem