<commit_before>/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package edu.wpi.first.wpilibj;

import edu.wpi.first.wpilibj.hal.DIOJNI;
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.hal.PWMJNI;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import edu.wpi.first.wpilibj.util.AllocationException;

/**
 * Class implements the PWM generation in the FPGA.
 *
 * <p>The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They are mapped to
 * the hardware dependent values, in this case 0-2000 for the FPGA. Changes are immediately sent to
 * the FPGA, and the update occurs at the next FPGA cycle. There is no delay.
 *
 * <p>As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-2000 values as follows: - 2000 =
 * maximum pulse width - 1999 to 1001 = linear scaling from "full forward" to "center" - 1000 =
 * center value - 999 to 2 = linear scaling from "center" to "full reverse" - 1 = minimum pulse
 * width (currently .5ms) - 0 = disabled (i.e. PWM output is held low)
 */
public class PWM extends SensorBase implements LiveWindowSendable {
  /**
   * Represents the amount to multiply the minimum servo-pulse pwm period by.
   */
  public enum PeriodMultiplier {
    /**
     * Period Multiplier: don't skip pulses.
     */
    k1X,
    /**
     * Period Multiplier: skip every other pulse.
     */
    k2X,
    /**
     * Period Multiplier: skip three out of four pulses.
     */
    k4X
  }

  private int m_channel;
  private int m_handle;

  /**
   * Allocate a PWM given a channel.
   *
   * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
   */
  public PWM(final int channel) {
    checkPWMChannel(channel);
    m_channel = channel;

    m_handle = PWMJNI.initializePWMPort(DIOJNI.getPort((byte) channel));

    setDisabled();

    PWMJNI.setPWMEliminateDeadband(m_handle, false);

    HAL.report(tResourceType.kResourceType_PWM, channel);
  }

  /**
   * Free the PWM channel.
   *
   * <p>Free the resource associated with the PWM channel and set the value to 0.
   */
  public void free() {
    if (m_handle == 0) {
      return;
    }
    setDisabled();
    PWMJNI.freePWMPort(m_handle);
    m_handle = 0;
  }

  /**
   * Optionally eliminate the deadband from a speed controller.
   *
   * @param eliminateDeadband If true, set the motor curve on the Jaguar to eliminate the deadband
   *                          in the middle of the range. Otherwise, keep the full range without
   *                          modifying any values.
   */
  public void enableDeadbandElimination(boolean eliminateDeadband) {
    PWMJNI.setPWMEliminateDeadband(m_handle, eliminateDeadband);
  }

  /**
   * Set the bounds on the PWM values. This sets the bounds on the PWM values for a particular each
   * type of controller. The values determine the upper and lower speeds as well as the deadband
   * bracket.
   *
   * @param max         The Minimum pwm value
   * @param deadbandMax The high end of the deadband range
   * @param center      The center speed (off)
   * @param deadbandMin The low end of the deadband range
   * @param min         The minimum pwm value
   * @deprecated Recommended to set bounds in ms using {@link #setBounds(double, double, double,
   * double, double)}
   */
  @Deprecated
  public void setRawBounds(final int max, final int deadbandMax, final int center,
                        final int deadbandMin, final int min) {
    PWMJNI.setPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMax, min);
  }

  /**
   * Set the bounds on the PWM pulse widths. This sets the bounds on the PWM values for a particular
   * type of controller. The values determine the upper and lower speeds as well as the deadband
   * bracket.
   *
   * @param max         The max PWM pulse width in ms
   * @param deadbandMax The high end of the deadband range pulse width in ms
   * @param center      The center (off) pulse width in ms
   * @param deadbandMin The low end of the deadband pulse width in ms
   * @param min         The minimum pulse width in ms
   */
  public void setBounds(double max, double deadbandMax, double center, double deadbandMin,
                           double min) {
    PWMJNI.setPWMConfig(m_handle, max, deadbandMax, center, deadbandMax, min);
  }

  /**
   * Gets the bounds on the PWM pulse widths. This Gets the bounds on the PWM values for a
   * particular type of controller. The values determine the upper and lower speeds as well
   * as the deadband bracket.
   */
  public PWMConfigDataResult getRawBounds() {
    return PWMJNI.getPWMConfigRaw(m_handle);
  }

  /**
   * Gets the channel number associated with the PWM Object.
   *
   * @return The channel number.
   */
  public int getChannel() {
    return m_channel;
  }

  /**
   * Set the PWM value based on a position.
   *
   * <p>This is intended to be used by servos.
   *
   * @param pos The position to set the servo between 0.0 and 1.0.
   * @pre SetMaxPositivePwm() called.
   * @pre SetMinNegativePwm() called.
   */<commit_msg>Fixes incorrect deadband value being passed over PWM (#223)<commit_after>/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package edu.wpi.first.wpilibj;

import edu.wpi.first.wpilibj.hal.DIOJNI;
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.hal.PWMJNI;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import edu.wpi.first.wpilibj.util.AllocationException;

/**
 * Class implements the PWM generation in the FPGA.
 *
 * <p>The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They are mapped to
 * the hardware dependent values, in this case 0-2000 for the FPGA. Changes are immediately sent to
 * the FPGA, and the update occurs at the next FPGA cycle. There is no delay.
 *
 * <p>As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-2000 values as follows: - 2000 =
 * maximum pulse width - 1999 to 1001 = linear scaling from "full forward" to "center" - 1000 =
 * center value - 999 to 2 = linear scaling from "center" to "full reverse" - 1 = minimum pulse
 * width (currently .5ms) - 0 = disabled (i.e. PWM output is held low)
 */
public class PWM extends SensorBase implements LiveWindowSendable {
  /**
   * Represents the amount to multiply the minimum servo-pulse pwm period by.
   */
  public enum PeriodMultiplier {
    /**
     * Period Multiplier: don't skip pulses.
     */
    k1X,
    /**
     * Period Multiplier: skip every other pulse.
     */
    k2X,
    /**
     * Period Multiplier: skip three out of four pulses.
     */
    k4X
  }

  private int m_channel;
  private int m_handle;

  /**
   * Allocate a PWM given a channel.
   *
   * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
   */
  public PWM(final int channel) {
    checkPWMChannel(channel);
    m_channel = channel;

    m_handle = PWMJNI.initializePWMPort(DIOJNI.getPort((byte) channel));

    setDisabled();

    PWMJNI.setPWMEliminateDeadband(m_handle, false);

    HAL.report(tResourceType.kResourceType_PWM, channel);
  }

  /**
   * Free the PWM channel.
   *
   * <p>Free the resource associated with the PWM channel and set the value to 0.
   */
  public void free() {
    if (m_handle == 0) {
      return;
    }
    setDisabled();
    PWMJNI.freePWMPort(m_handle);
    m_handle = 0;
  }

  /**
   * Optionally eliminate the deadband from a speed controller.
   *
   * @param eliminateDeadband If true, set the motor curve on the Jaguar to eliminate the deadband
   *                          in the middle of the range. Otherwise, keep the full range without
   *                          modifying any values.
   */
  public void enableDeadbandElimination(boolean eliminateDeadband) {
    PWMJNI.setPWMEliminateDeadband(m_handle, eliminateDeadband);
  }

  /**
   * Set the bounds on the PWM values. This sets the bounds on the PWM values for a particular each
   * type of controller. The values determine the upper and lower speeds as well as the deadband
   * bracket.
   *
   * @param max         The Minimum pwm value
   * @param deadbandMax The high end of the deadband range
   * @param center      The center speed (off)
   * @param deadbandMin The low end of the deadband range
   * @param min         The minimum pwm value
   * @deprecated Recommended to set bounds in ms using {@link #setBounds(double, double, double,
   * double, double)}
   */
  @Deprecated
  public void setRawBounds(final int max, final int deadbandMax, final int center,
                        final int deadbandMin, final int min) {
    PWMJNI.setPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min);
  }

  /**
   * Set the bounds on the PWM pulse widths. This sets the bounds on the PWM values for a particular
   * type of controller. The values determine the upper and lower speeds as well as the deadband
   * bracket.
   *
   * @param max         The max PWM pulse width in ms
   * @param deadbandMax The high end of the deadband range pulse width in ms
   * @param center      The center (off) pulse width in ms
   * @param deadbandMin The low end of the deadband pulse width in ms
   * @param min         The minimum pulse width in ms
   */
  public void setBounds(double max, double deadbandMax, double center, double deadbandMin,
                           double min) {
    PWMJNI.setPWMConfig(m_handle, max, deadbandMax, center, deadbandMin, min);
  }

  /**
   * Gets the bounds on the PWM pulse widths. This Gets the bounds on the PWM values for a
   * particular type of controller. The values determine the upper and lower speeds as well
   * as the deadband bracket.
   */
  public PWMConfigDataResult getRawBounds() {
    return PWMJNI.getPWMConfigRaw(m_handle);
  }

  /**
   * Gets the channel number associated with the PWM Object.
   *
   * @return The channel number.
   */
  public int getChannel() {
    return m_channel;
  }

  /**
   * Set the PWM value based on a position.
   *
   * <p>This is intended to be used by servos.
   *
   * @param pos The position to set the servo between 0.0 and 1.0.
   * @pre SetMaxPositivePwm() called.
   * @pre SetMinNegativePwm() called.
   */
{'fraction_non_alphanumeric': 0.10714285714285714, 'fraction_numerical': 0.011728395061728396, 'mean_word_length': 2.3985615822595143, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3830284', 'n_tokens_mistral': 3489, 'n_tokens_neox': 3304, 'n_words': 1274}