Waveform Generator

If a device provides waveform generator functionality, such as the NV200 amplifier, you can use the WaveformGenerator class to access this functionality. For example the NV200 provides an arbitrary waveform generator that can be used to generate a variety of waveforms. The arbitrary waveform generator can generate a single or repetitive setpoint signal. The curve shape can be freely defined by up to 1024 samples.

The following example demonstrates how to use the nv200.waveform_generator module with the NV200Device from the nv200.nv200_device. It covers setting up the WaveformGenerator, generating a sine wave, and starting the waveform generator.

import asyncio
from nv200.nv200_device import NV200Device
from nv200.waveform_generator import WaveformGenerator, WaveformType, WaveformUnit
from nv200.connection_utils import connect_to_single_device

async def waveform_generator_test():
   # Connect to first device
   device = await connect_to_single_device(NV200Device)

   # Initialize the waveform generator with the NV200 device
   waveform_generator = WaveformGenerator(device)

   # Generate a sine wave with a frequency of 1 Hz, low level of 0, and high level of 80 µm
   sine = waveform_generator.generate_waveform(WaveformType.SINE, freq_hz=1, low_level=0, high_level=80)
   print(f"Sample factor {sine.sample_factor}")

   # Transfer the waveform data to the device - the waveform is given in position units
   await waveform_generator.set_waveform(sine, WaveformUnit.POSITION)

   # Start the waveform generator with 1 cycle and starting index of 0
   await waveform_generator.start(cycles=1, start_index=0)

   # Wait until the waveform generator finishes the move
   await waveform_generator.wait_until_finished()

   # Close the device client connection
   await device.close()

if __name__ == "__main__":
   asyncio.run(waveform_generator_test())

Step by step guide to using the Waveform Generator

This guide will walk you through the steps to set up and use the waveform generator using the given example code.

Step 1: Import Necessary Modules

To get started, you’ll need to import the relevant modules. The WaveformGenerator class is imported from the nv200.waveform_generator module, along with other necessary components such as NV200Device and TelnetProtocol.

import asyncio
from nv200.nv200_device import NV200Device
from nv200.waveform_generator import WaveformGenerator, WaveformType, WaveformUnit
from nv200.connection_utils import connect_to_single_device

Step 2: Create the NV200Device

To interact with the NV200 device, you must create a NV200Device instance. This client communicates with the device using the TelnetProtocol, which requires the device’s MAC address for connection.

# Connect to first device
device = await connect_to_single_device(NV200Device)

Step 3: Initialize the Waveform Generator

After setting up the device client, initialize the WaveformGenerator with the device instance. This allows you to interact with the waveform generation functionality.

# Initialize the waveform generator with the NV200 device
 waveform_generator = WaveformGenerator(device)

Step 4: Generate the Waveform

In this example, we will generate a sine wave using the generate_waveform method of the WaveformGenerator class with the waveform type specified as WaveformType.SINE. You can specify the frequency, low level (minimum value), and high level (maximum value) of the sine wave. Optionally, you can also apply a phase shift to the waveform.

# Generate a sine wave with a frequency of 1 Hz, low level of 0, and high level of 80 µm
sine = waveform_generator.generate_waveform(WaveformType.SINE, freq_hz=1, low_level=0, high_level=80)
print(f"Sample factor {sine.sample_factor}")

Step 5: Transfer the Waveform to the Device

Once the sine wave has been generated, you need to transfer the waveform data to the device. The set_waveform method is used for this purpose, which takes in the generated waveform data and uploads it to the connected device.

# Transfer the waveform data to the device
await waveform_generator.set_waveform(sine)

Important

Transferring the waveform data to the device may take some seconds, depending on the size of the waveform. Ensure that you wait for the transfer to complete before proceeding with any further operations.

Step 6: Start the Wavform Generator

With the waveform data uploaded to the device, you can now start the waveform generator. You can specify the number of cycles for the waveform to repeat and the starting index of the waveform data. In this example, we are starting the generator with one cycle, using an index of 0.

# Start the waveform generator with 1 cycle and starting index of 0
await waveform_generator.start(cycles=1, start_index=0)

Step 7: Wait for the Waveform to Finish

After starting the waveform generator, you can wait for the waveform to finish its cycle. The WaveformGenerator.wait_until_finished method can be used to wait until the waveform generator has completed its operation. It waits until the is_running function returns false.

# Wait until the waveform generator finishes the move
await waveform_generator.wait_until_finished()

Step 8: Close the Device Connection

Once the waveform has finished executing, it is good practice to close the connection to the device to free up resources.

# Close the device client connection
await device.close()

API Reference

You will find a detailed description of the API methods and their usage in the API Reference.