uPDHBox.ipynb 8.68 KB
Newer Older
Anchal Gupta's avatar
Anchal Gupta committed
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from zero import Circuit\n",
    "from zero.components import Resistor, OpAmp\n",
    "from math import log10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Variable Gain Amplifier. Will be used by changing a0.\n",
    "AD8336 = {'model': 'AD8336',\n",
    "          'a0': 10,      # Nominal 20 dB gain. Will be changed.   \n",
    "          'gbw': 80e6,\n",
    "          'vnoise': '3n',\n",
    "          'vcorner': 1e3,\n",
    "          'inoise': '3p',\n",
    "          'icorner': 1e3, # Guess\n",
    "          'vmax': 10,\n",
    "          'imax': 72e-3,\n",
    "          'sr': 550e6}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Insertion Amplifier Stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "uPDH = Circuit()\n",
    "uPDH.add_resistor(value=0, name='J3A', node1='ServoIn', node2='J3A')\n",
    "uPDH.add_resistor(value=0, name='TP1', node1='ServoIn', node2='TP1')\n",
    "uPDH.add_library_opamp(model='OP27', name='U1', node1='ServoIn', node2='n1', node3='InMon')\n",
    "uPDH.add_resistor(value=49.9, name='R17', node1='ServoIn', node2='gnd')\n",
    "uPDH.add_resistor(value=1e3, name='R14', node1='n1', node2='gnd')\n",
    "uPDH.add_resistor(value=1e3, name='R12', node1='n1', node2='InMon')\n",
    "uPDH.add_resistor(value=0, name='TP3', node1='InMon', node2='TP3')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variable Gain Stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "uPDH.add_component(OpAmp(node1='InMon', node2='gnd',\n",
    "                         node3='n2', name='U6', **AD8336))\n",
    "def VarGain(circuit, Rpot=None, G=None, Vg=None, vb=True,\n",
    "            R27=1e3, R26=1e3):\n",
    "    preAmpGain = 20 * log10(1 + R26/R27)\n",
    "    if Rpot is None:\n",
    "        if G is None:\n",
    "            if Vg is None:\n",
    "                print('Error: Provide gain in dB or gain setting voltage in V.')\n",
    "                print('Nothing changed.')\n",
    "                return 0\n",
    "            else:\n",
    "                G = 50 * Vg + 4.4 + preAmpGain\n",
    "    else:\n",
    "        G = -11.19375 + Rpot * 3.11875e-3 + preAmpGain\n",
    "    if G>30.0 or G<-10.0:\n",
    "        print('Error: Gain can be set between -10dB to 30 dB only.')\n",
    "        print('Nothing changed.')\n",
    "        return 0\n",
    "    circuit[\"U6\"].a0 = 10**(G/20) #Gain in absolute value\n",
    "    if vb:\n",
    "        print('Variable Gain set to '+str(round(G,2))+' dB.')\n",
    "# Default potentiometer set to 6.6kOhms\n",
    "VarGain(uPDH, Rpot=6.6e3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Optional Transfer Function Stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "uPDH.add_resistor(value=0, name='TP13', node1='n2', node2='TP13')\n",
    "uPDH.add_resistor(value=1e3, name='R29', node1='n2', node2='n4')\n",
    "uPDH.add_resistor(value=1e3, name='R32', node1='n3', node2='gnd')\n",
    "uPDH.add_library_opamp(model='OP27', name='U7', node1='n3', node2='n4', node3='n5', )\n",
    "uPDH.add_resistor(value=1e3, name='R30', node1='n4', node2='n5')\n",
    "uPDH.add_resistor(value=10, name='R31', node1='n4', node2='n6')\n",
    "uPDH.add_capacitor(value=3.3e-6, name='C28', node1='n6', node2='n5')\n",
    "uPDH.add_capacitor(value=3.3e-6, name='C18', node1='n7', node2='n4')\n",
    "uPDH.add_resistor(value=0, name='TP16', node1='n5', node2='TP16')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Integrator Stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "uPDH.add_resistor(value=1e3, name='R23', node1='n5', node2='n9')\n",
    "uPDH.add_resistor(value=1e3, name='R20', node1='n8', node2='gnd')\n",
    "uPDH.add_library_opamp(model='LF356', name='U4', node1='n8', node2='n9', node3='n10')\n",
    "uPDH.add_resistor(value=10, name='R24', node1='n9', node2='n12')\n",
    "uPDH.add_capacitor(value=3.3e-6, name='C6', node1='n12', node2='n10')\n",
    "uPDH.add_resistor(value=10e3, name='R16', node1='n11', node2='n10')\n",
    "uPDH.add_capacitor(value=3.3e-6, name='C15', node1='n13', node2='n9')\n",
    "uPDH.add_resistor(value=0, name='TP10', node1='n10', node2='TP10')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Boost Switch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Boost(circuit, state):\n",
    "    boost = Resistor(name=\"boost\", value=\"0\", node1=\"n9\", node2=\"n11\")\n",
    "    if state.upper() == 'ON':\n",
    "        if circuit.has_component(boost):\n",
    "            circuit.remove_component(boost)\n",
    "            print('Switched ON Boost')\n",
    "        else:\n",
    "            print('Boost is already ON')\n",
    "    elif state.upper() == 'OFF':\n",
    "        if circuit.has_component(boost):\n",
    "            print('Boost is already OFF')\n",
    "        else:\n",
    "            circuit.add_component(boost)\n",
    "            print('Switched OFF Boost')\n",
    "    else:\n",
    "        print('Error: Use options ON or OFF')\n",
    "        print('Nothing changed.')\n",
    "\n",
    "# Default Boost ON\n",
    "Boost(uPDH,'ON')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inverter and Piezo Driver Stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "uPDH.add_resistor(value=0, name='TP2', node1='n10', node2='TP2')\n",
    "uPDH.add_resistor(value=10e3, name='R19', node1='n10', node2='n15')\n",
    "uPDH.add_resistor(value=10e3, name='R21', node1='n10', node2='n14')\n",
    "uPDH.add_library_opamp(model='OP27', name='U3', node1='n14', node2='n15', node3='n16')\n",
    "uPDH.add_resistor(value=10e3, name='R15', node1='n15', node2='n16')\n",
    "uPDH.add_resistor(value=0, name='TP4', node1='n16', node2='TP4')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Inverter Switch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Inverter(circuit,state):\n",
    "    invert = Resistor(name=\"invert\", value=\"0\", node1=\"n14\", node2=\"gnd\")\n",
    "    if state.upper() == 'OFF':\n",
    "        if circuit.has_component(invert):\n",
    "            circuit.remove_component(invert)\n",
    "            print('Switched OFF inversion')\n",
    "        else:\n",
    "            print('Inversion is already OFF')\n",
    "    elif state.upper() == 'ON':\n",
    "        if circuit.has_component(invert):\n",
    "            print('Inversion is already ON')\n",
    "        else:\n",
    "            circuit.add_component(invert)\n",
    "            print('Switched ON inversion')\n",
    "    else:\n",
    "        print('Error: Use options ON or OFF')\n",
    "        print('Nothing changed.')\n",
    "\n",
    "# Default invert ON\n",
    "Inverter(uPDH,'ON')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "uPDH.add_resistor(value=10, name='R11', node1='n16', node2='OutMon')\n",
    "uPDH.add_resistor(value=0, name='J1A', node1='OutMon', node2='J1A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!jupyter nbconvert --to script uPDHBox.ipynb\n",
    "\n",
    "# Delete the last cell\n",
    "with open('uPDHBox.py','r') as f:\n",
    "    lines = f.readlines()\n",
    "for ii,line in enumerate(lines):\n",
    "    if line.find('# In[') is not -1 and line.find('Skip This Line') == -1:\n",
    "        lastCellLineNo = ii\n",
    "newlines = lines[:lastCellLineNo]\n",
    "with open('uPDHBox.py','w') as f:\n",
    "    f.writelines(newlines)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}