-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrigol.py
More file actions
102 lines (86 loc) · 3.55 KB
/
Copy pathrigol.py
File metadata and controls
102 lines (86 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
@author M.Rog
@date 19 November 2018
Program takes a snapshot from a RIGOL 1000E series oscilloscope using visa.
Includes option to save the snapshot to hard drive.
Dependencies: Visa"""
import numpy as np
import random
import time
import sys
import os
import pyvisa as visa
class RigolOscilloscope(object):
"""Controls the connection with Rigol, allowing this object to take snapshots from the Rigol's screen"""
def __init__(self):
"""Establishes connection"""
rm = visa.ResourceManager()
self.rigol = rm.open_resource(rm.list_resources()[0])
# Define some constants:
self.zeroVoltPixel = 124.5 # Acquired by reading out a 0 V DC Signal
self.verticalDivs = 9 # 8 on the screen, half divs above and below
self.horizontalDivs = 12 # all on the screen
# Display constants:
self.smallestPixel = 11
self.biggestPixel = 236
self.timePixels = 600
def captureChannel1(self):
return self.takeSnapshot(ch1=True)
def flipsignal(self, intdata):
"""Flips the signal so that it corresponds to the RIGOL display. Also convert to numpy"""
# Flip the signal in intdata around the zero-volt pixel
flippedSignal = []
for i in intdata:
flippedSignal.append(self.zeroVoltPixel - (i - self.zeroVoltPixel))
flippedSignal = np.array(flippedSignal)
return flippedSignal
def readscales(self, chan):
"""Calibrates the axes of the RIGOL"""
self.voltPerDiv = np.float64(self.rigol.query(":CHAN" + str(chan) + ":SCAL?"))
self.timePerDiv = np.float64(self.rigol.query(":TIM:SCAL? CHAN" + str(chan)))
self.voltOffset = np.float64(self.rigol.query(":CHAN" + str(chan) + ":OFFS?"))
def rawsignal(self, chan):
"""Get unchanged data from the screen, throw away junk and return this data"""
self.rigol.write(":WAVeform:DATA? CHAN" + str(chan)) #command to request data
data = self.rigol.read_raw() # read data (read raw reads bytes)
intdata = np.array(list(data))[10:]
return intdata
def takeSnapshot(self, ch1=True, ch2=False):
"""Takes snapshot from the Rigol"""
# Channel 1:
if ch1:
self.readscales(1) # Calibrate the axes
rawsignal = self.rawsignal(1)
flippedSignal = self.flipsignal(rawsignal)
# Now scale the data
# Find volts per pixel;
totalAmountOfPixels = self.biggestPixel-self.smallestPixel
totalAmountOfVolts = self.verticalDivs * self.voltPerDiv
voltsPerPixel = totalAmountOfVolts / totalAmountOfPixels
# Find the offsetted zero pixel
offsetZeroVoltPixel = self.zeroVoltPixel + self.voltOffset / voltsPerPixel
# Now scale the time axis
timePerPixel = (self.timePerDiv*self.horizontalDivs)/self.timePixels
timeAxis = np.arange(0, self.timePixels, 1)*timePerPixel
voltSignalCH1 = (flippedSignal - offsetZeroVoltPixel)*voltsPerPixel
if ch2:
self.readscales(2) # Calibrate the axes
rawsignal = self.rawsignal(2)
flippedSignal = self.flipsignal(rawsignal)
# Now scale the data
# Find volts per pixel;
totalAmountOfPixels = self.biggestPixel-self.smallestPixel
totalAmountOfVolts = self.verticalDivs * self.voltPerDiv
voltsPerPixel = totalAmountOfVolts / totalAmountOfPixels
# Find the offsetted zero pixel
offsetZeroVoltPixel = self.zeroVoltPixel + self.voltOffset / voltsPerPixel
# Now scale the time axis
timePerPixel = (self.timePerDiv*self.horizontalDivs)/self.timePixels
timeAxis = np.arange(0, self.timePixels, 1)*timePerPixel
voltSignalCH2 = (flippedSignal - offsetZeroVoltPixel)*voltsPerPixel
if ch1 and not ch2:
return timeAxis, voltSignalCH1
if not ch1 and ch2:
return timeAxis, voltSignalCH2
if ch1 and ch2:
return timeAxis, voltSignalCH1, voltSignalCH2