Skip to content

Commit 317a576

Browse files
andycateBBScholar
authored andcommitted
Dev/dc motor transmission (#25)
* Add translated transmission class * Add docs and unit tests * Update build.gradle
1 parent 1be86af commit 317a576

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
}
3333

3434
group = 'org.team5499'
35-
version = '2.1.2'
35+
version = '2.2.0'
3636

3737
task sourcesJar(type: Jar) {
3838
from sourceSets.main.allJava
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.team5499.monkeyLib.math.physics
2+
3+
/**
4+
* Model of a DC motor rotating a shaft. All parameters refer to the output (e.g. should already consider gearing
5+
* and efficiency losses). The motor is assumed to be symmetric forward/reverse.
6+
*
7+
* @property speedPerVolt kV, or rad/s per V (no load)
8+
* @property torquePerVolt N•m per V (stall)
9+
* @property frictionVoltage the voltage needed to overcome static
10+
*/
11+
class DCMotorTransmission(
12+
val speedPerVolt: Double,
13+
val torquePerVolt: Double,
14+
val frictionVoltage: Double
15+
) {
16+
// TODO add electrical constants? (e.g. current)
17+
18+
/**
19+
* Returns the idle speed of the motor at this voltage
20+
*
21+
* @param voltage The voltage across the motor
22+
* @return The theoretical speed in rad/s
23+
*/
24+
@Suppress("ReturnCount")
25+
fun freeSpeedAtVoltage(voltage: Double): Double {
26+
if (voltage > 0.0) {
27+
return Math.max(0.0, voltage - frictionVoltage) * speedPerVolt
28+
} else if (0.0 > voltage) {
29+
return Math.min(0.0, voltage + frictionVoltage) * speedPerVolt
30+
} else {
31+
return 0.0
32+
}
33+
}
34+
35+
/**
36+
* Get the theoretical torque applied by the motor at a given speed and voltage
37+
*
38+
* @param outputSpeed The speed of the motor in rad/s
39+
* @param voltage The voltage across the motor
40+
* @return The theoretical torque in N•m
41+
*/
42+
fun getTorqueForVoltage(outputSpeed: Double, voltage: Double): Double {
43+
var effectiveVoltage = voltage
44+
if (outputSpeed > 0.0) {
45+
// Forward motion, rolling friction.
46+
effectiveVoltage -= frictionVoltage
47+
} else if (0.0 > outputSpeed) {
48+
// Reverse motion, rolling friction.
49+
effectiveVoltage += frictionVoltage
50+
} else if (voltage > 0.0) {
51+
// System is static, forward torque.
52+
effectiveVoltage = Math.max(0.0, voltage - frictionVoltage)
53+
} else if (0.0 > voltage) {
54+
// System is static, reverse torque.
55+
effectiveVoltage = Math.min(0.0, voltage + frictionVoltage)
56+
} else {
57+
// System is idle.
58+
return 0.0
59+
}
60+
return torquePerVolt * (-outputSpeed / speedPerVolt + effectiveVoltage)
61+
}
62+
63+
/**
64+
* Get the required voltage for the requested torque at a speed.
65+
*
66+
* @param outputSpeed The output speed of the motor in rad/s
67+
* @param torque The output torque of the motor in N•m
68+
* @return The theoretical voltage for the requested torque and speed
69+
*/
70+
fun getVoltageForTorque(outputSpeed: Double, torque: Double): Double {
71+
var modifiedFrictionVoltage: Double = 0.0
72+
if (outputSpeed > 0.0) {
73+
// Forward motion, rolling friction.
74+
modifiedFrictionVoltage = frictionVoltage
75+
} else if (0.0 > outputSpeed) {
76+
// Reverse motion, rolling friction.
77+
modifiedFrictionVoltage = -frictionVoltage
78+
} else if (torque > 0.0) {
79+
// System is static, forward torque.
80+
modifiedFrictionVoltage = frictionVoltage
81+
} else if (0.0 > torque) {
82+
// System is static, reverse torque.
83+
modifiedFrictionVoltage = -frictionVoltage
84+
} else {
85+
// System is idle.
86+
return 0.0
87+
}
88+
return torque / torquePerVolt + outputSpeed / speedPerVolt + modifiedFrictionVoltage
89+
}
90+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tests.math.physics
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
import org.team5499.monkeyLib.math.physics.DCMotorTransmission
7+
8+
class DCMotorTransmissionTest {
9+
val motor = DCMotorTransmission(1000.0, 0.5, 1.0)
10+
val stallTorque = 5.5 // N•m
11+
val freeSpeed = 11000.0 // rad/s
12+
val epsilon = 0.1
13+
14+
@Test
15+
fun stallTorqueTest() {
16+
val calculatedStallTorque = motor.getTorqueForVoltage(0.0, 12.0)
17+
println("Expected $stallTorque, but got $calculatedStallTorque.")
18+
assertEquals(stallTorque, calculatedStallTorque, epsilon)
19+
}
20+
21+
@Test
22+
fun freeSpeedTest() {
23+
val calculatedFreeSpeed = motor.freeSpeedAtVoltage(12.0)
24+
println("Expected $freeSpeed, but got $calculatedFreeSpeed.")
25+
assertEquals(freeSpeed, calculatedFreeSpeed, epsilon)
26+
}
27+
28+
@Test
29+
fun constantsTest() {
30+
assertEquals(1000.0, motor.speedPerVolt, 0.0)
31+
assertEquals(0.5, motor.torquePerVolt, 0.0)
32+
assertEquals(1.0, motor.frictionVoltage, 0.0)
33+
}
34+
}

0 commit comments

Comments
 (0)