-See (1)
conda install
pySerial in (2)――The automation method is shown by taking the automation of manual operation in my environment as an example.
There is a package called pandas that can be used in Python and is suitable for table operations like Excel. We will present an example of "Graphing the automatically acquired voltage value" using this and a package called matplotlib that can easily and neatly draw a graph.
Suppose that when a current is flowing in a wide range in space in one direction, the current is measured using a mat smaller than the space. When the mate is gradually crossed perpendicular to the current and the current is recorded step by step, the "movement control" and "current measurement" are controlled from pyserial. Sigma Kouki SHOT102 is used for movement control, and ADVANTEST's R6441 series is used for current measurement. I will.
Cell1
import serial
import time
import pandas as pd
import matplotlib.pyplot as plt
serial
uses pyserial, time
creates the interval time for serial communication, pandas
uses dataframe, and matplotlib.pyplot
is used for drawing graphs.
Cell2
##Constant specification
MAX = 42000
COMpulse = "COM9"
COMampere = "COM10"
bitRate = 9600
Specify the constant used in pyserial
.
Cell3
##Variable initialization
pulse = 0
ampere_list = []
pulse_list = []
ampere_average_list =[]
Initialize the variables to use.
##Move to the back origin(Initialization)
ser = serial.Serial(COMpulse, bitRate, timeout=0.1)
ser.write(b"H:2-\r\n")
time.sleep(0.1)
print(ser.read_all())
ser.close()
Move to the operation check and measurement origin.
Cell5
pulse
takes a value of 0-42000 and moves 3 μm per pulse. (Actually it moves 6 μm per 2 pulses, but the command seems to be written like this). The current value at each position is sampled 5 times and averaged to the value at that point. The average number of times is arbitrary. After moving the optical table to the end, attach the list of current and position values as a dataframe, convert the pulse value to the actual length, and draw with matplotlib. It has become a flow. (By the way, I put the dataframe in csv.)
##Start measurement
while 1:
if pulse >= MAX:
##End the while statement when the position reaches MAX
break
##Record current location information
pulse_list.append(pulse/2) #For some reason, pulse is specified by 2 and commanded, so it is divided by 2.
##Measure the current(Take 5 times and average it as the value at that position)
for i in range(5):
ser = serial.Serial(COMampere,bitRate,timeout=0.1)
time.sleep(0.5)
ser.write(b"MD?\r\n")
time.sleep(0.5)
tmp = ser.read_all()
ampere = float(tmp.split()[1])
ampere_average_list.append(ampere)
ser.close()
##Current and pulse(position)To list
ampere_list.append(sum(ampere_average_list)/len(ampere_average_list))
ampere_average_list = [] #Initialize list
##Move the optical table
pulse += 1000
position = "A:2+P"+str(pulse)+"\r\n" #Create a command by combining strings
ser = serial.Serial(COMpulse,bitRate,timeout=0.1)
ser.write(bytes(position, 'UTF-8'))
time.sleep(0.5)
ser.write(b"G:\r\n")
ser.close()
##Turn the list into a dataframe
print(ampere_list)
print(pulse_list)
df = pd.DataFrame({'ampere(A)':ampere_list,'pulse':pulse_list})
##Change pulse value to length
def pulseToMilliMeter(pulse):
return pulse*0.006
df["position(mm)"] = df["pulse"].map(pulseToMilliMeter) #You can do things like apply batch functions in Excel
df.to_csv('./csv/hoge/result.csv',index=False)
##Draw a graph using matplotlib
plt.figure()
df.plot(x='position(mm)',y='ampere(A)')
plt.savefig('./img/sample.png')
plt.close('all')
Suppose that the efficiency has been improved. In fact, all you have to do is press Enter, which makes it much simpler and more reproducible. If there are other sources that can be made more efficient, I will write more.
Recommended Posts