/** * Set the PWM value based on a position. * * This is intended to be used by servos. * * @pre SetMaxPositivePwm() called. * @pre SetMinNegativePwm() called. * * @param pos The position to set the servo between 0.0 and 1.0. */ void PWM::SetPosition(float pos) { if (pos < 0.0) { pos = 0.0; } else if (pos > 1.0) { pos = 1.0; } INT32 rawValue; // note, need to perform the multiplication below as floating point // before converting to int rawValue = (INT32) ((pos * (float)GetFullRangeScaleFactor()) + GetMinNegativePwm()); wpi_assert((rawValue >= GetMinNegativePwm()) && (rawValue <= GetMaxPositivePwm())); wpi_assert(rawValue != kPwmDisabled); // send the computed pwm value to the FPGA SetRaw((UINT8) rawValue); }
/** * Get the PWM value in terms of a position. * * This is intended to be used by servos. * * @pre SetMaxPositivePwm() called. * @pre SetMinNegativePwm() called. * * @return The position the servo is set to between 0.0 and 1.0. */ float PWM::GetPosition() const { if (StatusIsFatal()) return 0.0; int32_t value = GetRaw(); if (value < GetMinNegativePwm()) { return 0.0; } else if (value > GetMaxPositivePwm()) { return 1.0; } else { return (float)(value - GetMinNegativePwm()) / (float)GetFullRangeScaleFactor(); } }
/** * Get the PWM value in terms of a position. * * This is intended to be used by servos. * * @pre SetMaxPositivePwm() called. * @pre SetMinNegativePwm() called. * * @return The position the servo is set to between 0.0 and 1.0. */ float PWM::GetPosition() { INT32 value = GetRaw(); if (value < GetMinNegativePwm()) { return 0.0; } else if (value > GetMaxPositivePwm()) { return 1.0; } else { return (float)(value - GetMinNegativePwm()) / (float)GetFullRangeScaleFactor(); } }
/** * Set the PWM value based on a position. * * This is intended to be used by servos. * * @pre SetMaxPositivePwm() called. * @pre SetMinNegativePwm() called. * * @param pos The position to set the servo between 0.0 and 1.0. */ void PWM::SetPosition(float pos) { if (StatusIsFatal()) return; if (pos < 0.0) { pos = 0.0; } else if (pos > 1.0) { pos = 1.0; } // note, need to perform the multiplication below as floating point before // converting to int unsigned short rawValue = (int32_t)((pos * (float)GetFullRangeScaleFactor()) + GetMinNegativePwm()); // printf("MinNegPWM: %d FullRangeScaleFactor: %d Raw value: %5d Input //value: %4.4f\n", GetMinNegativePwm(), GetFullRangeScaleFactor(), rawValue, //pos); // wpi_assert((rawValue >= GetMinNegativePwm()) && (rawValue <= //GetMaxPositivePwm())); wpi_assert(rawValue != kPwmDisabled); // send the computed pwm value to the FPGA SetRaw((unsigned short)rawValue); }