-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_RT_Edit_VoltageClampPIDCurrent.cpp
403 lines (341 loc) · 12.8 KB
/
GUI_RT_Edit_VoltageClampPIDCurrent.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* /////////////////////////////////////////////////////////////////////////////
NetSuite: NetClamp/NetSim/NetFit Neural Network Modeling and Dynamic Clamp Software
Copyright (C) 2011-2014 E. Brady Trexler, Ph.D.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Please direct correspondence to ebtrexler _at_ gothamsci _dot_ com
///////////////////////////////////////////////////////////////////////////// */
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "GUI_RT_Edit_VoltageClampPIDCurrent.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TVoltageClamp_PID_CurrentForm *VoltageClamp_PID_CurrentForm;
//---------------------------------------------------------------------------
//===========================================================================
//===========================================================================
//===========================================================================
//
// RT -- TVoltageClamp_PID_Current methods
//
//===========================================================================
//===========================================================================
//===========================================================================
#pragma region RT classes with methods that define TVoltageClamp_PID_Current
#include "RT_Network.h"
//---------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//
// ******************** TVoltageClamp_PID_Current ***************************
//
//
/**
Uses Proportional-Integral-Differential method to determine injected current necessary
to hold a cell at a command potential
@author E. Brady Trexler <ebtrexler (at) gothamsci.com>, 2011 - 2013
@brief Implementation of current designed to clamp a cells voltage to a command potential
*/
class TVoltageClamp_PID_Current: public TCurrent
{
#ifdef SERIALIZE
// Required for serialization and saving networks to disk
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<TCurrent>(*this);
ar & BOOST_SERIALIZATION_NVP(FpGain);
ar & BOOST_SERIALIZATION_NVP(FiGain);
ar & BOOST_SERIALIZATION_NVP(FdGain);
ar & BOOST_SERIALIZATION_NVP(FiMax);
ar & BOOST_SERIALIZATION_NVP(FiMin);
ar & BOOST_SERIALIZATION_NVP(FiState);
ar & BOOST_SERIALIZATION_NVP(FdState);
ar & BOOST_SERIALIZATION_NVP(Ftau);
ar & BOOST_SERIALIZATION_NVP(FVCommand);
}
#endif
private:
double FpGain;
double FiGain;
double FdGain;
double FiMax, FiMin;
double FiState, FdState;
double Ftau;
double FVCommand;
protected:
// moved to public -- 1/24/14 -- ebt (shouldn't have been hidden in protected anyway)
// /// Returns downcasted TVoltageClamp_PID_CurrentForm* that is used to set values for this object
// virtual void* const __fastcall
// GetEditForm()
// {
// static TVoltageClamp_PID_CurrentForm *form = new TVoltageClamp_PID_CurrentForm(NULL);
//// form->HHTNCurrent = this;
// return form;
// }
/// Writes data members to a stream
virtual void const __fastcall WriteToStream(ostream &stream) const
{
stream.write((char *)&FpGain, sizeof(double));
stream.write((char *)&FiGain, sizeof(double));
stream.write((char *)&FdGain, sizeof(double));
stream.write((char *)&FiMax, sizeof(double));
stream.write((char *)&FiMin, sizeof(double));
stream.write((char *)&FiState, sizeof(double));
stream.write((char *)&FdState, sizeof(double));
stream.write((char *)&Ftau, sizeof(double));
stream.write((char *)&FVCommand, sizeof(double));
}
/// Reads data members from a stream
virtual void const __fastcall ReadFromStream(istream &stream)
{
stream.read((char *)&FpGain, sizeof(double));
stream.read((char *)&FiGain, sizeof(double));
stream.read((char *)&FdGain, sizeof(double));
stream.read((char *)&FiMax, sizeof(double));
stream.read((char *)&FiMin, sizeof(double));
stream.read((char *)&FiState, sizeof(double));
stream.read((char *)&FdState, sizeof(double));
stream.read((char *)&Ftau, sizeof(double));
stream.read((char *)&FVCommand, sizeof(double));
}
public:
// implement pure virtual
bool __fastcall Initialize(bool Reset)
{
if (Reset) {
FiState = 0;
FdState = 0;
}
return true;
}
// implement pure virtual
#define TVoltageClamp_PID_Current_KEY L"Voltage Clamp (PID) Current"
const std::wstring & __fastcall ClassKey() const
{
static std::wstring Key(TVoltageClamp_PID_Current_KEY);
return Key;
}
// implement pure virtual
const void __fastcall GetParamLogHeader(
std::vector<std::string>& params) const
{
params.clear(); // do nothing, set size to zero
}
/// Compute current based on PID control
/*!
<pre>
*
* computes pTerm + iTerm + (dTerm * step/tau) ;
</pre>
*/
double UpdatePID(double step, double error, double position)
{
double pTerm, dTerm, iTerm;
// calculate the proportional term
pTerm = FpGain * error;
// calculate the integral state with appropriate limiting
FiState += error;
if (FiState > FiMax) FiState = FiMax;
else if (FiState < FiMin) FiState = FiMin;
// calculate the integral term
iTerm = FiGain * FiState;
// calculate the derivative term
dTerm = FdGain * (position - FdState);
FdState = position;
return pTerm + iTerm + dTerm * step / Ftau ;
}
/// Uses Proportional-Integral-Differential method to determine injected current
/*!
<pre>
*
* Calls UpdatePID with error = Vcommand - Vkin.
* Uses Vkin so a synapse can clamp a cell. Vdrv is ignored.
</pre>
*/
virtual double __fastcall DoUpdate(double step, double Vkin, double Vdrv,
std::vector<double> & params)
{
double error = FVCommand - Vkin;
params.clear(); // no logging
return UpdatePID(step, error, Vkin) * -15; // don't know why 15 is magic number
}
/// Called by GUI to synchronize edit form with current values of object params
virtual void __fastcall PopulateEditForm()
{
TVoltageClamp_PID_CurrentForm *form =
(TVoltageClamp_PID_CurrentForm * )GetEditForm();
form->PGainEdit->Text = FpGain;
form->IGainEdit->Text = FiGain;
form->DGainEdit->Text = FdGain;
form->TauEdit->Text = Ftau;
form->VCommandEdit->Text = FVCommand;
form->ImaxEdit->Text = FiMax;
form->IminEdit->Text = FiMin;
}
/// Called by GUI to check if changed values are satisfactory
virtual bool __fastcall ValidateEditForm()
{
TVoltageClamp_PID_CurrentForm *form = (TVoltageClamp_PID_CurrentForm * )GetEditForm();
bool ok = true;
try {
FpGain = StrToFloat(form->PGainEdit->Text);
FiGain = StrToFloat(form->IGainEdit->Text);
FdGain = StrToFloat(form->DGainEdit->Text);
Ftau = StrToFloat(form->TauEdit->Text);
FVCommand = StrToFloat(form->VCommandEdit->Text);
FiMax = StrToFloat(form->ImaxEdit->Text);
FiMin = StrToFloat(form->IminEdit->Text);
} catch (EConvertError & e) {
ok = false;
Application->MessageBox(e.ToString().c_str(), L"Please correct input");
}
return ok;
}
/// Returns downcasted TVoltageClamp_PID_CurrentForm* that is used to set values for this object
virtual void* const __fastcall
GetEditForm()
{
static TVoltageClamp_PID_CurrentForm *form = new TVoltageClamp_PID_CurrentForm(NULL);
return form;
}
/// default constructor
__fastcall TVoltageClamp_PID_Current():
TCurrent(NULL, L""),
FpGain(0),
FiGain(0),
FdGain(0),
FiMax(1),
FiMin(-1),
FiState(0),
FdState(0),
Ftau(1),
FVCommand(0)
{
}
/// specialized constructor 2 param
__fastcall TVoltageClamp_PID_Current(TCurrentUser *owner, const std::wstring & name):
TCurrent(owner, name),
FpGain(0),
FiGain(0),
FdGain(0),
FiMax(1),
FiMin(-1),
FiState(0),
FdState(0),
Ftau(1),
FVCommand(0)
{
}
/// specialized constructor 1 param
__fastcall TVoltageClamp_PID_Current(const std::wstring & name):
TCurrent(NULL, name),
FpGain(0),
FiGain(0),
FdGain(0),
FiMax(1),
FiMin(-1),
FiState(0),
FdState(0),
Ftau(1),
FVCommand(0)
{
}
/// copy constructor
__fastcall TVoltageClamp_PID_Current( const TVoltageClamp_PID_Current & source ) :
TCurrent(source.Owner(), source.Name()),
FpGain(source.FpGain),
FiGain(source.FiGain),
FdGain(source.FdGain),
FiMax(source.FiMax),
FiMin(source.FiMin),
FiState(source.FiState),
FdState(source.FdState),
Ftau(source.Ftau),
FVCommand(source.FVCommand)
{
}
/// overloaded assignment operator
TVoltageClamp_PID_Current & operator = (const TVoltageClamp_PID_Current & source)
{
if (this != &source) { // make sure not same object
this->TCurrent::operator=(source); //copy base class members
// now copy TVoltageClamp_PID_Current fields
FpGain = (source.FpGain),
FiGain = (source.FiGain),
FdGain = (source.FdGain),
FiMax = (source.FiMax),
FiMin = (source.FiMin),
FiState = (source.FiState),
FdState = (source.FdState),
Ftau = (source.Ftau),
FVCommand = (source.FVCommand);
}
return *this;
}
/// overloaded method for duplicating currents without complete assignment
virtual void __fastcall CopyParamsFrom(const TCurrent * const source )
{
if (this != source) { // make sure not same object
if (this->ClassType() == source->ClassType()) { // make sure same type
TVoltageClamp_PID_Current *cast_source = (TVoltageClamp_PID_Current *)source;
FpGain = (cast_source->FpGain),
FiGain = (cast_source->FiGain),
FdGain = (cast_source->FdGain),
FiMax = (cast_source->FiMax),
FiMin = (cast_source->FiMin),
FiState = (cast_source->FiState),
FdState = (cast_source->FdState),
Ftau = (cast_source->Ftau),
FVCommand = (cast_source->FVCommand);
}
}
}
virtual ~TVoltageClamp_PID_Current(){;}
};
#ifdef SERIALIZE
BOOST_CLASS_EXPORT(TVoltageClamp_PID_Current)
#endif
////////////////////////////////////////////////////////////////////////////////
// Register classes with factories
//
namespace FNadim
{
static struct regVoltageClamp_PID_Current {
regVoltageClamp_PID_Current(){
GetCurrentFactory().registerBuilder(
TVoltageClamp_PID_Current_KEY, //L"Voltage Clamp (PID) Current",
TypeID<TVoltageClamp_PID_Current>(),
TypeID<TCurrentUser*const>(),
TypeID<const std::wstring>());
}
} RegVoltageClamp_PID_Current;
}
#pragma end_region
//===========================================================================
//===========================================================================
//===========================================================================
//
// GUI -- TVoltageClamp_PID_CurrentForm methods
//
//===========================================================================
//===========================================================================
//===========================================================================
#pragma region GUI methods of TVoltageClamp_PID_CurrentForm
//---------------------------------------------------------------------------
__fastcall TVoltageClamp_PID_CurrentForm::TVoltageClamp_PID_CurrentForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
#pragma end_region