-
Notifications
You must be signed in to change notification settings - Fork 3
/
SPiiPlusDriver.cpp
2156 lines (1808 loc) · 71.3 KB
/
SPiiPlusDriver.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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstdarg>
#include <iocsh.h>
#include <epicsThread.h>
#include <asynOctetSyncIO.h>
#include "asynDriver.h"
#include "asynMotorController.h"
#include "asynMotorAxis.h"
#include <epicsExport.h>
#include <epicsString.h>
#include <cantProceed.h>
// SPiiPlusDriver.h includes SPiiPlusCommDriver.h
#include "SPiiPlusDriver.h"
static const char *driverName = "SPiiPlusController";
static void SPiiPlusProfileThreadC(void *pPvt);
#ifndef MAX
#define MAX(a,b) ((a)>(b)? (a): (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a)<(b)? (a): (b))
#endif
SPiiPlusController::SPiiPlusController(const char* ACSPortName, const char* asynPortName, int numAxes,
double movingPollPeriod, double idlePollPeriod)
: asynMotorController(ACSPortName, numAxes, NUM_SPIIPLUS_PARAMS, 0, 0, ASYN_CANBLOCK | ASYN_MULTIDEVICE, 1, 0, 0),
drvUser_(NULL),
initialized_(false)
{
const char* ACSCommPortSuffix = "Comm";
char* ACSCommPortName;
// Don't connect to the asyn port associated with the controller's ip address, the comm class will do that
//asynStatus status = pasynOctetSyncIO->connect(asynPortName, 0, &pasynUserController_, NULL);
ACSCommPortName = (char *) malloc(strlen(ACSPortName) + strlen(ACSCommPortSuffix));
strcpy(ACSCommPortName, ACSPortName);
strcat(ACSCommPortName, ACSCommPortSuffix);
// should numAxes be hard-coded to zero for the comm class?
pComm_ = new SPiiPlusComm(ACSCommPortName, asynPortName, numAxes);
pAxes_ = (SPiiPlusAxis **)(asynMotorController::pAxes_);
std::stringstream cmd;
static const char *functionName="SPiiPlusController";
// Create parameters
createParam(SPiiPlusHomingMethodString, asynParamInt32, &SPiiPlusHomingMethod_);
createParam(SPiiPlusMaxVelocityString, asynParamFloat64, &SPiiPlusMaxVelocity_);
createParam(SPiiPlusMaxAccelerationString, asynParamFloat64, &SPiiPlusMaxAcceleration_);
createParam(SPiiPlusReadIntVarString, asynParamInt32, &SPiiPlusReadIntVar_);
createParam(SPiiPlusWriteIntVarString, asynParamInt32, &SPiiPlusWriteIntVar_);
createParam(SPiiPlusReadRealVarString, asynParamFloat64, &SPiiPlusReadRealVar_);
createParam(SPiiPlusWriteRealVarString, asynParamFloat64, &SPiiPlusWriteRealVar_);
createParam(SPiiPlusStartProgramString, asynParamInt32, &SPiiPlusStartProgram_);
createParam(SPiiPlusStopProgramString, asynParamInt32, &SPiiPlusStopProgram_);
createParam(SPiiPlusSafeTorqueOffString, asynParamInt32, &SPiiPlusSafeTorqueOff_);
createParam(SPiiPlusHomingProcedureDoneString, asynParamInt32, &SPiiPlusHomingProcedureDone_);
//
createParam(SPiiPlusStepFactorString, asynParamFloat64, &SPiiPlusStepFactor_);
createParam(SPiiPlusEncTypeString, asynParamInt32, &SPiiPlusEncType_);
createParam(SPiiPlusEnc2TypeString, asynParamInt32, &SPiiPlusEnc2Type_);
createParam(SPiiPlusEncFactorString, asynParamFloat64, &SPiiPlusEncFactor_);
createParam(SPiiPlusEnc2FactorString, asynParamFloat64, &SPiiPlusEnc2Factor_);
//
createParam(SPiiPlusAxisPosString, asynParamFloat64, &SPiiPlusAxisPos_);
createParam(SPiiPlusRefPosString, asynParamFloat64, &SPiiPlusRefPos_);
createParam(SPiiPlusEncPosString, asynParamFloat64, &SPiiPlusEncPos_);
createParam(SPiiPlusFdbkPosString, asynParamFloat64, &SPiiPlusFdbkPos_);
createParam(SPiiPlusFdbk2PosString, asynParamFloat64, &SPiiPlusFdbk2Pos_);
//
createParam(SPiiPlusRefOffsetString, asynParamFloat64, &SPiiPlusRefOffset_);
createParam(SPiiPlusEncOffsetString, asynParamFloat64, &SPiiPlusEncOffset_);
createParam(SPiiPlusEnc2OffsetString, asynParamFloat64, &SPiiPlusEnc2Offset_);
createParam(SPiiPlusAbsEncOffsetString, asynParamFloat64, &SPiiPlusAbsEncOffset_);
createParam(SPiiPlusAbsEnc2OffsetString, asynParamFloat64, &SPiiPlusAbsEnc2Offset_);
//
createParam(SPiiPlusTestString, asynParamInt32, &SPiiPlusTest_);
// Initialize this variable to avoid freeing random memory
fullProfileTimes_ = 0;
// Query setup parameters
pComm_->getIntegerArray((char *)motorFlags_, "MFLAGS", 0, numAxes_-1, 0, 0);
pComm_->getDoubleArray((char *)stepperFactor_, "STEPF", 0, numAxes_-1, 0, 0);
pComm_->getDoubleArray((char *)encoderFactor_, "EFAC", 0, numAxes_-1, 0, 0);
pComm_->getDoubleArray((char *)encoder2Factor_, "E2FAC", 0, numAxes_-1, 0, 0);
pComm_->getIntegerArray((char *)encoderType_, "E_TYPE", 0, numAxes_-1, 0, 0);
pComm_->getIntegerArray((char *)encoder2Type_, "E2_TYPE", 0, numAxes_-1, 0, 0);
for (int index = 0; index < numAxes; index += 1)
{
new SPiiPlusAxis(this, index);
// Parse the setup parameters
// Bit 0 is #DUMMY (dummy axis)
pAxes_[index]->dummy_ = motorFlags_[index] & SPIIPLUS_MFLAGS_DUMMY;
// Bit 1 is #OPEN (open-loop control)
pAxes_[index]->open_ = motorFlags_[index] & SPIIPLUS_MFLAGS_OPEN;
// Bit 2 is #MICRO (microstepper mode)
pAxes_[index]->micro_ = motorFlags_[index] & SPIIPLUS_MFLAGS_MICRO;
// Bit 3 is #HOME (homing procedure done)
pAxes_[index]->home_ = motorFlags_[index] & SPIIPLUS_MFLAGS_HOME;
// Bit 4 is #STEPPER (pulse/direction stepper)
pAxes_[index]->stepper_ = motorFlags_[index] & SPIIPLUS_MFLAGS_STEPPER;
// Bit 5 is #ENCLOOP (stepper with steps as feedback)
pAxes_[index]->encloop_ = motorFlags_[index] & SPIIPLUS_MFLAGS_ENCLOOP;
// Bit 6 is #STEPENC (stepper with encoder as feedback)
pAxes_[index]->stepenc_ = motorFlags_[index] & SPIIPLUS_MFLAGS_STEPENC;
// Bit 8 is #BRUSHL (brushless motor)
pAxes_[index]->brushl_ = motorFlags_[index] & SPIIPLUS_MFLAGS_BRUSHL;
// Bit 9 is #BRUSHOK (brushless commutation OK)
pAxes_[index]->brushok_ = motorFlags_[index] & SPIIPLUS_MFLAGS_BRUSHOK;
// Bit 10 is #PHASE2 (2-phase motor)
pAxes_[index]->phase2_ = motorFlags_[index] & SPIIPLUS_MFLAGS_PHASE2;
// Bit 21 is #LINEAR (linear motor)
pAxes_[index]->linear_ = motorFlags_[index] & SPIIPLUS_MFLAGS_LINEAR;
// Bit 22 is #ABSCOMM (absolute encoder commutation)
pAxes_[index]->abscomm_ = motorFlags_[index] & SPIIPLUS_MFLAGS_ABSCOMM;
// Bit 27 is #HALL (hall commutation)
pAxes_[index]->hall_ = motorFlags_[index] & SPIIPLUS_MFLAGS_HALL;
// axis resolution (used to convert motor record steps into controller EGU)
// TODO: how should nanomotion piezo ceramic motors (bit 7 of mflags) be handled?
if ((pAxes_[index]->brushl_ == 0) && (pAxes_[index]->linear_ == 0))
{
// Use the stepper factor as the resolution for stepper motors
pAxes_[index]->resolution_ = stepperFactor_[index];
}
else
{
// Use the encoder factor as the resolution for brushless and linear motors
// TODO: how to handle mutliple encoders?
pAxes_[index]->resolution_ = encoderFactor_[index];
}
// Update parameters that shouldn't change while the IOC is running
setDoubleParam(index, SPiiPlusStepFactor_, stepperFactor_[index]);
setIntegerParam(index, SPiiPlusEncType_, encoderType_[index]);
setIntegerParam(index, SPiiPlusEnc2Type_, encoder2Type_[index]);
setDoubleParam(index, SPiiPlusEncFactor_, encoderFactor_[index]);
setDoubleParam(index, SPiiPlusEnc2Factor_, encoder2Factor_[index]);
// Initialize absolute encoders
if (encoderType_[index] > 4)
{
// Clear encoder error so absolute encoder position will be valid
cmd << "FCLEAR " << index;
pComm_->writeReadAck(cmd);
}
// Initialize this variable to avoid freeing random memory
pAxes_[index]->fullProfilePositions_ = 0;
}
drvUser_ = (SPiiPlusDrvUser_t *) callocMustSucceed(1, sizeof(SPiiPlusDrvUser_t), functionName);
drvUser_->programName = "undefined";
drvUser_->len = -1;
this->startPoller(movingPollPeriod, idlePollPeriod, 2);
// Create the event that wakes up the thread for profile moves
profileExecuteEvent_ = epicsEventMustCreate(epicsEventEmpty);
// Create the thread that will execute profile moves
epicsThreadCreate("SPiiPlusProfile",
epicsThreadPriorityLow,
epicsThreadGetStackSize(epicsThreadStackMedium),
(EPICSTHREADFUNC)SPiiPlusProfileThreadC, (void *)this);
initialized_ = true;
}
asynStatus SPiiPlusController::drvUserCreate(asynUser *pasynUser,
const char *drvInfo,
const char **pptypeName, size_t *psize)
{
static const char *functionName = "drvUserCreate";
int index;
const char *drvInfoNew;
pasynUser->drvUser = drvUser_;
if (initialized_ == false) {
pasynManager->enable(pasynUser, 0);
return asynDisabled;
}
// drvUserCreate(pasynUser=0x23e29e8, drvInfo=SPIIPLUS_READ_REAL_VAR, pptypeName=(nil), psize=(nil))
//printf("drvUserCreate(pasynUser=%p, drvInfo=%s, pptypeName=%p, psize=%p)\n", pasynUser, drvInfo, pptypeName, psize);
// findParam returns 0 if drvInfo matches a parameter string
if (findParam(drvInfo, &index))
{
//printf("drvUserCreate(pasynUser=%p, drvInfo=%s, pptypeName=%p, psize=%p, drvUser=%p)\n", pasynUser, drvInfo, pptypeName, psize, (const char*) pasynUser->drvUser);
if (strlen(drvInfo) > 15 && !epicsStrnCaseCmp(drvInfo, SPiiPlusStartProgramString, 15))
{
SPiiPlusDrvUser_t *drvUser = (SPiiPlusDrvUser_t *) callocMustSucceed(1, sizeof(SPiiPlusDrvUser_t), functionName);
drvUser->programName = epicsStrDup(drvInfo+15);
drvUser->len = strlen(drvUser->programName);
pasynUser->drvUser = drvUser;
drvInfo = SPiiPlusStartProgramString;
//printf("drvUserCreate(pasynUser=%p, drvInfo=%s, pptypeName=%p, psize=%p, drvUser=%p)\n", pasynUser, drvInfo, pptypeName, psize, pasynUser->drvUser);
}
else if (strlen(drvInfo) > 14 && !epicsStrnCaseCmp(drvInfo, SPiiPlusStopProgramString, 14))
{
SPiiPlusDrvUser_t *drvUser = (SPiiPlusDrvUser_t *) callocMustSucceed(1, sizeof(SPiiPlusDrvUser_t), functionName);
drvUser->programName = epicsStrDup(drvInfo+14);
drvUser->len = strlen(drvUser->programName);
pasynUser->drvUser = drvUser;
drvInfo = SPiiPlusStopProgramString;
//printf("drvUserCreate(pasynUser=%p, drvInfo=%s, pptypeName=%p, psize=%p, drvUser=%p)\n", pasynUser, drvInfo, pptypeName, psize, pasynUser->drvUser);
}
}
return asynPortDriver::drvUserCreate(pasynUser, drvInfo, pptypeName, psize);
}
asynStatus SPiiPlusController::drvUserDestroy(asynUser *pasynUser)
{
if (pasynUser->drvUser != drvUser_) {
free(pasynUser->drvUser);
}
pasynUser->drvUser = NULL;
return asynSuccess;
}
// It is necessary to implement getAddress to avoid the maxAddr checks from the base class
asynStatus SPiiPlusController::getAddress(asynUser *pasynUser, int *address)
{
pasynManager->getAddr(pasynUser, address);
if (*address > this->maxAddr-1)
{
// A larger address corresponds to a global variable tag
*address = 0;
}
else
{
// asynMotorController doesn't implement get address, so asynPortDriver::getAddress is called
asynMotorController::getAddress(pasynUser, address);
}
return asynSuccess;
}
asynStatus SPiiPlusController::readInt32(asynUser *pasynUser, epicsInt32 *value)
{
int function = pasynUser->reason;
int status = asynSuccess;
//static const char *functionName = "readInt32";
*value = 0;
if (function == SPiiPlusReadIntVar_)
{
status = readGlobalIntVar(pasynUser, value);
}
/*else if (function == SPiiPlusSafeTorqueOff_)
{
// Is this necessary? Would the default method do the same thing?
int axisNo_;
// Get the addr, which is the axis number
pasynManager->getAddr(pasynUser, &axisNo_);
// The poller queries the STO value; return the latest value
getIntegerParam(axisNo_, SPiiPlusSafeTorqueOff_, value);
}*/
else
{
/* Call base class method */
status = asynMotorController::readInt32(pasynUser, value);
}
return (asynStatus)status;
}
asynStatus SPiiPlusController::writeInt32(asynUser *pasynUser, epicsInt32 value)
{
int function = pasynUser->reason;
int status = asynSuccess;
SPiiPlusAxis *pAxis;
static const char *functionName = "writeInt32";
pAxis = this->getAxis(pasynUser);
if (!pAxis) return asynError;
/* Set the parameter and readback in the parameter library. This may be overwritten when we read back the
* status at the end, but that's OK */
status = pAxis->setIntegerParam(function, value);
if (function == SPiiPlusHomingMethod_)
{
//
asynPrint(this->pasynUserSelf, ASYN_TRACEIO_DRIVER, "%s:%s: homingMethod = %i\n", driverName, functionName, value);
}
if (function == SPiiPlusTest_)
{
/* Do something helpful during development */
status = test();
}
else if (function == SPiiPlusWriteIntVar_)
{
status = writeGlobalIntVar(pasynUser, value);
}
else if (function == SPiiPlusStartProgram_)
{
status = startProgram(pasynUser, value);
}
else if (function == SPiiPlusStopProgram_)
{
status = stopProgram(pasynUser, value);
}
else
{
/* Call base class method */
status = asynMotorController::writeInt32(pasynUser, value);
}
/* Do callbacks so higher layers see any changes */
pAxis->callParamCallbacks();
return (asynStatus)status;
}
asynStatus SPiiPlusController::readFloat64(asynUser *pasynUser, epicsFloat64 *value)
{
int function = pasynUser->reason;
int status = asynSuccess;
//static const char *functionName = "readFloat64";
*value = 0;
if (function == SPiiPlusReadRealVar_)
{
status = readGlobalRealVar(pasynUser, value);
}
else
{
/* Call base class method */
status = asynMotorController::readFloat64(pasynUser, value);
}
return (asynStatus)status;
}
/** Called when asyn clients call pasynFloat64->write().
* \param[in] pasynUser asynUser structure that encodes the reason and address.
* \param[in] value Value to write. */
asynStatus SPiiPlusController::writeFloat64(asynUser *pasynUser, epicsFloat64 value)
{
int function = pasynUser->reason;
SPiiPlusAxis *pAxis;
asynStatus status = asynError;
//static const char *functionName = "writeFloat64";
pAxis = getAxis(pasynUser);
if (!pAxis) return asynError;
/* Set the parameter and readback in the parameter library. */
status = pAxis->setDoubleParam(function, value);
if (function == SPiiPlusMaxVelocity_)
{
status = pAxis->setMaxVelocity(value);
}
else if (function == SPiiPlusMaxAcceleration_)
{
status = pAxis->setMaxAcceleration(value);
}
else if (function == SPiiPlusWriteRealVar_)
{
status = writeGlobalRealVar(pasynUser, value);
}
else
{
/* Call base class method */
status = asynMotorController::writeFloat64(pasynUser, value);
}
/* Do callbacks so higher layers see any changes */
pAxis->callParamCallbacks();
return status;
}
SPiiPlusAxis* SPiiPlusController::getAxis(asynUser *pasynUser)
{
return static_cast<SPiiPlusAxis*>(asynMotorController::getAxis(pasynUser));
}
SPiiPlusAxis* SPiiPlusController::getAxis(int axisNo)
{
return static_cast<SPiiPlusAxis*>(asynMotorController::getAxis(axisNo));
}
asynStatus SPiiPlusController::poll()
{
asynStatus status;
static const char *functionName = "poll";
/*
* Read position and status using binary queries here and parse the replies in the axis poll method
*
* FPOS = FP*EFAC + EOFFS
* F2POS = FP2*E2FAC + E2OFFS
*
*/
asynPrint(this->pasynUserSelf, ASYN_TRACE_FLOW, "%s:%s: POLL_START\n", driverName, functionName);
/* positions */
status = pComm_->getDoubleArray((char *)axisPosition_, "APOS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
// RPOS = APOS if MFLAGS(index).#DEFCON=1
status = pComm_->getDoubleArray((char *)referencePosition_, "RPOS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)encoderPosition_, "EPOS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)feedbackPosition_, "FPOS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)feedback2Position_, "F2POS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
/* offsets */
// RPOS = 0 if MFLAGS(index).#DEFCON=1
status = pComm_->getDoubleArray((char *)referenceOffset_, "ROFFS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)encoderOffset_, "EOFFS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)encoder2Offset_, "E2OFFS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)absoluteEncoderOffset_, "E_AOFFS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
// TODO: re-add E2_AOFFS query after querying the firmware version
// E2_AOFFS doesn't exist in firmware v2.70
//status = pComm_->getDoubleArray((char *)absoluteEncoder2Offset_, "E2_AOFFS", 0, numAxes_-1, 0, 0);
//if (status != asynSuccess) return status;
/* statuses */
status = pComm_->getIntegerArray((char *)axisStatus_, "AST", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getIntegerArray((char *)motorStatus_, "MST", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getIntegerArray((char *)faultStatus_, "FAULT", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
// MFLAGS need to be polled here for the homed status
status = pComm_->getIntegerArray((char *)motorFlags_, "MFLAGS", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
// TODO: only get max values when idle polling
/* max values */
status = pComm_->getDoubleArray((char *)maxVelocity_, "XVEL", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
status = pComm_->getDoubleArray((char *)maxAcceleration_, "XACC", 0, numAxes_-1, 0, 0);
if (status != asynSuccess) return status;
asynPrint(this->pasynUserSelf, ASYN_TRACE_FLOW, "%s:%s: POLL_END\n", driverName, functionName);
return status;
}
asynStatus SPiiPlusController::readGlobalIntVar(asynUser *pasynUser, epicsInt32 *value)
{
asynStatus status;
std::stringstream cmd;
int tag;
// Get the address, which is the tag of the global integer, rather than an axis index
pasynManager->getAddr(pasynUser, &tag);
// ?GETVAR(tag)
cmd << "?GETVAR(" << tag << ")";
status = pComm_->writeReadInt(cmd, value);
return status;
}
asynStatus SPiiPlusController::writeGlobalIntVar(asynUser *pasynUser, epicsInt32 value)
{
asynStatus status;
std::stringstream cmd;
int tag;
// Get the address, which is the tag of the global integer, rather than an axis index
pasynManager->getAddr(pasynUser, &tag);
// SETVAR(value, tag)
cmd << "SETVAR(" << value << "," << tag << ")";
status = pComm_->writeReadAck(cmd);
return status;
}
asynStatus SPiiPlusController::readGlobalRealVar(asynUser *pasynUser, epicsFloat64 *value)
{
asynStatus status;
std::stringstream cmd;
int tag;
// Get the address, which is the tag of the global integer, rather than an axis index
pasynManager->getAddr(pasynUser, &tag);
// ?GETVAR(tag)
cmd << "?GETVAR(" << tag << ")";
status = pComm_->writeReadDouble(cmd, value);
return status;
}
asynStatus SPiiPlusController::writeGlobalRealVar(asynUser *pasynUser, epicsFloat64 value)
{
asynStatus status;
std::stringstream cmd;
int tag;
// Get the address, which is the tag of the global integer, rather than an axis index
pasynManager->getAddr(pasynUser, &tag);
// SETVAR(value, tag)
cmd << "SETVAR(" << value << "," << tag << ")";
status = pComm_->writeReadAck(cmd);
return status;
}
asynStatus SPiiPlusController::startProgram(asynUser *pasynUser, epicsFloat64 value)
{
asynStatus status;
std::stringstream cmd;
int buffer;
static const char *functionName = "startProgram";
// Get the address, which is the buffer # containing the program, rather than an axis index
pasynManager->getAddr(pasynUser, &buffer);
asynPrint(this->pasynUserSelf, ASYN_TRACEIO_DRIVER, "%s:%s: drvUser->programName = %s, drvUser->len = %i\n", driverName, functionName, ((SPiiPlusDrvUser_t *)pasynUser->drvUser)->programName, ((SPiiPlusDrvUser_t *)pasynUser->drvUser)->len);
// START buffer,label -or- START buffer,line_no
cmd << "START " << buffer << "," << ((SPiiPlusDrvUser_t *)pasynUser->drvUser)->programName;
status = pComm_->writeReadAck(cmd);
return status;
}
asynStatus SPiiPlusController::stopProgram(asynUser *pasynUser, epicsFloat64 value)
{
asynStatus status;
std::stringstream cmd;
int buffer;
static const char *functionName = "stopProgram";
// Get the address, which is the buffer # containing the program, rather than an axis index
pasynManager->getAddr(pasynUser, &buffer);
asynPrint(this->pasynUserSelf, ASYN_TRACEIO_DRIVER, "%s:%s: drvUser->programName = %s, drvUser->len = %i\n", driverName, functionName, ((SPiiPlusDrvUser_t *)pasynUser->drvUser)->programName, ((SPiiPlusDrvUser_t *)pasynUser->drvUser)->len);
// STOP buffer
cmd << "STOP " << buffer;
status = pComm_->writeReadAck(cmd);
return status;
}
SPiiPlusAxis::SPiiPlusAxis(SPiiPlusController *pC, int axisNo)
: asynMotorAxis(pC, axisNo),
pC_(pC)
{
setIntegerParam(pC->motorStatusHasEncoder_, 1);
// Gain Support is required for setClosedLoop to be called
setIntegerParam(pC->motorStatusGainSupport_, 1);
}
asynStatus SPiiPlusAxis::poll(bool* moving)
{
asynStatus status = asynSuccess;
SPiiPlusController* controller = (SPiiPlusController*) pC_;
//static const char *functionName = "poll";
std::stringstream cmd;
// APOS (queried in controller poll method)
setDoubleParam(controller->motorPosition_, (controller->axisPosition_[axisNo_] / resolution_));
if (dummy_)
{
// APOS (queried in controller poll method)
setDoubleParam(controller->motorEncoderPosition_, (controller->axisPosition_[axisNo_] / resolution_));
// Faults are disabled for dummy axes
setIntegerParam(controller->motorStatusLowLimit_, 0);
setIntegerParam(controller->motorStatusHighLimit_, 0);
}
else
{
// FPOS (queried in controller poll method) = FP * EFAC + EOFFS
// TODO: detect when there is no encoder and fix the encoder position at zero
setDoubleParam(controller->motorEncoderPosition_, controller->feedbackPosition_[axisNo_] / controller->encoderFactor_[axisNo_]);
// FAULT (queried in controller poll method)
int left_limit, right_limit, sto, home;
left_limit = controller->faultStatus_[axisNo_] & SPIIPLUS_FAULT_HARD_LEFT_LIMIT;
setIntegerParam(controller->motorStatusLowLimit_, left_limit);
right_limit = controller->faultStatus_[axisNo_] & SPIIPLUS_FAULT_HARD_RIGHT_LIMIT;
setIntegerParam(controller->motorStatusHighLimit_, right_limit);
sto = controller->faultStatus_[axisNo_] & SPIIPLUS_FAULT_SAFE_TORQUE_OFF;
setIntegerParam(controller->SPiiPlusSafeTorqueOff_, sto);
// MFLAGS HOME (queried in controller poll method)
home = controller->motorFlags_[axisNo_] & SPIIPLUS_MFLAGS_HOME;
setIntegerParam(controller->SPiiPlusHomingProcedureDone_, home);
}
getMaxParams();
updateFeedbackParams();
// AST (queried in controller poll method)
int enabled;
int motion;
//int open_loop;
//int in_pos;
if (dummy_)
{
enabled = 0;
motion = controller->axisStatus_[axisNo_] & (1<<5);
}
else
{
// MST (queried in controller poll method)
enabled = controller->motorStatus_[axisNo_] & (1<<0);
motion = controller->motorStatus_[axisNo_] & (1<<5);
//open_loop = controller->motorStatus_[axisNo_] & (1<<1);
//in_pos = controller->motorStatus_[axisNo_] & (1<<4);
}
setIntegerParam(controller->motorStatusDone_, !motion);
setIntegerParam(controller->motorStatusMoving_, motion);
setIntegerParam(controller->motorStatusPowerOn_, enabled);
callParamCallbacks();
moving_ = *moving;
if (motion) { *moving = true; }
else { *moving = false; }
return status;
}
asynStatus SPiiPlusAxis::getMaxParams()
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status = asynSuccess;
double motorRecResolution;
double maxVelocity, maxAcceleration;
std::stringstream cmd;
// motorRecResolution (EGU/step)
controller->getDoubleParam(axisNo_, controller->motorRecResolution_, &motorRecResolution);
// XVEL [SPiiPlus-units/sec] (queried in controller poll method)
maxVelocity = controller->maxVelocity_[axisNo_] / resolution_ * motorRecResolution;
// XACC [SPiiPlus-units/sec^2] (queried in controller poll method)
maxAcceleration = controller->maxAcceleration_[axisNo_] / resolution_ * motorRecResolution;
// (SPiiPlus-units/time-unit) / (SPiiPlus-units/step) * (EGU/step) = (EGU/time-unit)
controller->setDoubleParam(axisNo_, controller->SPiiPlusMaxVelocity_, maxVelocity);
controller->setDoubleParam(axisNo_, controller->SPiiPlusMaxAcceleration_, maxAcceleration);
// Assume the calling method will call callParamCallbacks()
return status;
}
asynStatus SPiiPlusAxis::updateFeedbackParams()
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status = asynSuccess;
std::stringstream cmd;
// Update the axis parameters with the values that were queried in the controller poll method
controller->setDoubleParam(axisNo_, controller->SPiiPlusAxisPos_, controller->axisPosition_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusRefPos_, controller->referencePosition_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusEncPos_, controller->encoderPosition_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusFdbkPos_, controller->feedbackPosition_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusFdbk2Pos_, controller->feedback2Position_[axisNo_]);
//
controller->setDoubleParam(axisNo_, controller->SPiiPlusRefOffset_, controller->referenceOffset_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusEncOffset_, controller->encoderOffset_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusEnc2Offset_, controller->encoder2Offset_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusAbsEncOffset_, controller->absoluteEncoderOffset_[axisNo_]);
controller->setDoubleParam(axisNo_, controller->SPiiPlusAbsEnc2Offset_, controller->absoluteEncoder2Offset_[axisNo_]);
// Assume the calling method will call callParamCallbacks()
return status;
}
asynStatus SPiiPlusAxis::setMaxVelocity(double maxVelocity)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status;
double motorRecResolution;
std::stringstream cmd;
// motorRecResolution is in EGU / step
controller->getDoubleParam(axisNo_, controller->motorRecResolution_, &motorRecResolution);
// (EGU/s) / (EGU/step) * (SPiiPlus-units/step) = (SPiiPlus-units/s)
cmd << "XVEL(" << axisNo_ << ")=" << (maxVelocity / motorRecResolution * resolution_);
status = controller->pComm_->writeReadAck(cmd);
return status;
}
asynStatus SPiiPlusAxis::setMaxAcceleration(double maxAcceleration)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status;
double motorRecResolution;
std::stringstream cmd;
// motorRecResolution is in EGU / step
controller->getDoubleParam(axisNo_, controller->motorRecResolution_, &motorRecResolution);
// (EGU/s^2) / (EGU/step) * (SPiiPlus-units/step) = (SPiiPlus-units/s^2)
cmd << "XACC(" << axisNo_ << ")=" << (maxAcceleration / motorRecResolution * resolution_);
status = controller->pComm_->writeReadAck(cmd);
return status;
}
asynStatus SPiiPlusAxis::move(double position, int relative, double minVelocity, double maxVelocity, double acceleration)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status;
double deviceUnits;
std::stringstream cmd;
//cmd << "XACC(" << axisNo_ << ")=" << ((acceleration + 10) * resolution_);
//status = writeReadAck(controller, cmd);
cmd << "ACC(" << axisNo_ << ")=" << (acceleration * resolution_);
status = controller->pComm_->writeReadAck(cmd);
cmd << "DEC(" << axisNo_ << ")=" << (acceleration * resolution_);
status = controller->pComm_->writeReadAck(cmd);
//cmd << "XVEL(" << axisNo_ << ")=" << ((maxVelocity + 10) * resolution_);
//status = writeReadAck(controller, cmd);
cmd << "VEL(" << axisNo_ << ")=" << (maxVelocity * resolution_);
status = controller->pComm_->writeReadAck(cmd);
if (relative)
{
cmd << "PTP/r " << axisNo_ << ", " << (position * resolution_);
status = controller->pComm_->writeReadAck(cmd);
}
else
{
cmd << "PTP " << axisNo_ << ", " << (position * resolution_);
status = controller->pComm_->writeReadAck(cmd);
}
return status;
}
asynStatus SPiiPlusAxis::setPosition(double position)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status;
std::stringstream cmd;
// The controller automatically updates APOS and FPOS when RPOS is updated
cmd << "SET RPOS(" << axisNo_ << ")=" << (position * resolution_);
status = controller->pComm_->writeReadAck(cmd);
return status;
}
asynStatus SPiiPlusAxis::stop(double acceleration)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status;
std::stringstream cmd;
cmd << "HALT " << axisNo_;
status = controller->pComm_->writeReadAck(cmd);
return status;
}
/** Set the motor closed loop status.
* \param[in] closedLoop true = close loop, false = open looop. */
asynStatus SPiiPlusAxis::setClosedLoop(bool closedLoop)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status=asynSuccess;
std::stringstream cmd;
if (!dummy_)
{
/*
Enable/disable the axis instead of changing the closed-loop state.
*/
if (closedLoop)
{
cmd << "ENABLE " << axisNo_;
}
else
{
cmd << "DISABLE " << axisNo_;
}
status = controller->pComm_->writeReadAck(cmd);
}
return status;
}
/** Move the motor to the home position.
* \param[in] minVelocity The initial velocity, often called the base velocity. Units=steps/sec.
* \param[in] maxVelocity The maximum velocity, often called the slew velocity. Units=steps/sec.
* \param[in] acceleration The acceleration value. Units=steps/sec/sec.
* \param[in] forwards Flag indicating to move the motor in the forward direction(1) or reverse direction(0).
* Some controllers need to be told the direction, others know which way to go to home. */
asynStatus SPiiPlusAxis::home(double minVelocity, double maxVelocity, double acceleration, int forwards)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
asynStatus status=asynSuccess;
std::stringstream cmd;
epicsInt32 mbboHomingMethod;
epicsInt32 homingMethod;
static const char *functionName = "home";
controller->getIntegerParam(axisNo_, controller->SPiiPlusHomingMethod_, &mbboHomingMethod);
switch(mbboHomingMethod)
{
case MBBO_HOME_NONE:
homingMethod = SPIIPLUS_HOME_NONE;
break;
case MBBO_HOME_LIMIT_INDEX:
if (forwards == 0)
homingMethod = SPIIPLUS_HOME_NEG_LIMIT_INDEX;
else
homingMethod = SPIIPLUS_HOME_POS_LIMIT_INDEX;
break;
case MBBO_HOME_LIMIT:
if (forwards == 0)
homingMethod = SPIIPLUS_HOME_NEG_LIMIT;
else
homingMethod = SPIIPLUS_HOME_POS_LIMIT;
break;
case MBBO_HOME_INDEX:
if (forwards == 0)
homingMethod = SPIIPLUS_HOME_NEG_INDEX;
else
homingMethod = SPIIPLUS_HOME_POS_INDEX;
break;
case MBBO_HOME_CURRENT_POS:
homingMethod = SPIIPLUS_HOME_CURRENT_POS;
break;
case MBBO_HOME_HARDSTOP_INDEX:
if (forwards == 0)
homingMethod = SPIIPLUS_HOME_NEG_HARDSTOP_INDEX;
else
homingMethod = SPIIPLUS_HOME_POS_HARDSTOP_INDEX;
break;
case MBBO_HOME_HARDSTOP:
if (forwards == 0)
homingMethod = SPIIPLUS_HOME_NEG_HARDSTOP;
else
homingMethod = SPIIPLUS_HOME_POS_HARDSTOP;
break;
case MBBO_HOME_CUSTOM:
//if (forwards == 0)
// ???
//else
// ???
// Do nothing for now
homingMethod = SPIIPLUS_HOME_NONE;
break;
default:
homingMethod = SPIIPLUS_HOME_NONE;
break;
}
if (homingMethod == SPIIPLUS_HOME_NONE)
{
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s:%s: No homing method selected for axis %i\n", driverName, functionName, axisNo_);
// Should status be set to asynError here?
}
else
{
// HOME Axis, [opt]HomingMethod,[opt]HomingVel,[opt]MaxDistance,[opt]HomingOffset,[opt]HomingCurrLimit,[opt]HardStopThreshold
cmd << "HOME " << axisNo_ << "," << homingMethod << "," << (maxVelocity * resolution_);
//asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s:%s: home command = %s\n", driverName, functionName, cmd.str().c_str());
status = controller->pComm_->writeReadAck(cmd);
}
return status;
}
/** Function to define the motor positions for a profile move.
* Called by asynMotorController::writeFloat64Array
* This calls the base class defineProfile method to convert to steps, but since the
* SPiiPlus works in user-units we need to do an additional conversion by resolution_.
* \param[in] positions Array of profile positions for this axis in user units.
* \param[in] numPoints The number of positions in the array.
*/
asynStatus SPiiPlusAxis::defineProfile(double *positions, size_t numPoints)
{
size_t i;
asynStatus status;
//static const char *functionName = "defineProfile";
// Call the base class function (converts from EGU to steps)
status = asynMotorAxis::defineProfile(positions, numPoints);
if (status) return status;
// Convert from steps to SPiiPlus user units
for (i=0; i<numPoints; i++) {
profilePositions_[i] = profilePositions_[i]*resolution_;
}
return asynSuccess;
}
/** Reports on status of the axis
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* If details > 0 then information is printed about each axis.
* After printing controller-specific information it calls asynMotorController::report()
*/
void SPiiPlusAxis::report(FILE *fp, int level)
{
SPiiPlusController* controller = (SPiiPlusController*) pC_;
epicsInt32 homingMethod;
controller->getIntegerParam(axisNo_, controller->SPiiPlusHomingMethod_, &homingMethod);
fprintf(fp, "Configuration for axis %i:\n", axisNo_);
fprintf(fp, " mflags: %i\n", controller->motorFlags_[axisNo_]);
fprintf(fp, " dummy: %i\n", dummy_);
fprintf(fp, " open: %i\n", open_);
fprintf(fp, " micro: %i\n", micro_);
fprintf(fp, " homed: %i\n", home_);
fprintf(fp, " stepper: %i\n", stepper_);
fprintf(fp, " encloop: %i\n", encloop_);
fprintf(fp, " stepenc: %i\n", stepenc_);
fprintf(fp, " brushl: %i\n", brushl_);
fprintf(fp, " brushok %i\n", brushok_);
fprintf(fp, " phase2: %i\n", phase2_);
fprintf(fp, " linear: %i\n", linear_);
fprintf(fp, " abscomm: %i\n", abscomm_);
fprintf(fp, " hall: %i\n", hall_);
fprintf(fp, " resolution: %.6e\n", resolution_);
fprintf(fp, " reference offset: %lf\n", controller->referenceOffset_[axisNo_]);
fprintf(fp, " homing method: %i\n", homingMethod);