Loading...
Searching...
No Matches
pulseData.py
1"""! @example pulseData.py """
2
3import sys
4from PySide6.QtWidgets import QApplication
5from SquidstatPyLibrary import AisDeviceTracker
6from SquidstatPyLibrary import AisCompRange
7from SquidstatPyLibrary import AisDCData
8from SquidstatPyLibrary import AisACData
9from SquidstatPyLibrary import AisExperimentNode
10from SquidstatPyLibrary import AisErrorCode
11from SquidstatPyLibrary import AisExperiment
12from SquidstatPyLibrary import AisInstrumentHandler
13from SquidstatPyLibrary import AisCyclicVoltammetryElement
14from SquidstatPyLibrary import AisDiffPulseVoltammetryElement
15from SquidstatPyLibrary import AisDataManipulator
16from SquidstatPyLibrary import AisPulseType
17
18# do you want headers in your file?
19write_header = True
20# instantiate the data manipulator
21data_manipulator = AisDataManipulator()
22# setup COM port
23COMPORT = "COM5"
24CHANNEL = 0
25
26def create_logic(handler):
27 def on_active_dc_data_ready(channel, data):
28 # define global parameters
29 global write_header, data_manipulator
30
31 # convert time to UTC
32 # utc_time = handler.getExperimentUTCStartTime(0)
33 # read data via data manipulator
34 data_manipulator.loadPrimaryData(data)
35
36 # upon completed pulse, write data with header
37 if data_manipulator.isPulseCompleted():
38
39 # if write_header = true
40 # prints header for every pulse data point
41 if write_header:
42 print("time, Pulse_current, base_current, diff_current, base_voltage, pulse_voltage")
43 write_header = False
44
45 # will print data as defined by the header print statement above
46 print(f"{data.timestamp}, {data_manipulator.getPulseCurrent()}, {data_manipulator.getBaseCurrent()}, "
47 f"{data_manipulator.getPulseCurrent() - data_manipulator.getBaseCurrent()}, "
48 f"{data_manipulator.getBaseVoltage()}, {data_manipulator.getPulseVoltage()}")
49
50 # function for printing ac data when it is received
51 def on_active_ac_data_ready(channel, data):
52 print(f"channel: {channel}, frequency: {data.frequency}, absoluteImpedance: {data.absoluteImpedance}, "
53 f"phaseAngle: {data.phaseAngle}, timestamp: {data.timestamp}")
54
55 # function for printing a new node when it begins
56 def on_experiment_new_element_starting(channel, data):
57 print(f"New Node beginning {data.stepName}, step number {data.stepNumber}, step sub: {data.substepNumber}")
58
59 # fucntion for printing an experiment has stopped
60 def on_experiment_stopped(channel, reason):
61 print(f"Experiment has completed on channel {channel}, {reason}")
62 QApplication.quit()
63
64 # function to print an experiment has paused
65 def on_experiment_paused(channel):
66 print(f"Experiment on channel {channel} has been paused")
67
68 # function to print an experiment has resumed
69 def on_experiment_resumed(channel):
70 print(f"Experiment resumed on channel {channel}")
71
72 # pass dc data to data manipulator function
73 handler.activeDCDataReady.connect(on_active_dc_data_ready)
74
75 # pass ac data to print function
76 handler.activeACDataReady.connect(on_active_ac_data_ready)
77
78 # pass new node starting to print function
79 handler.experimentNewElementStarting.connect(on_experiment_new_element_starting)
80
81 # pass experiment stopped to print function
82 handler.experimentStopped.connect(on_experiment_stopped)
83
84 # pass experiment puased to print function
85 handler.experimentPaused.connect(on_experiment_paused)
86 handler.experimentResumed.connect(on_experiment_resumed)
87
88# setup experiment
89def main():
90 # initialize the application
91 app = QApplication()
92
93 # get a device tracker
95
96 # Create the AisDiffPulseVoltammetryElement pulse experiment.
97 # startPotential (V), endPotential (V), potentialStep (V), pulseHeight (V), pulseWidth (s), pulsePeriod (s)
98 dpv_element = AisDiffPulseVoltammetryElement(-0.115, 0.115, 0.005, 0.01, 0.02, 0.2)
99
100 # set start voltage VS reference
101 dpv_element.setStartVoltageVsOCP(False)
102
103 # set end voltage VS reference
104 dpv_element.setEndVoltageVsOCP(False)
105
106 # set current range
107 # will range up, but will not range down
108 dpv_element.setApproxMaxCurrent(0.2)
109
110 # initialize an experiment
111 experiment = AisExperiment()
112
113 # append differential pulse element to the experiment list
114 # will run 1 time
115 success = experiment.appendElement(dpv_element, 1)
116 if not success:
117 print("Error building experiment")
118 sys.exit()
119
120 # Create an `AisDataManipulator` class for calculating advance parameters.
121 data_manipulator.setPulseType(AisPulseType.DifferentialPulse, dpv_element.getPulseWidth(), dpv_element.getPulsePeriod())
122
123 # if device is connected, get name and use logic function to handle events
124 def on_new_device_connected(device_name):
125
126 # get instrument handler using device name
127 handler = tracker.getInstrumentHandler(device_name)
128
129 # create the required connections for the handler.
130 create_logic(handler)
131
132 # uplaod experiment to device.
133 error = handler.uploadExperimentToChannel(CHANNEL, experiment)
134 if error.value() != AisErrorCode.ErrorCode.Success:
135 print(f"Error: {error.message()}")
136 app.quit()
137
138 # start experiment on device on defined channel
139 error = handler.startUploadedExperiment(CHANNEL)
140 if error.value() != AisErrorCode.ErrorCode.Success:
141 print(f"Error: {error.message()}")
142 app.quit()
143
144 # connect call back handler which is called on connection of device.
145 tracker.newDeviceConnected.connect(on_new_device_connected)
146
147 # print which device has disconnected to terminal
148 tracker.deviceDisconnected.connect(lambda device_name: print(f"{device_name} has been disconnected"))
149
150 # if error is encountered, print to terminal
151 error = tracker.connectToDeviceOnComPort(COMPORT)
152 if error.value() != AisErrorCode.ErrorCode.Success:
153 print(f"Error: {error.message()}")
154 sys.exit()
155
156 # exit application
157 sys.exit(app.exec())
158
159# run main
160if __name__ == "__main__":
161 main()
This class offers advanced control over pulse data collection and manipulation. It provides methods t...
Definition AisDataManipulator.h:21
static AisDeviceTracker * Instance()
get the instance of the device tracker.
In this experiment, the working electrode holds at a starting potential during the quiet time....
Definition AisDiffPulseVoltammetryElement.h:26
this class is used to create custom experiments. A custom experiment contains one or more elements....
Definition AisExperiment.h:22