/
hal-api.cpp
1457 lines (1317 loc) · 34.9 KB
/
hal-api.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 "hal-api.h"
#include <deque>
#include <winsock2.h>
#include <string>
#include <sstream>
#include <math.h>
#include <cmath>
#define NUM_STEPS 32808
using namespace System;
//declarations-------------------------------------------------------------------
const int MAX_X = 0xd0c168;
const int MAX_Y = 0xd6bca8;
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
std::deque<unsigned int> CommandQueue;
bool SpeedFast = false;
bool vac1 = false;
bool vac2 = false;
bool blow1 = false;
bool blow2 = false;
bool pres1 = false;
bool pres2 = false;
bool strip = false;
bool yneg = true;
bool ypos = true;
bool xneg = true;
bool xpos = true;
bool pause = false;
bool pause_led = false;
bool start = false;
bool run_led = false;
bool dot_led = false;
bool con_led = false;
bool cam_led = false;
bool status_led = false;
bool step = false;
bool pump_stat = false;
bool pick = false;
bool strip_activated = false;
bool x_neg_active = false;
bool x_pos_active = false;
bool y_neg_active = false;
bool y_pos_active = false;
bool homing = false;
bool homed = false;
bool speed_button = false;
bool wait = true;
int speed_command = 0x00000006;
int x_accel = 0x03;
int x_descel = 0x03;
int x_speed = 0x01;
int y_accel = 0x03;
int y_descel = 0x03;
int y_speed = 0x01;
int strip_accel = 0x03;
int strip_descel = 0x03;
int strip_speed = 0x03e8;
int r1_accel = 0x03;
int r1_descel = 0x03;
int r1_speed = 0x01f4;
int r2_accel = 0x03;
int r2_descel = 0x03;
int r2_speed = 0x01f4;
int z_accel = 0x03;
int z_descel = 0x03;
int z_speed = 0x01;
int spd = 1;
int accel = 3;
int descel = 3;
int X_pos_raw = 0;
int Y_pos_raw = 0;
int strip_pos;
//int X_target = 0;
int X_pos = 0;
int Y_pos = 0;
double A1_pos = 0;
double A2_pos = 0;
double N_pos = 0;
static unsigned char RecvBuffer[2000];
static int BufFilled = 0;
std::string label2_text = "null";
std::string label_text = "null";
std::string label_text_0 = "null";
std::string label_text_1 = "null";
std::string label_text_2 = "null";
std::string label_text_3 = "null";
std::string label_text_4 = "null";
std::string label_text_5 = "null";
std::string label_text_6 = "null";
std::string label_text_7 = "null";
std::string label_text_8 = "null";
std::string label_text_9 = "null";
std::string label_text_10 = "null";
std::string label_text_11 = "null";
char text_buffer[33];
char text_buffer_0[33];
char text_buffer_1[33];
char text_buffer_2[33];
char text_buffer_3[33];
char text_buffer_4[33];
char text_buffer_5[33];
char text_buffer_6[33];
char text_buffer_7[33];
char text_buffer_8[33];
char text_buffer_9[33];
char text_buffer_10[33];
char text_buffer_11[33];
char test_buffer[33];
unsigned int axis_scan = 0;
unsigned int axis_lim = 0;
unsigned int pick_scan = 0;
unsigned int pick_lim = 0;
const unsigned char ReqStatus[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
const unsigned char Req15[8] = { 0x15, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 };
const unsigned char Req14[8] = { 0x14, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 };
//functions----------------------------------------------------------------------
int NetworkStart() {
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
//printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
//printf("Could not create socket : %d" , WSAGetLastError());
return 2;
}
server.sin_addr.s_addr = inet_addr("192.168.0.8");
server.sin_family = AF_INET;
server.sin_port = htons(701);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
//puts("connect error");
return 3;
}
int Timeout = 1;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&Timeout, sizeof(Timeout));
return 0;
}
//this function only used for debug
std::string StringPad(std::string str) {
std::string test = str;
if (test.length() < 8) {
int pad = 8 - test.length();
for (int i = 0; i < pad; i++) {
test = "0" + test;
}
}
return test;
}
void SendSlowCommand() {
CommandQueue.push_back(0x06); //speed setting command
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03); // -> 5
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03); // -> 5
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03); // -> 5
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03); // -> 5
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03e8); // 1000
CommandQueue.push_back(0x01); // 1
CommandQueue.push_back(0x01); // 1
CommandQueue.push_back(0x01f4); // 500
CommandQueue.push_back(0x01); // 1
CommandQueue.push_back(0x01f4); // 500
}
void SendFastCommand() {
CommandQueue.push_back(0x020006);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x05);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x05);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x05);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x05);
CommandQueue.push_back(0x03);
CommandQueue.push_back(0x07d0); // 2000
CommandQueue.push_back(0x03e8); // 500
CommandQueue.push_back(0x05dc); // 1500
CommandQueue.push_back(0x07d0); // 2000
CommandQueue.push_back(0x05dc); // 1500
CommandQueue.push_back(0x07d0); // 2000
}
void SendSpeedCommand() {
CommandQueue.push_back(speed_command);
CommandQueue.push_back(strip_accel);
CommandQueue.push_back(z_accel);
CommandQueue.push_back(x_accel);
CommandQueue.push_back(r1_accel);
CommandQueue.push_back(y_accel);
CommandQueue.push_back(r2_accel);
CommandQueue.push_back(strip_descel);
CommandQueue.push_back(z_descel);
CommandQueue.push_back(x_descel);
CommandQueue.push_back(r1_descel);
CommandQueue.push_back(y_descel);
CommandQueue.push_back(r2_descel);
CommandQueue.push_back(strip_speed);
CommandQueue.push_back(z_speed);
CommandQueue.push_back(x_speed);
CommandQueue.push_back(r1_speed);
CommandQueue.push_back(y_speed);
CommandQueue.push_back(r2_speed);
}
void GracefulStart() {
//this does something
CommandQueue.push_back(0x05);
CommandQueue.push_back(0x00);
//this turns off ConMove LED
CommandQueue.push_back(0x15);
CommandQueue.push_back(0x80);
//this turns on DotMove LED
CommandQueue.push_back(0x14);
CommandQueue.push_back(0x0100);
//this turns on the lamp
CommandQueue.push_back(0x14);
CommandQueue.push_back(0x0200);
//this is a button lockout?
CommandQueue.push_back(0x0b);
//this does something
CommandQueue.push_back(0x15);
CommandQueue.push_back(0x4800);
//------------------following emulates manual mode press
//CommandQueue.push_back(0x0b);
//this does something
//CommandQueue.push_back(0x15);
//CommandQueue.push_back(0x4020);
//this does something else
//CommandQueue.push_back(0x15);
//CommandQueue.push_back(0x20);
}
void step_x_neg() {
if (!(axis_lim & 0b00000010)) {
int X_target = X_pos_raw - 0x3e8;
//SendSlowCommand();
CommandQueue.push_back(0x040008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(X_target);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
}
}
void move_x_neg() {
if (!(axis_lim & 0b00000010)) {
//SendFastCommand();
CommandQueue.push_back(0x040008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0xfe0b63c0);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
}
}
void step_x_pos() {
if (!(axis_lim & 0b00000001)) {
int X_target = X_pos_raw + 0x3e8;
//SendSlowCommand();
CommandQueue.push_back(0x040008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(X_target);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
}
}
void move_x_pos() {
if (!(axis_lim & 0b00000001)) {
//SendFastCommand();
//SendTestCommand();
//SendSpeedCommand(speed, accel, descel);
CommandQueue.push_back(0x040008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x01f49c40);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
}
}
void step_y_neg() {
if (!(axis_lim & 0b00001000)) {
int Y_target = Y_pos_raw - 0x3e8;
//SendSlowCommand();
CommandQueue.push_back(0x100008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(Y_target);
CommandQueue.push_back(0x00);
}
}
void move_y_neg() {
if (!(axis_lim & 0b00001000)) {
//SendFastCommand();
CommandQueue.push_back(0x100008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0xfe0b63c0);
CommandQueue.push_back(0x00);
}
}
void step_y_pos() {
if (!(axis_lim & 0b00000100)) {
int Y_target = Y_pos_raw + 0x3e8;
//SendSlowCommand();
CommandQueue.push_back(0x100008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(Y_target);
CommandQueue.push_back(0x00);
}
}
void move_y_pos() {
if (!(axis_lim & 0b00000100)) {
//SendFastCommand();
CommandQueue.push_back(0x100008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x01f49c40);
CommandQueue.push_back(0x00);
}
}
void GoHome(bool reset) {
static int stage = 0;
if (reset) {
stage = 0;
}
else {
static int step = 0;
switch (stage) {
case 0: { //move y towards y+ limit
CommandQueue.push_back(0x100008);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x01f49c40);
CommandQueue.push_back(0x00);
stage = 1;
break;
}
case 1: {
if (axis_lim & 0b00000100) { //reached y+ limit
step = Y_pos_raw - 0x280a0;
CommandQueue.push_back(0x100008); //move y back a little
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(step);
CommandQueue.push_back(0x00);
//move_y_neg();
stage = 2;
}
break;
}
case 2: {
if (Y_pos_raw == step) {//finished moving back
step = Y_pos_raw + 0x280a0;
CommandQueue.push_back(0x100008); //move forward to limit
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(step);
CommandQueue.push_back(0x00);
stage = 3;
}
break;
}
case 3: {
if (axis_lim & 0b00000100) { //reached y+ limit again
//SendSpeedCommand(3, 3, 3);
CommandQueue.push_back(0x100007); //write limit to register
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(MAX_Y);
CommandQueue.push_back(0x00);
stage = 4;
}
break;
}
case 4: {
CommandQueue.push_back(0x040008); //move to x+ limit
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x01f49c40);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
stage = 5;
break;
}
case 5: {
if (axis_lim & 0b00000001) { //if reached x- limit
step = X_pos_raw - 0x280a0;
CommandQueue.push_back(0x040008); //move x back a little
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(step);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
stage = 6;
}
break;
}
case 6: {
if (X_pos_raw == step) { //reached desired location
step = X_pos_raw + 0x280a0;
CommandQueue.push_back(0x040008); //move x back to limit
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(step);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
stage = 7;
}
break;
}
case 7: {
if (axis_lim & 0b00000001) { //reached x+ limit again
//SendSpeedCommand(3, 3, 3);
CommandQueue.push_back(0x040007); //write limit to register
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(MAX_X);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
stage = 0;
homing = false;
homed = true;
}
break;
}
default: {
stage = 0;
step = 0;
homing = false;
//do nothing
}
}
}
}
void beep_on() {
CommandQueue.push_back(0x14);
CommandQueue.push_back(0x0800);
}
void beep_off() {
CommandQueue.push_back(0x15);
CommandQueue.push_back(0x0800);
}
int ToClosestStep(int value) {
int error = value % 1000;
int corrected = 0;
//X_target_raw = X_target_raw + 1000 - error;
if (error > 500)
corrected = value + (1000 - error); //if error greater than half step, round down to nearest step
else
corrected = value - (error); //if error less than half step, round up to nearest step
return corrected;
}
void ShiftRecvBuffer(int Size) {
if (Size >= BufFilled) {
BufFilled = 0;
return;
}
BufFilled -= Size;
for (int i = 0; i < BufFilled; i++)
RecvBuffer[i] = RecvBuffer[i + Size];
}
void FillBuffer(unsigned char* Buffer, signed int Number) {
unsigned N = Number;
for (int i = 0; i < 4; i++) {
*Buffer = N & 0xFF;
N >>= 8;
Buffer++;
}
}
int BuildNumber(const unsigned char* p, int size) {
int Res = 0;
for (int i = 0; i < size; i++) {
Res <<= 8;
Res += p[size - 1 - i];
}
return Res;
};
bool CheckBit(const unsigned char* p, int bit) {
int byteindex = bit / 32;
int bitindex = 32 - bit % 32;
int word = BuildNumber(p + byteindex * 4, 4);
return ((word & (1 << bitindex)) >> bitindex);
}
void ProcessResponse() {
int Size = recv(s, (char*)RecvBuffer + BufFilled, 2000 - BufFilled, 0);
if (Size > 0)
BufFilled += Size;
while (BufFilled > 2) {
unsigned int FirstWord = BuildNumber(RecvBuffer, 2);
unsigned int RecvWord_0 = BuildNumber(RecvBuffer, 4); //first 2 bytes
unsigned int RecvWord_1 = BuildNumber(RecvBuffer + 4, 4); //second 2 bytes
unsigned int RecvWord_2 = BuildNumber(RecvBuffer + 8, 4);
unsigned int RecvWord_3 = BuildNumber(RecvBuffer + 12, 4);
unsigned int RecvWord_4 = BuildNumber(RecvBuffer + 16, 4);
unsigned int RecvWord_5 = BuildNumber(RecvBuffer + 20, 4);
unsigned int RecvWord_6 = BuildNumber(RecvBuffer + 24, 4);
unsigned int RecvWord_7 = BuildNumber(RecvBuffer + 28, 4);
unsigned int RecvWord_8 = BuildNumber(RecvBuffer + 32, 4);
unsigned int RecvWord_9 = BuildNumber(RecvBuffer + 36, 4);
unsigned int RecvWord_10 = BuildNumber(RecvBuffer + 40, 4);
unsigned int RecvWord_11 = BuildNumber(RecvBuffer + 44, 4);
switch (FirstWord) {
case 0x00: {
if (BufFilled < 44)
return;
// Parse Status word
int SpeedWord = BuildNumber(RecvBuffer + 4, 2);
//THIS IS MY TEST VARIABLE TO DISPLAY IN LABEL 2-------------------------------------------
int test = BuildNumber(RecvBuffer + 4, 1);
int axis_lim_old = axis_lim;
axis_lim = BuildNumber(RecvBuffer + 8, 1);
pick_lim = CheckBit(RecvBuffer, 77);
int pick_comm = BuildNumber(RecvBuffer + 4, 2);
pick = (pick_comm & (1 << 14)) >> 14;
vac1 = CheckBit(RecvBuffer, 62);
vac2 = CheckBit(RecvBuffer, 51);
blow1 = CheckBit(RecvBuffer, 63);
blow2 = CheckBit(RecvBuffer, 52);
pres1 = CheckBit(RecvBuffer, 92);
pres2 = CheckBit(RecvBuffer, 76);
strip = CheckBit(RecvBuffer, 319);
//checking buttons on machine
int buttons = BuildNumber(RecvBuffer + 8, 4);
bool speed_old = speed_button;
yneg = (buttons & (1 << 11)) >> 11;
ypos = (buttons & (1 << 18)) >> 18;
xneg = (buttons & (1 << 16)) >> 16;
xpos = (buttons & (1 << 12)) >> 12;
pause = (buttons & (1 << 14)) >> 14;
start = (buttons & (1 << 15)) >> 15;
step = (buttons & (1 << 13)) >> 13;
speed_button = (buttons & (1 << 17)) >> 17;
//checking LED status
int status = BuildNumber(RecvBuffer + 4, 2);
pause_led = (status & (1 << 6)) >> 6;
run_led = (status & (1 << 5)) >> 5;
dot_led = (status & (1 << 8)) >> 8;
con_led = (status & (1 << 7)) >> 7;
cam_led = (status & (1 << 9)) >> 9;
pump_stat = CheckBit(RecvBuffer, 61);
//getting x position
X_pos_raw = BuildNumber(RecvBuffer + 20, 4); //X_pos_raw contains absolute postion
float X_pos_fl = X_pos_raw / 32808.00; //divide by stepsize returns position in mm (original was 32808)
X_pos = X_pos_fl * 1000; // convert to microns
//getting y position
Y_pos_raw = BuildNumber(RecvBuffer + 28, 4);
float Y_pos_fl = Y_pos_raw / 32808.00; //divide by stepsize (original was 32808)
Y_pos = Y_pos_fl * 1000;
//getting A1 rotation
int A1_pos_raw = BuildNumber(RecvBuffer + 24, 4);
A1_pos = A1_pos_raw / 4444.44;
A1_pos = round(A1_pos * 100) / 100;
//getting A2 rotation
int A2_pos_raw = BuildNumber(RecvBuffer + 32, 4);
A2_pos = A2_pos_raw / 4444.44;
A2_pos = round(A2_pos * 100) / 100;
//getting nozzle position
int nozzle = BuildNumber(RecvBuffer + 16, 4);
double slope = 1;
int step = 78000;
double offset = 0;
int nozzle_abs = abs(nozzle);
//linear interpolation of nozzle position
if (nozzle_abs >= 0 && nozzle_abs <= 78000) {
slope = (5.13 - 0) / step;
offset = 0;
}
else if (nozzle_abs > 78000 && nozzle_abs <= 156000) {
slope = (9.78 - 5.13) / step;
offset = 0.48;
}
else if (nozzle_abs > 156000 && nozzle_abs <= 234000) {
slope = (13.51 - 9.78) / step;
offset = 2.32;
}
else {
slope = (15.99 - 13.51) / step;
offset = 6.07;
}
if (nozzle < 0)
offset = -offset;
N_pos = nozzle * slope + offset;
N_pos = round(N_pos * 100) / 100;
//Check strip position
strip_pos = BuildNumber(RecvBuffer + 12, 4);
if (strip_pos == 0xffe17b80) { //807be1ff
CommandQueue.push_back(0x010007); //if strip has reached its destination, reset that register
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
CommandQueue.push_back(0x00);
strip_activated = false; //allow button operation again
}
bool slow_bit = (SpeedWord & (1 << 8)) >> 8;
bool fast_bit = (SpeedWord & (1 << 7)) >> 7;
if (fast_bit && !slow_bit)
SpeedFast = true;
else if (slow_bit && !fast_bit)
SpeedFast = false;
if ((speed_old != speed_button) && !speed_button) { // if the speed button has been pressed down (does not activate on release)
if (SpeedFast) { //send appropriate commands to change speed
CommandQueue.push_back(0x15);
CommandQueue.push_back(0x80);
CommandQueue.push_back(0x14);
CommandQueue.push_back(0x100);
}
else {
CommandQueue.push_back(0x14);
CommandQueue.push_back(0x80);
CommandQueue.push_back(0x15);
CommandQueue.push_back(0x100);
}
}
//homing
if (homing) {
GoHome(0);
}
else {
GoHome(1);
}
//turn on beeper if axis limit reached?
/*if (axis_lim_old != axis_lim) {
if (axis_lim & 0b00001111)
beep_on();
else
beep_off();
}*/
//THIS IS JUST FOR DEBUG*****************************************************************************************
//process integers into hex based chars in 4 byte increments
itoa(RecvWord_0, text_buffer_0, 16);
itoa(RecvWord_1, text_buffer_1, 16);
itoa(RecvWord_2, text_buffer_2, 16);
itoa(RecvWord_3, text_buffer_3, 16);
itoa(RecvWord_4, text_buffer_4, 16);
itoa(RecvWord_5, text_buffer_5, 16);
itoa(RecvWord_6, text_buffer_6, 16);
itoa(RecvWord_7, text_buffer_7, 16);
itoa(RecvWord_8, text_buffer_8, 16);
itoa(RecvWord_9, text_buffer_9, 16);
itoa(RecvWord_10, text_buffer_10, 16);
itoa(RecvWord_11, text_buffer_11, 16);
//make each char array into a std string
label_text_0 = text_buffer_0;
label_text_1 = text_buffer_1;
label_text_2 = text_buffer_2;
label_text_3 = text_buffer_3;
label_text_4 = text_buffer_4;
label_text_5 = text_buffer_5;
label_text_6 = text_buffer_6;
label_text_7 = text_buffer_7;
label_text_8 = text_buffer_8;
label_text_9 = text_buffer_9;
label_text_10 = text_buffer_10;
label_text_11 = text_buffer_11;
// pad strings with 0s
label_text_0 = StringPad(label_text_0);
label_text_1 = StringPad(label_text_1);
label_text_2 = StringPad(label_text_2);
label_text_3 = StringPad(label_text_3);
label_text_4 = StringPad(label_text_4);
label_text_5 = StringPad(label_text_5);
label_text_6 = StringPad(label_text_6);
label_text_7 = StringPad(label_text_7);
label_text_8 = StringPad(label_text_8);
label_text_9 = StringPad(label_text_9);
label_text_10 = StringPad(label_text_10);
label_text_11 = StringPad(label_text_11);
//concatenate all strings into one for display
label_text = label_text_0 + " " + label_text_1 + " " + label_text_2 + " " + label_text_3 + " " + label_text_4
+ " " + label_text_5 + " " + label_text_6 + " " + label_text_7 + " " + label_text_8 + " " + label_text_9 + " " + label_text_10 + " " + label_text_11;
//testing and stuff
//itoa(test, test_buffer, 16);
//label2_text = test_buffer;
//DEBUG ENDS***************************************************************************************************************************
ShiftRecvBuffer(44);
}
case 0x1:
ShiftRecvBuffer(12);
break;
case 0x14:
case 0x15:
if (BufFilled < 0x2)
return;
ShiftRecvBuffer(2);
break;
case 0x8: {
ShiftRecvBuffer(2);
break;
}
case 0x7: {
ShiftRecvBuffer(2);
break;
}
case 0x6: {
ShiftRecvBuffer(2);
break;
}
case 0x9: {
ShiftRecvBuffer(2);
break;
}
default:
ShiftRecvBuffer(4);
}
};
static int SendReadRegisterCount = 10;
SendReadRegisterCount--;
if (SendReadRegisterCount == 0) {
SendReadRegisterCount = 10;
const char ReadReq[12] = { 0x01, 0x00, 0x00, 0x00,
0xf4, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
int res = send(s, ReadReq, sizeof(ReadReq), 0);
return;
}
if (CommandQueue.size() == 0) {
// Send out new Status Request
int res = send(s, (char*)ReqStatus, sizeof(ReqStatus), 0);
return;
}
while (CommandQueue.size()) {
unsigned int Command = CommandQueue.front();
CommandQueue.pop_front();
unsigned char SendBuffer[200];
switch (Command) {
//------------------------cases 15 and 14 on/off commands--------------------------
case 0x15:
{
//in this case we only send 2 bytes, because this is a two byte command
FillBuffer(SendBuffer, 0x15); //pad the send buffer with 0s after the 0x15 command
int SecondByte = CommandQueue.front(); //grab second bytes meaningful data
CommandQueue.pop_front();
FillBuffer(SendBuffer + 4, SecondByte); //pad the meaningful data with 0s and push into the second byte of send buffer
send(s, (char*)SendBuffer, 8, 0); //send the send buffer
break;
}
case 0x14: {
//same as case 0x14, only command is different
FillBuffer(SendBuffer, 0x14);
int SecondByte = CommandQueue.front();
CommandQueue.pop_front();
FillBuffer(SendBuffer + 4, SecondByte);
send(s, (char*)SendBuffer, 8, 0);
break;
}
//-------------------case "8" move commands----------------------------------
case 0x010008:
case 0x080008:
case 0x200008:
case 0x020008:
case 0x100008:
case 0x040008:
FillBuffer(SendBuffer, Command);
for (int i = 0; i < 6; i++) {
int next_byte = CommandQueue.front();
CommandQueue.pop_front();
FillBuffer(SendBuffer + (i + 1) * 4, next_byte);
}
send(s, (char*)SendBuffer, 28, 0);
break;
//-------------case "7" register overwrites----------------------
case 0x010007:
case 0x100007:
case 0x040007:
{
FillBuffer(SendBuffer, Command);
for (int i = 0; i < 6; i++) { //repeat 6 times incrementing the the buffer accordingly
int next_byte = CommandQueue.front(); // grab next byte
CommandQueue.pop_front();
FillBuffer(SendBuffer + (i + 1) * 4, next_byte);
}
send(s, (char*)SendBuffer, 28, 0);
break;
}
//-------------case "9" stop motion cases--------------------
case 0x200009:
case 0x080009:
case 0x020009:
case 0x040009:
case 0x100009:
case 0x3f0009:
FillBuffer(SendBuffer, Command);
send(s, (char*)SendBuffer, 4, 0);
break;
//------------case 6, speed setting commands----------------------------
case 0x06:
case 0x010006:
case 0x020006:
case 0x030006:
case 0x040006:
case 0x050006:
case 0x060006:
case 0x070006:
case 0x080006:
case 0x090006:
case 0x0a0006:
case 0x0b0006:
FillBuffer(SendBuffer, Command);
for (int i = 0; i < 18; i++) {
int next_byte = CommandQueue.front();
CommandQueue.pop_front();
FillBuffer(SendBuffer + (i + 1) * 4, next_byte);
}
send(s, (char*)SendBuffer, 76, 0);
break;
//--------------------------individualized cases--------------------------------------
case 0x05: {
FillBuffer(SendBuffer, 0x05);
int second_byte = CommandQueue.front();
CommandQueue.pop_front();
FillBuffer(SendBuffer + 4, second_byte);
send(s, (char*)SendBuffer, 8, 0); //send the send buffer
break;
}
case 0x0b: {
FillBuffer(SendBuffer, 0x0b);
send(s, (char*)SendBuffer, 4, 0);
break;
}
}
Sleep(1);
}
}
//----------------------------------ACCESSIBILITY FUNCTIONS------------------------------------------------
int Initialize() {
int start_stat;
start_stat = NetworkStart();
GracefulStart();
return start_stat;
}
int SetSpeed(eAxis Axis, int Acceleration, int Desceleration, int MaxSpeed) {
eAxis ax = Axis;
double speed = MaxSpeed / 8.3333;
double acceleration = Acceleration / 16.6666;
double desceleration = Desceleration / 16.6666;
speed = speed + 0.5;
acceleration = acceleration + 0.5;
desceleration = desceleration + 0.5;
spd = (int)speed; //speed now has an integer range of 0 - 12
accel = (int)acceleration; //acceleration now has an integer range 0 - 6
descel = (int)desceleration; //desceleration now has integer range 0 - 6
if (spd == 0) spd = 1;
if (accel == 0) accel = 1;
if (descel == 0) descel = 1;
if (spd > 12) return 2;
if (accel > 6) return 3;
if (descel > 6) return 4;
int command;
int strp_spd;
int noz_spd;
int axis_spd;
int rot_spd;
switch (spd) { //build codewords for speed for different functions
case 1: {
command = 0x000006;
strp_spd = 0x03e8;
noz_spd = 0x01;
axis_spd = 0x01;
rot_spd = 0x01f4;
break;