def main(device): ''' Demonstrates detection of changes on lines configured for hardware-timed digital input. ''' def di_callback(names, data): print('{} samples from {}'.format(data.shape, names)) def change_callback(name, change, event_time): print('{} edge on {} at {}'.format(change, name, event_time)) engine = Engine() engine.hw_ai_monitor_period = 1 engine.configure_hw_di(100, '/{}/port0/line0:1'.format(device), names=['poke', 'spout'], clock='/Dev1/Ctr0') engine.register_di_callback(di_callback) engine.register_di_change_callback(change_callback, debounce=10) engine.start() raw_input('Demo running. Hit enter to exit.\n')
def main(device): ''' This demonstrates the basic Engine interface that we will be using to communicate with the DAQ hardware. A subclass of the Engine will implement NI hardware-specific logic. Another subclass will implement MCC (TBSI)-specific logic. This enables us to write code that does not care whether it's communicating with a NI or MCC device. ''' def ao_callback(names, offset, samples): print('{} samples needed at {} for {}'.format(samples, offset, names)) engine.write_hw_ao(np.zeros(samples)) def ai_callback(names, data): print('{} samples acquired from {}'.format(data.shape[-1], names)) def et_callback(change, line, event_time): print('{} edge on {} at {}'.format(change, line, event_time)) queue.put((event_time-100, 100)) def ai_epoch_callback(names, start, duration, data): print('Epoch {} acquired with {} samples from {}' \ .format(start, data.shape, names)) queue = Queue.Queue() engine = Engine() # Set the polling interval to a high value to minimize clutter on the scren. engine.hw_ao_monitor_period = 5 engine.hw_ai_monitor_period = 5 engine.configure_hw_ai(1e3, '/{}/ai0:2'.format(device), (-10, 10), sync_ao=False) engine.configure_hw_ao(1e3, '/{}/ao0'.format(device), (-10, 10)) engine.configure_et('/{}/port0/line0:7'.format(device), 'ao/SampleClock') engine.register_ao_callback(ao_callback) engine.register_ai_callback(ai_callback) engine.register_et_callback(et_callback) engine.register_ai_epoch_callback(ai_epoch_callback, queue) queue.put((0, 100)) queue.put((15, 100)) queue.put((55, 100)) engine.start() raw_input('Demo running. Hit enter to exit.\n')
def main(device): ''' Demonstrates the use of software-timed analog outputs and digital outputs. Software-timed outputs change the state (or value) of an output to the new level as soon as requested by the program. A common use-case for software-timed digital outputs is to generate a TTL (i.e., a square pulse) to trigger something (e.g., a water pump). I have included convenience functions (`fire_sw_do`) to facilitate this. I also have included name aliases. NI-DAQmx refers to the lines using the arcane syntax (e.g., 'Dev1/ao0' or 'Dev1/port0/line0'). However, it is probably easier (and generates more readable programs), if we can assign aliases to these lines. For example, if 'Dev1/ao0' is connected to a Coulbourn shock controller which uses the analog signal to control the intensity of the shock, we may want to call that output 'shock_level'. Likewise, if 'Dev1/port0/line1' is connected to the trigger port of a pellet dispenser, we probably want to call that line 'food_dispense'. ''' engine = Engine() # Configure four digital outputs and give them the aliases 'a', 'b', 'c', # and 'd'. engine.configure_sw_do('{}/port0/line0:3'.format(device), ['a', 'b', 'c', 'd'], initial_state=[1, 0, 1, 1]) # Configure two analog otuputs and give them aliases (i.e., assume ao0 # controls shock level and ao1 controls pump rate). engine.configure_sw_ao('{}/ao0:1'.format(device), (-10, 10), ['shock_level', 'pump_rate']) time.sleep(1) # You can connect ao0 to ai0 and use the analog input panel on the MAX test # panels to observe the changes to the analog output. engine.set_sw_ao('shock_level', 5) engine.set_sw_ao('pump_rate', 4) # You can connect the digital lines for port0 to port1 and then monitor the # changes on port1 using the digital panel on the MAX test panels. time.sleep(1) engine.write_sw_do([0, 0, 0, 0]) engine.start() engine.set_sw_do('a', 1) time.sleep(0.25) engine.set_sw_do('b', 1) time.sleep(0.25) engine.set_sw_do('c', 1) time.sleep(0.25) engine.set_sw_do('a', 0) time.sleep(0.25) engine.set_sw_do('b', 0) time.sleep(0.25) engine.set_sw_do('c', 0) time.sleep(0.25) # Generate a TTL pulse of varying duration (in sec). engine.fire_sw_do('a', 0.25) engine.fire_sw_do('b', 0.5) engine.fire_sw_do('c', 1) time.sleep(1)
def main(device): ''' This demonstrates the basic Engine interface that we will be using to communicate with the DAQ hardware. A subclass of the Engine will implement NI hardware-specific logic. Another subclass will implement MCC (TBSI)-specific logic. This enables us to write code that does not care whether it's communicating with a NI or MCC device. ''' def ao_callback(names, offset, samples): print('{} samples needed at {} for {}'.format(samples, offset, names)) engine.write_hw_ao(np.zeros(samples)) def ai_callback(names, data): print('{} samples acquired from {}'.format(data.shape[-1], names)) def et_callback(change, line, event_time): print('{} edge on {} at {}'.format(change, line, event_time)) queue.put((event_time - 100, 100)) def ai_epoch_callback(names, start, duration, data): print('Epoch {} acquired with {} samples from {}' \ .format(start, data.shape, names)) queue = Queue.Queue() engine = Engine() # Set the polling interval to a high value to minimize clutter on the scren. engine.hw_ao_monitor_period = 5 engine.hw_ai_monitor_period = 5 engine.configure_hw_ai(1e3, '/{}/ai0:2'.format(device), (-10, 10), sync_ao=False) engine.configure_hw_ao(1e3, '/{}/ao0'.format(device), (-10, 10)) engine.configure_et('/{}/port0/line0:7'.format(device), 'ao/SampleClock') engine.register_ao_callback(ao_callback) engine.register_ai_callback(ai_callback) engine.register_et_callback(et_callback) engine.register_ai_epoch_callback(ai_epoch_callback, queue) queue.put((0, 100)) queue.put((15, 100)) queue.put((55, 100)) engine.start() raw_input('Demo running. Hit enter to exit.\n')