-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathexample_dualslope.py
More file actions
94 lines (70 loc) · 2.15 KB
/
example_dualslope.py
File metadata and controls
94 lines (70 loc) · 2.15 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
#########################################################################################
##
## Dual-Slope (integrating) ADC
##
#########################################################################################
# IMPORTS ===============================================================================
import numpy as np
import matplotlib.pyplot as plt
from pathsim import Simulation, Connection
from pathsim.blocks import (
Integrator, Adder, Amplifier, Scope, Source,
Constant, Switch, SampleHold, Comparator
)
from pathsim.events import Schedule, ZeroCrossingDown
from pathsim.solvers import RKBS32
# SYSTEM SETUP AND SIMULATION ===========================================================
v_ref = -0.5 #dac reference
f_clk = 20 #sampling frequency
T_clk = 1.0 / f_clk #sampling period
K = 0.1
#blocks that define the system
src = Source(lambda t: 0.3*np.sin(2*np.pi*t)+0.5)
sah = SampleHold(T=T_clk)
ref = Constant(v_ref)
swt = Switch(None)
sub = Adder("+-")
itg = Integrator()
fbk = Amplifier(K)
sco = Scope(labels=["src", "sah", "swt", "itg"])
blocks = [src, sub, ref, swt, fbk, itg, sah, sco]
#connections between the blocks
connections = [
Connection(src, sah, sco[0]),
Connection(sah, swt[0], sco[1]),
Connection(ref, swt[1]),
Connection(swt, sub[0], sco[2]),
Connection(fbk, sub[1]),
Connection(sub, itg),
Connection(itg, fbk, sco[3])
]
#timing logic through events
events = [
ZeroCrossingDown(
func_evt=lambda *_: itg.engine.get(),
func_act=lambda *_: [itg.reset(), swt.select(None)]
),
Schedule(
t_start=0,
t_period=T_clk,
func_act=lambda *_: swt.select(0),
),
Schedule(
t_start=T_clk*0.2,
t_period=T_clk,
func_act=lambda *_: swt.select(1),
)
]
#simulation with adaptive solver
Sim = Simulation(
blocks,
connections,
events,
dt_max=T_clk*0.01,
Solver=RKBS32
)
# Run Example ===========================================================================
if __name__ == "__main__":
Sim.run(1)
Sim.plot()
plt.show()