001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.math.system.plant;
006
007import edu.wpi.first.math.util.Units;
008
009/** Holds the constants for a DC motor. */
010public class DCMotor {
011  public final double nominalVoltageVolts;
012  public final double stallTorqueNewtonMeters;
013  public final double stallCurrentAmps;
014  public final double freeCurrentAmps;
015  public final double freeSpeedRadPerSec;
016  public final double rOhms;
017  public final double KvRadPerSecPerVolt;
018  public final double KtNMPerAmp;
019
020  /**
021   * Constructs a DC motor.
022   *
023   * @param nominalVoltageVolts Voltage at which the motor constants were measured.
024   * @param stallTorqueNewtonMeters Current draw when stalled.
025   * @param stallCurrentAmps Current draw when stalled.
026   * @param freeCurrentAmps Current draw under no load.
027   * @param freeSpeedRadPerSec Angular velocity under no load.
028   * @param numMotors Number of motors in a gearbox.
029   */
030  public DCMotor(
031      double nominalVoltageVolts,
032      double stallTorqueNewtonMeters,
033      double stallCurrentAmps,
034      double freeCurrentAmps,
035      double freeSpeedRadPerSec,
036      int numMotors) {
037    this.nominalVoltageVolts = nominalVoltageVolts;
038    this.stallTorqueNewtonMeters = stallTorqueNewtonMeters * numMotors;
039    this.stallCurrentAmps = stallCurrentAmps * numMotors;
040    this.freeCurrentAmps = freeCurrentAmps * numMotors;
041    this.freeSpeedRadPerSec = freeSpeedRadPerSec;
042
043    this.rOhms = nominalVoltageVolts / this.stallCurrentAmps;
044    this.KvRadPerSecPerVolt =
045        freeSpeedRadPerSec / (nominalVoltageVolts - rOhms * this.freeCurrentAmps);
046    this.KtNMPerAmp = this.stallTorqueNewtonMeters / this.stallCurrentAmps;
047  }
048
049  /**
050   * Estimate the current being drawn by this motor.
051   *
052   * @param speedRadiansPerSec The speed of the motor.
053   * @param voltageInputVolts The input voltage.
054   * @return The estimated current.
055   */
056  public double getCurrent(double speedRadiansPerSec, double voltageInputVolts) {
057    return -1.0 / KvRadPerSecPerVolt / rOhms * speedRadiansPerSec + 1.0 / rOhms * voltageInputVolts;
058  }
059
060  /**
061   * Calculate the torque produced by the motor for a given current.
062   *
063   * @param currentAmpere The current drawn by the motor.
064   * @return The torque produced.
065   */
066  public double getTorque(double currentAmpere) {
067    return currentAmpere * KtNMPerAmp;
068  }
069
070  /**
071   * Calculate the voltage provided to the motor at a given torque and angular velocity.
072   *
073   * @param torqueNm The torque produced by the motor.
074   * @param speedRadiansPerSec The speed of the motor.
075   * @return The voltage of the motor.
076   */
077  public double getVoltage(double torqueNm, double speedRadiansPerSec) {
078    return 1.0 / KvRadPerSecPerVolt * speedRadiansPerSec + 1.0 / KtNMPerAmp * rOhms * torqueNm;
079  }
080
081  /**
082   * Calculate the speed of the motor at a given torque and input voltage.
083   *
084   * @param torqueNm The torque produced by the motor.
085   * @param voltageInputVolts The voltage applied to the motor.
086   * @return The speed of the motor.
087   */
088  public double getSpeed(double torqueNm, double voltageInputVolts) {
089    return voltageInputVolts - 1.0 / KtNMPerAmp * torqueNm * rOhms * KvRadPerSecPerVolt;
090  }
091
092  /**
093   * Returns a copy of this motor with the given gearbox reduction applied.
094   *
095   * @param gearboxReduction The gearbox reduction.
096   * @return A motor with the gearbox reduction applied.
097   */
098  public DCMotor withReduction(double gearboxReduction) {
099    return new DCMotor(
100        nominalVoltageVolts,
101        stallTorqueNewtonMeters * gearboxReduction,
102        stallCurrentAmps,
103        freeCurrentAmps,
104        freeSpeedRadPerSec / gearboxReduction,
105        1);
106  }
107
108  /**
109   * Return a gearbox of CIM motors.
110   *
111   * @param numMotors Number of motors in the gearbox.
112   * @return A gearbox of CIM motors.
113   */
114  public static DCMotor getCIM(int numMotors) {
115    return new DCMotor(
116        12, 2.42, 133, 2.7, Units.rotationsPerMinuteToRadiansPerSecond(5310), numMotors);
117  }
118
119  /**
120   * Return a gearbox of 775Pro motors.
121   *
122   * @param numMotors Number of motors in the gearbox.
123   * @return A gearbox of 775Pro motors.
124   */
125  public static DCMotor getVex775Pro(int numMotors) {
126    return new DCMotor(
127        12, 0.71, 134, 0.7, Units.rotationsPerMinuteToRadiansPerSecond(18730), numMotors);
128  }
129
130  /**
131   * Return a gearbox of NEO motors.
132   *
133   * @param numMotors Number of motors in the gearbox.
134   * @return A gearbox of NEO motors.
135   */
136  public static DCMotor getNEO(int numMotors) {
137    return new DCMotor(
138        12, 2.6, 105, 1.8, Units.rotationsPerMinuteToRadiansPerSecond(5676), numMotors);
139  }
140
141  /**
142   * Return a gearbox of MiniCIM motors.
143   *
144   * @param numMotors Number of motors in the gearbox.
145   * @return A gearbox of MiniCIM motors.
146   */
147  public static DCMotor getMiniCIM(int numMotors) {
148    return new DCMotor(
149        12, 1.41, 89, 3, Units.rotationsPerMinuteToRadiansPerSecond(5840), numMotors);
150  }
151
152  /**
153   * Return a gearbox of Bag motors.
154   *
155   * @param numMotors Number of motors in the gearbox.
156   * @return A gearbox of Bag motors.
157   */
158  public static DCMotor getBag(int numMotors) {
159    return new DCMotor(
160        12, 0.43, 53, 1.8, Units.rotationsPerMinuteToRadiansPerSecond(13180), numMotors);
161  }
162
163  /**
164   * Return a gearbox of Andymark RS775-125 motors.
165   *
166   * @param numMotors Number of motors in the gearbox.
167   * @return A gearbox of Andymark RS775-125 motors.
168   */
169  public static DCMotor getAndymarkRs775_125(int numMotors) {
170    return new DCMotor(
171        12, 0.28, 18, 1.6, Units.rotationsPerMinuteToRadiansPerSecond(5800.0), numMotors);
172  }
173
174  /**
175   * Return a gearbox of Banebots RS775 motors.
176   *
177   * @param numMotors Number of motors in the gearbox.
178   * @return A gearbox of Banebots RS775 motors.
179   */
180  public static DCMotor getBanebotsRs775(int numMotors) {
181    return new DCMotor(
182        12, 0.72, 97, 2.7, Units.rotationsPerMinuteToRadiansPerSecond(13050.0), numMotors);
183  }
184
185  /**
186   * Return a gearbox of Andymark 9015 motors.
187   *
188   * @param numMotors Number of motors in the gearbox.
189   * @return A gearbox of Andymark 9015 motors.
190   */
191  public static DCMotor getAndymark9015(int numMotors) {
192    return new DCMotor(
193        12, 0.36, 71, 3.7, Units.rotationsPerMinuteToRadiansPerSecond(14270.0), numMotors);
194  }
195
196  /**
197   * Return a gearbox of Banebots RS 550 motors.
198   *
199   * @param numMotors Number of motors in the gearbox.
200   * @return A gearbox of Banebots RS 550 motors.
201   */
202  public static DCMotor getBanebotsRs550(int numMotors) {
203    return new DCMotor(
204        12, 0.38, 84, 0.4, Units.rotationsPerMinuteToRadiansPerSecond(19000.0), numMotors);
205  }
206
207  /**
208   * Return a gearbox of NEO 550 motors.
209   *
210   * @param numMotors Number of motors in the gearbox.
211   * @return A gearbox of NEO 550 motors.
212   */
213  public static DCMotor getNeo550(int numMotors) {
214    return new DCMotor(
215        12, 0.97, 100, 1.4, Units.rotationsPerMinuteToRadiansPerSecond(11000.0), numMotors);
216  }
217
218  /**
219   * Return a gearbox of Falcon 500 motors.
220   *
221   * @param numMotors Number of motors in the gearbox.
222   * @return A gearbox of Falcon 500 motors.
223   */
224  public static DCMotor getFalcon500(int numMotors) {
225    return new DCMotor(
226        12, 4.69, 257, 1.5, Units.rotationsPerMinuteToRadiansPerSecond(6380.0), numMotors);
227  }
228
229  /**
230   * Return a gearbox of Romi/TI_RSLK MAX motors.
231   *
232   * @param numMotors Number of motors in the gearbox.
233   * @return A gearbox of Romi/TI_RSLK MAX motors.
234   */
235  public static DCMotor getRomiBuiltIn(int numMotors) {
236    // From https://www.pololu.com/product/1520/specs
237    return new DCMotor(
238        4.5, 0.1765, 1.25, 0.13, Units.rotationsPerMinuteToRadiansPerSecond(150.0), numMotors);
239  }
240}