-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoFilterWheel.cpp
More file actions
486 lines (383 loc) · 14.9 KB
/
ArduinoFilterWheel.cpp
File metadata and controls
486 lines (383 loc) · 14.9 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//////////////////////////////////////////////////////////////////////////////
// FILE: ArduinoFilterWheel.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION: Adapter for Arduino board
// Needs accompanying firmware to be installed on the board
//
// AUTHOR: Shirish Goyal, shirish.goyal@gmail.com 09/14/2016
//
// COPYRIGHT: Shirish Goyal, BioCurious, Sunnyvale, 2016
//
// LICENSE: This file is distributed under the BSD license.
// License text is included with the source distribution.
//
// This file 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.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
#include "ArduinoFilterWheel.h"
#include "../../MMDevice/ModuleInterface.h"
#include <sstream>
#include <cstdio>
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define snprintf _snprintf
#endif
const char* g_DeviceNameArduinoFilterWheelHub = "ArduinoFilterWheel-Hub";
const char* g_DeviceNameArduinoFilterWheel = "ArduinoFilterWheel-FilterWheel";
const char* g_DeviceDescriptionArduinoFilterWheel="Arduino Filter Wheel Driver";
const char* g_versionProp = "Version";
const int g_Min_MMVersion = 0;
const int g_Max_MMVersion = 1;
const char* g_On = "On";
const char* g_Off = "Off";
// static lock
MMThreadLock CArduinoFilterWheelHub::lock_;
///////////////////////////////////////////////////////////////////////////////
// Exported MMDevice API
///////////////////////////////////////////////////////////////////////////////
MODULE_API void InitializeModuleData() {
RegisterDevice(g_DeviceNameArduinoFilterWheelHub, MM::HubDevice, "Hub (required)");
RegisterDevice(g_DeviceNameArduinoFilterWheel, MM::StateDevice, "Filter Wheel");
}
MODULE_API MM::Device *CreateDevice(const char *deviceName) {
if (deviceName == 0)
return 0;
if (strcmp(deviceName, g_DeviceNameArduinoFilterWheelHub) == 0) {
return new CArduinoFilterWheelHub;
} else if (strcmp(deviceName, g_DeviceNameArduinoFilterWheel) == 0) {
return new CArduinoFilterWheel;
}
return 0;
}
MODULE_API void DeleteDevice(MM::Device *pDevice) {
delete pDevice;
}
///////////////////////////////////////////////////////////////////////////////
CArduinoFilterWheelHub::CArduinoFilterWheelHub() :
initialized_(false),
filterWheelState_(0) {
portAvailable_ = false;
InitializeDefaultErrorMessages();
SetErrorText(ERR_PORT_OPEN_FAILED, "Failed opening Arduino USB device");
SetErrorText(ERR_BOARD_NOT_FOUND,
"Did not find an Arduino board with the correct firmware. Is the Arduino board connected to this serial port?");
SetErrorText(ERR_NO_PORT_SET, "Hub Device not found. The Arduino Hub device is needed to create this device");
std::ostringstream errorText;
errorText
<< "The firmware version on the Arduino is not compatible with this adapter. Please use firmware version ";
errorText << g_Min_MMVersion << " to " << g_Max_MMVersion;
SetErrorText(ERR_VERSION_MISMATCH, errorText.str().c_str());
CPropertyAction *pAct = new CPropertyAction(this, &CArduinoFilterWheelHub::OnPort);
CreateProperty(MM::g_Keyword_Port, "Undefined", MM::String, false, pAct, true);
}
CArduinoFilterWheelHub::~CArduinoFilterWheelHub() {
Shutdown();
}
void CArduinoFilterWheelHub::GetName(char *name) const {
CDeviceUtils::CopyLimitedString(name, g_DeviceNameArduinoFilterWheelHub);
}
bool CArduinoFilterWheelHub::Busy() {
return false;
}
int CArduinoFilterWheelHub::GetControllerVersion(int &version) {
int ret = DEVICE_OK;
//unsigned char command[1];
std::string answer;
//command[0] = 1;
version = 0;
if(!portAvailable_){
return ERR_NO_PORT_SET;
}
//ret = SendSerialCommand(port_.c_str(), (const char *) command, 1);
//if (ret != DEVICE_OK)
// return ret;
ret = SendSerialCommand(port_.c_str(), "V", "\r");
if (ret != DEVICE_OK)
return ret;
ret = GetSerialAnswer(port_.c_str(), "\r", answer);
if (ret != DEVICE_OK) {
return ret;
}
LogMessage("Found Board", false);
LogMessage(answer, false);
if (answer != "ArduinoFilterWheel" && answer != "\nArduinoFilterWheel") {
return ERR_BOARD_NOT_FOUND;
}
version = 1;
return ret;
}
int CArduinoFilterWheelHub::WriteToComPortH(const char* command, size_t len)
{
int ret = DEVICE_OK;
std::string answer;
ret = SendSerialCommand(port_.c_str(), (const char *)command, "\r");
return ret;
}
bool CArduinoFilterWheelHub::SupportsDeviceDetection(void) {
return true;
}
MM::DeviceDetectionStatus CArduinoFilterWheelHub::DetectDevice(void) {
if (initialized_)
return MM::CanCommunicate;
MM::DeviceDetectionStatus result = MM::Misconfigured;
char answerTO[MM::MaxStrLength];
try {
std::string portLowerCase = port_;
for (std::string::iterator its = portLowerCase.begin(); its != portLowerCase.end(); ++its) {
*its = (char) tolower(*its);
}
if (0 < portLowerCase.length() && 0 != portLowerCase.compare("undefined") &&
0 != portLowerCase.compare("unknown")) {
result = MM::CanNotCommunicate;
// record the default answer time out
GetCoreCallback()->GetDeviceProperty(port_.c_str(), "AnswerTimeout", answerTO);
// device specific default communication parameters
// for Arduino Duemilanova
GetCoreCallback()->SetDeviceProperty(port_.c_str(), MM::g_Keyword_Handshaking, g_Off);
GetCoreCallback()->SetDeviceProperty(port_.c_str(), MM::g_Keyword_BaudRate, "9600");
GetCoreCallback()->SetDeviceProperty(port_.c_str(), MM::g_Keyword_StopBits, "1");
// Arduino timed out in GetArduinoVersion even if AnswerTimeout = 300 ms
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "AnswerTimeout", "500.0");
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "DelayBetweenCharsMs", "0");
MM::Device *pS = GetCoreCallback()->GetDevice(this, port_.c_str());
pS->Initialize();
// The first second or so after opening the serial port, the Arduino is waiting for firmware upgrades.
// Simply sleep 2 seconds.
CDeviceUtils::SleepMs(2000);
MMThreadGuard myLock(lock_);
PurgeComPort(port_.c_str());
int v = 0;
int ret = GetControllerVersion(v);
// later, Initialize will explicitly check the version #
if (DEVICE_OK != ret) {
LogMessageCode(ret, true);
} else {
// to succeed must reach here....
result = MM::CanCommunicate;
}
pS->Shutdown();
// always restore the AnswerTimeout to the default
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "AnswerTimeout", answerTO);
}
}
catch (...) {
LogMessage("Exception in Detect Device!", false);
}
return result;
}
int CArduinoFilterWheelHub::Initialize() {
// Name
int ret = CreateProperty(MM::g_Keyword_Name, g_DeviceNameArduinoFilterWheelHub, MM::String, true);
if (DEVICE_OK != ret)
return ret;
LogMessage("Initializing Filter Wheel", false);
// The first second or so after opening the serial port, the Arduino is waiting for firmwareupgrades. Simply sleep 1 second.
CDeviceUtils::SleepMs(2000);
MMThreadGuard myLock(lock_);
// Check that we have a controller:
PurgeComPort(port_.c_str());
ret = GetControllerVersion(version_);
if( DEVICE_OK != ret)
return ret;
if (version_ < g_Min_MMVersion || version_ > g_Max_MMVersion)
return ERR_VERSION_MISMATCH;
CPropertyAction* pAct = new CPropertyAction(this, &CArduinoFilterWheelHub::OnVersion);
std::ostringstream sversion;
sversion << version_;
CreateProperty(g_versionProp, sversion.str().c_str(), MM::Integer, true, pAct);
ret = UpdateStatus();
if (ret != DEVICE_OK)
return ret;
// turn on verbose serial debug messages
GetCoreCallback()->SetDeviceProperty(port_.c_str(), "Verbose", "1");
initialized_ = true;
return DEVICE_OK;
}
int CArduinoFilterWheelHub::DetectInstalledDevices()
{
if (MM::CanCommunicate == DetectDevice())
{
std::vector<std::string> peripherals;
peripherals.clear();
peripherals.push_back(g_DeviceNameArduinoFilterWheel);
for (size_t i=0; i < peripherals.size(); i++)
{
MM::Device* pDev = ::CreateDevice(peripherals[i].c_str());
if (pDev)
{
AddInstalledDevice(pDev);
}
}
}
return DEVICE_OK;
}
int CArduinoFilterWheelHub::Shutdown() {
LogMessage("Shutdown", false);
initialized_ = false;
return DEVICE_OK;
}
int CArduinoFilterWheelHub::OnPort(MM::PropertyBase *pProp, MM::ActionType eAct) {
LogMessage("On Port", false);
if (eAct == MM::BeforeGet) {
pProp->Set(port_.c_str());
} else if (eAct == MM::AfterSet) {
pProp->Get(port_);
portAvailable_ = true;
}
return DEVICE_OK;
}
int CArduinoFilterWheelHub::OnVersion(MM::PropertyBase* pProp, MM::ActionType pAct)
{
LogMessage("On Version", false);
if (pAct == MM::BeforeGet)
{
pProp->Set((long)version_);
}
return DEVICE_OK;
}
///////////////////////////////////////////////////////////////////////////////
// CArduinoFilterWheel implementation
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
CArduinoFilterWheel::CArduinoFilterWheel() :
busy_(false),
initialized_(false),
name_(g_DeviceNameArduinoFilterWheel),
changedTime_(0.0),
position_(6),
numPos_(7)
{
InitializeDefaultErrorMessages();
EnableDelay();
SetErrorText(ERR_NO_PORT_SET, "Hub Device not found. The ArduinoFilterWheel Hub device is needed to create this device");
// Name
int ret = CreateProperty(MM::g_Keyword_Name, g_DeviceNameArduinoFilterWheel, MM::String, true);
assert(DEVICE_OK == ret);
// Description
ret = CreateProperty(MM::g_Keyword_Description, g_DeviceDescriptionArduinoFilterWheel, MM::String, true);
assert(DEVICE_OK == ret);
// parent ID display
CreateHubIDProperty();
}
CArduinoFilterWheel::~CArduinoFilterWheel() {
LogMessage("Destructor", false);
Shutdown();
}
void CArduinoFilterWheel::GetName(char *Name) const {
CDeviceUtils::CopyLimitedString(Name, g_DeviceNameArduinoFilterWheel);
}
bool CArduinoFilterWheel::Busy() {
LogMessage("Busy", false);
return busy_;
//MM::MMTime interval = GetCurrentMMTime() - changedTime_;
//MM::MMTime delay(GetDelayMs() * 1000.0);
//return (interval < delay);
}
int CArduinoFilterWheel::Initialize() {
LogMessage("Initialize FW", false);
CArduinoFilterWheelHub* hub = static_cast<CArduinoFilterWheelHub*>(GetParentHub());
if (!hub || !hub->IsPortAvailable()) {
LogMessage("Port failed for Filter Wheel", true);
return ERR_NO_PORT_SET;
}
char hubLabel[MM::MaxStrLength];
hub->GetLabel(hubLabel);
SetParentID(hubLabel); // for backward comp.
// set property list
// -----------------
// Set timer for the Busy signal, or we'll get a time-out the first time we check the state of the shutter, for good measure, go back 'delay' time into the past
//changedTime_ = GetCurrentMMTime();
LogMessage("Set Positions FW", false);
// create default positions and labels
const int bufSize = 64;
char buf[bufSize];
snprintf(buf, bufSize, "Cy3");
SetPositionLabel(1, buf);
snprintf(buf, bufSize, "TxRed");
SetPositionLabel(2, buf);
snprintf(buf, bufSize, "Cy5");
SetPositionLabel(3, buf);
snprintf(buf, bufSize, "Mirror");
SetPositionLabel(4, buf);
snprintf(buf, bufSize, "Empty");
SetPositionLabel(5, buf);
snprintf(buf, bufSize, "Fitc");
SetPositionLabel(6, buf);
// add Stop Position
snprintf(buf, bufSize, "Stop");
SetPositionLabel(0, buf);
// add Home Position **&*&*&&*&*&* TODO: HOME position value *^^*^*^*^^
//snprintf(buf, bufSize, "Home");
//SetPositionLabel(5, buf);
//for (unsigned long i = 1; i <= numPos_; i++) {
// snprintf(buf, bufSize, "Filter %d", i);
// SetPositionLabel(i, buf);
//}
// State
// -----
CPropertyAction *pAct = new CPropertyAction(this, &CArduinoFilterWheel::OnState);
int ret = CreateProperty(MM::g_Keyword_State, "State", MM::Integer, false, pAct);
if (ret != DEVICE_OK)
return ret;
SetPropertyLimits(MM::g_Keyword_State, 0, numPos_- 1);
// Label
// -----
pAct = new CPropertyAction(this, &CStateBase::OnLabel);
ret = CreateProperty(MM::g_Keyword_Label, "", MM::String, false, pAct);
if (ret != DEVICE_OK)
return ret;
ret = UpdateStatus();
if (ret != DEVICE_OK)
return ret;
initialized_ = true;
return DEVICE_OK;
}
int CArduinoFilterWheel::Shutdown() {
if (initialized_) {
initialized_ = false;
}
return DEVICE_OK;
}
int CArduinoFilterWheel::OnState(MM::PropertyBase *pProp, MM::ActionType eAct) {
const int bufSize = 128;
char msg[bufSize];
long pos;
CArduinoFilterWheelHub* hub = static_cast<CArduinoFilterWheelHub*>(GetParentHub());
if (!hub || !hub->IsPortAvailable())
return ERR_NO_PORT_SET;
if (eAct == MM::BeforeGet) {
pProp->Set((long)position_);
// nothing to do, let the caller to use cached property
} else if (eAct == MM::AfterSet) {
// Set timer for the Busy signal
changedTime_ = GetCurrentMMTime();
pProp->Get(pos);
//char* deviceName;
//GetName(deviceName);
//if (pos >= numPos_ || pos < 0) {
// LogMessage("Invalid position - reverting",false);
// pProp->Set(pos); // revert
// //return ERR_UNKNOWN_POSITION;
//}
snprintf(msg, bufSize, "Moving to %d", pos);
LogMessage(msg,false);
const int bufSize = 16;
char buf[bufSize];
snprintf(buf, bufSize, "%d", pos);
//SendSerialCommand(port_.c_str(), buf, "\r");
position_ = pos;
hub->SetFilterWheelState(pos);
if (hub->GetFilterWheelState() >= 0){
snprintf(msg, bufSize, "New %d", position_);
LogMessage(msg,false);
return hub->WriteToComPortH((const char*)buf, bufSize);
}
}
return DEVICE_OK;
}