aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetstepper.h
blob: cd1c0bfa532f8c71dc9dce0563196bfb4e5063a0 (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
#ifndef __CPHIDGETSTEPPER
#define __CPHIDGETSTEPPER
#include "cphidget.h"

/** \defgroup phidstepper Phidget Stepper 
 * \ingroup phidgets
 * These calls are specific to the Phidget Stepper object. See your device's User Guide for more specific API details, technical information, and revision details. The User Guide, along with other resources, can be found on the product page for your device.
 * @{
 */

DPHANDLE(Stepper)
CHDRSTANDARD(Stepper)


/**
 * Gets the number of digital inputs supported by this board.
 * @param phid An attached phidget stepper handle.
 * @param count The ditial input count.
 */
CHDRGET(Stepper,InputCount,int *count)
/**
 * Gets the state of a digital input.
 * @param phid An attached phidget stepper handle.
 * @param index The input index.
 * @param inputState The input state. Possible values are \ref PTRUE and \ref PFALSE.
 */
CHDRGETINDEX(Stepper,InputState,int *inputState)
/**
 * Set a digital input change handler. This is called when a digital input changes.
 * @param phid An attached phidget stepper handle.
 * @param fptr Callback function pointer.
 * @param userPtr A pointer for use by the user - this value is passed back into the callback function.
 */
CHDREVENTINDEX(Stepper,InputChange,int inputState)

/**
 * Gets the number of motors supported by this controller
 * @param phid An attached phidget stepper handle.
 * @param count The motor count.
 */
CHDRGET(Stepper,MotorCount,int *count)

/**
 * Gets the last set acceleration for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param acceleration The acceleration
 */
CHDRGETINDEX(Stepper,Acceleration,double *acceleration)
/**
 * Sets the acceleration for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param acceleration The acceleration
 */
CHDRSETINDEX(Stepper,Acceleration,double acceleration)
/**
 * Gets the maximum acceleration supported by a motor
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param max The maximum acceleration.
 */
CHDRGETINDEX(Stepper,AccelerationMax,double *max)
/**
 * Gets the minimum acceleration supported by a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param min The minimum acceleration
 */
CHDRGETINDEX(Stepper,AccelerationMin,double *min)

/**
 * Gets the last set velocity limit for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param limit The velocity limit.
 */
CHDRGETINDEX(Stepper,VelocityLimit,double *limit)
/**
 * Sets the velocity limit for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param limit The velocity limit.
 */
CHDRSETINDEX(Stepper,VelocityLimit,double limit)
/**
 * Gets the current velocity of a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param velocity The current velocity.
 */
CHDRGETINDEX(Stepper,Velocity,double *velocity)
/**
 * Gets the maximum velocity that can be set for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param max The maximum velocity
 */
CHDRGETINDEX(Stepper,VelocityMax,double *max)
/**
 * Gets the minimum velocity that can be set for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param min The minimum velocity.
 */
CHDRGETINDEX(Stepper,VelocityMin,double *min)
/**
 * Sets a velocity change event handler. This is called when the velocity changes.
 * @param phid An attached phidget stepper handle
 * @param fptr Callback function pointer.
 * @param userPtr A pointer for use by the user - this value is passed back into the callback function.
 */
CHDREVENTINDEX(Stepper,VelocityChange,double velocity)

/**
 * Gets the last set target position of a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param position The position.
 */
CHDRGETINDEX(Stepper,TargetPosition,__int64 *position)
/**
 * Sets the target position of a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param position The position.
 */
CHDRSETINDEX(Stepper,TargetPosition,__int64 position)
/**
 * Gets the current position of a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param position The position.
 */
CHDRGETINDEX(Stepper,CurrentPosition,__int64 *position)
/**
 * Sets the current position of a motor. This will not move the motor, just update the position value.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param position The position.
 */
CHDRSETINDEX(Stepper,CurrentPosition,__int64 position)
/**
 * Gets the maximum position that a motor can go to.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param max The maximum position.
 */
CHDRGETINDEX(Stepper,PositionMax,__int64 *max)
/**
 * Gets the minimum position that a motor can go to.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param min The minimum position
 */
CHDRGETINDEX(Stepper,PositionMin,__int64 *min)
/**
 * Sets a position change event handler. This is called when the position changes.
 * @param phid An attached phidget stepper handle
 * @param fptr Callback function pointer.
 * @param userPtr A pointer for use by the user - this value is passed back into the callback function.
 */
CHDREVENTINDEX(Stepper,PositionChange,__int64 position)

/**
 * Gets the current limit for a motor.
 * @param phid An attached phidget stepper handle.
 * @param index The motor index.
 * @param limit The current limit.
 */
CHDRGETINDEX(Stepper,CurrentLimit,double *limit)
/**
 * Sets the current limit for a motor.
 * @param phid An attached phidget stepper handle.
 * @param index The motor index.
 * @param limit The current limit.
 */
CHDRSETINDEX(Stepper,CurrentLimit,double limit)
/**
 * Gets the current current draw for a motor.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param current The current.
 */
CHDRGETINDEX(Stepper,Current,double *current)
/**
 * Gets the maximum current limit.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param max The maximum current limit.
 */
CHDRGETINDEX(Stepper,CurrentMax,double *max)
/**
 * Gets the minimum current limit.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param min The minimum current limit.
 */
CHDRGETINDEX(Stepper,CurrentMin,double *min)
/**
 * Sets a current change event handler. This is called when the current draw changes.
 * @param phid An attached phidget stepper handle
 * @param fptr Callback function pointer.
 * @param userPtr A pointer for use by the user - this value is passed back into the callback function.
 */
CHDREVENTINDEX(Stepper,CurrentChange,double current)

/**
 * Gets the engaged state of a motor. This is whether the motor is powered or not.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param engagedState The engaged state. Possible values are \ref PTRUE and \ref PFALSE.
 */
CHDRGETINDEX(Stepper,Engaged,int *engagedState)
/**
 * Sets the engaged state of a motor. This is whether the motor is powered or not.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param engagedState The engaged state. Possible values are \ref PTRUE and \ref PFALSE.
 */
CHDRSETINDEX(Stepper,Engaged,int engagedState)
/**
 * Gets the stopped state of a motor. This is true when the motor is not moving and there are no outstanding commands.
 * @param phid An attached phidget stepper handle
 * @param index The motor index.
 * @param stoppedState The stopped state. Possible values are \ref PTRUE and \ref PFALSE.
 */
CHDRGETINDEX(Stepper,Stopped,int *stoppedState)

#ifndef EXTERNALPROTO

#define STEPPER_MAXSTEPPERS 8
#define STEPPER_MAXINPUTS 8

#define BIPOLAR_STEPPER_CURRENT_SENSE_GAIN 8.5
#define BIPOLAR_STEPPER_CURRENT_LIMIT_Rs 0.150

//flags - make sure these are in the upper 4 bits
#define MOTOR_DONE_STEPPER	 	0x10
#define MOTOR_DISABLED_STEPPER	0x20

//packet types - room for one more
#define STEPPER_POSITION_PACKET		0x00
#define STEPPER_VEL_ACCEL_PACKET	0x10
#define STEPPER_RESET_PACKET		0x20

struct _CPhidgetStepper{
	CPhidget phid;
          
	int (CCONV *fptrPositionChange)(CPhidgetStepperHandle, void *, int, __int64);       
	int (CCONV *fptrPositionChange32)(CPhidgetStepperHandle, void *, int, int);    
	int (CCONV *fptrVelocityChange)(CPhidgetStepperHandle, void *, int, double);                   
	int (CCONV *fptrCurrentChange)(CPhidgetStepperHandle, void *, int, double);       
	int (CCONV *fptrInputChange)(CPhidgetStepperHandle, void *, int, int);           

	void *fptrPositionChangeptr;
	void *fptrPositionChange32ptr;
	void *fptrInputChangeptr;
	void *fptrCurrentChangeptr;
	void *fptrVelocityChangeptr;

	//data from the device
	unsigned char inputState[STEPPER_MAXINPUTS];
	__int64 motorPositionEcho[STEPPER_MAXSTEPPERS];
	double motorSpeedEcho[STEPPER_MAXSTEPPERS];
	double motorSensedCurrent[STEPPER_MAXSTEPPERS];
	unsigned char motorEngagedStateEcho[STEPPER_MAXSTEPPERS];
	unsigned char motorStoppedState[STEPPER_MAXSTEPPERS];
	int packetCounterEcho[STEPPER_MAXSTEPPERS];

	//data from the user
	__int64 motorPosition[STEPPER_MAXSTEPPERS];
	__int64 motorPositionReset[STEPPER_MAXSTEPPERS];
	double motorAcceleration[STEPPER_MAXSTEPPERS];
	double motorSpeed[STEPPER_MAXSTEPPERS];
	double motorCurrentLimit[STEPPER_MAXSTEPPERS];
	unsigned char motorEngagedState[STEPPER_MAXSTEPPERS];
	int packetCounter[STEPPER_MAXSTEPPERS];
	
	double motorSpeedMax, motorSpeedMin;
	double accelerationMax, accelerationMin;
	__int64 motorPositionMax, motorPositionMin;
	double currentMax, currentMin;
	int microSteps;

	unsigned char outputPacket[MAX_OUT_PACKET_SIZE];
	unsigned int outputPacketLen;
} typedef CPhidgetStepperInfo;
#endif

/** @} */

#endif