/ examples / analog_output.py
analog_output.py
 1  """
 2  	Example: AnalogOutput
 3  
 4  	This example shows how to write to analog output modules.
 5  	Analog ouputs generate a voltage or current signal.
 6      Ranges can be between 0-10V, 4-20mA, etc.
 7  	This example works with all P1000 Series:
 8  	 - Analog ouput modules such as P1-04DAL-1, P1-04DAL-2,etc.
 9  	 - Analog combo modules such as P1-4ADL2DAL-1, P1-4ADL2DAL-2 etc.
10  
11  	This example has an option for an interactive mode where you can select a channel
12  	and value as well as a automatic mode where channel 2 automatically increments.
13  	 _____  _____
14  	|  P  ||  S  |
15  	|  1  ||  L  |
16  	|  A  ||  O  |
17  	|  M  ||  T  |
18  	|  -  ||     |
19  	|  2  ||  0  |
20  	|  0  ||  1  |
21  	|  0  ||     |
22  	 ¯¯¯¯¯  ¯¯¯¯¯
23   To confirm the analog output value you can use either a P1 analog input module or a multimeter.
24  
25   Written by FACTS Engineering
26   Copyright (c) 2023 FACTS Engineering, LLC
27   Licensed under the MIT license.
28  
29  """
30  
31  import time
32  import P1AM
33  
34  # Use a channel's "real" property instead of its "value" to use real-life units instead of counts
35  # The "real" property converts the value you enter to the next closest count
36  
37  def interactive():
38      """Select a channel and a value to write to"""
39      sel = int(input("Select a channel: "))
40      channel = module[sel] 
41      # channel = module.outputs[sel] # on combos select outputs with module.outputs
42      
43  	# the range_bottom and range_top properties are in mA or V depending on module type
44      target = float(input(f"Enter a value between {channel.range_bottom}-{channel.range_top} units: "))
45      channel.real = target
46      print(channel.real) # print units
47      print(channel.value) # print counts
48  
49  def automatic():
50      """Automatically increment channel 2"""    
51      if ch2.real <= ch2.range_top - 1:
52          ch2.real = round(ch2.real + 1.0) # round to account for count error
53  
54      else:
55          ch2.real = ch2.range_bottom
56      print(ch2.real) # print units
57      print(ch2.value) # print counts
58      time.sleep(1)
59  
60  base = P1AM.Base() # Intializes base. Returns the base object.
61  module = base[1] # module object for slot 1
62  ch2 = module[2] # channel object for slot 1 channel 2
63  # ch2 = module.outputs[2] # on combos select outputs with module.outputs
64  
65  while True:
66      automatic() # Automatically increment channel 2
67      # interactive() # Select a channel and value. Requires a serial terminal program.
68