aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetmotorcontrol.h
blob: 4bd76b82b02bcffd54820c74f0f63e909c9cc55d (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
#ifndef __CPHIDGETMOTORCONTROL
#define __CPHIDGETMOTORCONTROL
#include "cphidget.h"

/** \defgroup phidmotorcontrol Phidget Motor Control 
 * \ingroup phidgets
 * These calls are specific to the Phidget Motor Control 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(MotorControl)
CHDRSTANDARD(MotorControl)

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

/**
 * Gets the current velocity of a motor.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param velocity The current velocity.
 */
CHDRGETINDEX(MotorControl,Velocity,double *velocity)
/**
 * Sets the velocity of a motor.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param velocity The velocity.
 */
CHDRSETINDEX(MotorControl,Velocity,double velocity)
/**
 * Sets a velocity change event handler. This is called when the velocity changes.
 * @param phid An attached phidget motor control 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(MotorControl,VelocityChange,double velocity)

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


/**
 * Gets the current current draw for a motor.
 * @param phid An attached phidget motor control handle
 * @param index The motor index.
 * @param current The current.
 */
CHDRGETINDEX(MotorControl,Current,double *current)
/**
 * Sets a current change event handler. This is called when the current draw changes.
 * @param phid An attached phidget motor control 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(MotorControl,CurrentChange,double current)

/**
 * Gets the number of digital inputs supported by this board.
 * @param phid An attached phidget motor control handle.
 * @param count The ditial input count.
 */
CHDRGET(MotorControl,InputCount,int *count)
/**
 * Gets the state of a digital input.
 * @param phid An attached phidget motor control handle.
 * @param index The input index.
 * @param inputState The input state. Possible values are \ref PTRUE and \ref PFALSE.
 */
CHDRGETINDEX(MotorControl,InputState,int *inputState)
/**
 * Set a digital input change handler. This is called when a digital input changes.
 * @param phid An attached phidget motor control 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(MotorControl,InputChange,int inputState)

/**
 * Gets the number of encoder inputs supported by this board.
 * @param phid An attached phidget motor control handle.
 * @param count The encoder input count.
 */
CHDRGET(MotorControl,EncoderCount,int *count)
/**
 * Gets the position of an encoder. This position starts at 0 every time the phidget is opened.
 * @param phid An attached phidget motor control handle.
 * @param index The encoder index.
 * @param position The encoder position.
 */
CHDRGETINDEX(MotorControl,EncoderPosition,int *position)
/**
 * Sets the encoder position. This can be used to set the position to a known value, and should only be called when the encoder is not moving.
 * @param phid An attached phidget motor control handle.
 * @param index The encoder index.
 * @param position The encoder position.
 */
CHDRSETINDEX(MotorControl,EncoderPosition,int position)
/**
 * Set an encoder position change handler. This is called when the encoder position changes.
 * @param phid An attached phidget motor control 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(MotorControl,EncoderPositionChange,int time,int positionChange)
/**
 * Set an encoder position update handler. This is called at a constant rate; every 8ms, whether the encoder position has changed or not.
 * @param phid An attached phidget motor control 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(MotorControl,EncoderPositionUpdate,int positionChange)

/**
 * Gets the Back EMF sensing state for a motor.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param bEMFState The back EMF sensing state.
 */
CHDRGETINDEX(MotorControl,BackEMFSensingState,int *bEMFState)
/**
 * Sets the Back EMF sensing state for a motor.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param bEMFState The back EMF sensing state.
 */
CHDRSETINDEX(MotorControl,BackEMFSensingState,int bEMFState)
/**
 * Gets the Back EMF voltage for a motor.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param voltage The back EMF voltage, in volts.
 */
CHDRGETINDEX(MotorControl,BackEMF,double *voltage)
/**
 * Set a back EMF update handler. This is called at a constant rate; every 16ms, when back EMF sensing is enabled for that motor.
 * @param phid An attached phidget motor control 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(MotorControl,BackEMFUpdate,double voltage)

/**
 * Gets the Supply voltage for the motors. This could be higher then the actual supply voltage.
 * @param phid An attached phidget motor control handle.
 * @param supplyVoltage The supply voltage, in volts.
 */
CHDRGET(MotorControl,SupplyVoltage,double *supplyVoltage)

/**
 * Gets the Braking value for a motor.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param braking The braking value, in percent.
 */
CHDRGETINDEX(MotorControl,Braking,double *braking)
/**
 * Sets the Braking value for a motor. This is applied when velocity is 0. Default is 0%.
 * @param phid An attached phidget motor control handle.
 * @param index The motor index.
 * @param braking The braking value, in percent.
 */
CHDRSETINDEX(MotorControl,Braking,double braking)

/**
 * Gets the number of sensor inputs supported by this board.
 * @param phid An attached phidget motor control handle.
 * @param count The sensor input count.
 */
CHDRGET(MotorControl, SensorCount, int *count)
/**
 * Gets the value of a sensor.
 * @param phid An attached phidget motor control handle.
 * @param index The sensor index.
 * @param sensorValue The sensor value, range: 0-1000.
 */
CHDRGETINDEX(MotorControl, SensorValue, int *sensorValue)
/**
 * Gets the raw value of a sensor (12-bit).
 * @param phid An attached phidget motor control handle.
 * @param index The sensor index.
 * @param sensorRawValue The sensor value, range: 0-4096.
 */
CHDRGETINDEX(MotorControl, SensorRawValue, int *sensorRawValue)
/**
 * Set a sensor update handler. This is called at a constant rate; every 8ms.
 * @param phid An attached phidget motor control 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(MotorControl, SensorUpdate, int sensorValue)
/**
 * Gets the ratiometric state.
 * @param phid An attached phidget motor control handle.
 * @param ratiometric The ratiometric state.
 */
CHDRGET(MotorControl, Ratiometric, int *ratiometric)
/**
 * Sets the ratiometric state. This control the voltage reference used for sampling the analog sensors.
 * @param phid An attached phidget motor control handle.
 * @param ratiometric The ratiometric state.
 */
CHDRSET(MotorControl, Ratiometric, int ratiometric)

/**
 * Set a current update handler. This is called at a constant rate; every 8ms.
 * @param phid An attached phidget motor control 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(MotorControl,CurrentUpdate,double current)

#ifndef REMOVE_DEPRECATED
DEP_CHDRGET("Deprecated - use CPhidgetMotorControl_getMotorCount",MotorControl,NumMotors,int *)
DEP_CHDRGET("Deprecated - use CPhidgetMotorControl_getInputCount",MotorControl,NumInputs,int *)
DEP_CHDRGETINDEX("Deprecated - use CPhidgetMotorControl_getVelocity",MotorControl,MotorSpeed,double *)
DEP_CHDRSETINDEX("Deprecated - use CPhidgetMotorControl_setVelocity",MotorControl,MotorSpeed,double)
DEP_CHDREVENTINDEX("Deprecated - use CPhidgetMotorControl_set_OnVelocityChange_Handler",MotorControl,MotorChange,double motorSpeed)
#endif

#ifndef EXTERNALPROTO
#define MOTORCONTROL_MAXMOTORS 2
#define MOTORCONTROL_MAXINPUTS 4
#define MOTORCONTROL_MAXENCODERS 1
#define MOTORCONTROL_MAXSENSORS 2

#define MOTORCONTROL_ERRORFLAG_OVERTEMP				0x0001
#define MOTORCONTROL_ERRORFLAG_UNDERVOLTAGE_LOCKOUT	0x0002
#define MOTORCONTROL_ERRORFLAG_NOPOWER				0x0004
#define MOTORCONTROL_ERRORFLAG_LOWPOWER				0x0008
#define MOTORCONTROL_ERRORFLAG_HIGHPOWER			0x0010
#define MOTORCONTROL_ERRORFLAG_DANGERPOWER			0x0020

struct _CPhidgetMotorControl {
	CPhidget phid;
   
	int (CCONV *fptrVelocityChange)(CPhidgetMotorControlHandle, void *, int, double);               
	int (CCONV *fptrInputChange)(CPhidgetMotorControlHandle, void *, int, int);     
	int (CCONV *fptrCurrentChange)(CPhidgetMotorControlHandle, void *, int, double);
	int (CCONV *fptrEncoderPositionChange)(CPhidgetMotorControlHandle, void *, int, int, int);
	int (CCONV *fptrEncoderPositionUpdate)(CPhidgetMotorControlHandle, void *, int, int);
	int (CCONV *fptrBackEMFUpdate)(CPhidgetMotorControlHandle, void *, int, double);       
	int (CCONV *fptrSensorUpdate)(CPhidgetMotorControlHandle, void *, int, int);    
	int (CCONV *fptrCurrentUpdate)(CPhidgetMotorControlHandle, void *, int, double);   

	void *fptrInputChangeptr;
	void *fptrVelocityChangeptr;
	void *fptrCurrentChangeptr;
	void *fptrEncoderPositionChangeptr;
	void *fptrEncoderPositionUpdateptr;
	void *fptrBackEMFUpdateptr;
	void *fptrSensorUpdateptr;
	void *fptrCurrentUpdateptr;

	//Deprecated
	int (CCONV *fptrMotorChange)(CPhidgetMotorControlHandle, void *, int, double);   
	void *fptrMotorChangeptr;

	//Returned from the device
	unsigned char inputState[MOTORCONTROL_MAXINPUTS];
	double motorSpeedEcho[MOTORCONTROL_MAXMOTORS];
	double motorSensedCurrent[MOTORCONTROL_MAXMOTORS];
	double motorSensedBackEMF[MOTORCONTROL_MAXMOTORS];
	unsigned char backEMFSensingStateEcho[MOTORCONTROL_MAXMOTORS];
	double motorSetSpeedEcho[MOTORCONTROL_MAXMOTORS];
	double motorAccelerationEcho[MOTORCONTROL_MAXMOTORS];
	double motorBrakingEcho[MOTORCONTROL_MAXMOTORS];
	double supplyVoltage;
	int encoderPositionEcho[MOTORCONTROL_MAXENCODERS];
	int encoderTimeStamp[MOTORCONTROL_MAXENCODERS];
	unsigned char ratiometricEcho;
	int sensorRawValue[MOTORCONTROL_MAXSENSORS];
	int sensorValue[MOTORCONTROL_MAXSENSORS];

	int lastPacketCount;

	//Local set data
	double motorSpeed[MOTORCONTROL_MAXMOTORS];
	double motorAcceleration[MOTORCONTROL_MAXMOTORS];
	unsigned char backEMFSensingState[MOTORCONTROL_MAXMOTORS];
	double motorBraking[MOTORCONTROL_MAXMOTORS];
	unsigned char ratiometric;

	int encoderPositionDelta[MOTORCONTROL_MAXENCODERS];
	unsigned short motorErrors[MOTORCONTROL_MAXMOTORS];

	//Constants
	double accelerationMax, accelerationMin;

	//for Webservice
	double lastVoltage;
	int encoderPositionUpdates[MOTORCONTROL_MAXENCODERS];

	unsigned char outputPacket[8];
	unsigned int outputPacketLen;
} typedef CPhidgetMotorControlInfo;
#endif

/** @} */

#endif