Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aerobs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Gattringer
aerobs
Commits
732d3361
Commit
732d3361
authored
9 months ago
by
Andreas Gattringer
Browse files
Options
Downloads
Patches
Plain Diff
adc/dma test v2: bug fixes
parent
9a9498a7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
programs/dmatest_v2.py
+29
-23
29 additions, 23 deletions
programs/dmatest_v2.py
with
29 additions
and
23 deletions
programs/dmatest_v2.py
+
29
−
23
View file @
732d3361
...
@@ -88,17 +88,19 @@ class AdcDevice:
...
@@ -88,17 +88,19 @@ class AdcDevice:
read_err
=
True
)
read_err
=
True
)
def
set_sampling_rate
(
self
,
rate
):
def
set_sampling_rate
(
self
,
rate
):
async
with
self
.
__lock
:
self
.
_rate
=
int
(
rate
)
self
.
_rate
=
rate
self
.
__adc_device
.
DIV_REG
=
(
48000000
//
self
.
_rate
-
1
)
<<
8
self
.
__adc_device
.
DIV_REG
=
(
48000000
//
self
.
_rate
-
1
)
<<
8
def
get_sampling_rate
(
self
):
def
get_sampling_rate
(
self
):
return
self
.
_rate
return
self
.
_rate
def
get_sample_count
(
self
):
return
self
.
_sample_count
def
set_sample_count
(
self
,
samples
):
def
set_sample_count
(
self
,
samples
):
async
with
self
.
__lock
:
self
.
_sample_count
=
samples
self
.
_sample_count
=
samples
self
.
_buffer
=
array
.
array
(
"
H
"
,
(
0
for
_
in
range
(
samples
)))
self
.
_buffer
=
array
.
array
(
"
H
"
,
(
0
for
_
in
range
(
samples
)))
def
__stop_adc
(
self
):
def
__stop_adc
(
self
):
dev
=
self
.
__adc_device
dev
=
self
.
__adc_device
...
@@ -183,7 +185,7 @@ def calculate_frequency(voltages_centered, sampling_rate):
...
@@ -183,7 +185,7 @@ def calculate_frequency(voltages_centered, sampling_rate):
consecutive_sign
=
0
consecutive_sign
=
0
counts
=
[]
counts
=
[]
old_sign
=
voltages_centered
>
0
old_sign
=
voltages_centered
[
0
]
>
0
for
v
in
voltages_centered
:
for
v
in
voltages_centered
:
sign
=
v
>
0
sign
=
v
>
0
if
sign
!=
old_sign
:
if
sign
!=
old_sign
:
...
@@ -193,10 +195,11 @@ def calculate_frequency(voltages_centered, sampling_rate):
...
@@ -193,10 +195,11 @@ def calculate_frequency(voltages_centered, sampling_rate):
old_sign
=
sign
old_sign
=
sign
consecutive_sign
+=
1
consecutive_sign
+=
1
if
len
(
counts
)
<
3
:
if
len
(
counts
)
<
5
:
return
SAMPLING_FREQ_TO_HIGH
return
SAMPLING_FREQ_TO_HIGH
counts
=
counts
[
1
:
-
1
]
counts
=
counts
[
3
:
-
1
]
#print(counts)
count_mean
=
sum
(
counts
)
/
len
(
counts
)
count_mean
=
sum
(
counts
)
/
len
(
counts
)
if
count_mean
<
5
:
if
count_mean
<
5
:
return
SAMPLING_FREQ_TO_LOW
return
SAMPLING_FREQ_TO_LOW
...
@@ -205,20 +208,20 @@ def calculate_frequency(voltages_centered, sampling_rate):
...
@@ -205,20 +208,20 @@ def calculate_frequency(voltages_centered, sampling_rate):
return
f
return
f
def
calculate_peak_to_peak
(
voltages_centered
):
def
calculate_peak_to_peak
(
voltages_centered
,
voltage_mean
):
positive_peaks
=
[]
positive_peaks
=
[]
negative_peaks
=
[]
negative_peaks
=
[]
maximum
=
-
9999
maximum
=
-
9999
minimum
=
9999
minimum
=
9999
start_idx
=
0
old_sign
=
voltages_centered
>
0
old_sign
=
voltages_centered
[
start_idx
]
>
0
for
v
in
voltages_centered
:
for
v
in
voltages_centered
[
start_idx
:]
:
sign
=
v
>
0
sign
=
v
>
0
if
sign
!=
old_sign
:
if
sign
!=
old_sign
:
if
maximum
>
-
9999
:
if
maximum
>
-
9999
:
positive_peaks
.
append
(
maximum
)
positive_peaks
.
append
(
maximum
+
voltage_mean
)
if
minimum
<
9999
:
if
minimum
<
9999
:
negative_peaks
.
append
(
minimum
)
negative_peaks
.
append
(
minimum
+
voltage_mean
)
maximum
=
-
9999
maximum
=
-
9999
minimum
=
9999
minimum
=
9999
...
@@ -226,12 +229,15 @@ def calculate_peak_to_peak(voltages_centered):
...
@@ -226,12 +229,15 @@ def calculate_peak_to_peak(voltages_centered):
maximum
=
v
maximum
=
v
if
v
<
0
and
v
<
minimum
:
if
v
<
0
and
v
<
minimum
:
minimum
=
v
minimum
=
v
old_sign
=
sign
if
len
(
positive_peaks
)
<
3
or
len
(
negative_peaks
)
<
3
:
if
len
(
positive_peaks
)
<
3
or
len
(
negative_peaks
)
<
3
:
print
(
positive_peaks
)
return
0
return
0
negative_peaks
=
negative_peaks
[
1
:
-
1
]
negative_peaks
=
negative_peaks
[
1
:
-
1
]
positive_peaks
=
positive_peaks
[
1
:
-
1
]
positive_peaks
=
positive_peaks
[
1
:
-
1
]
return
sum
(
positive_peaks
)
/
len
(
positive_peaks
)
-
sum
(
negative_peaks
)
/
len
(
negative_peaks
)
return
sum
(
positive_peaks
)
/
len
(
positive_peaks
)
-
sum
(
negative_peaks
)
/
len
(
negative_peaks
)
...
@@ -242,28 +248,28 @@ async def main():
...
@@ -242,28 +248,28 @@ async def main():
while
True
:
while
True
:
voltages
=
[
v
/
4096
*
3.3
for
v
in
await
device
.
measure
()]
voltages
=
[
v
/
4096
*
3.3
for
v
in
await
device
.
measure
()]
voltage_mean
=
sum
(
voltages
)
/
SAMPLES
voltage_mean
=
sum
(
voltages
)
/
device
.
get_sample_count
()
voltages_centered
=
[
v
-
voltage_mean
for
v
in
voltages
]
voltages_centered
=
[
v
-
voltage_mean
for
v
in
voltages
]
freq
=
calculate_frequency
(
voltages_centered
,
sampling_rate
=
SAMPLING_RATE_HZ
)
freq
=
calculate_frequency
(
voltages_centered
,
sampling_rate
=
device
.
get_sampling_rate
()
)
if
freq
==
SAMPLING_FREQ_TO_HIGH
:
if
freq
==
SAMPLING_FREQ_TO_HIGH
:
rate
=
device
.
get_sampling_rate
()
rate
=
device
.
get_sampling_rate
()
if
rate
<
3
:
if
rate
<
3
:
print
(
f
"
Cannot reduce sampling frequency rate further than
{
rate
}
"
)
#
print(f"Cannot reduce sampling frequency rate further than {rate}")
continue
continue
device
.
set_sampling_rate
(
rate
/
2.0
)
device
.
set_sampling_rate
(
rate
/
2.0
)
print
(
f
"
Reducing sampling rate to
{
device
.
get_sampling_rate
()
}
"
)
print
(
f
"
Reducing sampling rate to
{
device
.
get_sampling_rate
()
}
"
)
continue
continue
elif
freq
==
SAMPLING_FREQ_TO_LOW
:
elif
freq
==
SAMPLING_FREQ_TO_LOW
:
rate
=
device
.
get_sampling_rate
()
rate
=
device
.
get_sampling_rate
()
if
rate
>
10
0000
:
if
rate
>
4
0000
:
print
(
f
"
Cannot reduce increase frequency rate further than
{
rate
}
"
)
#
print(f"Cannot reduce increase frequency rate further than {rate}")
continue
continue
device
.
set_sampling_rate
(
rate
*
2
)
device
.
set_sampling_rate
(
rate
*
2
)
print
(
f
"
Increasing sampling rate to
{
device
.
get_sampling_rate
()
}
"
)
print
(
f
"
Increasing sampling rate to
{
device
.
get_sampling_rate
()
}
"
)
continue
continue
v_to_v
=
calculate_peak_to_peak
(
voltages_centered
)
v_to_v
=
calculate_peak_to_peak
(
voltages_centered
,
voltage_mean
)
print
(
f
"
{
freq
:
0.2
f
}
Hz,
{
v_to_v
:
0.2
f
}
V peak to peak
"
)
print
(
f
"
{
freq
:
0.2
f
}
Hz,
{
v_to_v
:
0.2
f
}
V peak to peak
, sampling at
{
device
.
get_sampling_rate
()
}
"
)
gc
.
collect
()
gc
.
collect
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment