int CCONV AttachHandler(CPhidgetHandle ENC, void *userptr) { int serialNo; CPhidget_DeviceID deviceID; int i, inputcount; CPhidget_getSerialNumber(ENC, &serialNo); //Retrieve the device ID and number of encoders so that we can set the enables if needed CPhidget_getDeviceID(ENC, &deviceID); CPhidgetEncoder_getEncoderCount((CPhidgetEncoderHandle)ENC, &inputcount); printf("Encoder %10d attached! \n", serialNo); //the 1047 requires enabling of the encoder inputs, so enable them if this is a 1047 if (deviceID == PHIDID_ENCODER_HS_4ENCODER_4INPUT) { printf("Encoder requires Enable. Enabling inputs....\n"); for (i = 0 ; i < inputcount ; i++) CPhidgetEncoder_setEnabled((CPhidgetEncoderHandle)ENC, i, 1); } return 0; }
void encoderAttach(const int which) /** * @brief function that handles registering the an encoder has been attached * @param which indicates which encoder was seen (1 or 2) **/ { if (which == 1) { m_encoder1Seen = true; // this could either be the left encoder board, or it's a single board that // supports both encoders. Check how many inputs it has int count; CPhidgetEncoder_getEncoderCount(m_leftEncoder, &count); if (count > 1) { // we have only one board that supports both motor encoders m_leftEncoderNumber = 0; m_rightEncoderNumber = 1; if(motors_inverted || encoders_inverted){ m_leftEncoderNumber = 1; m_rightEncoderNumber = 0; } m_rightEncoder = m_leftEncoder; m_encoder2Seen = true; m_encodersGood = true; } else { m_leftEncoderNumber = 0; m_rightEncoderNumber = 0; // open the second encoder (assume it's the right one) CPhidgetEncoder_create(&m_rightEncoder); CPhidget_set_OnAttach_Handler((CPhidgetHandle) m_rightEncoder, RightEncoderAttach, NULL); CPhidget_open((CPhidgetHandle) m_rightEncoder, -1); } } else { m_encoder2Seen = true; } // have we seen both of the encoders? if (m_encoder1Seen && m_encoder2Seen) { // now check the assumption we have the encoders correct int leftSerial; int rightSerial; CPhidget_getSerialNumber((CPhidgetHandle) m_leftEncoder, &leftSerial); CPhidget_getSerialNumber((CPhidgetHandle) m_rightEncoder, &rightSerial); if (leftSerial > rightSerial) { // oops, we go them backwards so swap them CPhidgetEncoderHandle tempEncoder = m_leftEncoder; m_leftEncoder = m_rightEncoder; m_rightEncoder = tempEncoder; int tempNumber = m_leftEncoderNumber; m_leftEncoderNumber = m_rightEncoderNumber; m_rightEncoderNumber = tempNumber; } CPhidgetEncoder_setEnabled(m_leftEncoder, m_leftEncoderNumber, PTRUE); CPhidgetEncoder_setEnabled(m_rightEncoder, m_rightEncoderNumber, PTRUE); m_encodersGood = true; } }