GalileoFlowSDK


NameGalileoFlowSDK JSON
Version 0.1.6 PyPI version JSON
download
home_pageNone
SummaryReading a flow rate from Galileo Flow Sensor
upload_time2024-07-25 09:51:11
maintainerNone
docs_urlNone
authorGalileo (MIC)
requires_pythonNone
licenseNone
keywords python galileo
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Galileo-Flow SDK is a class based interface to easily communicate with the Galileo Flow Sensor. It contains essential modules to read the flow rate and do other necessary operations. 

**Setting up the GALILEO Sensor**

First, insert the cartridge to the base and power on the sensor using type-C USB connector. Make sure that all three flags on the screen are green.

![Galileo Cartridge](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/Galileo_cartridge_insert.png)

Next, check the COMPORT number of the sensor. In Windows, you can open the Device Manager and find the COMPORT under Ports list:

![Comport number](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/Comport_number.png)

**Example 1. Reading the flow rate**

One can use the code snippet below to read the flow rate every second. In your script, instead of 'COM18',
do not forget to write the actual comport value.

```python
from GalileoFlowSDK import GalileoFlowSDK
import time

galileo_sensor = GalileoFlowSDK("COM18")
while(1):
    time.sleep(1)
    print('flow is ' , galileo_sensor.read_flow())
```

**Example 2. Reading the liquid type and changing it**

The code snippet below allows to check the liquid type in the Galileo Sensor
```python
from GalileoFlowSDK import GalileoFlowSDK

galileo_sensor = GalileoFlowSDK("COM18")
print('liquid is ' , galileo_sensor.read_liquid())
```

To change the liquid, one can use update_liquid method. The argument ot this method is a number which cooresponds to
a liquid type:
-  0: water, 
-  1:ip, 
-  2:dmem, 
-  3:ethanol

For example, in the code below, we set ethanol as a liquid type:
```python
from GalileoFlowSDK import GalileoFlowSDK
import time

galileo_sensor = GalileoFlowSDK("COM18")
galileo_sensor.update_liquid(3)
time.sleep(1)
print('liquid is ' , galileo_sensor.read_liquid())
galileo_sensor.disconnect()
```

**Example 3. Reading the serial number of the Cartridge**

Every cartridge has its own unique serial number. This number is useful when utilizing several Galileo
Sensors to easily distinguish sensors from each other. In the interface, there is a function to read the serial number:

```python
from GalileoFlowSDK import GalileoFlowSDK
import time
galileo_sensor = GalileoFlowSDK("COM18")
time.sleep(1)
print('Serial number of the cartridge is ' , galileo_sensor.read_serial_number())
galileo_sensor.disconnect()
```

**Example 4. Updating the firmware of the base**

The firmware of your base will need to be updated to access to the latest version of Galileo. For that, you will need to upload a binary file called GALILEO_PROJECT.bin (for now, it is sent directly by us when you need an update). Then, you should connect a base with a cartridge, and run the method update_firmware. 

Here is an example code to update the firmware and check which firmware version you have. Don't forget to change the path of your binary file to the right location, and to select the right comport.

````python
from GalileoFlowSDK import GalileoFlowSDK
import time

galileo_sensor = GalileoFlowSDK("COM6") #Change to the right comport

while(1):
    command = int(input("command number (1: update firmware, 2: read firmware version) "))
    if command == 1:
        galileo_sensor.update_firmware("C:/Users/Anatole/Downloads/GALILEO_PROJECT.bin")
    elif command == 2:
        print("firmware version: "+galileo_sensor.read_firmware_version())
    else:
        break
````

**Example 5. Setting a feedback loop for flow control using Galileo flow sensor and OB1 pressure controller**

This tutorial has been initially written using Windows 10, 64bits, Python 3.8.8 in October, 2021, and modified using Windows 10, 64bits, Python 3.9 in July, 2024.
It has been tested using an OB1 mk3+ and a Galileo flow sensor with a range of 0.5 to 20uL/min.

**0. Galileo flow sensor addition**

Create your main example script and start by importing the GalileoFlowSDK package.

```python
from GalileoFlowSDK import GalileoFlowSDK
import time
```

**1. Elveflow SDK Environment Setup**

Make sure you have read all the documentation related to your Elveflow products. Disregarding that information could increase the risk of damage to the equipment, or the risk of personal injuries. In case you don't have a copy, you can download all the documents from Elveflow website.

The content of this chapter of the tutorial is the following:

- Installation
- Instrument configuration
- Instrument calibration

**1.1 Installation**

The Elveflow Software Development Kit (SDK) latest stable version can be downloaded from the Elveflow website. To alleviate bandwidth and access issues, two links for the same file are provided. You can also find the ESI in the same zip archive file. In case you previously installed the ESI software, a zip folder will also have been added in the same folder of ESI. Unzip it at an easily accessible path as for now, it is necessary to hardcode some paths on the library to customize it for your own system.

Then, go into: '../SDK_V3_08_06/DLL/Python/Python_64' (at the path where you unzipped the SDK), or your equivalent for 32bits, open the file 'Elveflow64.py' with a text editor, edit the fifth line of the code to match the path of your SDK and save your file.

```python
# This python routine load the ElveflowDLL.
# It defines all function prototype for use with python lib

from ctypes import *
ElveflowDLL=CDLL('C:/Users/Anatole/ESI_V3_08_06/SDK_V3_08_06/DLL/DLL64/Elveflow64.dll')# <- change this path
```

From now on, all the pieces of python scripts written below should be added to the main example script in which you previously imported the GalileoFlowSDK package.

In your main example script, change the paths of the following code cell to match yours and run it. If everything is fine, it should not print any message on the screen.

```python
# tested with Python 3.9 (Pycharm 2023.1.2)
# add python_xx and python_xx/DLL to the project path
# coding: utf8

import sys
from email.header import UTF8

sys.path.append('C:/Users/Anatole/ESI_V3_08_06/SDK_V3_08_06/DLL/DLL64')#add the path of the library here
sys.path.append('C:/Users/Anatole/ESI_V3_08_06/SDK_V3_08_06/DLL/Python/Python_64')#add the path of the LoadElveflow.py

from ctypes import *
from array import array

import matplotlib.pyplot as plt

from Elveflow64 import *
```

Did you get some outputs? Maybe something is wrong.

In some computers with 64bits, it is necessary to install some additional libraries. To do it, just go again into your SDK folder, find the path '../SDK_V3_08_06/Extra Install For x64 Libraries' and run the setup application that you will find inside. Then, try to launch again the previous code cell.

**1.2 Instrument configuration**

Once the SDK has been correctly installed, it is also necessary to configure it to work with the desired system. Depending on the version of your OB1, you will need to know either the OB1 name, or the comport your OB1 is connected to.

1.2.1 Finding OB1 name

If you previously installed the ESI, an application called NI MAX should have been installed as well. It is needed to check the exact name of the device.

How? First, connect your device to the power supply, connect its USB cable to the computer you are using and turn it on. After that, go to Windows search bar and type 'NI MAX' without the quotes.

Then, just unzip the left tabs: 'My System > Devices and Interfaces'.

If you don't have more National Instrument's devices connected to your computer, the only result should be the OB1 Pressure Controller (or the one that you try to configure). Click on it and write down its Serial Number. You will need it for your custom software to interact with the device. In this example (in french here), the device Serial Number is 01E5A2A7.

![NIMax](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/NIMax.png)

1.2.2 Finding OB1 comport

The comport number can be found in the Device Manager.

1.2.3 Pressure regulators encoding

The next step is to know the arrangement of pressure regulators that your system has. In order to identify them, you can check it on the OB1 User guide, at the installation chapter.

Each regulator has a different encoding value (between 0 and 5). The correspondance is the following:

- Non-installed : 0
- (0, 200) mbar	: 1
- (0, 2000) mbar : 2
- (0, 8000) mbar : 3
- (-1000, 1000) mbar : 4
- (-1000, 6000) mbar : 5

At max, the OB1 controller will have four pressure outputs, thus, in the OB1_Initialization() call of the following code cell, after the serial number, you must specify the regulator type of every channel, setting a zero if a channel is not physically installed. In the following code, four pressure outputs are installed with regulators of type 1 and 2.
```python
# Initialization of OB1

Instr_ID = c_int32()
print("Instrument name and regulator types are hardcoded in the Python script")

# See User Guide to determine regulator types and NIMAX to determine the instrument name
error = OB1_Initialization('01E5A2A7'.encode('ascii'),1,2,2,2,byref(Instr_ID))

# All functions will return error codes to help you to debug your code, for further information refer to User Guide
print('error:%d' % error)
print("OB1 ID: %d" % Instr_ID.value)
```

If everything went fine, you should have received a simple message as follows:

```
Instrument name and regulator types are hardcoded in the Python script
error:0
OB1 ID: 0
```

If it is not the case, one or several of the following issues can be happening:

- The OB1 is not connected to the power supply or to the computer through the USB wire.
- The OB1 is not on. The screen should emit blue light.
- The Serial Number or Com Port of the device was not correctly written.
- There are several instances trying to reach the OB1. Close any ESI window running and be sure of using only one python script (of OB1 interfacing code) at a time.
- If you installed extra libraries, try restarting your computer.
- The USB wire may need to be unplugged and plugged or changed to another USB port.

**1.3 Instrument calibration**

Here you can choose, essentially, one of the three available options: default, load and new. The calibration consists of an array which stores the actual values associated to your instrument.

If you previously proceeded with a calibration process, you can load it from the Calib_path that you must previously specify. Otherwise, if you want to run a calibration process, ensure that ALL channels are properly closed with adequate caps. You can find more detailed instructions about the calibration procedure at the OB1 User Guide.

````python
Calib = (c_double*1000)() # Always define array this way, calibration should have 1000 elements

while True:
    answer = input('select calibration type (default, load, new ) : ')
    Calib_path = 'C:\\Users\\Public\\Desktop\\Calibration\\Calib.txt'
    if answer == 'default':
        error = Elveflow_Calibration_Default (byref(Calib),1000)
        break

    if answer == 'load':
        error = Elveflow_Calibration_Load (Calib_path.encode('ascii'), byref(Calib), 1000)
        break

    if answer == 'new':
        OB1_Calib (Instr_ID.value, Calib, 1000)
        error = Elveflow_Calibration_Save(Calib_path.encode('ascii'), byref(Calib), 1000)
        print('Calib saved in %s' % Calib_path.encode('ascii'))
        break
````

The expected output would be something like:

````
select calibration type (default, load, new ) : default
````

Here finishes the first part of this tutorial. The next part continues the script to implement the automatic PI control.

**2. Automatic PI Control**

In this part of the tutorial, the basic process for setting up an automatic control is shown. It is assumed that you have correctly set up the SDK environments as it was shown in chapters 0 and 1 of the tutorial. If you didn't see it before, please, go for it first.

The content of this chapter of the tutorial is the following:

- PI basics
- PI software
- Parameters tuning

**2.1 PI basics**

The PI Controller or Proportional-Integrative Controller is a basic linear controller, yet powerful for lot of situations, which makes the output of the controlled system to follow a desired curve of values, called reference, and ensures that the system properly works and that it is stable for the operating expected situation.

To achieve this control of the system's output, it takes the current system's output value, calculates the difference to the reference (the desired output), which is called error, and makes some operations with it.

The operations for a basic PI Control are two: the proportional term calculus and the integrative term calculus.

The proportional term corresponds to the current error multiplied by a proportional gain $K_p$. The larger this gain is, the more aggressive the control will react. For an instant k:

$$P_{term} = K_p(y_k-y_{ref})$$

The integrative term corresponds to the accumulated error multiplied by an integrative gain $K_i$. The larger this gain is, the faster the control will react to the changes between the real output and the desired one. For an instant k:

$$I_{term} = K_i \sum_{j=0}^{k} (y_j-y_{ref})$$

Note. Usually, the integrative gain is expressed as integrative time which is related to the integrative gain through the sampling period of the controlling system, but it is shown here this way as may be considered more understandable with gains.

**2.2 PI software**

In the following code, there are two functions which run the control over the system.

If your microfluidic circuit is not very hydraulically resistive, you may need to add some flow resistance to it in order to make use of the whole ranges of your flow sensor and pressure regulators.

````python
flow_list = []
error_list = []
control_list = []
p_error = 0
i_error = 0
meas_flow = c_double()

def pid_run():
    global p_error
    global i_error
    start_t = time.time()  # <- This must be close to the routine
    last_t = start_t
    # Main routine
    while True:
        # Get the current output
        flow_list.append(galileo_flow.read_flow())

        # Calculate the mathematical error
        p_error = ref_flow - galileo_flow.read_flow()
        i_error += p_error
        i_error = min(i_error, i_max)
        # PI Controller equations
        P_val = K_p * p_error
        I_val = K_i * i_error
        p_control = P_val + I_val

        p_control = max(p_min, min(p_max, p_control))  # Safety saturation

        error_list.append(p_error)
        control_list.append(p_control)

        p_control = c_double(float(p_control))  # Convert to c_double
        error = OB1_Set_Press(Instr_ID.value, pchannel, p_control, byref(Calib), 1000)  # Return error message.value, p_channel, p_control, byref(Calib), 1000)  # Return error message

        # Check if the elapsed time match the time limit
        if (time.time() - start_t) > experiment_t:
            break
        # Wait until desired period time
        sleep_t = period - (time.time() - last_t)
        if sleep_t > 0:
            time.sleep(sleep_t)
        last_t = time.time()  # And update the last time

    # Turn off the pressure
    p_control = 0.0
    error = OB1_Set_Press(Instr_ID.value, pchannel, p_control, byref(Calib), 1000)  # Return error message.value, p_channel, p_control, byref(Calib), 1000)  # Return error message


def plot():
    # Plot the signals
    plt.rcParams['axes.grid'] = True
    fig = plt.figure()
    fig.suptitle("Microfluidic Circuit Control at {0:.2f}Hz".format(1 / period))

    plt.subplot(2, 1, 1)
    plt.plot(flow_list)
    plt.ylabel('flow [uL/min]')
    plt.subplot(2, 1, 2)
    plt.plot(control_list)
    plt.ylabel('pressure [mbar]')

    plt.show()

def run():
    pid_run()
    plot()
````

**2.3 Parameters tuning**

Each microfluidic system needs a different PI Controller tuning, thus, the results you will have may be different to the ones obtained with our testing setup.

Change the following values to tune the controller for your system. You can also change the reference value and the experiment execution time by some values more useful for your application. If the system follows your reference too slowly, you may increase the proportional and/or the integrative gain. If it is too aggressive, the contrary. Feel free to play with them by changing the values in small steps.

**Important**: check that the control maximum and minimum values are good for your system.

````python
galileo_flow = GalileoFlowSDK("COM4") #Change this to the correct com port

# Controller parameters:

period = 0.1 # 10Hz

K_p = 50 # <- Change this value to tune the controller
K_i = 5 # <- Change this value too to tune the controller
ref_flow = 5 # uL/min

p_min = 0 # <- This is only negative if you have some vacuum source
p_max = 2000 # <- This depends on the regulator of each channel
i_max = p_max / (K_i)

# OB1 arrangement
pchannel = 2 # <- Change this to your real configuration

experiment_t = 10.0 # Seconds
flow_list = []
error_list = []
control_list = []

run()
````
![PIResult](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/PIDcontrol_ESISDK_Galileo_230724.png)

**Conclusion**

In this tutorial we have seen how a simple PI Control works, how can it be implemented with the Galileo flow sensor and the OB1 instrument and how to tune its parameters.

Hope you found this tutorial useful !



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "GalileoFlowSDK",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "python, galileo",
    "author": "Galileo (MIC)",
    "author_email": "<galileo@microfluidic.fr>",
    "download_url": null,
    "platform": null,
    "description": "Galileo-Flow SDK is a class based interface to easily communicate with the Galileo Flow Sensor. It contains essential modules to read the flow rate and do other necessary operations. \r\n\r\n**Setting up the GALILEO Sensor**\r\n\r\nFirst, insert the cartridge to the base and power on the sensor using type-C USB connector. Make sure that all three flags on the screen are green.\r\n\r\n![Galileo Cartridge](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/Galileo_cartridge_insert.png)\r\n\r\nNext, check the COMPORT number of the sensor. In Windows, you can open the Device Manager and find the COMPORT under Ports list:\r\n\r\n![Comport number](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/Comport_number.png)\r\n\r\n**Example 1. Reading the flow rate**\r\n\r\nOne can use the code snippet below to read the flow rate every second. In your script, instead of 'COM18',\r\ndo not forget to write the actual comport value.\r\n\r\n```python\r\nfrom GalileoFlowSDK import GalileoFlowSDK\r\nimport time\r\n\r\ngalileo_sensor = GalileoFlowSDK(\"COM18\")\r\nwhile(1):\r\n    time.sleep(1)\r\n    print('flow is ' , galileo_sensor.read_flow())\r\n```\r\n\r\n**Example 2. Reading the liquid type and changing it**\r\n\r\nThe code snippet below allows to check the liquid type in the Galileo Sensor\r\n```python\r\nfrom GalileoFlowSDK import GalileoFlowSDK\r\n\r\ngalileo_sensor = GalileoFlowSDK(\"COM18\")\r\nprint('liquid is ' , galileo_sensor.read_liquid())\r\n```\r\n\r\nTo change the liquid, one can use update_liquid method. The argument ot this method is a number which cooresponds to\r\na liquid type:\r\n-  0: water, \r\n-  1:ip, \r\n-  2:dmem, \r\n-  3:ethanol\r\n\r\nFor example, in the code below, we set ethanol as a liquid type:\r\n```python\r\nfrom GalileoFlowSDK import GalileoFlowSDK\r\nimport time\r\n\r\ngalileo_sensor = GalileoFlowSDK(\"COM18\")\r\ngalileo_sensor.update_liquid(3)\r\ntime.sleep(1)\r\nprint('liquid is ' , galileo_sensor.read_liquid())\r\ngalileo_sensor.disconnect()\r\n```\r\n\r\n**Example 3. Reading the serial number of the Cartridge**\r\n\r\nEvery cartridge has its own unique serial number. This number is useful when utilizing several Galileo\r\nSensors to easily distinguish sensors from each other. In the interface, there is a function to read the serial number:\r\n\r\n```python\r\nfrom GalileoFlowSDK import GalileoFlowSDK\r\nimport time\r\ngalileo_sensor = GalileoFlowSDK(\"COM18\")\r\ntime.sleep(1)\r\nprint('Serial number of the cartridge is ' , galileo_sensor.read_serial_number())\r\ngalileo_sensor.disconnect()\r\n```\r\n\r\n**Example 4. Updating the firmware of the base**\r\n\r\nThe firmware of your base will need to be updated to access to the latest version of Galileo. For that, you will need to upload a binary file called GALILEO_PROJECT.bin (for now, it is sent directly by us when you need an update). Then, you should connect a base with a cartridge, and run the method update_firmware. \r\n\r\nHere is an example code to update the firmware and check which firmware version you have. Don't forget to change the path of your binary file to the right location, and to select the right comport.\r\n\r\n````python\r\nfrom GalileoFlowSDK import GalileoFlowSDK\r\nimport time\r\n\r\ngalileo_sensor = GalileoFlowSDK(\"COM6\") #Change to the right comport\r\n\r\nwhile(1):\r\n    command = int(input(\"command number (1: update firmware, 2: read firmware version) \"))\r\n    if command == 1:\r\n        galileo_sensor.update_firmware(\"C:/Users/Anatole/Downloads/GALILEO_PROJECT.bin\")\r\n    elif command == 2:\r\n        print(\"firmware version: \"+galileo_sensor.read_firmware_version())\r\n    else:\r\n        break\r\n````\r\n\r\n**Example 5. Setting a feedback loop for flow control using Galileo flow sensor and OB1 pressure controller**\r\n\r\nThis tutorial has been initially written using Windows 10, 64bits, Python 3.8.8 in October, 2021, and modified using Windows 10, 64bits, Python 3.9 in July, 2024.\r\nIt has been tested using an OB1 mk3+ and a Galileo flow sensor with a range of 0.5 to 20uL/min.\r\n\r\n**0. Galileo flow sensor addition**\r\n\r\nCreate your main example script and start by importing the GalileoFlowSDK package.\r\n\r\n```python\r\nfrom GalileoFlowSDK import GalileoFlowSDK\r\nimport time\r\n```\r\n\r\n**1. Elveflow SDK Environment Setup**\r\n\r\nMake sure you have read all the documentation related to your Elveflow products. Disregarding that information could increase the risk of damage to the equipment, or the risk of personal injuries. In case you don't have a copy, you can download all the documents from Elveflow website.\r\n\r\nThe content of this chapter of the tutorial is the following:\r\n\r\n- Installation\r\n- Instrument configuration\r\n- Instrument calibration\r\n\r\n**1.1 Installation**\r\n\r\nThe Elveflow Software Development Kit (SDK) latest stable version can be downloaded from the Elveflow website. To alleviate bandwidth and access issues, two links for the same file are provided. You can also find the ESI in the same zip archive file. In case you previously installed the ESI software, a zip folder will also have been added in the same folder of ESI. Unzip it at an easily accessible path as for now, it is necessary to hardcode some paths on the library to customize it for your own system.\r\n\r\nThen, go into: '../SDK_V3_08_06/DLL/Python/Python_64' (at the path where you unzipped the SDK), or your equivalent for 32bits, open the file 'Elveflow64.py' with a text editor, edit the fifth line of the code to match the path of your SDK and save your file.\r\n\r\n```python\r\n# This python routine load the ElveflowDLL.\r\n# It defines all function prototype for use with python lib\r\n\r\nfrom ctypes import *\r\nElveflowDLL=CDLL('C:/Users/Anatole/ESI_V3_08_06/SDK_V3_08_06/DLL/DLL64/Elveflow64.dll')# <- change this path\r\n```\r\n\r\nFrom now on, all the pieces of python scripts written below should be added to the main example script in which you previously imported the GalileoFlowSDK package.\r\n\r\nIn your main example script, change the paths of the following code cell to match yours and run it. If everything is fine, it should not print any message on the screen.\r\n\r\n```python\r\n# tested with Python 3.9 (Pycharm 2023.1.2)\r\n# add python_xx and python_xx/DLL to the project path\r\n# coding: utf8\r\n\r\nimport sys\r\nfrom email.header import UTF8\r\n\r\nsys.path.append('C:/Users/Anatole/ESI_V3_08_06/SDK_V3_08_06/DLL/DLL64')#add the path of the library here\r\nsys.path.append('C:/Users/Anatole/ESI_V3_08_06/SDK_V3_08_06/DLL/Python/Python_64')#add the path of the LoadElveflow.py\r\n\r\nfrom ctypes import *\r\nfrom array import array\r\n\r\nimport matplotlib.pyplot as plt\r\n\r\nfrom Elveflow64 import *\r\n```\r\n\r\nDid you get some outputs? Maybe something is wrong.\r\n\r\nIn some computers with 64bits, it is necessary to install some additional libraries. To do it, just go again into your SDK folder, find the path '../SDK_V3_08_06/Extra Install For x64 Libraries' and run the setup application that you will find inside. Then, try to launch again the previous code cell.\r\n\r\n**1.2 Instrument configuration**\r\n\r\nOnce the SDK has been correctly installed, it is also necessary to configure it to work with the desired system. Depending on the version of your OB1, you will need to know either the OB1 name, or the comport your OB1 is connected to.\r\n\r\n1.2.1 Finding OB1 name\r\n\r\nIf you previously installed the ESI, an application called NI MAX should have been installed as well. It is needed to check the exact name of the device.\r\n\r\nHow? First, connect your device to the power supply, connect its USB cable to the computer you are using and turn it on. After that, go to Windows search bar and type 'NI MAX' without the quotes.\r\n\r\nThen, just unzip the left tabs: 'My System > Devices and Interfaces'.\r\n\r\nIf you don't have more National Instrument's devices connected to your computer, the only result should be the OB1 Pressure Controller (or the one that you try to configure). Click on it and write down its Serial Number. You will need it for your custom software to interact with the device. In this example (in french here), the device Serial Number is 01E5A2A7.\r\n\r\n![NIMax](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/NIMax.png)\r\n\r\n1.2.2 Finding OB1 comport\r\n\r\nThe comport number can be found in the Device Manager.\r\n\r\n1.2.3 Pressure regulators encoding\r\n\r\nThe next step is to know the arrangement of pressure regulators that your system has. In order to identify them, you can check it on the OB1 User guide, at the installation chapter.\r\n\r\nEach regulator has a different encoding value (between 0 and 5). The correspondance is the following:\r\n\r\n- Non-installed : 0\r\n- (0, 200) mbar\t: 1\r\n- (0, 2000) mbar : 2\r\n- (0, 8000) mbar : 3\r\n- (-1000, 1000) mbar : 4\r\n- (-1000, 6000) mbar : 5\r\n\r\nAt max, the OB1 controller will have four pressure outputs, thus, in the OB1_Initialization() call of the following code cell, after the serial number, you must specify the regulator type of every channel, setting a zero if a channel is not physically installed. In the following code, four pressure outputs are installed with regulators of type 1 and 2.\r\n```python\r\n# Initialization of OB1\r\n\r\nInstr_ID = c_int32()\r\nprint(\"Instrument name and regulator types are hardcoded in the Python script\")\r\n\r\n# See User Guide to determine regulator types and NIMAX to determine the instrument name\r\nerror = OB1_Initialization('01E5A2A7'.encode('ascii'),1,2,2,2,byref(Instr_ID))\r\n\r\n# All functions will return error codes to help you to debug your code, for further information refer to User Guide\r\nprint('error:%d' % error)\r\nprint(\"OB1 ID: %d\" % Instr_ID.value)\r\n```\r\n\r\nIf everything went fine, you should have received a simple message as follows:\r\n\r\n```\r\nInstrument name and regulator types are hardcoded in the Python script\r\nerror:0\r\nOB1 ID: 0\r\n```\r\n\r\nIf it is not the case, one or several of the following issues can be happening:\r\n\r\n- The OB1 is not connected to the power supply or to the computer through the USB wire.\r\n- The OB1 is not on. The screen should emit blue light.\r\n- The Serial Number or Com Port of the device was not correctly written.\r\n- There are several instances trying to reach the OB1. Close any ESI window running and be sure of using only one python script (of OB1 interfacing code) at a time.\r\n- If you installed extra libraries, try restarting your computer.\r\n- The USB wire may need to be unplugged and plugged or changed to another USB port.\r\n\r\n**1.3 Instrument calibration**\r\n\r\nHere you can choose, essentially, one of the three available options: default, load and new. The calibration consists of an array which stores the actual values associated to your instrument.\r\n\r\nIf you previously proceeded with a calibration process, you can load it from the Calib_path that you must previously specify. Otherwise, if you want to run a calibration process, ensure that ALL channels are properly closed with adequate caps. You can find more detailed instructions about the calibration procedure at the OB1 User Guide.\r\n\r\n````python\r\nCalib = (c_double*1000)() # Always define array this way, calibration should have 1000 elements\r\n\r\nwhile True:\r\n    answer = input('select calibration type (default, load, new ) : ')\r\n    Calib_path = 'C:\\\\Users\\\\Public\\\\Desktop\\\\Calibration\\\\Calib.txt'\r\n    if answer == 'default':\r\n        error = Elveflow_Calibration_Default (byref(Calib),1000)\r\n        break\r\n\r\n    if answer == 'load':\r\n        error = Elveflow_Calibration_Load (Calib_path.encode('ascii'), byref(Calib), 1000)\r\n        break\r\n\r\n    if answer == 'new':\r\n        OB1_Calib (Instr_ID.value, Calib, 1000)\r\n        error = Elveflow_Calibration_Save(Calib_path.encode('ascii'), byref(Calib), 1000)\r\n        print('Calib saved in %s' % Calib_path.encode('ascii'))\r\n        break\r\n````\r\n\r\nThe expected output would be something like:\r\n\r\n````\r\nselect calibration type (default, load, new ) : default\r\n````\r\n\r\nHere finishes the first part of this tutorial. The next part continues the script to implement the automatic PI control.\r\n\r\n**2. Automatic PI Control**\r\n\r\nIn this part of the tutorial, the basic process for setting up an automatic control is shown. It is assumed that you have correctly set up the SDK environments as it was shown in chapters 0 and 1 of the tutorial. If you didn't see it before, please, go for it first.\r\n\r\nThe content of this chapter of the tutorial is the following:\r\n\r\n- PI basics\r\n- PI software\r\n- Parameters tuning\r\n\r\n**2.1 PI basics**\r\n\r\nThe PI Controller or Proportional-Integrative Controller is a basic linear controller, yet powerful for lot of situations, which makes the output of the controlled system to follow a desired curve of values, called reference, and ensures that the system properly works and that it is stable for the operating expected situation.\r\n\r\nTo achieve this control of the system's output, it takes the current system's output value, calculates the difference to the reference (the desired output), which is called error, and makes some operations with it.\r\n\r\nThe operations for a basic PI Control are two: the proportional term calculus and the integrative term calculus.\r\n\r\nThe proportional term corresponds to the current error multiplied by a proportional gain $K_p$. The larger this gain is, the more aggressive the control will react. For an instant k:\r\n\r\n$$P_{term} = K_p(y_k-y_{ref})$$\r\n\r\nThe integrative term corresponds to the accumulated error multiplied by an integrative gain $K_i$. The larger this gain is, the faster the control will react to the changes between the real output and the desired one. For an instant k:\r\n\r\n$$I_{term} = K_i \\sum_{j=0}^{k} (y_j-y_{ref})$$\r\n\r\nNote. Usually, the integrative gain is expressed as integrative time which is related to the integrative gain through the sampling period of the controlling system, but it is shown here this way as may be considered more understandable with gains.\r\n\r\n**2.2 PI software**\r\n\r\nIn the following code, there are two functions which run the control over the system.\r\n\r\nIf your microfluidic circuit is not very hydraulically resistive, you may need to add some flow resistance to it in order to make use of the whole ranges of your flow sensor and pressure regulators.\r\n\r\n````python\r\nflow_list = []\r\nerror_list = []\r\ncontrol_list = []\r\np_error = 0\r\ni_error = 0\r\nmeas_flow = c_double()\r\n\r\ndef pid_run():\r\n    global p_error\r\n    global i_error\r\n    start_t = time.time()  # <- This must be close to the routine\r\n    last_t = start_t\r\n    # Main routine\r\n    while True:\r\n        # Get the current output\r\n        flow_list.append(galileo_flow.read_flow())\r\n\r\n        # Calculate the mathematical error\r\n        p_error = ref_flow - galileo_flow.read_flow()\r\n        i_error += p_error\r\n        i_error = min(i_error, i_max)\r\n        # PI Controller equations\r\n        P_val = K_p * p_error\r\n        I_val = K_i * i_error\r\n        p_control = P_val + I_val\r\n\r\n        p_control = max(p_min, min(p_max, p_control))  # Safety saturation\r\n\r\n        error_list.append(p_error)\r\n        control_list.append(p_control)\r\n\r\n        p_control = c_double(float(p_control))  # Convert to c_double\r\n        error = OB1_Set_Press(Instr_ID.value, pchannel, p_control, byref(Calib), 1000)  # Return error message.value, p_channel, p_control, byref(Calib), 1000)  # Return error message\r\n\r\n        # Check if the elapsed time match the time limit\r\n        if (time.time() - start_t) > experiment_t:\r\n            break\r\n        # Wait until desired period time\r\n        sleep_t = period - (time.time() - last_t)\r\n        if sleep_t > 0:\r\n            time.sleep(sleep_t)\r\n        last_t = time.time()  # And update the last time\r\n\r\n    # Turn off the pressure\r\n    p_control = 0.0\r\n    error = OB1_Set_Press(Instr_ID.value, pchannel, p_control, byref(Calib), 1000)  # Return error message.value, p_channel, p_control, byref(Calib), 1000)  # Return error message\r\n\r\n\r\ndef plot():\r\n    # Plot the signals\r\n    plt.rcParams['axes.grid'] = True\r\n    fig = plt.figure()\r\n    fig.suptitle(\"Microfluidic Circuit Control at {0:.2f}Hz\".format(1 / period))\r\n\r\n    plt.subplot(2, 1, 1)\r\n    plt.plot(flow_list)\r\n    plt.ylabel('flow [uL/min]')\r\n    plt.subplot(2, 1, 2)\r\n    plt.plot(control_list)\r\n    plt.ylabel('pressure [mbar]')\r\n\r\n    plt.show()\r\n\r\ndef run():\r\n    pid_run()\r\n    plot()\r\n````\r\n\r\n**2.3 Parameters tuning**\r\n\r\nEach microfluidic system needs a different PI Controller tuning, thus, the results you will have may be different to the ones obtained with our testing setup.\r\n\r\nChange the following values to tune the controller for your system. You can also change the reference value and the experiment execution time by some values more useful for your application. If the system follows your reference too slowly, you may increase the proportional and/or the integrative gain. If it is too aggressive, the contrary. Feel free to play with them by changing the values in small steps.\r\n\r\n**Important**: check that the control maximum and minimum values are good for your system.\r\n\r\n````python\r\ngalileo_flow = GalileoFlowSDK(\"COM4\") #Change this to the correct com port\r\n\r\n# Controller parameters:\r\n\r\nperiod = 0.1 # 10Hz\r\n\r\nK_p = 50 # <- Change this value to tune the controller\r\nK_i = 5 # <- Change this value too to tune the controller\r\nref_flow = 5 # uL/min\r\n\r\np_min = 0 # <- This is only negative if you have some vacuum source\r\np_max = 2000 # <- This depends on the regulator of each channel\r\ni_max = p_max / (K_i)\r\n\r\n# OB1 arrangement\r\npchannel = 2 # <- Change this to your real configuration\r\n\r\nexperiment_t = 10.0 # Seconds\r\nflow_list = []\r\nerror_list = []\r\ncontrol_list = []\r\n\r\nrun()\r\n````\r\n![PIResult](https://raw.githubusercontent.com/galileo-microfluidics/Galileo-images/main/PIDcontrol_ESISDK_Galileo_230724.png)\r\n\r\n**Conclusion**\r\n\r\nIn this tutorial we have seen how a simple PI Control works, how can it be implemented with the Galileo flow sensor and the OB1 instrument and how to tune its parameters.\r\n\r\nHope you found this tutorial useful !\r\n\r\n\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Reading a flow rate from Galileo Flow Sensor",
    "version": "0.1.6",
    "project_urls": null,
    "split_keywords": [
        "python",
        " galileo"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cfbbc98319f1583ac0c4ef40e0bc1b1efd107ce9bdb545432b2472ddda0bb8e0",
                "md5": "a050118f7c029f6c3153d0fbb47a9270",
                "sha256": "2b0ac281db4af8eb72b9416d698a67fee740f6b4285837792ae1320c764e99c4"
            },
            "downloads": -1,
            "filename": "GalileoFlowSDK-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a050118f7c029f6c3153d0fbb47a9270",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 12955,
            "upload_time": "2024-07-25T09:51:11",
            "upload_time_iso_8601": "2024-07-25T09:51:11.355619Z",
            "url": "https://files.pythonhosted.org/packages/cf/bb/c98319f1583ac0c4ef40e0bc1b1efd107ce9bdb545432b2472ddda0bb8e0/GalileoFlowSDK-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-25 09:51:11",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "galileoflowsdk"
}
        
Elapsed time: 0.26338s