aboutsummaryrefslogtreecommitdiffstats
path: root/cthread.h
blob: c3af6643a84bb41f214fbc906f6a01f1dd8e1538 (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
#ifndef __CTHREAD
#define __CTHREAD

#ifndef EXTERNALPROTO

#ifdef _WINDOWS
typedef void *CThread_thread_t;
typedef DWORD CThread_func_return_t;
typedef LPVOID CThread_func_arg_t;
typedef CRITICAL_SECTION CThread_mutex_t;
typedef HANDLE EVENT;
typedef DWORD EVENT_TIME;
#else

typedef struct {
	pthread_mutex_t mutex;
	pthread_cond_t condition;
	int ready_to_go;
} UNIX_EVENT;

typedef unsigned long EVENT_TIME;
typedef UNIX_EVENT EVENT;
typedef pthread_t CThread_thread_t;
typedef void *CThread_func_return_t;
typedef void *CThread_func_arg_t;
typedef pthread_mutex_t CThread_mutex_t;
#endif
typedef CThread_func_return_t(* CThread_func_t)(CThread_func_arg_t);

typedef struct {
	CThread_thread_t m_ThreadHandle;
	unsigned long m_ThreadIdentifier;
	unsigned char thread_status;
#ifdef _MACOSX
	CFRunLoopRef runLoop;
	unsigned char macInitDone;
#endif
} CThread;

int CThread_create(CThread *cp, CThread_func_t fp, CThread_func_arg_t arg);
int CThread_create_detached(CThread *cp, CThread_func_t fp, CThread_func_arg_t arg);
void CThread_join(CThread *cp);
//void CThread_kill(CThread *cp);
int CThread_is_my_thread(CThread cp);
int CThread_mutex_init(CThread_mutex_t *);
int CThread_mutex_destroy(CThread_mutex_t *mp);
void CThread_mutex_lock(CThread_mutex_t *mp);
void CThread_mutex_unlock(CThread_mutex_t *mp);

void CThread_set_event(EVENT *ev);
void CThread_reset_event(EVENT *ev);
int CThread_wait_on_event(EVENT *ev, EVENT_TIME time);
void CThread_create_event(EVENT *ev);
int CThread_destroy_event(EVENT *ev);

int RegisterLocalDevice(CPhidgetHandle phid);
int StartCentralThread();
int JoinCentralThread();

CThread_func_return_t ReadThreadFunction(CThread_func_arg_t arg);
CThread_func_return_t WriteThreadFunction(CThread_func_arg_t arg);

#endif
#endif
* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#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