-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTM_Communicator.cpp
253 lines (214 loc) · 6.27 KB
/
STM_Communicator.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
/*
* Copyright 2015 Eileen Mazzochette, et al <[email protected]>
*/
#include "StdAfx.h"
#include "STM_Communicator.h"
using namespace System;
using namespace System::Runtime::InteropServices;
#pragma region Write Functions
bool STM_Communicator::triggerStimulus(void)
{
if ((connected) && (waveTablePresentParameter == 1)) {
dataRead = true;
messageReceivedCount = 0;
piezoSignalData.clear();
actuatorPositionData.clear();
actuatorCommandData.clear();
desiredSignalData.clear();
stimulusStarted = true;
CByteArray msgData;
msgData.SetSize(sizeof(int));
int one = 1;
intFlatten(&one, (char*)msgData.GetData());
CString command("write_Actuate");
STM.SendMsg(command, &msgData);
return true;
}
else {
return false;
}
}
void STM_Communicator::cancelStimulus(void)
{
if (connected) {
CByteArray msgData;
msgData.SetSize(sizeof(int));
int zero = 0;
intFlatten(&zero, (char*)msgData.GetData());
CString command("write_Actuate");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendActuatorPosition(double position)
{
if (connected) {
CByteArray msgData;
msgData.SetSize(sizeof(double));
dblFlatten(&position, (char*)msgData.GetData());
CString command("write_ActuatorManual");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendWaveTable(double *waveTable, int arrSize)
{
if (connected) {
CByteArray msgData;
msgData.SetSize(sizeof(double) * arrSize + sizeof(int));
dblArrFlatten(waveTable, (char*)msgData.GetData(), arrSize);
CString command("write_ActuatorWaveTable");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendWaveInterval(int deltaT)
{
if (connected) {
CByteArray msgData;
msgData.SetSize(sizeof(int));
intFlatten(&deltaT, (char*)msgData.GetData());
CString command("write_WaveTableInterval");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendAcquisitionInterval(int deltaAcqusitionT)
{
if (connected) {
CByteArray msgData;
msgData.SetSize(sizeof(int));
intFlatten(&deltaAcqusitionT, (char*)msgData.GetData());
CString command("write_AcquisitionInterval");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendPIDParameters(double *PIDParameters, int arrSize)
{
if (connected){
CByteArray msgData;
msgData.SetSize(sizeof(double) * arrSize + sizeof(int));
dblArrFlatten(PIDParameters, (char*)msgData.GetData(), arrSize);
CString command("write_PIDParameters");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendTriggerMode(int triggerMode)
{
if (connected){
CByteArray msgData;
msgData.SetSize(sizeof(int));
intFlatten(&triggerMode, (char*)msgData.GetData());
CString command("write_TriggerMode");
STM.SendMsg(command, &msgData);
}
}
void STM_Communicator::sendClampMode(int clampMode)
{
if (connected){
CByteArray msgData;
msgData.SetSize(sizeof(int));
intFlatten(&clampMode, (char*)msgData.GetData());
CString command("write_ClampMode");
STM.SendMsg(command, &msgData);
}
}
#pragma endregion
#pragma region Read Functions
/*read_Data: retrieve an array of double composed of four channels: Set Point (V), Actuation Command (V), Actuator Sensor (V), Piezoresistive Signal (V)
read_FPGAStatus: retrieve a boolean about the status of the actuator (actuating:true, idle: false)
*/
void STM_Communicator::OnNewData(CString MetaName, CByteArray *MsgData)
{
if (MetaName == "read_Data") {
double* channelData;
int arrSize;
channelData = dblArrUnflatten((char*)(MsgData->GetData()), &arrSize);
messageReceivedCount++;
if (dataRead == true){
for (int i = 0; i < arrSize; i += 4) {
desiredSignalData.push_back(channelData[i + 0]); // channel 1
actuatorCommandData.push_back(channelData[i + 1]); // channel 2
actuatorPositionData.push_back(channelData[i + 2]); // channel 3
piezoSignalData.push_back(channelData[i + 3]); // channel 4
}
}
} else if(MetaName == "read_FPGAParameters"){
double* parameterData;
int arrSize;
parameterData = dblArrUnflatten((char*)(MsgData->GetData()), &arrSize);
int numParameters = 10;
double oldActuatorMovingParameter;
for (int i=0; i<arrSize/numParameters; i += numParameters)
{
P_Parameter = parameterData[i+0];
I_Parameter = parameterData[i+1];
D_Parameter = parameterData[i+2];
clampModeParameter = parameterData[i+3];
triggerModeParameter = parameterData[i+4];
acquisitionFreqParameter = parameterData[i+5];
actuatorFreqParameter = parameterData[i+ 6];
fifoMemoryFullParameter = parameterData[i+7]; // should be false, if true, there is a problem with the FPGA, don't trust data
oldActuatorMovingParameter = actuatorMovingParameter;
actuatorMovingParameter = parameterData[i+8]; // true if actuator is moving.
waveTablePresentParameter = parameterData[i+9];
}
if(stimulusStarted == true & actuatorMovingParameter == 0 & oldActuatorMovingParameter == 1)
{
stimulusCompleted = true;
}
}
STMDispatch::OnNewData(MetaName, MsgData);
}
void STM_Communicator::getfpgaData(int stimNum, vector<double> piezoSignalData, vector<double> actuatorPositionData, vector<double> actuatorCommandData, vector<double> desiredSignalData){
reportedStimNum;
reportedPiezoSignalData.clear();
reportedActuatorPositionData.clear();
reportedActuatorCommandData.clear();
reportedDesiredSignalData.clear();
reportedStimNum = stimNum;
reportedPiezoSignalData = piezoSignalData;
reportedActuatorPositionData = actuatorPositionData;
reportedActuatorCommandData = actuatorCommandData;
reportedDesiredSignalData = desiredSignalData;
}
#pragma endregion
#pragma region Initialization and Open/Close Functions
STM_Communicator::STM_Communicator()
{
STM.SetDispatch(this);
connected = false;
stimulusStarted = false;
stimulusCompleted = false;
CString ip("171.64.164.81");
int port = 55555;
connect(ip, port);
}
STM_Communicator::~STM_Communicator()
{
disconnect();
}
void STM_Communicator::connect(CString ip, int port)
{
if (!connected) {
int error=STM.Connect(ip, port);
} else {
//already connected
}
}
void STM_Communicator::OnConnect()
{
connected = true;
STMDispatch::OnConnect();
}
void STM_Communicator::disconnect(void)
{
if (connected) {
int error = STM.Disconnect();
connected = false;
} else {
//already disconnected
}
}
void STM_Communicator::OnClose()
{
connected = false;
STMDispatch::OnClose();
}
#pragma endregion