-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqladisp.cpp
976 lines (911 loc) · 39.2 KB
/
qladisp.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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
/*
Author(s): Peter Kazanzides, Zihan Chen, Anton Deguet
(C) Copyright 2012-2023 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/
/******************************************************************************
*
* This program continuously displays the sensor feedback from the selected
* board. It relies on the Amp1394 library (which depends on libraw1394 and/or pcap)
* and on the Amp1394Console library (which may depend on curses).
*
* Usage: qladisp [-pP] [-b<r|w>] [-v] <board num> [<board_num>]
* where P is the port number (default 0),
* or a string such as ethP and fwP, where P is the port number
* -br or -bw specify to use a broadcast protocol
* -v specifies to display full velocity feedback
*
******************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm> // for std::max
#include <Amp1394/AmpIORevision.h>
#include "PortFactory.h"
#include "AmpIO.h"
#include "Amp1394Time.h"
#include "Amp1394Console.h"
#include "EthBasePort.h"
// Maximum number of axes (channels) to display
const unsigned int MAX_AXES = 11;
const uint32_t current_incr = 0x100; // 0x100 is about 50 mA
class AxisInfo {
public:
enum AxisType { AXIS_UNKNOWN, MOTOR_ENCODER, MOTOR_ONLY, ENCODER_ONLY };
AxisInfo() : axisType(AXIS_UNKNOWN) {}
AxisInfo(AxisType ax) : axisType(ax) {}
~AxisInfo() {}
bool HasMotor(void) const { return (axisType == MOTOR_ENCODER) || (axisType == MOTOR_ONLY); }
bool HasEncoder(void) const { return (axisType == MOTOR_ENCODER) || (axisType == ENCODER_ONLY); }
protected:
AxisType axisType;
};
/*!
\brief Increment encoder counts
\param[in] bd FPGA Board
*/
void EncUp(AmpIO &bd)
{
bd.WriteDigitalOutput(0x03, 0x03);
bd.WriteDigitalOutput(0x03, 0x02);
bd.WriteDigitalOutput(0x03, 0x00);
bd.WriteDigitalOutput(0x03, 0x01);
bd.WriteDigitalOutput(0x03, 0x03);
}
/*!
\brief Decrement encoder counts
\param[in] bd FPGA Board
*/
void EncDown(AmpIO &bd)
{
bd.WriteDigitalOutput(0x03, 0x03);
bd.WriteDigitalOutput(0x03, 0x01);
bd.WriteDigitalOutput(0x03, 0x00);
bd.WriteDigitalOutput(0x03, 0x02);
bd.WriteDigitalOutput(0x03, 0x03);
}
void PrintDebugStream(std::stringstream &debugStream)
{
std::cerr << debugStream.str() << std::endl;
debugStream.clear();
debugStream.str("");
}
unsigned int collectFileNum = 0;
std::ofstream collectFile;
bool isCollecting = false;
bool CollectCB(quadlet_t *buffer, short nquads)
{
collectFile.write(reinterpret_cast<const char *>(buffer), nquads*sizeof(quadlet_t));
if (!isCollecting)
collectFile.close();
return true;
}
bool CollectFileConvert(const char *inFilename, const char *outFilename)
{
std::cerr << "Converting data collection file " << inFilename << " to " << outFilename << std::endl;
std::ifstream inFile(inFilename, std::ifstream::binary);
if (!inFile.good()) {
std::cerr << "Failed to open input data collection file " << inFilename << std::endl;
return false;
}
std::ofstream outFile(outFilename, std::ofstream::trunc);
if (!outFile.good()) {
std::cerr << "Failed to open output data collection file " << outFilename << std::endl;
return false;
}
quadlet_t value;
while (inFile.good()) {
inFile.read(reinterpret_cast<char *>(&value), sizeof(quadlet_t));
outFile << std::dec
<< ((value&0x80000000)>>31) << ", " // type (0->commanded current, 1-> measured current)
<< ((value&0x40000000)>>30) << ", " // timer overflow
<< ((value&0x3FFF0000)>>16) << ", " // timer (14-bits)
<< (value&0x0000FFFF) // data
<< std::endl;
}
inFile.close();
outFile.close();
return true;
}
void UpdateStatusStrings(char *statusStr1, char *statusStr2, uint32_t statusChanged, uint32_t status)
{
if (statusChanged&0x00080000) { // power (mv-good)
if (status&0x00080000) {
statusStr1[0] = 'P';
statusStr1[1] = '+';
}
else {
statusStr1[3] = 'P';
statusStr1[4] = '-';
}
}
if (statusChanged&0x00020000) { // safety relay
if (status&0x00020000) {
statusStr1[6] = 'S';
statusStr1[7] = '+';
}
else {
statusStr1[9] = 'S';
statusStr1[10] = '-';
}
}
if (statusChanged&0x00800000) { // watchdog
if (status&0x00800000) {
statusStr1[12] = 'W';
statusStr1[13] = '+';
}
else {
statusStr1[15] = 'W';
statusStr1[16] = '-';
}
}
// NOTE: hard-coded for 4 amplifiers
for (unsigned int i = 0; i < 4; i++) { // amplifier status
uint32_t mask = (0x00000100 << i);
if (statusChanged&mask) {
if (status&mask) {
statusStr2[0] = 'A';
statusStr2[2+4*i] = '+';
}
else {
statusStr2[0] = 'A';
statusStr2[2+4*i+1] = '-';
}
}
}
}
int main(int argc, char** argv)
{
const unsigned int lm = 5; // left margin
unsigned int i, j;
BasePort::ProtocolType protocol = BasePort::PROTOCOL_SEQ_RW;
bool fullvel = false; // whether to display full velocity feedback
bool showTime = false; // whether to display time information
bool useMaxAxis = false; // true --> use max(NumEncoders, NumMotors); false --> NumEncoders
bool readOnly = false; // true --> program does not call WriteAllBoards
std::vector<AmpIO*> BoardList;
std::vector<uint32_t> BoardStatusList;
// measure time between reads
uint32_t maxTime = 0;
uint32_t lastTime = 0;
unsigned int curAxis = 0; // Current axis (0=all, 1-MAX_AXES)
unsigned int Axis2BoardNum[MAX_AXES+1]; // Maps curAxis (1-MAX_AXES) to board number (0 or 1)
unsigned int Axis2BoardAxis[MAX_AXES+1]; // Maps curAxis (1-MAX_AXES) to board axis (0 to NumEncoders)
AxisInfo AxisData[MAX_AXES+1]; // Indicates whether motor, encoder or both
unsigned int curBoardIndex = 0; // Current board (when curAxis != 0)
unsigned int curAxisIndex = 0; // Current axis on current board (when curAxis != 0)
char axisString[4] = "all";
std::string portDescription = BasePort::DefaultPort();
std::string hardwareList;
for (i = 1; i < (unsigned int)argc; i++) {
if (argv[i][0] == '-') {
if (argv[i][1] == 'p') {
portDescription = argv[i]+2;
}
else if (argv[i][1] == 'h') {
hardwareList = argv[i]+2;
}
else if (argv[i][1] == 'b') {
// -br -- enable broadcast read/write
// -bw -- enable broadcast write (sequential read)
if (argv[i][2] == 'r')
protocol = BasePort::PROTOCOL_BC_QRW;
else if (argv[i][2] == 'w')
protocol = BasePort::PROTOCOL_SEQ_R_BC_W;
}
else if (argv[i][1] == 'v')
fullvel = true;
else if (argv[i][1] == 't')
showTime = true;
else if (argv[i][1] == 'm')
useMaxAxis = true;
else if (argv[i][1] == 'r')
readOnly = true;
}
else {
int bnum = atoi(argv[i]);
if ((bnum >= 0) && (bnum < BoardIO::MAX_BOARDS)) {
BoardList.push_back(new AmpIO(bnum));
std::cerr << "Selecting board " << bnum << std::endl;
}
else
std::cerr << "Invalid board number: " << argv[i] << std::endl;
}
}
if (BoardList.size() < 1) {
// usage
std::cerr << "Usage: qladisp <board-num> [<board-num>] [-pP] [-hH] [-b<r|w>] [-v] [-t] [-m]" << std::endl
<< " where P = port number (default 0)" << std::endl
<< " can also specify -pfw[:P], -peth:P or -pudp[:xx.xx.xx.xx]" << std::endl
<< " H = additional supported hardware versions" << std::endl
<< " -br enables broadcast read/write" << std::endl
<< " -bw enables broadcast write" << std::endl
<< " -v displays full velocity feedback" << std::endl
<< " -t displays time information" << std::endl
<< " -m include motors without encoders (if any)" << std::endl
<< " -r read-only (does not write to boards)" << std::endl
<< std::endl
<< "Trying to detect boards on port:" << std::endl;
}
std::stringstream debugStream(std::stringstream::out|std::stringstream::in);
BasePort::AddHardwareVersionStringList(hardwareList);
BasePort *Port = PortFactory(portDescription.c_str(), debugStream);
if (!Port) {
PrintDebugStream(debugStream);
std::cerr << "Failed to create port using: " << portDescription << std::endl;
return -1;
}
if (!Port->IsOK()) {
PrintDebugStream(debugStream);
std::cerr << "Failed to initialize " << Port->GetPortTypeString() << std::endl;
return -1;
}
if (BoardList.size() < 1) {
PrintDebugStream(debugStream);
// keys
std::cerr << std::endl << "Keys:" << std::endl
<< "'r': reset FireWire port" << std::endl
<< "'a': axis to address (1-N) or all (0, default)" << std::endl;
if (!readOnly) {
std::cerr
<< "'d': turn watchdog on/off (0.25 ms, this should be triggered immediately)" << std::endl
<< "'D': turn watchdog on/off (25.0 ms, can be triggered by unplugging cable to PC)" << std::endl
<< "'p': turn power on/off (board and axis)" << std::endl
<< "'o': turn power on/off (board only)" << std::endl
<< "'i': turn power on/off (axis only, requires board first)" << std::endl
<< "'v': toggle voltage/current mode (QLA 1.5+)" << std::endl
<< "'w': increment encoders" << std::endl
<< "'s': decrement encoders" << std::endl
<< "'=': increase motor current by about 50mA" << std::endl
<< "'-': increase motor current by about 50mA" << std::endl
<< "'c': start/stop data collection" << std::endl
<< "'z': clear status events and r/w errors" << std::endl
<< "'0'-'3': toggle digital output bit" << std::endl;
}
else {
std::cerr
<< "'z': clear status events and r/w errors" << std::endl;
}
return 0;
}
if (Port->GetNumOfNodes() == 0) {
PrintDebugStream(debugStream);
std::cerr << "Failed to find any boards" << std::endl;
return -1;
}
// Check if an Ethernet port
EthBasePort *ethPort = dynamic_cast<EthBasePort *>(Port);
// Number of boards to display (currently 1 or 2)
unsigned int numDisp = 0;
// Total number of axes to display
unsigned int numAxes = 0;
// Note that Axis2BoardNum and Axis2BoardAxis start at 1 because
// curAxis 0 is used to select all axes. The first entry should
// not be used, but we initialize it to something reasonable (0).
Axis2BoardNum[0] = 0;
Axis2BoardAxis[0] = 0;
AxisData[0] = AxisInfo::AXIS_UNKNOWN;
for (i = 0; i < BoardList.size(); i++) {
Port->AddBoard(BoardList[i]);
int numEnc = BoardList[i]->GetNumEncoders();
int numMot = BoardList[i]->GetNumMotors();
int numThisBoard = useMaxAxis ? std::max(numEnc,numMot) : numEnc;
if (numAxes + numThisBoard <= MAX_AXES) {
for (int j = 0; j < numThisBoard; j++) {
Axis2BoardNum[1+numAxes+j] = i;
Axis2BoardAxis[1+numAxes+j] = j;
if ((j < numEnc) && (j < numMot))
AxisData[1+numAxes+j] = AxisInfo(AxisInfo::MOTOR_ENCODER);
else if (j < numEnc)
AxisData[1+numAxes+j] = AxisInfo(AxisInfo::ENCODER_ONLY);
else
AxisData[1+numAxes+j] = AxisInfo(AxisInfo::MOTOR_ONLY);
}
numDisp++;
numAxes += numThisBoard;
}
}
// Set protocol; default is PROTOCOL_SEQ_RW (not broadcast), but can be changed to
// one of the broadcast protocols by specifying -br or -bw command line parameter.
if (protocol == BasePort::PROTOCOL_BC_QRW)
std::cerr << "Setting protocol to broadcast read/write" << std::endl;
else if (protocol == BasePort::PROTOCOL_SEQ_R_BC_W)
std::cerr << "Setting protocol to broadcast write" << std::endl;
if (!Port->SetProtocol(protocol))
protocol = Port->GetProtocol(); // on failure, get current protocol
// Initialize motor currents at mid-range
uint32_t MotorCurrents[MAX_AXES];
bool VoltageModeAvail[MAX_AXES]; // Whether voltage control available (QLA 1.5+)
bool VoltageMode[MAX_AXES]; // Whether voltage control active
for (i = 0; i < MAX_AXES; i++) {
MotorCurrents[i] = 0x8000;
VoltageModeAvail[i] = false;
VoltageMode[i] = false;
}
bool someRev7plus = false;
bool someRev8plus = false;
BoardStatusList.clear();
for (j = 0; j < BoardList.size(); j++) {
uint32_t fver = BoardList[j]->GetFirmwareVersion();
if (fver >= 7) someRev7plus = true;
if (fver >= 8) someRev8plus = true;
}
if (!readOnly) {
for (j = 0; j < numDisp; j++) {
for (i = 0; i < BoardList[j]->GetNumEncoders(); i++)
BoardList[j]->WriteEncoderPreload(i, 0x1000*i + 0x1000);
}
for (j = 0; j < numDisp; j++) {
BoardList[j]->WriteSafetyRelay(false);
BoardList[j]->WritePowerEnable(false);
BoardList[j]->WriteAmpEnable(0x0f, 0);
}
}
for (j = 0; j < numDisp; j++) {
uint32_t bstat = BoardList[j]->ReadStatus();
BoardStatusList.push_back(bstat);
}
bool watchdog_on = false;
Amp1394Console console;
console.Init();
if (!console.IsOK()) {
std::cerr << "Failed to initialize console" << std::endl;
return -1;
}
int board1 = BoardList[0]->GetBoardId();
unsigned int hub_board = Port->GetHubBoardId();
if (numDisp > 1) {
int board2 = BoardList[1]->GetBoardId();
if (protocol == BasePort::PROTOCOL_BC_QRW)
console.Print(1, lm, "Sensor Feedback for Boards %d, %d (hub %d)", board1, board2, hub_board);
else
console.Print(1, lm, "Sensor Feedback for Boards %d, %d", board1, board2);
} else {
if (protocol == BasePort::PROTOCOL_BC_QRW)
console.Print(1, lm, "Sensor Feedback for Board %d (hub %d)", board1, hub_board);
else
console.Print(1, lm, "Sensor Feedback for Board %d", board1);
}
if (readOnly) {
console.Print(2, lm, "Press ESC to quit, r to reset port (READ ONLY)");
}
else {
console.Print(2, lm, "Press ESC to quit, r to reset port, 0-3 to toggle digital output bit, p to enable/disable power,");
console.Print(3, lm, "+/- to increase/decrease commanded current (DAC) by 0x100");
}
for (i = 1; i <= numAxes; i++) {
unsigned int dx = lm+8+(i-1)*13;
if (i >= 10) dx--;
unsigned int bd = Axis2BoardNum[i];
unsigned int ax = Axis2BoardAxis[i];
uint32_t mconfig;
if (BoardList[bd]->ReadMotorConfig(ax, mconfig))
VoltageModeAvail[i-1] = mconfig & AmpIO::MCFG_VOLTAGE_CONTROL;
if (VoltageModeAvail[i-1])
console.Print(5, dx-2, "Axis %d I", i);
else
console.Print(5, dx, "Axis %d", i);
}
console.Print(6, lm, "Enc:");
console.Print(7, lm, "Pot:");
console.Print(8, lm, "Vel:");
console.Print(9, lm, "Cur:");
console.Print(10, lm, "DAC:");
int nextLine = 11;
if (someRev8plus)
console.Print(nextLine++, lm, "MSt:"); // Motor status
if (fullvel) {
if (someRev7plus) {
console.Print(nextLine++, lm, "Qtr1:");
console.Print(nextLine++, lm, "Qtr5:");
console.Print(nextLine++, lm, "Run:");
}
else {
console.Print(nextLine++, lm, "Acc:");
}
}
console.Refresh();
unsigned char dig_out = 0x0f;
unsigned char collect_axis = 1;
uint32_t status;
uint32_t statusChanged = 0;
const unsigned int STATUS_STR_LENGTH = 18;
char statusStr1[2][STATUS_STR_LENGTH];
memset(statusStr1[0], ' ', STATUS_STR_LENGTH-1);
memset(statusStr1[1], ' ', STATUS_STR_LENGTH-1);
statusStr1[0][STATUS_STR_LENGTH-1] = 0;
statusStr1[1][STATUS_STR_LENGTH-1] = 0;
char statusStr2[2][STATUS_STR_LENGTH];
memset(statusStr2[0], ' ', STATUS_STR_LENGTH-1);
memset(statusStr2[1], ' ', STATUS_STR_LENGTH-1);
statusStr2[0][STATUS_STR_LENGTH-1] = 0;
statusStr2[1][STATUS_STR_LENGTH-1] = 0;
unsigned int loop_cnt = 0;
const int STATUS_LINE = fullvel ? 17 : 14;
int timeLines = 0; // how many lines for timing info
if (showTime) {
timeLines++;
if (protocol == BasePort::PROTOCOL_BC_QRW)
timeLines += 2;
}
else if (ethPort)
timeLines++;
const int DEBUG_START_LINE = fullvel ? (23+timeLines) : (20+timeLines);
unsigned int last_debug_line = DEBUG_START_LINE;
const int ESC_CHAR = 0x1b;
int c;
if (showTime)
console.Print(STATUS_LINE+5, lm, "Time (s):");
else if (ethPort)
console.Print(STATUS_LINE+5, lm, "Ethernet:");
double startTime = -1.0; // indicates that startTime not yet set
double pcTime = 0.0;
// control loop
while ((c = console.GetChar()) != ESC_CHAR)
{
unsigned int startIndex = (curAxis == 0) ? 0 : curBoardIndex;
unsigned int endIndex = (curAxis == 0) ? numDisp : curBoardIndex+1;
if (c == 'r') {
Port->Reset();
Port->SetProtocol(protocol);
}
else if (c == 'a') {
if (curAxis < numAxes) {
curAxis++;
curBoardIndex = Axis2BoardNum[curAxis];
curAxisIndex = Axis2BoardAxis[curAxis];
sprintf(axisString, "%d", curAxis);
}
else {
curAxis = 0;
strcpy(axisString, "all");
}
}
else if (c == 'z') {
for (j = 0; j < numDisp; j++) {
memset(statusStr1[j], ' ', STATUS_STR_LENGTH-1);
statusStr1[j][STATUS_STR_LENGTH-1] = 0;
memset(statusStr2[j], ' ', STATUS_STR_LENGTH-1);
statusStr2[j][STATUS_STR_LENGTH-1] = 0;
console.Print(STATUS_LINE+4, lm+45+58*j, " ");
}
for (j = 0; j < BoardList.size(); j++) {
BoardList[j]->ClearReadErrors();
BoardList[j]->ClearWriteErrors();
}
if (showTime && (protocol == BasePort::PROTOCOL_BC_QRW)) {
Port->ClearBroadcastReadInfo();
}
}
else if (readOnly) {
// Options below not available in readOnly mode
}
else if ((c >= '0') && (c <= '3')) {
// toggle digital output bit
dig_out = dig_out^(1<<(c-'0'));
for (j = startIndex; j < endIndex; j++)
BoardList[j]->WriteDigitalOutput(0x0f, dig_out);
}
else if (c == 'v') {
if (curAxis == 0) {
for (unsigned int axis = 0; axis < numAxes; axis++) {
if (VoltageModeAvail[axis]) {
VoltageMode[axis] = !VoltageMode[axis];
unsigned int dx = lm+6+axis*13;
if (axis >= 9) dx--;
console.Print(5, dx, "Axis %d %c", axis+1, VoltageMode[axis] ? 'V' : 'I');
}
}
}
else if (VoltageModeAvail[curAxis-1]) {
VoltageMode[curAxis-1] = !VoltageMode[curAxis-1];
unsigned int dx = lm+6+(curAxis-1)*13;
if (curAxis >= 10) dx--;
console.Print(5, dx, "Axis %d %c", curAxis, VoltageMode[curAxis-1] ? 'V' : 'I');
}
}
else if (c == 'w') {
for (j = startIndex; j < endIndex; j++)
EncUp(*(BoardList[j]));
}
else if (c == 's') {
for (j = startIndex; j < endIndex; j++)
EncDown(*(BoardList[j]));
}
else if (c == 'd') {
watchdog_on = !watchdog_on;
for (j = startIndex; j < endIndex; j++)
// 0.25 ms
BoardList[j]->WriteWatchdogPeriodInSeconds(watchdog_on?(0.00025):0);
}
else if (c == 'D') {
watchdog_on = !watchdog_on;
for (j = startIndex; j < endIndex; j++)
// 25.00 ms
BoardList[j]->WriteWatchdogPeriodInSeconds(watchdog_on?(0.025):0);
}
else if (c == 'p') {
// Only power on system if completely off; otherwise, we power off.
if (curAxis == 0) { // All axes on all boards
bool anyBoardPowered = false;
for (j = 0; j < numDisp; j++) {
// Also use safety relay for power status because GetPowerStatus relies on the motor
// power supply being connected to the QLA. Check GetPowerEnable in addition to
// GetPowerStatus because GetPowerStatus returns true if the QLA is not connected.
if (BoardList[j]->GetSafetyRelayStatus() || (BoardList[j]->GetPowerEnable() && BoardList[j]->GetPowerStatus()))
anyBoardPowered = true;
}
for (j = 0; j < numDisp; j++) {
if (anyBoardPowered) {
BoardList[j]->SetPowerEnable(false);
BoardList[j]->SetSafetyRelay(false);
for (i = 0; i < BoardList[j]->GetNumMotors(); i++)
BoardList[j]->SetAmpEnable(i, false);
}
else {
BoardList[j]->SetSafetyRelay(true);
// Cannot enable Amp power unless Board power is
// already enabled.
BoardList[j]->WritePowerEnable(true);
//BoardList[j]->SetPowerEnable(true);
for (i = 0; i < BoardList[j]->GetNumMotors(); i++)
BoardList[j]->SetAmpEnable(i, true);
}
}
}
else {
bool isPowered = BoardList[curBoardIndex]->GetAmpEnable(curAxisIndex);
if (isPowered) {
// Note that all axes will be disabled when board power is removed
BoardList[curBoardIndex]->SetAmpEnable(curAxisIndex, false);
BoardList[curBoardIndex]->SetPowerEnable(false);
BoardList[curBoardIndex]->SetSafetyRelay(false);
}
else {
BoardList[curBoardIndex]->SetSafetyRelay(true);
// Cannot enable Amp power unless Board power is
// already enabled.
BoardList[curBoardIndex]->WritePowerEnable(true);
//BoardList[curBoardIndex]->SetPowerEnable(true);
BoardList[curBoardIndex]->SetAmpEnable(curAxisIndex, true);
}
}
}
else if (c == 'o') {
bool anyBoardPowered = false;
for (j = startIndex; j < endIndex; j++) {
// Also use safety relay for power status because GetPowerStatus relies on the motor
// power supply being connected to the QLA. Check GetPowerEnable in addition to
// GetPowerStatus because GetPowerStatus returns true if the QLA is not connected.
if (BoardList[j]->GetSafetyRelayStatus() || (BoardList[j]->GetPowerEnable() && BoardList[j]->GetPowerStatus()))
anyBoardPowered = true;
}
for (j = startIndex; j < endIndex; j++) {
if (anyBoardPowered) {
BoardList[j]->SetPowerEnable(false);
BoardList[j]->SetSafetyRelay(false);
}
else {
BoardList[j]->SetSafetyRelay(true);
BoardList[j]->SetPowerEnable(true);
}
}
}
else if (c == 'i') {
if (curAxis == 0) {
bool anyAxisPowered = false;
for (j = 0; j < numDisp; j++) {
if (BoardList[j]->GetAmpEnableMask() != 0)
anyAxisPowered = true;
}
for (unsigned int axisNum = 1; axisNum <= numAxes; axisNum++) {
j = Axis2BoardNum[axisNum];
i = Axis2BoardAxis[axisNum];
if (i < BoardList[j]->GetNumMotors())
BoardList[j]->SetAmpEnable(i, anyAxisPowered ? 0 : 1);
}
}
else {
bool isPowered = BoardList[curBoardIndex]->GetAmpEnable(curAxisIndex);
BoardList[curBoardIndex]->SetAmpEnable(curAxisIndex, !isPowered);
}
}
else if (c == '=') {
if (curAxis == 0) {
for (unsigned int axis = 0; axis < numAxes; axis++)
MotorCurrents[axis] += current_incr;
}
else {
MotorCurrents[curAxis-1] += current_incr;
}
}
else if (c == '-') {
if (curAxis == 0) {
for (unsigned int axis = 0; axis < numAxes; axis++)
MotorCurrents[axis] -= current_incr;
}
else {
MotorCurrents[curAxis-1] -= current_incr;
}
}
else if (c == 'c') {
int collect_board = (collect_axis <= BoardList[0]->GetNumEncoders()) ? 0 : 1;
if (BoardList[collect_board]->IsCollecting()) {
BoardList[collect_board]->DataCollectionStop();
console.Print(STATUS_LINE-1, lm, " ");
console.Print(STATUS_LINE-1, lm+15, " ");
if (collect_axis > collectFileNum)
collectFileNum = collect_axis;
collect_axis = (collect_axis == numAxes) ? 1 : collect_axis+1;
isCollecting = false;
}
else {
char fileName[20];
sprintf(fileName, "Collect%d.raw", collect_axis);
collectFile.open(fileName, std::ofstream::binary|std::ofstream::trunc);
if (!collectFile.good())
std::cerr << "Failed to open data collection file " << fileName << std::endl;
unsigned char collect_chan = collect_axis-(BoardList[0]->GetNumEncoders())*collect_board;
if (BoardList[collect_board]->DataCollectionStart(collect_chan, CollectCB)) {
isCollecting = true;
console.Print(STATUS_LINE-1, lm, "Collecting %d:", collect_axis);
}
}
}
if (!debugStream.str().empty()) {
int cur_line = DEBUG_START_LINE;
char line[120];
memset(line, ' ', sizeof(line)-1);
line[sizeof(line)-1] = 0;
for (i = cur_line; i < last_debug_line; i++)
console.Print(i, lm, line);
while (!debugStream.eof()) {
std::string stringLine;
std::getline(debugStream, stringLine);
console.Print(cur_line++, lm, stringLine.c_str());
}
debugStream.clear();
debugStream.str("");
last_debug_line = cur_line;
}
if (!Port->IsOK()) continue;
char nodeStr[2][3];
int node = Port->GetNodeId(BoardList[0]->GetBoardId());
if (node < BoardIO::MAX_BOARDS)
sprintf(nodeStr[0], "%2d", node);
else
strcpy(nodeStr[0], "none");
if (numDisp > 1) {
node = Port->GetNodeId(BoardList[1]->GetBoardId());
if (node < BasePort::MAX_NODES)
sprintf(nodeStr[1], "%2d", node);
else
strcpy(nodeStr[1], "??");
}
EncoderVelocity encVelData;
Port->ReadAllBoards();
unsigned int j;
if (showTime) {
if (startTime < 0.0) {
startTime = Amp1394_GetTime();
pcTime = 0.0;
for (j = 0; j < numDisp; j++)
BoardList[j]->SetFirmwareTime(0.0);
}
else {
pcTime = Amp1394_GetTime()-startTime;
}
}
// Check if hub board may have been updated
if (protocol == BasePort::PROTOCOL_BC_QRW) {
unsigned int new_hub_board = Port->GetHubBoardId();
if (new_hub_board != hub_board) {
hub_board = new_hub_board;
if (numDisp > 1) {
int board2 = BoardList[1]->GetBoardId();
console.Print(1, lm, "Sensor Feedback for Boards %d, %d (hub %d)", board1, board2, hub_board);
}
else {
console.Print(1, lm, "Sensor Feedback for Board %d (hub %d)", board1, hub_board);
}
}
}
bool allValid = true;
for (unsigned int axisNum = 1; axisNum <= numAxes; axisNum++) {
j = Axis2BoardNum[axisNum];
unsigned int dx = (axisNum-1)*13; // offset between columns
if (BoardList[j]->ValidRead()) {
i = Axis2BoardAxis[axisNum];
uint32_t fver = BoardList[j]->GetFirmwareVersion();
if (AxisData[axisNum].HasEncoder()) {
console.Print(6, lm+7+dx, "%07X",
BoardList[j]->GetEncoderPosition(i)+BoardList[j]->GetEncoderMidRange());
console.Print(7, lm+10+dx, "%04X", BoardList[j]->GetAnalogInput(i));
if (fullvel)
console.Print(8, lm+6+dx, "%08X", BoardList[j]->GetEncoderVelocityRaw(i));
else {
BoardList[j]->GetEncoderVelocityData(i, encVelData);
console.Print(8, lm+6+dx, "%08X", encVelData.GetEncoderVelocityPeriod());
}
}
if (AxisData[axisNum].HasMotor()) {
console.Print(9, lm+10+dx, "%04X", BoardList[j]->GetMotorCurrent(i));
console.Print(10, lm+10+dx, "%04X", MotorCurrents[axisNum-1]);
if (VoltageMode[axisNum-1])
BoardList[j]->SetMotorVoltage(i, MotorCurrents[axisNum-1]);
else
BoardList[j]->SetMotorCurrent(i, MotorCurrents[axisNum-1]);
if (someRev8plus) {
if (fver >= 8)
console.Print(11, lm+6+dx, "%08X", BoardList[j]->GetMotorStatus(i));
}
}
nextLine = someRev8plus ? 12 : 11;
if (AxisData[axisNum].HasEncoder()) {
if (fullvel) {
if (someRev7plus) {
if (fver >= 7) {
console.Print(nextLine++, lm+6+dx, "%08X", BoardList[j]->GetEncoderQtr1Raw(i));
console.Print(nextLine++, lm+6+dx, "%08X", BoardList[j]->GetEncoderQtr5Raw(i));
console.Print(nextLine++, lm+6+dx, "%08X", BoardList[j]->GetEncoderRunningCounterRaw(i));
}
}
else
console.Print(nextLine++, lm+6+dx, "%08X", BoardList[j]->GetEncoderAccelerationRaw(i));
}
}
}
else {
allValid = false;
}
}
unsigned int bdx = 0;
for (j = 0; j < numDisp; j++) {
if (BoardList[j]->ValidRead()) {
if (isCollecting) {
bool fpgaCollecting;
unsigned char fpgaChan;
unsigned short fpgaAddr;
if (BoardList[j]->GetCollectionStatus(fpgaCollecting, fpgaChan, fpgaAddr)) {
console.Print(STATUS_LINE-1, lm+15, "writeAddr: %4d", fpgaAddr);
}
}
dig_out = BoardList[j]->GetDigitalOutput();
status = BoardList[j]->GetStatus();
if (status != BoardStatusList[j]) {
statusChanged = status^BoardStatusList[j];
BoardStatusList[j] = status;
UpdateStatusStrings(statusStr1[j], statusStr2[j], statusChanged, status);
}
console.Print(STATUS_LINE, lm+bdx, "Status: %08X Timestamp: %08X DigOut: %01X",
status, BoardList[j]->GetTimestamp(),
(unsigned int)dig_out);
console.Print(STATUS_LINE+1, lm+bdx, "%17s NegLim: %01X PosLim: %01X Home: %01X",
statusStr1[j],
BoardList[j]->GetNegativeLimitSwitches(),
BoardList[j]->GetPositiveLimitSwitches(),
BoardList[j]->GetHomeSwitches());
console.Print(STATUS_LINE+2, lm+bdx, "%17s EncA: %01X EncB: %01X EncI: %01X",
statusStr2[j],
BoardList[j]->GetEncoderChannelA(),
BoardList[j]->GetEncoderChannelB(),
BoardList[j]->GetEncoderIndex());
console.Print(STATUS_LINE+4, lm+bdx, "Node: %s", nodeStr[j]);
if (BoardList[j]->GetHardwareVersion() == DQLA_String) {
console.Print(STATUS_LINE+4, lm+14+bdx, "Temp: %02X %02X %02X %02X ",
(unsigned int)BoardList[j]->GetAmpTemperature(0),
(unsigned int)BoardList[j]->GetAmpTemperature(1),
(unsigned int)BoardList[j]->GetAmpTemperature(2),
(unsigned int)BoardList[j]->GetAmpTemperature(3));
}
else {
console.Print(STATUS_LINE+4, lm+14+bdx, "Temp: %02X %02X",
(unsigned int)BoardList[j]->GetAmpTemperature(0),
(unsigned int)BoardList[j]->GetAmpTemperature(1));
}
if (loop_cnt > 500) {
lastTime = BoardList[j]->GetTimestamp();
if (lastTime > maxTime) {
maxTime = lastTime;
}
}
}
int err_dx = 35+bdx;
if (BoardList[j]->GetHardwareVersion() == DQLA_String)
err_dx += 12;
console.Print(STATUS_LINE+4, lm+err_dx, "Err(r/w): %2d %2d",
BoardList[j]->GetReadErrors(),
BoardList[j]->GetWriteErrors());
if (showTime)
console.Print(STATUS_LINE+5, lm+20+bdx, "%9.3lf (%7.4lf)", BoardList[j]->GetFirmwareTime(),
pcTime-BoardList[j]->GetFirmwareTime());
int numEnc = BoardList[j]->GetNumEncoders();
int numMot = BoardList[j]->GetNumMotors();
int numTot = useMaxAxis ? std::max(numEnc,numMot) : numEnc;
bdx += (6 + numTot*13);
loop_cnt++;
}
if (showTime) {
console.Print(STATUS_LINE+5, lm+53, "%8.3lf", pcTime);
if ((protocol == BasePort::PROTOCOL_BC_QRW) && allValid) {
BasePort::BroadcastReadInfo bcReadInfo;
bcReadInfo = Port->GetBroadcastReadInfo();
std::stringstream timingStr;
bcReadInfo.PrintTiming(timingStr);
char bcBuf[256];
timingStr.getline(bcBuf, 256);
strcat(bcBuf, " ");
console.Print(STATUS_LINE+6, lm, bcBuf);
timingStr.getline(bcBuf, 256);
strcat(bcBuf, " ");
console.Print(STATUS_LINE+7, lm, bcBuf);
}
}
else if (ethPort) {
EthBasePort::FPGA_Status fpgaStatus;
ethPort->GetFpgaStatus(fpgaStatus);
char flagStr[8];
flagStr[0] = fpgaStatus.FwBusReset ? 'R' : ' ';
flagStr[1] = ' ';
flagStr[2] = fpgaStatus.FwPacketDropped ? 'D' : ' ';
flagStr[3] = ' ';
flagStr[4] = fpgaStatus.EthInternalError ? 'I' : ' ';
flagStr[5] = ' ';
flagStr[6] = fpgaStatus.EthSummaryError ? 'S' : ' ';
flagStr[7] = 0;
console.Print(STATUS_LINE+5, lm+12, "%s StateError: %3d PacketError: %3d",
flagStr, fpgaStatus.numStateInvalid, fpgaStatus.numPacketError);
}
if (!readOnly)
Port->WriteAllBoards();
unsigned int readErrors = 0;
unsigned int writeErrors = 0;
for (j = 0; j < BoardList.size(); j++) {
readErrors += BoardList[j]->GetReadErrors();
writeErrors += BoardList[j]->GetWriteErrors();
}
console.Print(1, lm+42, "Gen: %d", Port->GetBusGeneration());
console.Print(1, lm+54, "Axis: %4s", axisString);
console.Print(1, lm+70, "dt: %.3f", (1.0 / 49125.0) * maxTime);
console.Print(1, lm+85, "Ct: %8u", loop_cnt++);
console.Print(1, lm+100, "Err(r/w): %2d %2d", readErrors, writeErrors);
console.Refresh();
// Skip sleep when using Zynq EMIO because it is already slow
if (Port->GetPortType() != BasePort::PORT_ZYNQ_EMIO)
Amp1394_Sleep(0.0005); // 500 usec
}
if (!readOnly) {
for (j = 0; j < numDisp; j++) {
BoardList[j]->WritePowerEnable(false); // Turn power off
BoardList[j]->WriteAmpEnable(0x0f, 0x00); // Turn power off
BoardList[j]->WriteSafetyRelay(false);
}
// Reset encoder preloads to default
for (j = 0; j < numDisp; j++) {
for (i = 0; i < BoardList[j]->GetNumEncoders(); i++) {
BoardList[j]->WriteEncoderPreload(i, 0);
}
}
}
console.End();
for (j = 0; j < BoardList.size(); j++)
Port->RemoveBoard(BoardList[j]->GetBoardId());
delete Port;
// Process any data collection files (convert from binary to text)
for (j = 1; j <= collectFileNum; j++) {
char inFile[20];
char outFile[20];
sprintf(inFile, "Collect%d.raw", j);
sprintf(outFile, "Collect%d.csv", j);
CollectFileConvert(inFile, outFile);
}
return 0;
}