-
Notifications
You must be signed in to change notification settings - Fork 10
/
DcMotor.java
87 lines (77 loc) · 2.63 KB
/
DcMotor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/** DcMotor class
* Simulator for FTC's DcMotor class.
* Janky as hell, but gets the job done
* */
public class DcMotor {
enum RunMode {
RUN_TO_POSITION,
RUN_USING_ENCODER,
RUN_WITHOUT_ENCODER,
STOP_AND_RESET_ENCODER
}
enum Direction {
FORWARD,
REVERSE
}
private double power;
private Direction direction;
private RunMode runMode;
public DcMotor() {
this.power = 0;
this.direction = null;
this.runMode = null;
}
/* Sets the power level of the motor, expressed as a fraction of the maximum possible power / speed supported according to the run mode in which the motor is operating. */
void setPower(double input) {
if (runMode == RunMode.RUN_USING_ENCODER) {
if (direction == Direction.FORWARD) {
power = input;
} else {
power = -1 * input;
}
}
}
/* Returns the current configured power level of the motor. */
double getPower() { return power; }
/* Sets the current run mode for this motor */
void setMode(RunMode input){
switch (input) {
case RUN_TO_POSITION:
runMode = RunMode.RUN_TO_POSITION;
break;
case RUN_USING_ENCODER:
runMode = RunMode.RUN_USING_ENCODER;
break;
case RUN_WITHOUT_ENCODER:
runMode = RunMode.RUN_WITHOUT_ENCODER;
break;
case STOP_AND_RESET_ENCODER:
runMode = RunMode.STOP_AND_RESET_ENCODER;
break;
}
}
/* Returns the current run mode for this motor */
RunMode getMode() { return runMode; }
/* Sets the logical direction in which this motor operates. */
void setDirection(Direction input){
switch (input) {
case FORWARD:
direction = Direction.FORWARD;
break;
case REVERSE:
direction = Direction.REVERSE;
break;
}
}
/* Returns the current logical direction in which this motor is set as operating. */
Direction getDirection() { return direction; }
public static void main(String[] args){
// Init enums
final RunMode RUN_TO_POSITION = RunMode.RUN_TO_POSITION;
final RunMode RUN_USING_ENCODER = RunMode.RUN_USING_ENCODER;
final RunMode RUN_WITHOUT_ENCODER = RunMode.RUN_WITHOUT_ENCODER;
final RunMode STOP_AND_RESET_ENCODER = RunMode.STOP_AND_RESET_ENCODER;
final Direction FORWARD = Direction.FORWARD;
final Direction REVERSE = Direction.REVERSE;
}
}