/** * Free the PWM channel. * * <p>Free the resource associated with the PWM channel and set the value to 0. */ public void free() { if (m_port == 0) return; PWMJNI.setPWM(m_port, (short) 0); PWMJNI.freePWMChannel(m_port); PWMJNI.freeDIO(m_port); DIOJNI.freeDigitalPort(m_port); m_port = 0; }
/** * Initialize PWMs given a channel. * * <p>This method is private and is the common path for all the constructors for creating PWM * instances. Checks channel value ranges and allocates the appropriate channel. The allocation is * only done to help users ensure that they don't double assign channels. $ * * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port */ private void initPWM(final int channel) { checkPWMChannel(channel); m_channel = channel; m_port = DIOJNI.initializeDigitalPort(DIOJNI.getPort((byte) m_channel)); if (!PWMJNI.allocatePWMChannel(m_port)) { throw new AllocationException("PWM channel " + channel + " is already allocated"); } PWMJNI.setPWM(m_port, (short) 0); m_eliminateDeadband = false; UsageReporting.report(tResourceType.kResourceType_PWM, channel); }
/** * Set the PWM value based on a speed. * * <p>This is intended to be used by speed controllers. * * @pre SetMaxPositivePwm() called. * @pre SetMinPositivePwm() called. * @pre SetCenterPwm() called. * @pre SetMaxNegativePwm() called. * @pre SetMinNegativePwm() called. * @param speed The speed to set the speed controller between -1.0 and 1.0. */ final void setSpeed(double speed) { // clamp speed to be in the range 1.0 >= speed >= -1.0 if (speed < -1.0) { speed = -1.0; } else if (speed > 1.0) { speed = 1.0; } // // calculate the desired output pwm value by scaling the speed // appropriately // int rawValue; // if (speed == 0.0) { // rawValue = getCenterPwm(); // } else if (speed > 0.0) { // rawValue = // (int) (speed * ((double) getPositiveScaleFactor()) + ((double) // getMinPositivePwm()) + 0.5); // } else { // rawValue = // (int) (speed * ((double) getNegativeScaleFactor()) + ((double) // getMaxNegativePwm()) + 0.5); // } // // // send the computed pwm value to the FPGA // setRaw(rawValue); // The above is too complicated to simulate, just fake it PWMJNI.__setPWM(m_port, speed); }
/** * Slow down the PWM signal for old devices. * * @param mult The period multiplier to apply to this channel */ public void setPeriodMultiplier(PeriodMultiplier mult) { switch (mult.value) { case PeriodMultiplier.k4X_val: // Squelch 3 out of 4 outputs PWMJNI.setPWMPeriodScale(m_port, 3); break; case PeriodMultiplier.k2X_val: // Squelch 1 out of 2 outputs PWMJNI.setPWMPeriodScale(m_port, 1); break; case PeriodMultiplier.k1X_val: // Don't squelch any outputs PWMJNI.setPWMPeriodScale(m_port, 0); break; default: // Cannot hit this, limited by PeriodMultiplier enum } }
/** * Get the PWM value in terms of speed. * * <p>This is intended to be used by speed controllers. * * @pre SetMaxPositivePwm() called. * @pre SetMinPositivePwm() called. * @pre SetMaxNegativePwm() called. * @pre SetMinNegativePwm() called. * @return The most recently set speed between -1.0 and 1.0. */ public double getSpeed() { // int value = getRaw(); // if (value > getMaxPositivePwm()) { // return 1.0; // } else if (value < getMinNegativePwm()) { // return -1.0; // } else if (value > getMinPositivePwm()) { // return (double) (value - getMinPositivePwm()) / (double) // getPositiveScaleFactor(); // } else if (value < getMaxNegativePwm()) { // return (double) (value - getMaxNegativePwm()) / (double) // getNegativeScaleFactor(); // } else { // return 0.0; // } // The above is too complicated to simulate, just fake it return PWMJNI.__getPWM(m_port); }
protected void setZeroLatch() { PWMJNI.latchPWMZero(m_port); }
/** * Get the PWM value directly from the hardware. * * <p>Read a raw value from a PWM channel. * * @return Raw PWM control value. Range: 0 - 255. */ public int getRaw() { return PWMJNI.getPWM(m_port); }
/** * Set the PWM value directly to the hardware. * * <p>Write a raw value to a PWM channel. * * @param value Raw PWM value. Range 0 - 255. */ public void setRaw(int value) { PWMJNI.setPWM(m_port, (short) value); }