@@ -31,43 +31,71 @@ def __init__(self, device: packet_handler.Handler = None):
31
31
self ._channel_one_map = "ID1"
32
32
self ._channel_two_map = "ID2"
33
33
34
- def get_frequency (self , channel : str , timeout : float = 1 ):
35
- tmp = self ._channel_one_map
36
- self ._channel_one_map = channel
37
- t = self .capture (1 , 2 , modes = ["sixteen rising" ], timeout = timeout )[0 ]
38
- self ._channel_one_map = tmp
39
- period = (t [1 ] - t [0 ]) * 1e-6 / 16
40
- frequency = period ** - 1
41
-
42
- if frequency >= 1e7 :
43
- frequency = self ._get_high_frequency (channel )
44
-
45
- return frequency
46
-
47
- def _get_high_frequency (self , channel : str ):
34
+ def get_frequency (
35
+ self , channel : str , simultaneous_oscilloscope : bool = False , timeout : float = 1
36
+ ) -> float :
37
+ """Measure the frequency of a signal.
38
+
39
+ Parameters
40
+ ----------
41
+ channel : {"ID1", "ID2", "ID3", "ID4"}
42
+ Name of the digital input channel in which to measure the frequency.
43
+ simultaneous_oscilloscope: bool, optional
44
+ Set this to True if you need to use the oscilloscope at the same time.
45
+ Uses firmware instead of software to measure the frequency, which may fail
46
+ and return 0. Will not give accurate results above 10 MHz. The default
47
+ value is False.
48
+ timeout : float, optional
49
+ Timeout in seconds before cancelling measurement. The default value is
50
+ 1 second.
51
+
52
+ Returns
53
+ -------
54
+ float
55
+ The signal's frequency in Hz.
48
56
"""
49
- retrieves the frequency of the signal connected to ID1. for frequencies > 1MHz
50
- also good for lower frequencies, but avoid using it since
51
- the oscilloscope cannot be used simultaneously due to hardware limitations.
52
-
53
- The input frequency is fed to a 32 bit counter for a period of 100mS.
54
- The value of the counter at the end of 100mS is used to calculate the frequency.
57
+ if simultaneous_oscilloscope :
58
+ return self ._get_frequency_firmware (channel , timeout )
59
+ else :
60
+ tmp = self ._channel_one_map
61
+ self ._channel_one_map = channel
62
+ t = self .capture (1 , 2 , modes = ["sixteen rising" ], timeout = timeout )[0 ]
63
+ self ._channel_one_map = tmp
64
+ period = (t [1 ] - t [0 ]) * 1e-6 / 16
65
+ frequency = period ** - 1
55
66
56
- see :ref:`freq_video`
67
+ if frequency >= 1e7 :
68
+ frequency = self ._get_high_frequency (channel )
57
69
70
+ return frequency
58
71
59
- .. seealso:: :func:`get_freq`
72
+ def _get_frequency_firmware (
73
+ self , channel : str , timeout : float , retry : bool = True
74
+ ) -> float :
75
+ self ._device .send_byte (CP .COMMON )
76
+ self ._device .send_byte (CP .GET_FREQUENCY )
77
+ self ._device .send_int (int (timeout * 64e6 ) >> 16 )
78
+ self ._device .send_byte (self ._channels [channel ].number )
79
+ self ._device .wait_for_data (timeout )
60
80
61
- .. tabularcolumns:: |p{3cm}|p{11cm}|
81
+ error = self ._device .get_byte ()
82
+ t = [self ._device .get_long () for a in range (2 )]
83
+ self ._device .get_ack ()
84
+ edges = 16
85
+ period = (t [1 ] - t [0 ]) / edges / CLOCK_RATE
62
86
63
- ============== ============================================================================================
64
- **Arguments**
65
- ============== ============================================================================================
66
- pin The input pin to measure frequency from : ['ID1','ID2','ID3','ID4','SEN','EXT','CNTR']
67
- ============== ============================================================================================
87
+ if error or period == 0 :
88
+ # Retry once.
89
+ if retry :
90
+ return self ._get_frequency_firmware (channel , timeout , False )
91
+ else :
92
+ return 0
93
+ else :
94
+ return period ** - 1
68
95
69
- :return: frequency
70
- """
96
+ def _get_high_frequency (self , channel : str ) -> float :
97
+ # The input frequency is fed to a 32 bit counter for a period of 100 ms. The
98
+ # value of the counter at the end of 100 ms is used to calculate the frequency.
71
99
self ._device .send_byte (CP .COMMON )
72
100
self ._device .send_byte (CP .GET_ALTERNATE_HIGH_FREQUENCY )
73
101
self ._device .send_byte (self ._channels [channel ].number )
0 commit comments