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.system.plant.proto.DCMotorProto;
008import edu.wpi.first.math.system.plant.struct.DCMotorStruct;
009import edu.wpi.first.math.util.Units;
010import edu.wpi.first.util.protobuf.ProtobufSerializable;
011import edu.wpi.first.util.struct.StructSerializable;
012
013/** Holds the constants for a DC motor. */
014public class DCMotor implements ProtobufSerializable, StructSerializable {
015  /** Voltage at which the motor constants were measured. */
016  public final double nominalVoltageVolts;
017
018  /** Torque when stalled. */
019  public final double stallTorqueNewtonMeters;
020
021  /** Current draw when stalled. */
022  public final double stallCurrentAmps;
023
024  /** Current draw under no load. */
025  public final double freeCurrentAmps;
026
027  /** Angular velocity under no load. */
028  public final double freeSpeedRadPerSec;
029
030  /** Motor internal resistance. */
031  public final double rOhms;
032
033  /** Motor velocity constant. */
034  public final double KvRadPerSecPerVolt;
035
036  /** Motor torque constant. */
037  public final double KtNMPerAmp;
038
039  /** DCMotor protobuf for serialization. */
040  public static final DCMotorProto proto = new DCMotorProto();
041
042  /** DCMotor struct for serialization. */
043  public static final DCMotorStruct struct = new DCMotorStruct();
044
045  /**
046   * Constructs a DC motor.
047   *
048   * @param nominalVoltageVolts Voltage at which the motor constants were measured.
049   * @param stallTorqueNewtonMeters Torque when stalled.
050   * @param stallCurrentAmps Current draw when stalled.
051   * @param freeCurrentAmps Current draw under no load.
052   * @param freeSpeedRadPerSec Angular velocity under no load.
053   * @param numMotors Number of motors in a gearbox.
054   */
055  public DCMotor(
056      double nominalVoltageVolts,
057      double stallTorqueNewtonMeters,
058      double stallCurrentAmps,
059      double freeCurrentAmps,
060      double freeSpeedRadPerSec,
061      int numMotors) {
062    this.nominalVoltageVolts = nominalVoltageVolts;
063    this.stallTorqueNewtonMeters = stallTorqueNewtonMeters * numMotors;
064    this.stallCurrentAmps = stallCurrentAmps * numMotors;
065    this.freeCurrentAmps = freeCurrentAmps * numMotors;
066    this.freeSpeedRadPerSec = freeSpeedRadPerSec;
067
068    this.rOhms = nominalVoltageVolts / this.stallCurrentAmps;
069    this.KvRadPerSecPerVolt =
070        freeSpeedRadPerSec / (nominalVoltageVolts - rOhms * this.freeCurrentAmps);
071    this.KtNMPerAmp = this.stallTorqueNewtonMeters / this.stallCurrentAmps;
072  }
073
074  /**
075   * Calculate current drawn by motor with given speed and input voltage.
076   *
077   * @param speedRadiansPerSec The current angular velocity of the motor.
078   * @param voltageInputVolts The voltage being applied to the motor.
079   * @return The estimated current.
080   */
081  public double getCurrent(double speedRadiansPerSec, double voltageInputVolts) {
082    return -1.0 / KvRadPerSecPerVolt / rOhms * speedRadiansPerSec + 1.0 / rOhms * voltageInputVolts;
083  }
084
085  /**
086   * Calculate torque produced by the motor with a given current.
087   *
088   * @param currentAmpere The current drawn by the motor.
089   * @return The torque output.
090   */
091  public double getTorque(double currentAmpere) {
092    return currentAmpere * KtNMPerAmp;
093  }
094
095  /**
096   * Calculate the voltage provided to the motor for a given torque and angular velocity.
097   *
098   * @param torqueNm The torque produced by the motor.
099   * @param speedRadiansPerSec The current angular velocity of the motor.
100   * @return The voltage of the motor.
101   */
102  public double getVoltage(double torqueNm, double speedRadiansPerSec) {
103    return 1.0 / KvRadPerSecPerVolt * speedRadiansPerSec + 1.0 / KtNMPerAmp * rOhms * torqueNm;
104  }
105
106  /**
107   * Calculates the angular speed produced by the motor at a given torque and input voltage.
108   *
109   * @param torqueNm The torque produced by the motor.
110   * @param voltageInputVolts The voltage applied to the motor.
111   * @return The angular speed of the motor.
112   */
113  public double getSpeed(double torqueNm, double voltageInputVolts) {
114    return voltageInputVolts * KvRadPerSecPerVolt
115        - 1.0 / KtNMPerAmp * torqueNm * rOhms * KvRadPerSecPerVolt;
116  }
117
118  /**
119   * Returns a copy of this motor with the given gearbox reduction applied.
120   *
121   * @param gearboxReduction The gearbox reduction.
122   * @return A motor with the gearbox reduction applied.
123   */
124  public DCMotor withReduction(double gearboxReduction) {
125    return new DCMotor(
126        nominalVoltageVolts,
127        stallTorqueNewtonMeters * gearboxReduction,
128        stallCurrentAmps,
129        freeCurrentAmps,
130        freeSpeedRadPerSec / gearboxReduction,
131        1);
132  }
133
134  /**
135   * Return a gearbox of CIM motors.
136   *
137   * @param numMotors Number of motors in the gearbox.
138   * @return A gearbox of CIM motors.
139   */
140  public static DCMotor getCIM(int numMotors) {
141    return new DCMotor(
142        12, 2.42, 133, 2.7, Units.rotationsPerMinuteToRadiansPerSecond(5310), numMotors);
143  }
144
145  /**
146   * Return a gearbox of 775Pro motors.
147   *
148   * @param numMotors Number of motors in the gearbox.
149   * @return A gearbox of 775Pro motors.
150   */
151  public static DCMotor getVex775Pro(int numMotors) {
152    return new DCMotor(
153        12, 0.71, 134, 0.7, Units.rotationsPerMinuteToRadiansPerSecond(18730), numMotors);
154  }
155
156  /**
157   * Return a gearbox of NEO motors.
158   *
159   * @param numMotors Number of motors in the gearbox.
160   * @return A gearbox of NEO motors.
161   */
162  public static DCMotor getNEO(int numMotors) {
163    return new DCMotor(
164        12, 2.6, 105, 1.8, Units.rotationsPerMinuteToRadiansPerSecond(5676), numMotors);
165  }
166
167  /**
168   * Return a gearbox of MiniCIM motors.
169   *
170   * @param numMotors Number of motors in the gearbox.
171   * @return A gearbox of MiniCIM motors.
172   */
173  public static DCMotor getMiniCIM(int numMotors) {
174    return new DCMotor(
175        12, 1.41, 89, 3, Units.rotationsPerMinuteToRadiansPerSecond(5840), numMotors);
176  }
177
178  /**
179   * Return a gearbox of Bag motors.
180   *
181   * @param numMotors Number of motors in the gearbox.
182   * @return A gearbox of Bag motors.
183   */
184  public static DCMotor getBag(int numMotors) {
185    return new DCMotor(
186        12, 0.43, 53, 1.8, Units.rotationsPerMinuteToRadiansPerSecond(13180), numMotors);
187  }
188
189  /**
190   * Return a gearbox of Andymark RS775-125 motors.
191   *
192   * @param numMotors Number of motors in the gearbox.
193   * @return A gearbox of Andymark RS775-125 motors.
194   */
195  public static DCMotor getAndymarkRs775_125(int numMotors) {
196    return new DCMotor(
197        12, 0.28, 18, 1.6, Units.rotationsPerMinuteToRadiansPerSecond(5800.0), numMotors);
198  }
199
200  /**
201   * Return a gearbox of Banebots RS775 motors.
202   *
203   * @param numMotors Number of motors in the gearbox.
204   * @return A gearbox of Banebots RS775 motors.
205   */
206  public static DCMotor getBanebotsRs775(int numMotors) {
207    return new DCMotor(
208        12, 0.72, 97, 2.7, Units.rotationsPerMinuteToRadiansPerSecond(13050.0), numMotors);
209  }
210
211  /**
212   * Return a gearbox of Andymark 9015 motors.
213   *
214   * @param numMotors Number of motors in the gearbox.
215   * @return A gearbox of Andymark 9015 motors.
216   */
217  public static DCMotor getAndymark9015(int numMotors) {
218    return new DCMotor(
219        12, 0.36, 71, 3.7, Units.rotationsPerMinuteToRadiansPerSecond(14270.0), numMotors);
220  }
221
222  /**
223   * Return a gearbox of Banebots RS 550 motors.
224   *
225   * @param numMotors Number of motors in the gearbox.
226   * @return A gearbox of Banebots RS 550 motors.
227   */
228  public static DCMotor getBanebotsRs550(int numMotors) {
229    return new DCMotor(
230        12, 0.38, 84, 0.4, Units.rotationsPerMinuteToRadiansPerSecond(19000.0), numMotors);
231  }
232
233  /**
234   * Return a gearbox of NEO 550 motors.
235   *
236   * @param numMotors Number of motors in the gearbox.
237   * @return A gearbox of NEO 550 motors.
238   */
239  public static DCMotor getNeo550(int numMotors) {
240    return new DCMotor(
241        12, 0.97, 100, 1.4, Units.rotationsPerMinuteToRadiansPerSecond(11000.0), numMotors);
242  }
243
244  /**
245   * Return a gearbox of Falcon 500 motors.
246   *
247   * @param numMotors Number of motors in the gearbox.
248   * @return A gearbox of Falcon 500 motors.
249   */
250  public static DCMotor getFalcon500(int numMotors) {
251    return new DCMotor(
252        12, 4.69, 257, 1.5, Units.rotationsPerMinuteToRadiansPerSecond(6380.0), numMotors);
253  }
254
255  /**
256   * Return a gearbox of Falcon 500 motors with FOC (Field-Oriented Control) enabled.
257   *
258   * @param numMotors Number of motors in the gearbox.
259   * @return A gearbox of Falcon 500 FOC enabled motors.
260   */
261  public static DCMotor getFalcon500Foc(int numMotors) {
262    // https://store.ctr-electronics.com/falcon-500-powered-by-talon-fx/
263    return new DCMotor(
264        12, 5.84, 304, 1.5, Units.rotationsPerMinuteToRadiansPerSecond(6080.0), numMotors);
265  }
266
267  /**
268   * Return a gearbox of Romi/TI_RSLK MAX motors.
269   *
270   * @param numMotors Number of motors in the gearbox.
271   * @return A gearbox of Romi/TI_RSLK MAX motors.
272   */
273  public static DCMotor getRomiBuiltIn(int numMotors) {
274    // From https://www.pololu.com/product/1520/specs
275    return new DCMotor(
276        4.5, 0.1765, 1.25, 0.13, Units.rotationsPerMinuteToRadiansPerSecond(150.0), numMotors);
277  }
278
279  /**
280   * Return a gearbox of Kraken X60 brushless motors.
281   *
282   * @param numMotors Number of motors in the gearbox.
283   * @return a gearbox of Kraken X60 motors.
284   */
285  public static DCMotor getKrakenX60(int numMotors) {
286    // From https://store.ctr-electronics.com/announcing-kraken-x60/
287    return new DCMotor(
288        12, 7.09, 366, 2, Units.rotationsPerMinuteToRadiansPerSecond(6000), numMotors);
289  }
290
291  /**
292   * Return a gearbox of Kraken X60 brushless motors with FOC (Field-Oriented Control) enabled.
293   *
294   * @param numMotors Number of motors in the gearbox.
295   * @return A gearbox of Kraken X60 FOC enabled motors.
296   */
297  public static DCMotor getKrakenX60Foc(int numMotors) {
298    // From https://store.ctr-electronics.com/announcing-kraken-x60/
299    return new DCMotor(
300        12, 9.37, 483, 2, Units.rotationsPerMinuteToRadiansPerSecond(5800), numMotors);
301  }
302
303  /**
304   * Return a gearbox of Neo Vortex brushless motors.
305   *
306   * @param numMotors Number of motors in the gearbox.
307   * @return a gearbox of Neo Vortex motors.
308   */
309  public static DCMotor getNeoVortex(int numMotors) {
310    // From https://www.revrobotics.com/next-generation-spark-neo/
311    return new DCMotor(
312        12, 3.60, 211, 3.6, Units.rotationsPerMinuteToRadiansPerSecond(6784), numMotors);
313  }
314}