Beispiel #1
0
/*----------------------------------------------------------------------
 * i3dmgx3_RawSensor  0xC1
 *
 * parameters   portNum : the number of the sensor device (1..n)
 *              pI3Record : struct to receive floating point values for
 *                          raw Accel (x,y,z) and raw Ang (x,y,z)
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *---------------------------------------------------------------------*/
int i3dmgx3_RawSensor(int portNum, unsigned char *pRecord) {
    int responseLength = 31;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_RAW_ACCELEROMETER; /* value is 0xC1 */
	int i = 0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
   		status = receiveData(portNum, &pRecord[0], responseLength);
        if (DEBUG) printf("Raw Sensor i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength);
			
    	if (status==I3DMGX3_COMM_OK) {
            wChecksum = convert2ushort(&pRecord[responseLength-2]);
	        wCalculatedCheckSum = i3dmgx3_Checksum(&pRecord[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
			if(wChecksum != wCalculatedCheckSum)		
				return	status = I3DMGX3_CHECKSUM_ERROR; 
		}else
			return status = I3DMGX3_COMM_READ_ERROR;	
	}else
		status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
}
Beispiel #2
0
/*----------------------------------------------------------------------
 * i3dmgx3_getDeviceIdentity  0xEA
 *
 * parameters   portNum : the number of the sensor device (1..n)
 *              flag      : identifier for which Identity to obtain.
 *              id        : a pointer to char string, already allocated.
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *
 * WARNING - does not check to see if you have allocated enough space
 *           12 bytes for the string to contain the firmware version.
 *--------------------------------------------------------------------*/
int i3dmgx3_getDeviceIdentiy(int portNum, char flag, unsigned char* Bresponse) {
    char cmd = (char) CMD_GET_DEVICE_ID;		//0xEA
    unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
    int status; 
	int responseLength = 20; 
	unsigned char iden_buff[2];
	
	iden_buff[0] = 0xEA; // Device Identity Command Code
	iden_buff[1] = flag; // Identifier of specific device identity component to obtain

	status = sendBuffData(portNum, &iden_buff[0], 2);
	if (DEBUG) printf("Get Identity_send: tx status : %d\n", status);
	if (status == I3DMGX3_COMM_OK) {
    	status = receiveData(portNum, &Bresponse[0], responseLength);
		if (DEBUG) printf("Get Identity i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength);
    	if (status==I3DMGX3_COMM_OK) {
			wChecksum = convert2ushort(&Bresponse[responseLength-2]);
			wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
			if(wChecksum != wCalculatedCheckSum)
				return	status = I3DMGX3_CHECKSUM_ERROR;
		}else
			return status = I3DMGX3_COMM_READ_ERROR;
	}else return status;
   	
return I3DMGX3_OK;
}
Beispiel #3
0
/*----------------------------------------------------------------------
 * i3dmgx3_Tempatures	0xD1
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              pI3Temps     : pointer to a struct containing Temperature
 *                             Accel and Temp gyro x y and z values.
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_Tempatures(int portNum, unsigned char* Bresponse) {
    int responseLength = 15;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_TEMPERATURES; /* value is 0xD1 */
	int i = 0;
	WORD zetemp = 0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("Temperatures i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
    	status = receiveData(portNum, &Bresponse[0], responseLength);
        if (DEBUG) printf("Temperatures i3dmgx3_send: rx status : %d and responseLength %d\n", status);
    	if (status==I3DMGX3_COMM_OK) {
            wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	        wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
			if(wChecksum != wCalculatedCheckSum)
				return status = I3DMGX3_CHECKSUM_ERROR;
		}else
			return status = I3DMGX3_COMM_READ_ERROR;		
	}else
		return status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
 }
Beispiel #4
0
/*----------------------------------------------------------------------
 * i3dmgx3_OrientUpMatrix 0xC6
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              pI3Record    : pointer to a  float matrix
 *                             which will contain the Orientation Update
 *                             data upon return.
 *              
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns 
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_OrientUpMatrix(int portNum, unsigned char* Bresponse) {
    int responseLength = 43;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_ATTITUDE_UP_MATRIX; /* value is 0xC6 */
	int i = 0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("Orient Update i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
      		status = receiveData(portNum, &Bresponse[0], responseLength);
            if (DEBUG) printf("Orient Update i3dmgx3_send: rx status : %d  and responseLength %d\n", status, responseLength);
					   
    		if (status==I3DMGX3_COMM_OK) {
                wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
					if(wChecksum != wCalculatedCheckSum)
						return	status = I3DMGX3_CHECKSUM_ERROR;
			}else
			    return status = I3DMGX3_COMM_READ_ERROR;
	}else
		status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
}
Beispiel #5
0
/*----------------------------------------------------------------------
 * i3dmgx3_TransferNonVolatile	0xD0
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              transfer     : numeric value identifying which quantity 
 *                             to Transfer: 1 == Accel Bias						   
 *                                          2 == Gyro Bias
 *                            Any other value results in no action taken
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_TransferNonVolatile(int portNum, int transfer, unsigned char *Bresponse) {
    int responseLength = 9;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_TRANSFER_NONV_MEM; /* value is 0xD0 */
	int i = 0;
	unsigned char outbuff[5];
			 
    outbuff[0] = 0xD0; // Command Code Identifier
	outbuff[1] = 0xC1; // Required identifier
	outbuff[2] = 0x29; // Required identifier
	outbuff[3] =  (transfer & MSB_MASK) >> 8; //1st byte of quantity to transfer
	outbuff[4] =  transfer & LSB_MASK;       //2ond byte of quantity to transfer
	                  // 1==Accel Bias 2==Gyro Bias, any other value results in no action 

	status = sendBuffData(portNum, &outbuff[0], 5);
   	if (DEBUG) printf(" Transfer Non-V MEM Send struct status is %d\n", status);
        if (status == I3DMGX3_COMM_OK) {
	    status = receiveData(portNum, &Bresponse[0], responseLength);
	     if (DEBUG) printf(" Transfer Non-V MEM Receive  status is %d\n", status);
		     if (status == I3DMGX3_COMM_OK) {
			     wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	             wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2);
                  
			    if(wChecksum != wCalculatedCheckSum)
				   return status = I3DMGX3_CHECKSUM_ERROR;
	        }else			   
 			    return status = I3DMGX3_COMM_READ_ERROR;   
		}else
	       return status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
}
Beispiel #6
0
int i3dmgx3_WriteGyroBias(int portNum, BYTE *BiasBuff, unsigned char* Bresponse) {
    unsigned char cmd = CMD_WRITE_GYRO_BIAS; //0xCD
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	int responseLength = 19;
    int status;
	int i = 0;
    unsigned char BiasoutBuff[15];

    BiasoutBuff[0] = 0xCA; //CMD_WRITE_GYRO_BIAS;
	BiasoutBuff[1] = 0x12; // Required identifier
	BiasoutBuff[2] = 0xA5; // Required identifier
	for (i=0; i<12; i++)   //repacks the float values in big endian format
		BiasoutBuff[3 + i] = BiasBuff[i];
		
	status = sendBuffData(portNum, &BiasoutBuff[0], 15); // 15 equals command byte + 14 bytes of data
     
	if (DEBUG) printf(" Write Gyro Bias Send struct status is %d\n", status);
        if (status == I3DMGX3_COMM_OK) {
	    status = receiveData(portNum, &Bresponse[0], responseLength);
	    if (DEBUG) printf(" Write  Gyro Bias Receive  status is %d\n", status);
        if (status == I3DMGX3_COMM_OK) {
			wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	        wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); 
		    if(wChecksum != wCalculatedCheckSum)
			   return status = I3DMGX3_CHECKSUM_ERROR;
	    }else			   
 		   return status = I3DMGX3_COMM_READ_ERROR;
	}else
	    return status = I3DMGX3_COMM_WRITE_ERROR;

	return status;
 }
Beispiel #7
0
/*----------------------------------------------------------------------
 * i3dmgx3_captureGyroBias	0xCD
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              Bresponse    : pointer to a buffer containing returned values
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns 
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_captureGyroBias(int portNum, short sampt, BYTE *BiasBuff, unsigned char *Bresponse) {
    unsigned char cmd = CMD_CAPTURE_GYRO_BIAS; //0xCD
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	int responseLength = 19;
    int status;
	int i = 0;
	unsigned char Coutbuff[5];
	
	/* initialize send buffer to obtain gyro bias */
	Coutbuff[0] = 0xCD;   //Command code for Gyro Bias
	Coutbuff[1] = 0xC1;   // Required identifier
    Coutbuff[2] = 0x29;   // Required identifier
    Coutbuff[3] = (sampt & MSB_MASK) >> 8;  //sample time with mask
	Coutbuff[4] =  sampt & LSB_MASK; 
	
	printf("...one %d/ms moment while gyro bias values are retrieved\n", sampt);

	status = sendBuffData(portNum, &Coutbuff[0], 5);
    if (DEBUG) printf(" Capture Gyro Bias  Send struct status is %d\n", status);
	if (status == I3DMGX3_COMM_OK) {
		    status = receiveData(portNum, &Bresponse[0], responseLength);
			if (DEBUG) printf(" Capture Gyro Bias Receive  status is %d\n", status);
			if (status == I3DMGX3_COMM_OK) {
			    wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
				if(wChecksum != wCalculatedCheckSum)
					return status = I3DMGX3_CHECKSUM_ERROR;
	        }else
			    return status = I3DMGX3_COMM_READ_ERROR;        			
			
			for (i=0; i<12; i++)
				BiasBuff[i] = Bresponse[1 +i];
	}else
		return status = I3DMGX3_COMM_WRITE_ERROR;
	return status;

}
Beispiel #8
0
int i3dmgx3_getFirmwareVersion(int portNum, char *firmware) {
    unsigned char cmd = (char) CMD_FIRWARE_VERSION;
    unsigned char Bresponse[7] = {0}; 
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	
	short firmwareNum=0;
    short majorNum, minorNum, buildNum;
    int status;
	int responseLength = 7; 
	  	
    status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("FirmWare_send: tx status : %d\n", status);
	if (status == I3DMGX3_COMM_OK) {
		if (responseLength>0) {
    		status = receiveData(portNum, &Bresponse[0], responseLength);
            if (DEBUG) printf("FirmWare i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength);
    		if (status==I3DMGX3_COMM_OK) {
                wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
					if(wChecksum != wCalculatedCheckSum)
						return	status = I3DMGX3_CHECKSUM_ERROR;
			}else
					return status = I3DMGX3_COMM_READ_ERROR;
		}

		firmwareNum = convert2short(&Bresponse[3]);
		if (firmwareNum > 0) {
			/* format for firmware number is #.#.## */
	        majorNum = firmwareNum / 1000;
		    minorNum = (firmwareNum % 1000) / 100;
			buildNum = firmwareNum % 100;
		    sprintf(firmware, "%d.%d.%d", majorNum, minorNum, buildNum);
		} 
		return I3DMGX3_OK;
	}else return status;
}
Beispiel #9
0
/*----------------------------------------------------------------------
 * i3dmgx3_EulerAngRate	0xCF
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *             I3dmgx3Set    : pointer to a struct containing the values 
 *                             for pitch angles in degrees and
 *							   Angle rates x y and z
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_EulerAngRate(int portNum, unsigned char* Bresponse) {
    int responseLength = 31;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_EULER_ANGLES_ANG_RT; /* value is 0xCF */
	int i = 0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("i3dmgx3_send Euler and Ang Rate: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) { 
	   	status = receiveData(portNum, &Bresponse[0], responseLength);
        if (DEBUG) printf("Euler Ang Rate i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength);
        if (status==I3DMGX3_COMM_OK) {
            wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	        wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
			if(wChecksum != wCalculatedCheckSum)
				return status = I3DMGX3_CHECKSUM_ERROR;
		}else
			return status = I3DMGX3_COMM_READ_ERROR;
	} else
		return status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
}
Beispiel #10
0
/*----------------------------------------------------------------------
 * i3dmgx3_GetFullMatrix 0xCC
 *  	
 * parameters   portNum    : the number of the sensor device (1..n)
 *              I3Record   : 18 positional stucture of defined type 
 *                           I3Record to capture data in floating point
 *							   
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_GetFullMatrix(int portNum, unsigned char* Bresponse) {
	int responseLength = 79;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_ACEL_ANG_MAG_VEC_OR; /* value is 0xCC */
	int i=0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
    		status = receiveData(portNum, &Bresponse[0], responseLength);
            if (DEBUG) printf("i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength); /* temp adj 8-1-08 */
    		if (status==I3DMGX3_COMM_OK) {
                wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
				if(wChecksum != wCalculatedCheckSum)
                   return status = I3DMGX3_CHECKSUM_ERROR;
				}else
				return status = I3DMGX3_COMM_READ_ERROR;
    }else
		status = I3DMGX3_COMM_WRITE_ERROR;  
	return status;
}
Beispiel #11
0
/*----------------------------------------------------------------------
 * i3dmgx3_AccAngMagRate	0xCB
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              mag       : array which will contain mag data 
 *                         (3 elements X Y and Z)
 *              accel     : array which will contain acceleration data 
 *                         (3 elements X Y and Z)
 *              angRate   : array which will contain angular rate data 
 *                         (3 elements X Y and Z)
 *              
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_AccAngMagRate(int portNum, unsigned char* Bresponse) {
	int responseLength = 43;
	int status = 0, i =0;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_ACCEL_ANG_MAG_VECTO;  //0xCB
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("Get Acc ang mag i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
    		status = receiveData(portNum, &Bresponse[0], responseLength);
			if (DEBUG) printf("Accel ang mag rate i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength); /* temp adj 8-1-08 */
    		if (status==I3DMGX3_COMM_OK) {
                wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
	            if(wChecksum != wCalculatedCheckSum){
                    status = I3DMGX3_CHECKSUM_ERROR;
                }
            }else
                status = I3DMGX3_COMM_READ_ERROR;
    }else
		status = I3DMGX3_COMM_WRITE_ERROR;       
	return status;
}
Beispiel #12
0
/*----------------------------------------------------------------------
 * i3dmgx3_AccelAngOreint	0xC8
 *
 * parameters   portNum : the number of the sensor device (1..n)
 *              pI3Record : struct to receive floating point values for
 *                          Acceleration and Angular Rates (x,y,z)
 *                          and Orientation Matrix M1.1 - M1.2 ... M3.3
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns 
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_AccelAngOreint(int portNum, unsigned char* Bresponse) {
    int responseLength = 67;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_ACCEL_ANG_ORIENT; /* value is 0xC8 */
	int i = 0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("Accel AngRate OrientMatx i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
       		status = receiveData(portNum, &Bresponse[0], responseLength);
            if (DEBUG) printf("Accel Ang Orient i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength);
    		if (status==I3DMGX3_COMM_OK) {
                wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
					if(wChecksum != wCalculatedCheckSum)
						return	status = I3DMGX3_CHECKSUM_ERROR;
			}else
					return status = I3DMGX3_COMM_READ_ERROR;  	
	}else
			status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
}
Beispiel #13
0
/*----------------------------------------------------------------------
 * i3dmgx3_ScaledMagVec	0xC7
 *
 * parameters   portNum : the number of the sensor device (1..n)
 *              pI3Record : struct to receive floating point values for
 *                          scaled Magnetometer Vectors (x,y,z)
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns 
 *                          an error code.
 *---------------------------------------------------------------------*/
int i3dmgx3_ScaledMagVec(int portNum, unsigned char* Bresponse) {
    int responseLength = 19;
	int status;
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;
	unsigned char cmd = CMD_MAGNETROMETER_VECT; /* value is 0xC7 */
	int i = 0;
	
	status = sendBuffData(portNum, &cmd, 1);
    if (DEBUG) printf("Scaled Mag Vector i3dmgx3_send: tx status : %d\n", status);
    if (status == I3DMGX3_COMM_OK) {
      		status = receiveData(portNum, &Bresponse[0], responseLength);
            if (DEBUG) printf("Scaled Mag Vectorr i3dmgx3_send: rx status : %d and responseLength %d\n", status, responseLength);
    		if (status==I3DMGX3_COMM_OK) {
                wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	            wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
					if(wChecksum != wCalculatedCheckSum)
						return	status = I3DMGX3_CHECKSUM_ERROR;
			}else
					return status = I3DMGX3_COMM_READ_ERROR;			
		}else
			status = I3DMGX3_COMM_WRITE_ERROR;
	return status;
}
Beispiel #14
0
//===========================================================================
// SetDataRate
//---------------------------------------------------------------------------
// Description: Allows user to set the DataRate between 1Hz and 300Hz
//              Default is 100Hz
//
// Return: A display of the DataRate value which has been set.
//===========================================================================
void SetDataRate(int portNum)
{
	int status = 0, Value = 0, Ival =0;
	int chOption = 0;
	int rDataRate = 0;
	int mDataRate = 0;
	int sDataRate = 0;
	int valCheck = 0;
	BYTE Record[20];
	char AddA = 0xFC;
	char AddB = 0xA2;
    unsigned short nvalS  = 0;
	char Jjunk[20];

	purge_port(portNum);
	
	status = ReadDataRate(portNum, &Record[0] );
	if(status == 1){ //0
		printf("\n\nCurrent value of the DataRate is set at ");
	    nvalS = convert2ushort(&Record[1]);
	    rDataRate = (1000 / nvalS);
	    printf("%d Hz\n\n", rDataRate);
	} else {
		printf("Error Reading Data-Rate %d\n", status); // %s explainError(status));
		return;
	}
	while(Value == 0)
	{
		printf("Would you like to Modify the DataRate? (Y)es or (N)o \n");
		while(!ReadCharNoReturn(&chOption))
		{
			Sleep(50);
		}
     
		switch (chOption){
			case 'n':
			case 'N': Value = 1; break;

			case 'y':
			case 'Y': while (Ival == 0){
				            mDataRate = 0;
				            printf("Enter a value between 1 and 1000 to set:");
				            scanf("%d", &mDataRate);
							
							if ((mDataRate < 1 )|| (mDataRate > 1000) ){
								printf(" Invalid entry: out of range  \n\n");
				                Ival = 0;
								scanf("%s", Jjunk);
								mDataRate = 0;
								break;
							} else Ival = 1;   	
							
					  }
			          if(Ival == 1){
				          valCheck = WriteDataRate(portNum, mDataRate, &Record[0]);
				          if(valCheck != 1 ){
						       printf("DataRate set Error %d\n", valCheck);
						       break;
					   }
				       if( valCheck == 1) {
					        status = ReadDataRate(portNum, &Record[0] );
	                              if(status == 1){  //0
		                              printf("Current value of DataRate is set at ");
	                              nvalS = convert2ushort(&Record[1]);
	                              rDataRate = (1000 / nvalS);
	                              printf("%dHz\n\n", rDataRate);
						      Ival = 0;
						      break;
	                    } else {
		                      printf("Error Reading Data-Rate %d\n", status); //explainError(status));
		                      return;
	                    }
				    }
					break;
					
			default: printf("Invalid Option\n\n");  Value = 0;
           }
		 }			
		
	}
}
Beispiel #15
0
/*----------------------------------------------------------------------
 * i3dmgx3_getEEPROMValue	0xE5
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              address      : the EEPROM address location
 *              readFlag     : specifies the number of reads required.
 *                             and identifies data type long or float.
 *              value        : the value to get at the address specified
 *
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_getEEPROMValue(int portNum, char address, int readFlag, int *readval) {
	int status = 0;
	long bytesRead = 0;
	unsigned char Bresponse[5]  = {0};
	unsigned char BFresponse[5]  = {0};
	unsigned char ConvertBuff[4] = {0};
	int responseLength = 5;
	unsigned long nvalL=0;
	unsigned short nvalA=0, nvalB=0;
	float nvalF=0.0;
	unsigned char zoutbuff[4];
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;

	zoutbuff[0] = 0xE5;     // EEPROM command identifier
	zoutbuff[1] = 0x00;     // Required identifier
	zoutbuff[2] = 0xFC;     // Required identifier
	zoutbuff[3] = address;  // EEPROM address location

	status = sendBuffData(portNum, &zoutbuff[0], 4);
	if (DEBUG) printf(" eeprom read status is %d\n", status);
    if (status == I3DMGX3_COMM_OK) { 
		status = receiveData(portNum, &Bresponse[0], 5);
        if (status == I3DMGX3_COMM_OK) {
            status = I3DMGX3_OK;
			  wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	          wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
			if(wChecksum != wCalculatedCheckSum)
				return status = I3DMGX3_CHECKSUM_ERROR;
			
            nvalA = convert2ushort(&Bresponse[1]);
			
			if (readFlag > 0) {
				ConvertBuff[3] = Bresponse[2];
				ConvertBuff[2] = Bresponse[1];
				zoutbuff[3] = address + 2;

				status = sendBuffData(portNum, &zoutbuff[0], 4);
				if (status != I3DMGX3_COMM_OK)
				   return status;
		
				status = receiveData(portNum, &BFresponse[0], 5);

				if (status == I3DMGX3_COMM_OK) {
					status = I3DMGX3_OK;
					wChecksum = convert2ushort(&Bresponse[responseLength-2]);
					wCalculatedCheckSum = i3dmgx3_Checksum(&BFresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
					if(wChecksum != wCalculatedCheckSum)
						return status = I3DMGX3_CHECKSUM_ERROR;
					nvalB = convert2ushort(&BFresponse[1]);
					
					ConvertBuff[1] = BFresponse[2];
				    ConvertBuff[0] = BFresponse[1];
					nvalF = FloatFromBytes(&ConvertBuff[0]);
					nvalL = convert2ulong(&ConvertBuff[0]);
					if (readFlag == 1){
						printf("  At addr.:0x%X where value is:   %ld \n", zoutbuff[3], nvalL);
						*readval = nvalL;
					}
					else{
						printf("  At addr.:0x%X where value is: %f \n", zoutbuff[3], nvalF);
						*readval = nvalF;
					}
				}else
					return status = I3DMGX3_COMM_READ_ERROR;
			}else{
					printf("  At addr.:0x%X where value is: %d \t0x%X\n", address, nvalA, nvalA);
					*readval = nvalA;
			}
        }else
            return status = I3DMGX3_COMM_READ_ERROR;
    }
    return status;
}