aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetstepper.c
blob: 55bc9f8881741ae0fa4e7288d5025c1933ceb1bb (plain)
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
#include "stdafx.h"
#include "cphidgetstepper.h"
#include "cusb.h"
#include "csocket.h"
#include "cthread.h"

// === Internal Functions === //

//clearVars - sets all device variables to unknown state
CPHIDGETCLEARVARS(Stepper)
	int i = 0;

	phid->motorSpeedMax = PUNI_DBL;
	phid->motorSpeedMin = PUNI_DBL;
	phid->accelerationMax = PUNI_DBL;
	phid->accelerationMin = PUNI_DBL;
	phid->motorPositionMax = PUNI_INT64;
	phid->motorPositionMin = PUNI_INT64;
	phid->currentMax = PUNI_DBL;
	phid->currentMin = PUNI_DBL;

	for (i = 0; i<STEPPER_MAXINPUTS; i++)
	{
		phid->inputState[i] = PUNI_BOOL;
	}

	for (i = 0; i<STEPPER_MAXSTEPPERS; i++)
	{
		phid->motorSpeedEcho[i] = PUNI_DBL;
		phid->motorPositionEcho[i] = PUNI_INT64;
		phid->motorSensedCurrent[i] = PUNI_DBL;
		phid->motorEngagedStateEcho[i] = PUNI_BOOL;
		phid->motorStoppedState[i] = PUNI_BOOL;
		phid->packetCounterEcho[i] = PUNK_INT;
		
		phid->motorPosition[i] = PUNI_INT64;
		phid->motorPositionReset[i] = PUNK_INT64;
		phid->motorEngagedState[i] = PUNK_BOOL;
		phid->motorAcceleration[i] = PUNK_DBL;
		phid->motorSpeed[i] = PUNK_DBL;
		phid->motorCurrentLimit[i] = PUNK_DBL;
		phid->packetCounter[i] = PUNK_INT;
	}
	return EPHIDGET_OK;
}

//initAfterOpen - sets up the initial state of an object, reading in packets from the device if needed
//				  used during attach initialization - on every attach
CPHIDGETINIT(Stepper)
	int i = 0;

	TESTPTR(phid);

	//Make sure no old writes are still pending
	phid->outputPacketLen = 0;
	
	//Setup max/min values
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
			phid->microSteps = 2;
			phid->motorSpeedMax = ((phid->microSteps * 0x100) - 1) * 0.75;
			phid->motorSpeedMin = 0;
			phid->accelerationMax = (250 * (0.75 * 0.75)) * 0x3f;
			phid->accelerationMin = (250 * (0.75 * 0.75));
			phid->motorPositionMax = 0x7FFFFFFFFFLL;
			phid->motorPositionMin = -0x7FFFFFFFFFLL;
			phid->currentMax = PUNK_DBL;
			phid->currentMin = PUNK_DBL;
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
			phid->microSteps = 16;
			phid->motorSpeedMax = 8 * phid->microSteps * 0x100; //0x8000
			phid->motorSpeedMin = 0;
			phid->accelerationMax = 4000 * 0xff;
			phid->accelerationMin = 4000;
			phid->motorPositionMax = 0x7FFFFFFFFFLL;
			phid->motorPositionMin = -0x7FFFFFFFFFLL;
			phid->currentMax = 2.492;
			phid->currentMin = 0.0542;
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			phid->microSteps = 16;
			phid->motorSpeedMax = 250000; //nice round number
			phid->motorSpeedMin = 0;
			phid->accelerationMax = 10000000;
			phid->accelerationMin = 2;
			phid->motorPositionMax = 1000000000000000LL; // +-1 Quadrillion - enough for 211 years at max speed
			phid->motorPositionMin = -1000000000000000LL;
			phid->currentMax = 4;
			phid->currentMin = 0;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	//set data arrays to unknown
	for (i = 0; i<phid->phid.attr.stepper.numInputs; i++)
	{
		phid->inputState[i] = PUNK_BOOL;
	}
	for (i = 0; i<phid->phid.attr.stepper.numMotors; i++)
	{
		phid->motorSpeedEcho[i] = PUNK_DBL;
		phid->motorPositionEcho[i] = PUNK_INT64;
		phid->motorSensedCurrent[i] = PUNK_DBL;
		phid->motorEngagedStateEcho[i] = PUNK_BOOL;
		phid->motorStoppedState[i] = PUNK_BOOL;
		phid->packetCounterEcho[i] = PUNK_INT;
		phid->packetCounter[i] = PUNK_INT;
	}

	//read in initial state - only one packet needed
	CPhidget_read((CPhidgetHandle)phid);

	//At this point, we can only recover (maybe) the position, engaged state, set others to unknown
	for (i = 0; i<phid->phid.attr.stepper.numMotors; i++)
	{
		phid->motorPosition[i] = phid->motorPositionEcho[i];
		phid->motorEngagedState[i] = phid->motorEngagedStateEcho[i];
		phid->motorAcceleration[i] = PUNK_DBL;
		phid->motorSpeed[i] = PUNK_DBL;
		phid->motorCurrentLimit[i] = PUNK_DBL;
		phid->packetCounter[i] = phid->packetCounterEcho[i];

		if(phid->motorStoppedState[i] == PUNK_BOOL)
		{
			if(phid->motorSpeedEcho[i] == 0 || phid->motorEngagedStateEcho[i] == PFALSE)
				phid->motorStoppedState[i] = PTRUE;
			else
				phid->motorStoppedState[i] = PFALSE;
		}
	}
	
	return EPHIDGET_OK;
}

//dataInput - parses device packets
CPHIDGETDATA(Stepper)
	int i = 0, j = 0;

	double speed[STEPPER_MAXSTEPPERS], current[STEPPER_MAXSTEPPERS];
	double lastSpeed[STEPPER_MAXSTEPPERS], lastCurrent[STEPPER_MAXSTEPPERS];
	__int64 position[STEPPER_MAXSTEPPERS], lastPosition[STEPPER_MAXSTEPPERS];
	unsigned char input[STEPPER_MAXINPUTS], lastInput[STEPPER_MAXINPUTS];
	unsigned char motorEngaged[STEPPER_MAXSTEPPERS], motorDone[STEPPER_MAXSTEPPERS], justStopped[STEPPER_MAXSTEPPERS];

	if (length < 0) return EPHIDGET_INVALIDARG;
	TESTPTR(phid);
	TESTPTR(buffer);

	ZEROMEM(input, sizeof(input));
	ZEROMEM(lastInput, sizeof(lastInput));

	//Parse device packets - store data locally
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
			for (i = 0; i < phid->phid.attr.stepper.numMotors; i++)
			{
				phid->packetCounterEcho[i] = buffer[0+(i*9)] & 0x0F;
				motorEngaged[i] = (buffer[0+(i*9)] & MOTOR_DISABLED_STEPPER) ? PFALSE : PTRUE;
				motorDone[i] = (buffer[0+(i*9)] & MOTOR_DONE_STEPPER) ? PTRUE : PFALSE;

				speed[i] = (double)((signed short)((buffer[1+(i*9)] << 8) | buffer[2+(i*9)]));
				speed[i] = (double)((speed[i] / 511.0) * phid->motorSpeedMax);

				position[i] = ((((__int64)(signed char)buffer[3+(i*9)]) << 40) +
					(((__uint64)buffer[4+(i*9)]) << 32) +
					(((__uint64)buffer[5+(i*9)]) << 24) +
					(((__uint64)buffer[6+(i*9)]) << 16) +
					(((__uint64)buffer[7+(i*9)]) << 8) +
					((__uint64)buffer[8+(i*9)]));

				position[i] -= 0x20; //round
				position[i] >>= 6;

				//current is not returned
				current[i] = PUNK_DBL;
			}
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
			{
				double Vad;
				phid->packetCounterEcho[0] = buffer[0] & 0x0F;
				motorEngaged[0] = (buffer[0] & MOTOR_DISABLED_STEPPER) ? PFALSE : PTRUE;
				motorDone[0] = (buffer[0] & MOTOR_DONE_STEPPER) ? PTRUE : PFALSE;

				speed[0] = (double)((signed short)((buffer[1] << 8) | buffer[2]));
				speed[0] = (double)((speed[i] / 4096.0) * phid->motorSpeedMax);

				position[0] = ((((__int64)(signed char)buffer[3]) << 40) +
					(((__uint64)buffer[4]) << 32) +
					(((__uint64)buffer[5]) << 24) +
					(((__uint64)buffer[6]) << 16) +
					(((__uint64)buffer[7]) << 8) +
					((__uint64)buffer[8]));

				position[0] -= 0x04; //round
				position[0] >>= 3;

				for (i = 0, j=0x01; i<phid->phid.attr.stepper.numInputs; i++, j<<=1)
				{
					if ((buffer[9] & j))
						input[i] = PFALSE;
					else
						input[i] = PTRUE;
				}

				//value is 0-4.16 v in 8 bits 
				//(4.16 = (Vbandgap + 1.6) * 2, min 4.096v, max 4.224v according to bandgap voltage min, max)
				//ie +-1.5% error
				Vad = (((unsigned char)buffer[10] * 4.16 ) / 255.0); // The voltage sensed, to a max of 4.16v
				current[0] = round_double((Vad / (BIPOLAR_STEPPER_CURRENT_SENSE_GAIN * BIPOLAR_STEPPER_CURRENT_LIMIT_Rs)), 3);
			}
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			{
				phid->packetCounterEcho[0] = buffer[0] & 0x0F;
				motorEngaged[0] = (buffer[0] & MOTOR_DISABLED_STEPPER) ? PFALSE : PTRUE;
				motorDone[0] = (buffer[0] & MOTOR_DONE_STEPPER) ? PTRUE : PFALSE;

				//24.8 floating point format
				speed[0] = (double)( ((signed int)((((signed char)buffer[1]) << 24) | (buffer[2] << 16) | (buffer[3] << 8) | buffer[4])) / 256.0);

				position[0] = ((((__int64)(signed char)buffer[5]) << 56) +
					(((__uint64)buffer[6]) << 48) +
					(((__uint64)buffer[7]) << 40) +
					(((__uint64)buffer[8]) << 32) +
					(((__uint64)buffer[9]) << 24) +
					(((__uint64)buffer[10]) << 16) +
					(((__uint64)buffer[11]) << 8) +
					((__uint64)buffer[12]));
			}
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}
	
	//Make sure values are within defined range, and store to structure
	for (i = 0; i < phid->phid.attr.stepper.numMotors; i++)
	{
		lastPosition[i] = phid->motorPositionEcho[i];
		lastSpeed[i] = phid->motorSpeedEcho[i];
		lastCurrent[i] = phid->motorSensedCurrent[i];

		if(position[i] > phid->motorPositionMax || position[i] < phid->motorPositionMin)
			phid->motorPositionEcho[i] = PUNK_INT64;
		else
			phid->motorPositionEcho[i] = position[i];

		if(speed[i] > phid->motorSpeedMax || speed[i] < -phid->motorSpeedMax)
			LOG(PHIDGET_LOG_WARNING, "Phidget stepper received out of range speed data: %lE", speed[i]);
		else
			phid->motorSpeedEcho[i] = speed[i];

		phid->motorSensedCurrent[i] = current[i];

		phid->motorEngagedStateEcho[i] = motorEngaged[i];
	}

	for (i = 0; i < phid->phid.attr.stepper.numInputs; i++)
	{
		lastInput[i] = phid->inputState[i];
		phid->inputState[i] = input[i];
	}

	//make sure phid->motorStoppedState isn't updated until the other data is filled in
	CThread_mutex_lock(&phid->phid.writelock);
	for (i = 0; i < phid->phid.attr.stepper.numMotors; i++)
	{
		//if we are up to date, and the motor is done, set stopped to true - this is the only place that this gets set true;
		justStopped[i] = PFALSE;
		if(phid->packetCounter[i] == phid->packetCounterEcho[i] && motorDone[i] == PTRUE
			&& ((phid->motorPositionEcho[i] == phid->motorPosition[i] && phid->motorSpeedEcho[i] == 0) || phid->motorEngagedStateEcho[i] == PFALSE))
		{
			if(phid->motorStoppedState[i] == PFALSE)
				justStopped[i] = PTRUE;
			phid->motorStoppedState[i] = PTRUE;
		}
		else if(motorDone[i] == PFALSE)
			phid->motorStoppedState[i] = PFALSE;
	}
	//LOG(PHIDGET_LOG_VERBOSE, " ECHO: %d %d %d %d",phid->packetCounterEcho[0],phid->packetCounterEcho[1],phid->packetCounterEcho[2],phid->packetCounterEcho[3]);
	CThread_mutex_unlock(&phid->phid.writelock);

	//send out any events for changed data
	for (i = 0; i < phid->phid.attr.stepper.numInputs; i++)
	{
		if(phid->inputState[i] != PUNK_BOOL && phid->inputState[i] != lastInput[i])
			FIRE(InputChange, i, phid->inputState[i]);
	}

	for (i = 0; i < phid->phid.attr.stepper.numMotors; i++)
	{
		if(phid->motorPositionEcho[i] != PUNK_INT64 && phid->motorEngagedStateEcho[i] == PTRUE
			&& (phid->motorPositionEcho[i] != lastPosition[i] || justStopped[i] == PTRUE) )
		{
			FIRE(PositionChange, i, phid->motorPositionEcho[i]);
			FIRE(PositionChange32, i, (int)phid->motorPositionEcho[i]);
		}

		if(phid->motorSpeedEcho[i] != PUNK_DBL && phid->motorSpeedEcho[i] != lastSpeed[i])
			FIRE(VelocityChange, i, phid->motorSpeedEcho[i]);

		if(phid->motorSensedCurrent[i] != PUNK_DBL && phid->motorSensedCurrent[i] != lastCurrent[i])
			FIRE(CurrentChange, i, phid->motorSensedCurrent[i]);
	}

	return EPHIDGET_OK;
}

//eventsAfterOpen - sends out an event for all valid data, used during attach initialization
CPHIDGETINITEVENTS(Stepper)

	for (i = 0; i<phid->phid.attr.stepper.numInputs; i++)
	{
		if(phid->inputState[i] != PUNK_BOOL)
			FIRE(InputChange, i, phid->inputState[i]);
	}
	for (i = 0; i<phid->phid.attr.stepper.numMotors; i++)
	{
		if(phid->motorSensedCurrent[i] != PUNK_DBL)
			FIRE(CurrentChange, i, phid->motorSensedCurrent[i]);
		if(phid->motorSpeedEcho[i] != PUNK_DBL)
			FIRE(VelocityChange, i, phid->motorSpeedEcho[i]);
		if(phid->motorPositionEcho[i] != PUNK_INT64 && phid->motorEngagedStateEcho[i] == PTRUE)
		{
			FIRE(PositionChange, i, phid->motorPositionEcho[i]);
			FIRE(PositionChange32, i, (int)phid->motorPositionEcho[i]);
		}
	}

	return EPHIDGET_OK;
}

//getPacket - used by write thread to get the next packet to send to device
CGETPACKET_BUF(Stepper)

//sendpacket - sends a packet to the device asynchronously, blocking if the 1-packet queue is full
CSENDPACKET_BUF(Stepper)

//makePacket - constructs a packet using current device state
CMAKEPACKETINDEXED(Stepper)
	unsigned char accel = 0, flags = 0;
	unsigned short speed = 0;
	__int64 position = 0;

	int packet_type = Index & 0x30;
	Index = Index & 0x0F;

	TESTPTRS(phid, buffer);

	if(phid->packetCounter[Index] == PUNK_INT)
		phid->packetCounter[Index] = 0;

	phid->packetCounter[Index]++;
	phid->packetCounter[Index] &= 0x0F;
	phid->motorStoppedState[Index] = PFALSE;

	//LOG(PHIDGET_LOG_VERBOSE, " OUT %d: %d",Index, phid->packetCounter[Index]);

	//have to make sure that everything to be sent has some sort of default value if the user hasn't set a value
	if(phid->motorEngagedState[Index] == PUNK_BOOL)
		phid->motorEngagedState[Index] = PFALSE; //note motors are not engaged by default

	if(phid->motorEngagedState[Index] == PFALSE)
		flags |= MOTOR_DISABLED_STEPPER;

	switch(packet_type)
	{
		case STEPPER_POSITION_PACKET:
			break;
		case STEPPER_VEL_ACCEL_PACKET:
			if(phid->motorSpeed[Index] == PUNK_DBL)
				phid->motorSpeed[Index] = phid->motorSpeedMax / 2;
			if(phid->motorAcceleration[Index] == PUNK_DBL)
				phid->motorAcceleration[Index] = phid->accelerationMax / 2; //mid-level acceleration
			break;
		case STEPPER_RESET_PACKET:
			phid->motorPosition[Index] = phid->motorPositionReset[Index];
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
			//2-bit index, 2-bit packet type, 4-bit counter 
			buffer[0] = (Index << 6) | packet_type | phid->packetCounter[Index]; 
			//flags
			buffer[1] = flags; 

			switch(packet_type)
			{
				case STEPPER_POSITION_PACKET:
					position = phid->motorPosition[Index] << 6;
					position += 0x20;

					//48-bit position
					buffer[2] = (unsigned char)(position >> 40);
					buffer[3] = (unsigned char)(position >> 32);
					buffer[4] = (unsigned char)(position >> 24);
					buffer[5] = (unsigned char)(position >> 16);
					buffer[6] = (unsigned char)(position >> 8);
					buffer[7] = (unsigned char)(position);
					break;
				case STEPPER_VEL_ACCEL_PACKET:
					accel = (unsigned char)round((phid->motorAcceleration[Index]/phid->accelerationMax) * 63.0);
					speed = (unsigned short)round((phid->motorSpeed[Index]/phid->motorSpeedMax) * 511.0);

					//6-bit acceleration
					buffer[2] = accel;
					//9-bit speed
					buffer[3] = speed >> 8;
					buffer[4] = speed & 0xFF;
					//not used
					buffer[5] = 0;
					buffer[6] = 0;
					buffer[7] = 0;
					break;
				case STEPPER_RESET_PACKET:
					position = phid->motorPositionReset[Index] << 6;
					position += 0x20;

					//48-bit position
					buffer[2] = (unsigned char)(position >> 40);
					buffer[3] = (unsigned char)(position >> 32);
					buffer[4] = (unsigned char)(position >> 24);
					buffer[5] = (unsigned char)(position >> 16);
					buffer[6] = (unsigned char)(position >> 8);
					buffer[7] = (unsigned char)(position);
					break;
				default:
					return EPHIDGET_UNEXPECTED;
			}
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
			{
				unsigned char currentLimit = 0;
				double Vref;

				//2-bit index, 2-bit packet type, 4-bit counter 
				buffer[0] = (Index << 6) | packet_type | phid->packetCounter[Index]; 
				//flags
				buffer[1] = flags; 

				switch(packet_type)
				{
					case STEPPER_POSITION_PACKET:
						position = phid->motorPosition[Index] << 3;
						position += 0x04;

						//48-bit position
						buffer[2] = (unsigned char)(position >> 40);
						buffer[3] = (unsigned char)(position >> 32);
						buffer[4] = (unsigned char)(position >> 24);
						buffer[5] = (unsigned char)(position >> 16);
						buffer[6] = (unsigned char)(position >> 8);
						buffer[7] = (unsigned char)(position);
						break;
					case STEPPER_VEL_ACCEL_PACKET:
						if(phid->motorCurrentLimit[Index] == PUNK_DBL)
							phid->motorCurrentLimit[Index] = 0.50; //choose 500mA - should at least work for the most part

						accel = (unsigned char)round((phid->motorAcceleration[Index]/phid->accelerationMax) * 255.0);
						speed = (unsigned short)round((phid->motorSpeed[Index]/phid->motorSpeedMax) * 4095.0);

						// highest Vref is 3v (2.5A limit as defined by stepping chip)
						// The 8 is defined by the stepping chip - (ItripMAX = Vref/8Rs)
						Vref = phid->motorCurrentLimit[Index] * 8 * BIPOLAR_STEPPER_CURRENT_LIMIT_Rs;
						// DAC output is 0-63 = 0-4.16v, highest value is 45 (3v)
						// 2.08 = Vbandgap * 1.6
						currentLimit = (unsigned char)round( (((Vref - 2.08)/2.08) * 32) + 31 );

						//8-bit acceleration
						buffer[2] = accel;
						//12-bit speed
						buffer[3] = speed >> 8;
						buffer[4] = speed & 0xFF;
						//6-bit current limit
						buffer[5] = currentLimit;
						//not used
						buffer[6] = 0;
						buffer[7] = 0;
						break;
					case STEPPER_RESET_PACKET:
						position = phid->motorPositionReset[Index] << 3;
						position += 0x04;

						//48-bit position
						buffer[2] = (unsigned char)(position >> 40);
						buffer[3] = (unsigned char)(position >> 32);
						buffer[4] = (unsigned char)(position >> 24);
						buffer[5] = (unsigned char)(position >> 16);
						buffer[6] = (unsigned char)(position >> 8);
						buffer[7] = (unsigned char)(position);
						break;
					default:
						return EPHIDGET_UNEXPECTED;
				}
			}
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			{
				unsigned char currentLimit = 0;
				int accelInt, velInt;

				//2-bit index, 2-bit packet type, 4-bit counter 
				buffer[0] = (Index << 6) | packet_type | phid->packetCounter[Index]; 
				//flags
				buffer[1] = flags; 

				switch(packet_type)
				{
					case STEPPER_POSITION_PACKET:
						//64-bit position
						buffer[2] = (unsigned char)(phid->motorPosition[Index] >> 56);
						buffer[3] = (unsigned char)(phid->motorPosition[Index] >> 48);
						buffer[4] = (unsigned char)(phid->motorPosition[Index] >> 40);
						buffer[5] = (unsigned char)(phid->motorPosition[Index] >> 32);
						buffer[6] = (unsigned char)(phid->motorPosition[Index] >> 24);
						buffer[7] = (unsigned char)(phid->motorPosition[Index] >> 16);
						buffer[8] = (unsigned char)(phid->motorPosition[Index] >> 8);
						buffer[9] = (unsigned char)(phid->motorPosition[Index]);
						break;
					case STEPPER_VEL_ACCEL_PACKET:
						if(phid->motorCurrentLimit[Index] == PUNK_DBL)
							phid->motorCurrentLimit[Index] = 0.50; //choose 500mA - should at least work for the most part

						accelInt = (int)phid->motorAcceleration[Index];
						velInt = (int)phid->motorSpeed[Index];
						currentLimit = (unsigned char)round((phid->motorCurrentLimit[Index]/phid->currentMax) * 255.0);

						//3-byte acceleration
						buffer[2] = accelInt >> 16;
						buffer[3] = accelInt >> 8;
						buffer[4] = accelInt;

						//3-byte velocity
						buffer[5] = velInt >> 16;
						buffer[6] = velInt >> 8;
						buffer[7] = velInt;

						//8-bit current limit
						buffer[8] = currentLimit;

						//not used
						buffer[9] = 0;
						break;
					case STEPPER_RESET_PACKET:
						//64-bit position
						buffer[2] = (unsigned char)(phid->motorPositionReset[Index] >> 56);
						buffer[3] = (unsigned char)(phid->motorPositionReset[Index] >> 48);
						buffer[4] = (unsigned char)(phid->motorPositionReset[Index] >> 40);
						buffer[5] = (unsigned char)(phid->motorPositionReset[Index] >> 32);
						buffer[6] = (unsigned char)(phid->motorPositionReset[Index] >> 24);
						buffer[7] = (unsigned char)(phid->motorPositionReset[Index] >> 16);
						buffer[8] = (unsigned char)(phid->motorPositionReset[Index] >> 8);
						buffer[9] = (unsigned char)(phid->motorPositionReset[Index]);
						break;
					default:
						return EPHIDGET_UNEXPECTED;
				}
			}
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	return EPHIDGET_OK;
}

// === Exported Functions === //

//create and initialize a device structure
CCREATE(Stepper, PHIDCLASS_STEPPER)

//event setup functions
CFHANDLE(Stepper, InputChange, int, int)
CFHANDLE(Stepper, PositionChange, int, __int64)
CFHANDLE(Stepper, PositionChange32, int, int)
CFHANDLE(Stepper, VelocityChange, int, double)
CFHANDLE(Stepper, CurrentChange, int, double)

CGET(Stepper,InputCount,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	MASGN(phid.attr.stepper.numInputs)
}

CGETINDEX(Stepper,InputState,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numInputs)
	TESTMASGN(inputState[Index], PUNK_BOOL)

	MASGN(inputState[Index])
}

CGET(Stepper,MotorCount,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	MASGN(phid.attr.stepper.numMotors)
}
CGETINDEX(Stepper,Acceleration,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorAcceleration[Index], PUNK_DBL)

	MASGN(motorAcceleration[Index])
}
CSETINDEX(Stepper,Acceleration,double)
	TESTPTR(phid) 
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTRANGE(phid->accelerationMin, phid->accelerationMax)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Acceleration, "%lE", motorAcceleration);
	else
		SENDPACKETINDEXED(Stepper, motorAcceleration[Index], Index + STEPPER_VEL_ACCEL_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,AccelerationMax,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(accelerationMax, PUNK_DBL)

	MASGN(accelerationMax)
}

CGETINDEX(Stepper,AccelerationMin,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(accelerationMin, PUNK_DBL)

	MASGN(accelerationMin)
}

CGETINDEX(Stepper,Velocity,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorSpeedEcho[Index], PUNK_DBL)

	MASGN(motorSpeedEcho[Index])
}

CGETINDEX(Stepper,VelocityLimit,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorSpeed[Index], PUNK_DBL)

	MASGN(motorSpeed[Index])
}
CSETINDEX(Stepper,VelocityLimit,double)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTRANGE(phid->motorSpeedMin, phid->motorSpeedMax)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(VelocityLimit, "%lE", motorSpeed);
	else
		SENDPACKETINDEXED(Stepper, motorSpeed[Index], Index + STEPPER_VEL_ACCEL_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,VelocityMax,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorSpeedMax, PUNK_DBL)

	MASGN(motorSpeedMax)
}

CGETINDEX(Stepper,VelocityMin,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorSpeedMin, PUNK_DBL)

	MASGN(motorSpeedMin)
}

CGETINDEX(Stepper,TargetPosition,__int64)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorPosition[Index], PUNK_INT64)

	MASGN(motorPosition[Index])
}
CSETINDEX(Stepper,TargetPosition,__int64)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTRANGE(phid->motorPositionMin, phid->motorPositionMax)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(TargetPosition, "%lld", motorPosition);
	else
		SENDPACKETINDEXED(Stepper, motorPosition[Index], Index + STEPPER_POSITION_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,CurrentPosition,__int64)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorPositionEcho[Index], PUNK_INT64)

	MASGN(motorPositionEcho[Index])
}
CSETINDEX(Stepper,CurrentPosition,__int64)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTRANGE(phid->motorPositionMin, phid->motorPositionMax)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(CurrentPosition, "%lld", motorPositionReset);
	else
		SENDPACKETINDEXED(Stepper, motorPositionReset[Index], Index + STEPPER_RESET_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,PositionMax,__int64)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorPositionMax, PUNK_INT64)

	MASGN(motorPositionMax)
}

CGETINDEX(Stepper,PositionMin,__int64)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorPositionMin, PUNK_INT64)

	MASGN(motorPositionMin)
}
CGETINDEX(Stepper,TargetPosition32,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorPosition[Index], PUNK_INT)

	*pVal = (int)phid->motorPosition[Index]; return EPHIDGET_OK;
}
CSETINDEX(Stepper,TargetPosition32,int)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTRANGE(phid->motorPositionMin, phid->motorPositionMax)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(TargetPosition, "%d", motorPosition);
	else
		SENDPACKETINDEXED(Stepper, motorPosition[Index], Index + STEPPER_POSITION_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,CurrentPosition32,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorPositionEcho[Index], PUNK_INT)

	*pVal = (int)phid->motorPositionEcho[Index]; return EPHIDGET_OK;
}
CSETINDEX(Stepper,CurrentPosition32,int)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTRANGE(phid->motorPositionMin, phid->motorPositionMax)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(CurrentPosition, "%d", motorPositionReset);
	else
		SENDPACKETINDEXED(Stepper, motorPositionReset[Index], Index + STEPPER_RESET_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,PositionMax32,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)

	*pVal = -(PUNK_INT-1); return EPHIDGET_OK;
}

CGETINDEX(Stepper,PositionMin32,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)

	*pVal = (PUNK_INT-1); return EPHIDGET_OK;
}

CGETINDEX(Stepper,Current,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	//only the original bipolar has current sense
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
			TESTINDEX(phid.attr.stepper.numMotors)
			TESTMASGN(motorSensedCurrent[Index], PUNK_DBL)
			MASGN(motorSensedCurrent[Index])
			break;
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
		default:
			return EPHIDGET_UNSUPPORTED;
	}
}

CGETINDEX(Stepper,CurrentLimit,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	//only the bipolar has currentLimit
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			TESTINDEX(phid.attr.stepper.numMotors)
			TESTMASGN(motorCurrentLimit[Index], PUNK_DBL)
			MASGN(motorCurrentLimit[Index])
			break;
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
		default:
			return EPHIDGET_UNSUPPORTED;
	}
}
CSETINDEX(Stepper,CurrentLimit,double)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	//only the bipolar has currentLimit
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			TESTINDEX(phid.attr.stepper.numMotors)
			TESTRANGE(phid->currentMin, phid->currentMax)
			if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
				ADDNETWORKKEYINDEXED(CurrentLimit, "%lE", motorCurrentLimit);
			else
				SENDPACKETINDEXED(Stepper, motorCurrentLimit[Index], Index + STEPPER_VEL_ACCEL_PACKET);
			break;
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
		default:
			return EPHIDGET_UNSUPPORTED;
	}

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,CurrentMax,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	//only the bipolar has current
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			TESTINDEX(phid.attr.stepper.numMotors)
				TESTMASGN(currentMax, PUNK_DBL)
				MASGN(currentMax)
			break;
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
		default:
			return EPHIDGET_UNSUPPORTED;
	}
}

CGETINDEX(Stepper,CurrentMin,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED

	//only the bipolar has current
	switch(phid->phid.deviceUID)
	{
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR:
		case PHIDUID_STEPPER_BIPOLAR_1MOTOR_M3:
			TESTINDEX(phid.attr.stepper.numMotors)
				TESTMASGN(currentMin, PUNK_DBL)
				MASGN(currentMin)
			break;
		case PHIDUID_STEPPER_UNIPOLAR_4MOTOR:
		default:
			return EPHIDGET_UNSUPPORTED;
	}
}

CGETINDEX(Stepper,Engaged,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorEngagedStateEcho[Index], PUNK_BOOL)

	MASGN(motorEngagedStateEcho[Index])
}
CSETINDEX(Stepper,Engaged,int)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTRANGE(PFALSE, PTRUE)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Engaged, "%d", motorEngagedState);
	else
		//hang off the STEPPER_VEL_ACCEL_PACKET because this will have the side effect of also setting these two states
		SENDPACKETINDEXED(Stepper, motorEngagedState[Index], Index + STEPPER_VEL_ACCEL_PACKET);

	return EPHIDGET_OK;
}

CGETINDEX(Stepper,Stopped,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_STEPPER)
	TESTATTACHED
	TESTINDEX(phid.attr.stepper.numMotors)
	TESTMASGN(motorStoppedState[Index], PUNK_BOOL)

	MASGN(motorStoppedState[Index])
}