Loading...
Searching...
No Matches
Running an Experiment

The following example demonstrates how to set up and run a basic experiment using the API. This example simply outlines the steps needed to connect to a Squidstat, build an experiment, and start it. Notably, it does not cover handling and reporting data from the Squidstat, which will be addressed in the Example on handling signals.

Setup

To begin, we need to include all neccessary headers or libraries, and setup the Qt event loop. At this stage, you can also retrieve the AisDeviceTracker Instance.

  • C++
    #include "AisDeviceTracker.h"
    #include "AisExperiment.h"
    #include "AisInstrumentHandler.h"
    #include "experiments/builder_elements/AisConstantPotElement.h"
    #include <QCoreApplication>
    #include <QDebug>
    // Define relevant device information, for easy access
    #define COMPORT "COM1"
    #define CHANNEL 0
    int main()
    {
    char** test = nullptr;
    int args;
    QCoreApplication a(args, test);
    auto tracker = AisDeviceTracker::Instance();
  • Python
    import sys
    from PySide6.QtWidgets import QApplication
    from SquidstatPyLibrary import AisDeviceTracker
    from SquidstatPyLibrary import AisExperiment, AisErrorCode
    from SquidstatPyLibrary import AisInstrumentHandler
    from SquidstatPyLibrary import AisConstantPotElement
    # Define relavant device information, for easy access
    COMPORT = "COM16"
    CHANNEL = 0
    app = QApplication()

Reference: https://doc.qt.io/qt-5/qcoreapplication.html

Building the experiment

Next, create an element (e.g. AisConstantPotElement) and pass in the required parameters. Then, we will create the experiment object AisExperiment and add the element to it using AisExperiment::appendElement.

  • C++
    // Voltage = 1V, Sampling Interval = 1s, Duration = 30s
    AisConstantPotElement cvElement(1, 1, 30);
    auto customExperiment = std::make_shared<AisExperiment>();
    // Append the constant potential element, and tell it to execute that element 1 time
    customExperiment->appendElement(cvElement, 1);
  • Python
    cvElement = AisConstantPotElement(1, 1, 30)
    # After this point, the experiment is empty, so we need to add some elements to it
    experiment = AisExperiment()
    # Append the constant potential element, and tell the experiment to execute that element 1 time
    success = experiment.appendElement(cvElement, 1)
    # Check if the element was added successfully
    if not success:
    print("Error adding element to experiment")
    app.quit()

Reference: https://en.cppreference.com/w/cpp/memory/shared_ptr

Connecting to the Device

Connect to the instrument by calling AisDeviceTracker::connectToDeviceOnComPort with the desired COM port to link the AisDeviceTracker to the device Alternatively, use AisDeviceTracker::connectAllPluggedInDevices to connect to all available devices.

  • C++
    AisErrorCode error = tracker->connectToDeviceOnComPort(COMPORT);
    if (error != error.Success) {
    qDebug() << error.message();
    return 0;
    }
  • Python
    error = tracker.connectToDeviceOnComPort(COMPORT)
    # Check if connection was successful
    if error.value() != AisErrorCode.Success:
    print(error.message())
    sys.exit()

Note: Any command to the instrument should always be checked for errors via the returned AisErrorCode

Starting Experiment

Finally, to start the experiment:

  1. Grab the instrument handler from the device tracker after establishing a connection to the instrument.
    • See Next Example for more details on establishing connections
  2. Upload the experiment to the desired channel and check for errors
  3. Start the experiment, again checking for any errors
  4. Run the QApplication event loop to allow all relevant signals to be processed (see the next example "Handling Signals")
  • C++
    auto& handler = tracker->getInstrumentHandler(deviceName);
    connectSignals(handler);
    AisErrorCode error = handler.uploadExperimentToChannel(CHANNEL, customExperiment);
    if (error) {
    qDebug() << error.message();
    return;
    }
    // Start the previously uploaded experiment on the same channel
    error = handler.startUploadedExperiment(CHANNEL);
    // Exit the application if there is any error starting the experiment
    if (error) {
    qDebug() << error.message();
    return;
    }
  • Python
    handler = tracker.getInstrumentHandler(deviceName)
    connectSignals(handler)
    error = handler.uploadExperimentToChannel(CHANNEL, experiment)
    # Exit the application if there is any error uploading experiment
    if error.value() != AisErrorCode.Success:
    print(error.message())
    app.quit()
    error = handler.startUploadedExperiment(CHANNEL)
    # Exit the application if there is any error starting experiment
    if error.value() != AisErrorCode.Success:
    print(error.message())
    app.quit()

See the full example here

Now that we have covered the basics of connecting to a Squidstat, building an experiment, and starting it, we can proceed to the next example. It demonstrates how to combine these steps and handle the signals emitted by the Squidstat.