/
pegasus.cpp
982 lines (809 loc) · 25.9 KB
/
pegasus.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//
// nexdome.cpp
// NexDome X2 plugin
//
// Created by Rodolphe Pineau on 6/11/2016.
#include "pegasus.h"
CPegasusController::CPegasusController()
{
m_globalStatus.nDeviceType = NONE;
m_globalStatus.bReady = false;
memset(m_globalStatus.szVersion,0,SERIAL_BUFFER_SIZE);
m_globalStatus.nMotorType = STEPPER;
m_nTargetPos = 0;
m_nPosLimit = 0;
m_bPosLimitEnabled = false;
m_bAbborted = false;
m_pSerx = NULL;
m_pLogger = NULL;
m_pSleeper = NULL;
#ifdef PEGA_DEBUG
#if defined(SB_WIN_BUILD)
m_sLogfilePath = getenv("HOMEDRIVE");
m_sLogfilePath += getenv("HOMEPATH");
m_sLogfilePath += "\\PegasusLog.txt";
#elif defined(SB_LINUX_BUILD)
m_sLogfilePath = getenv("HOME");
m_sLogfilePath += "/PegasusLog.txt";
#elif defined(SB_MAC_BUILD)
m_sLogfilePath = getenv("HOME");
m_sLogfilePath += "/PegasusLog.txt";
#endif
Logfile = fopen(m_sLogfilePath.c_str(), "w");
#endif
#ifdef PEGA_DEBUG
ltime = time(NULL);
char *timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController Constructor Called.\n", timestamp);
fflush(Logfile);
#endif
}
CPegasusController::~CPegasusController()
{
#ifdef PEGA_DEBUG
// Close LogFile
if (Logfile) fclose(Logfile);
#endif
}
int CPegasusController::Connect(const char *pszPort)
{
int nErr = DMFC_OK;
int nDevice;
if(!m_pSerx)
return ERR_COMMNOLINK;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::Connect Called %s\n", timestamp, pszPort);
fflush(Logfile);
#endif
// 19200 8N1
nErr = m_pSerx->open(pszPort, 19200, SerXInterface::B_NOPARITY, "-DTR_CONTROL 1");
if(nErr == 0)
m_bIsConnected = true;
else
m_bIsConnected = false;
if(!m_bIsConnected)
return nErr;
m_pSleeper->sleep(2000); // apparently needed on old DMFC
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::Connect connected to %s\n", timestamp, pszPort);
fflush(Logfile);
#endif
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::Connect] Connected.\n");
m_pLogger->out(m_szLogBuffer);
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::Connect] Getting device type.\n");
m_pLogger->out(m_szLogBuffer);
}
// get status so we can figure out what device we are connecting to.
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::Connect getting device type\n", timestamp);
fflush(Logfile);
#endif
nErr = getDeviceType(nDevice);
if(nErr) {
m_bIsConnected = false;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::Connect **** ERROR **** getting device type\n", timestamp);
fflush(Logfile);
#endif
return nErr;
}
// m_globalStatus.deviceType now contains the device type
return nErr;
}
void CPegasusController::Disconnect()
{
if(m_bIsConnected && m_pSerx)
m_pSerx->close();
m_bIsConnected = false;
}
#pragma mark move commands
int CPegasusController::haltFocuser()
{
int nErr;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("H\n", szResp, SERIAL_BUFFER_SIZE);
m_bAbborted = true;
return nErr;
}
int CPegasusController::gotoPosition(int nPos)
{
int nErr;
char szCmd[SERIAL_BUFFER_SIZE];
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
if (m_bPosLimitEnabled && nPos>m_nPosLimit)
return ERR_LIMITSEXCEEDED;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::gotoPosition moving to %d\n", timestamp, nPos);
fflush(Logfile);
#endif
sprintf(szCmd,"M:%d\n", nPos);
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
m_nTargetPos = nPos;
return nErr;
}
int CPegasusController::moveRelativeToPosision(int nSteps)
{
int nErr;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::moveRelativeToPosision moving by %d steps\n", timestamp, nSteps);
fflush(Logfile);
#endif
m_nTargetPos = m_globalStatus.nCurPos + nSteps;
nErr = gotoPosition(m_nTargetPos);
return nErr;
}
#pragma mark command complete functions
int CPegasusController::isGoToComplete(bool &bComplete)
{
int nErr = DMFC_OK;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
getPosition(m_globalStatus.nCurPos);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::isGoToComplete m_globalStatus.nCurPos = %d steps\n", timestamp, m_globalStatus.nCurPos);
fprintf(Logfile, "[%s] CPegasusController::isGoToComplete m_nTargetPos = %d steps\n", timestamp, m_nTargetPos);
fflush(Logfile);
#endif
if(m_bAbborted) {
bComplete = true;
m_nTargetPos = m_globalStatus.nCurPos;
m_bAbborted = false;
}
else if(m_globalStatus.nCurPos == m_nTargetPos)
bComplete = true;
else
bComplete = false;
return nErr;
}
int CPegasusController::isMotorMoving(bool &bMoving)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
// OK_SMFC or OK_DMFC
nErr = dmfcCommand("I\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
if(atoi(szResp)) {
bMoving = true;
m_globalStatus.bMoving = MOVING;
}
else {
bMoving = false;
m_globalStatus.bMoving = IDLE;
}
return nErr;
}
#pragma mark getters and setters
int CPegasusController::getDeviceType(int &nDevice)
{
int nErr;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
// OK_SMFC or OK_DMFC
nErr = dmfcCommand("#\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
if(strstr(szResp,"OK_")) {
if(strstr(szResp,"OK_SMFC")) {
m_globalStatus.nDeviceType = SMFC;
}
else if(strstr(szResp,"OK_DMFC")) {
m_globalStatus.nDeviceType = DMFC;
}
else if(strstr(szResp,"OK_DC")) {
m_globalStatus.nDeviceType = FC;
}
else if(strstr(szResp,"OK_SCOPS")) {
m_globalStatus.nDeviceType = SCOPS;
}
nDevice = m_globalStatus.nDeviceType;
nErr = DMFC_OK;
getConsolidatedStatus();
}
else {
nErr = COMMAND_FAILED;
}
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getDeviceType m_globalStatus.nDeviceType = %d\n", timestamp, m_globalStatus.nDeviceType);
fflush(Logfile);
#endif
return nErr;
}
int CPegasusController::getConsolidatedStatus()
{
int nErr;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("A\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus about to parse response\n", timestamp);
fflush(Logfile);
#endif
// parse response
nErr = parseResp(szResp, m_svParsedRespForA);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus response parsing done\n", timestamp);
fflush(Logfile);
#endif
if(m_svParsedRespForA.empty()) {
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus parsing returned an empty vector\n", timestamp);
fflush(Logfile);
#endif
return DMFC_BAD_CMD_RESPONSE;
}
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus Status = %s\n", timestamp, m_svParsedRespForA[fSTATUS].c_str());
fflush(Logfile);
#endif
if(m_svParsedRespForA[fSTATUS].find("OK_")!= -1) {
m_globalStatus.bReady = true;
if(m_svParsedRespForA[fSTATUS].find("_SMFC")!= -1) {
m_globalStatus.nDeviceType = SMFC;
}
else if(m_svParsedRespForA[fSTATUS].find("_DMFC")!= -1) {
m_globalStatus.nDeviceType = DMFC;
}
else if(m_svParsedRespForA[fSTATUS].find("_FC")!= -1) {
m_globalStatus.nDeviceType = FC;
}
}
else {
m_globalStatus.bReady = false;
}
strncpy(m_globalStatus.szVersion, m_svParsedRespForA[fVERSIONS].c_str(), SERIAL_BUFFER_SIZE);
m_globalStatus.nMotorType = atoi(m_svParsedRespForA[fMOTOR_MODE].c_str());
m_globalStatus.dTemperature = atof(m_svParsedRespForA[fTEMP].c_str());
m_globalStatus.nCurPos = atoi(m_svParsedRespForA[fPOS].c_str());
if(m_svParsedRespForA.size()>5){ // SMFC seems to not have these fields
m_globalStatus.bMoving = atoi(m_svParsedRespForA[fMoving].c_str());
m_globalStatus.nLedStatus = atoi(m_svParsedRespForA[fLED].c_str());
m_globalStatus.bReverse = atoi(m_svParsedRespForA[fREVERSE].c_str());
m_globalStatus.bEncodeEnabled = atoi(m_svParsedRespForA[fDIS_ENC].c_str());
m_globalStatus.nBacklash = atoi(m_svParsedRespForA[fBACKLASH].c_str());
}
#ifdef PEGA_DEBUG
else {
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus response is only 5 fields\n", timestamp);
fflush(Logfile);
}
#endif
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus nDeviceType : %d\n", timestamp, m_globalStatus.nDeviceType);
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus szVersion : %s\n", timestamp, m_globalStatus.szVersion);
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus nMotorType : %d\n", timestamp, m_globalStatus.nMotorType);
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus dTemperature : %3.2f\n", timestamp, m_globalStatus.dTemperature);
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus nCurPos : %d\n", timestamp, m_globalStatus.nCurPos);
if(m_svParsedRespForA.size()>5){
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus bMoving : %s\n", timestamp, m_globalStatus.bMoving?"Yes":"No");
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus nLedStatus : %d\n", timestamp, m_globalStatus.nLedStatus);
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus bReverse : %s\n", timestamp, m_globalStatus.bReverse?"Yes":"No");
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus bEncodeEnabled : %s\n", timestamp, m_globalStatus.bEncodeEnabled?"Yes":"No");
fprintf(Logfile, "[%s] CPegasusController::getConsolidatedStatus nBacklash : %d\n", timestamp, m_globalStatus.nBacklash);
}
fflush(Logfile);
#endif
return nErr;
}
int CPegasusController::getMotoMaxSpeed(int &nSpeed)
{
int nErr;
char szResp[SERIAL_BUFFER_SIZE];
std::vector<std::string> svParsedResp;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("B\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
// parse response
svParsedResp.clear();
nErr = parseResp(szResp, svParsedResp);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::getMotoMaxSpeed sParsedResp length : %lu\n", timestamp, svParsedResp.size());
fflush(Logfile);
#endif
nSpeed = atoi(svParsedResp[1].c_str());
return nErr;
}
int CPegasusController::setMotoMaxSpeed(int nSpeed)
{
int nErr;
char szCmd[SERIAL_BUFFER_SIZE];
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
sprintf(szCmd,"S:%d\n", nSpeed);
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
return nErr;
}
int CPegasusController::getBacklashComp(int &nSteps)
{
int nErr;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = getConsolidatedStatus();
nSteps = m_globalStatus.nBacklash;
return nErr;
}
int CPegasusController::setBacklashComp(int nSteps)
{
int nErr = DMFC_OK;
char szCmd[SERIAL_BUFFER_SIZE];
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setBacklashComp setting backlash comp : %s\n", timestamp, szCmd);
fflush(Logfile);
#endif
sprintf(szCmd,"C:%d\n", nSteps);
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
if(!nErr)
m_globalStatus.nBacklash = nSteps;
return nErr;
}
int CPegasusController::setEnableRotaryEncoder(bool bEnabled)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
char szCmd[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
if(bEnabled)
sprintf(szCmd,"E:%d\n", R_ON);
else
sprintf(szCmd,"E:%d\n", R_OFF);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setEnableRotaryEncoder setting rotary enable : %s\n", timestamp, szCmd);
fflush(Logfile);
#endif
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
return nErr;
}
int CPegasusController::getEnableRotaryEncoder(bool &bEnabled)
{
int nErr;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = getConsolidatedStatus();
bEnabled = m_globalStatus.bEncodeEnabled;
return nErr;
}
int CPegasusController::getFirmwareVersion(char *pszVersion, int nStrMaxLen)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
if(!m_bIsConnected)
return NOT_CONNECTED;
nErr = dmfcCommand("V\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
strncpy(pszVersion, szResp, nStrMaxLen);
return nErr;
}
int CPegasusController::getTemperature(double &dTemperature)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("T\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
// convert response
dTemperature = atof(szResp);
return nErr;
}
int CPegasusController::getPosition(int &nPosition)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("P\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
// convert response
nPosition = atoi(szResp);
m_globalStatus.nCurPos = nPosition;
return nErr;
}
int CPegasusController::getLedStatus(int &nStatus)
{
int nErr = DMFC_OK;
int nLedStatus = 0;
char szResp[SERIAL_BUFFER_SIZE];
std::vector<std::string> sParsedResp;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("P\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
// parse response
nErr = parseResp(szResp, sParsedResp);
nLedStatus = atoi(sParsedResp[1].c_str());
switch(nLedStatus) {
case 0:
nStatus = OFF;
break;
case 1:
nStatus = ON;
break;
}
return nErr;
}
int CPegasusController::setLedStatus(int nStatus)
{
int nErr = DMFC_OK;
char szCmd[SERIAL_BUFFER_SIZE];
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
switch (nStatus) {
case ON:
snprintf(szCmd, SERIAL_BUFFER_SIZE, "L:%d\n", SWITCH_ON);
break;
case OFF:
snprintf(szCmd, SERIAL_BUFFER_SIZE, "L:%d\n", SWITCH_OFF);
break;
default:
break;
}
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
return nErr;
}
int CPegasusController::getMotorType(int &nType)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nType = m_globalStatus.nMotorType;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setMotorType getting motor type, m_globalStatus.nMotorType : %d\n", timestamp, nType);
fflush(Logfile);
#endif
nErr = dmfcCommand("R\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr) {
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setMotorType Error getting motor type : %s\n", timestamp, szResp);
fflush(Logfile);
#endif
return nErr;
}
if(strstr(szResp,"1")) {
nType = STEPPER;
}
else if(strstr(szResp,"0")) {
nType = DC;
}
else {
nErr = ERR_CMDFAILED;
}
m_globalStatus.nMotorType = nType;
return nErr;
}
int CPegasusController::setMotorType(int nType)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setMotorType setting motor type : %d\n", timestamp, nType);
fflush(Logfile);
#endif
if(m_globalStatus.nDeviceType == DMFC) {
switch (nType) {
case STEPPER:
nErr = dmfcCommand("R:1\n", szResp, SERIAL_BUFFER_SIZE);
break;
case DC:
nErr = dmfcCommand("R:0\n", szResp, SERIAL_BUFFER_SIZE);
break;
default:
break;
}
}
return nErr;
}
int CPegasusController::syncMotorPosition(int nPos)
{
int nErr = DMFC_OK;
char szCmd[SERIAL_BUFFER_SIZE];
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
snprintf(szCmd, SERIAL_BUFFER_SIZE, "W:%d\n", nPos);
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
nErr |= getConsolidatedStatus();
return nErr;
}
int CPegasusController::getRotaryEncPos(int &nPos)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = dmfcCommand("X\n", szResp, SERIAL_BUFFER_SIZE);
if(nErr)
return nErr;
// convert response
nPos = atoi(szResp);
return nErr;
}
int CPegasusController::getPosLimit()
{
return m_nPosLimit;
}
void CPegasusController::setPosLimit(int nLimit)
{
m_nPosLimit = nLimit;
}
bool CPegasusController::isPosLimitEnabled()
{
return m_bPosLimitEnabled;
}
void CPegasusController::enablePosLimit(bool bEnable)
{
m_bPosLimitEnabled = bEnable;
}
int CPegasusController::setReverseEnable(bool bEnabled)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
char szCmd[SERIAL_BUFFER_SIZE];
if(!m_bIsConnected)
return ERR_COMMNOLINK;
if(bEnabled)
sprintf(szCmd,"N:%d\n", REVERSE);
else
sprintf(szCmd,"N:%d\n", NORMAL);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setReverseEnable setting reverse : %s\n", timestamp, szCmd);
fflush(Logfile);
#endif
nErr = dmfcCommand(szCmd, szResp, SERIAL_BUFFER_SIZE);
#ifdef PEGA_DEBUG
if(nErr) {
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::setReverseEnable **** ERROR **** setting reverse (\"%s\") : %d\n", timestamp, szCmd, nErr);
fflush(Logfile);
}
#endif
return nErr;
}
int CPegasusController::getReverseEnable(bool &bEnabled)
{
int nErr;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
nErr = getConsolidatedStatus();
bEnabled = m_globalStatus.bReverse;
return nErr;
}
void CPegasusController::debugLinked(void)
{
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::debugLinked **** ERROR **** m_bLinked is false !!!!!! \n", timestamp);
fflush(Logfile);
#endif
}
#pragma mark command and response functions
int CPegasusController::dmfcCommand(const char *pszszCmd, char *pszResult, int nResultMaxLen)
{
int nErr = DMFC_OK;
char szResp[SERIAL_BUFFER_SIZE];
unsigned long ulBytesWrite;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
m_pSerx->purgeTxRx();
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::dmfcCommand] Sending %s\n",pszszCmd);
m_pLogger->out(m_szLogBuffer);
}
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::dmfcCommand Sending %s\n", timestamp, pszszCmd);
fflush(Logfile);
#endif
nErr = m_pSerx->writeFile((void *)pszszCmd, strlen(pszszCmd), ulBytesWrite);
m_pSerx->flushTx();
// printf("Command %s sent. wrote %lu bytes\n", szCmd, ulBytesWrite);
if(nErr){
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::dmfcCommand] writeFile Error.\n");
m_pLogger->out(m_szLogBuffer);
}
return nErr;
}
if(pszResult) {
// read response
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::dmfcCommand] Getting response.\n");
m_pLogger->out(m_szLogBuffer);
}
nErr = readResponse(szResp, SERIAL_BUFFER_SIZE);
if(nErr){
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::dmfcCommand] readResponse Error.\n");
m_pLogger->out(m_szLogBuffer);
}
}
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::dmfcCommand response \"%s\"\n", timestamp, szResp);
fflush(Logfile);
#endif
// printf("Got response : %s\n",resp);
strncpy(pszResult, szResp, nResultMaxLen);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::dmfcCommand response copied to pszResult : \"%s\"\n", timestamp, pszResult);
fflush(Logfile);
#endif
}
return nErr;
}
int CPegasusController::readResponse(char *pszRespBuffer, int nBufferLen)
{
int nErr = DMFC_OK;
unsigned long ulBytesRead = 0;
unsigned long ulTotalBytesRead = 0;
char *pszBufPtr;
if(!m_bIsConnected)
return ERR_COMMNOLINK;
memset(pszRespBuffer, 0, (size_t) nBufferLen);
pszBufPtr = pszRespBuffer;
do {
nErr = m_pSerx->readFile(pszBufPtr, 1, ulBytesRead, MAX_TIMEOUT);
if(nErr) {
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::readResponse] readFile Error.\n");
m_pLogger->out(m_szLogBuffer);
}
return nErr;
}
if (ulBytesRead !=1) {// timeout
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::readResponse] readFile Timeout.\n");
m_pLogger->out(m_szLogBuffer);
}
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::readResponse timeout\n", timestamp);
fflush(Logfile);
#endif
nErr = ERR_NORESPONSE;
break;
}
ulTotalBytesRead += ulBytesRead;
if (m_bDebugLog && m_pLogger) {
snprintf(m_szLogBuffer,LOG_BUFFER_SIZE,"[CPegasusController::readResponse] ulBytesRead = %lu\n",ulBytesRead);
m_pLogger->out(m_szLogBuffer);
}
} while (*pszBufPtr++ != '\n' && ulTotalBytesRead < nBufferLen );
if(ulTotalBytesRead)
*(pszBufPtr-1) = 0; //remove the \n
return nErr;
}
int CPegasusController::parseResp(char *pszResp, std::vector<std::string> &svParsedResp)
{
std::string sSegment;
std::vector<std::string> svSeglist;
std::stringstream ssTmp(pszResp);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::parseResp parsing \"%s\"\n", timestamp, pszResp);
fflush(Logfile);
#endif
svParsedResp.clear();
// split the string into vector elements
while(std::getline(ssTmp, sSegment, ':'))
{
svSeglist.push_back(sSegment);
#ifdef PEGA_DEBUG
ltime = time(NULL);
timestamp = asctime(localtime(<ime));
timestamp[strlen(timestamp) - 1] = 0;
fprintf(Logfile, "[%s] CPegasusController::parseResp sSegment : %s\n", timestamp, sSegment.c_str());
fflush(Logfile);
#endif
}
svParsedResp = svSeglist;
return DMFC_OK;
}