@@ -16,20 +16,20 @@ import ev3dev4s.scala2measure.MilliSeconds
16
16
* @author David Walend
17
17
* @since v0.0.0
18
18
*/
19
- sealed abstract class Motor (port : MotorPort ,motorFS: Option [MotorFS ]) extends Gadget (port,motorFS){
19
+ sealed abstract class Motor (port : MotorPort , motorFS : Option [MotorFS ]) extends Gadget (port, motorFS) {
20
20
21
- def writeCommand (command : MotorCommand ): Unit = checkPort(_.writeCommand(command))
21
+ def writeCommand (command : MotorCommand ): Unit = checkPort(_.writeCommand(command))
22
22
23
- def writeStopAction (command: MotorStopCommand ): Unit = checkPort(_.writeStopAction(command))
23
+ def writeStopAction (command : MotorStopCommand ): Unit = checkPort(_.writeStopAction(command))
24
24
25
- def writeDutyCycle (dutyCycle: DutyCycle ): Unit = checkPort(_.writeDutyCycle(dutyCycle))
25
+ def writeDutyCycle (dutyCycle : DutyCycle ): Unit = checkPort(_.writeDutyCycle(dutyCycle))
26
26
27
- def maxSpeed : DegreesPerSecond
27
+ def maxSpeed : DegreesPerSecond
28
28
29
- def observedMaxSpeed : DegreesPerSecond
29
+ def observedMaxSpeed : DegreesPerSecond
30
30
31
- def writeSpeed (speed: DegreesPerSecond ): Unit = {
32
- val safeSpeed = if (speed.abs < maxSpeed ) speed
31
+ def writeSpeed (speed : DegreesPerSecond ): Unit = {
32
+ val safeSpeed = if (speed.abs < maxSpeed) speed
33
33
else {
34
34
Log .log(s " requested speed $speed is greater than $maxSpeed - using $maxSpeed" )
35
35
(speed.sign * maxSpeed).degreesPerSecond
@@ -45,25 +45,25 @@ sealed abstract class Motor(port: MotorPort,motorFS:Option[MotorFS]) extends Gad
45
45
checkPort(_.writeRampDownSpeed(fromMaxToZero))
46
46
}
47
47
48
- def writePosition (degrees: Degrees ): Unit = checkPort(_.writePosition(degrees))
48
+ def writePosition (degrees : Degrees ): Unit = checkPort(_.writePosition(degrees))
49
49
50
- def resetPosition (): Unit = writePosition(0 .degrees)
50
+ def resetPosition (): Unit = writePosition(0 .degrees)
51
51
52
- def writeGoalPosition (degrees: Degrees ): Unit = checkPort(_.writeGoalPosition(degrees))
52
+ def writeGoalPosition (degrees : Degrees ): Unit = checkPort(_.writeGoalPosition(degrees))
53
53
54
- def writeDuration (milliseconds: MilliSeconds ): Unit = checkPort(_.writeDuration(milliseconds))
54
+ def writeDuration (milliseconds : MilliSeconds ): Unit = checkPort(_.writeDuration(milliseconds))
55
55
56
56
/**
57
57
* @return position in degrees
58
58
*/
59
- def readPosition (): Degrees = checkPort(_.readPosition())
59
+ def readPosition (): Degrees = checkPort(_.readPosition())
60
60
61
61
def readState (): Array [MotorState ] = checkPort(_.readState())
62
62
63
- def readIsStalled (): Boolean =
63
+ def readIsStalled (): Boolean =
64
64
readState().contains(MotorState .STALLED )
65
65
66
- def readIsRunning (): Boolean =
66
+ def readIsRunning (): Boolean =
67
67
readState().contains(MotorState .RUNNING )
68
68
69
69
def coast (): Unit = {
@@ -75,40 +75,42 @@ sealed abstract class Motor(port: MotorPort,motorFS:Option[MotorFS]) extends Gad
75
75
writeStopAction(MotorStopCommand .BRAKE )
76
76
writeCommand(MotorCommand .STOP )
77
77
}
78
+
78
79
def hold (): Unit = {
79
80
writeStopAction(MotorStopCommand .HOLD )
80
81
writeCommand(MotorCommand .STOP )
81
82
}
82
- def runDutyCycle (dutyCycle: DutyCycle ): Unit = {
83
+
84
+ def runDutyCycle (dutyCycle : DutyCycle ): Unit = {
83
85
writeDutyCycle(dutyCycle)
84
86
writeCommand(MotorCommand .RUN_DIRECT )
85
87
}
86
88
87
- def run (speed: DegreesPerSecond ): Unit = {
89
+ def run (speed : DegreesPerSecond ): Unit = {
88
90
writeSpeed(speed)
89
91
writeCommand(MotorCommand .RUN )
90
92
}
91
93
92
- def runToAbsolutePosition (speed: DegreesPerSecond ,degrees: Degrees ): Unit = {
94
+ def runToAbsolutePosition (speed : DegreesPerSecond , degrees : Degrees ): Unit = {
93
95
writeSpeed(speed)
94
96
writeGoalPosition(degrees)
95
97
writeCommand(MotorCommand .RUN_TO_ABSOLUTE_POSITION )
96
98
}
97
99
98
- def runToRelativePosition (speed: DegreesPerSecond ,degrees: Degrees ): Unit = {
100
+ def runToRelativePosition (speed : DegreesPerSecond , degrees : Degrees ): Unit = {
99
101
writeSpeed(speed)
100
102
writeGoalPosition(degrees)
101
103
writeCommand(MotorCommand .RUN_TO_RELATIVE_POSITION )
102
104
}
103
105
104
- def runForDuration (speed: DegreesPerSecond ,milliseconds: MilliSeconds ): Unit = {
106
+ def runForDuration (speed : DegreesPerSecond , milliseconds : MilliSeconds ): Unit = {
105
107
writeSpeed(speed)
106
108
writeDuration(milliseconds)
107
109
writeCommand(MotorCommand .RUN_TIME )
108
110
}
109
111
}
110
112
111
- sealed case class Ev3LargeMotor (override val port : MotorPort , md : Option [MotorFS ]) extends Motor (port,md) {
113
+ sealed case class Ev3LargeMotor (override val port : MotorPort , md : Option [MotorFS ]) extends Motor (port, md) {
112
114
override def findGadgetFS (): Option [MotorFS ] =
113
115
MotorPortScanner .findGadgetDir(port, Ev3LargeMotor .driverName)
114
116
.map(MotorFS )
@@ -121,7 +123,7 @@ object Ev3LargeMotor {
121
123
val driverName = " lego-ev3-l-motor"
122
124
}
123
125
124
- sealed case class Ev3MediumMotor (override val port : MotorPort , md : Option [MotorFS ]) extends Motor (port,md) {
126
+ sealed case class Ev3MediumMotor (override val port : MotorPort , md : Option [MotorFS ]) extends Motor (port, md) {
125
127
override def findGadgetFS (): Option [MotorFS ] =
126
128
MotorPortScanner .findGadgetDir(port, Ev3MediumMotor .driverName)
127
129
.map(MotorFS )
@@ -134,7 +136,7 @@ object Ev3MediumMotor {
134
136
val driverName = " lego-ev3-m-motor"
135
137
}
136
138
137
- sealed case class MotorCommand (command: String )
139
+ sealed case class MotorCommand (command : String )
138
140
139
141
object MotorCommand {
140
142
/**
@@ -148,11 +150,11 @@ object MotorCommand {
148
150
val RUN_TO_ABSOLUTE_POSITION : MotorCommand = MotorCommand (" run-to-abs-pos" )
149
151
150
152
/**
151
- run-to-rel-pos: Runs the motor to a position relative to the current position v. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the command specified by stop_action. */
153
+ * run-to-rel-pos: Runs the motor to a position relative to the current position v. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the command specified by stop_action. */
152
154
val RUN_TO_RELATIVE_POSITION : MotorCommand = MotorCommand (" run-to-rel-pos" )
153
155
154
156
/**
155
- run-timed: Run the motor for the amount of time specified in time_sp and then stops the motor using the command specified by stop_action.
157
+ * run-timed: Run the motor for the amount of time specified in time_sp and then stops the motor using the command specified by stop_action.
156
158
*/
157
159
val RUN_TIME : MotorCommand = MotorCommand (" run-timed" )
158
160
@@ -166,15 +168,16 @@ object MotorCommand {
166
168
val STOP : MotorCommand = MotorCommand (" stop" )
167
169
168
170
/**
169
- reset: Resets all of the motor parameter attributes to their default values. This will also have the effect of stopping the motor.
171
+ * reset: Resets all of the motor parameter attributes to their default values. This will also have the effect of stopping the motor.
170
172
*/
171
173
}
174
+
172
175
/**
173
176
* Determines the motors behavior when command is set to stop. Possible values are:
174
177
*/
175
- sealed case class MotorStopCommand (command: String )
178
+ sealed case class MotorStopCommand (command : String )
176
179
177
- object MotorStopCommand {
180
+ object MotorStopCommand {
178
181
/**
179
182
* Removes power from the motor. The motor will freely coast to a stop.
180
183
*/
@@ -191,29 +194,29 @@ object MotorStopCommand{
191
194
val HOLD : MotorStopCommand = MotorStopCommand (" hold" )
192
195
}
193
196
194
- sealed case class MotorState (name: String )
197
+ sealed case class MotorState (name : String )
195
198
196
- object MotorState {
199
+ object MotorState {
197
200
/**
198
- running: Power is being sent to the motor.
201
+ * running: Power is being sent to the motor.
199
202
*/
200
203
val RUNNING : MotorState = MotorState (" running" )
201
204
/**
202
- ramping: The motor is ramping up or down and has not yet reached a constant output level.
205
+ * ramping: The motor is ramping up or down and has not yet reached a constant output level.
203
206
*/
204
207
val RAMPING : MotorState = MotorState (" ramping" )
205
208
/**
206
- holding: The motor is not turning, but rather attempting to hold a fixed position.
209
+ * holding: The motor is not turning, but rather attempting to hold a fixed position.
207
210
*/
208
211
val HOLDING : MotorState = MotorState (" holding" )
209
212
/**
210
- overloaded: The motor is turning as fast as possible, but cannot reach its speed_sp.
213
+ * overloaded: The motor is turning as fast as possible, but cannot reach its speed_sp.
211
214
*/
212
215
val OVERLOADED : MotorState = MotorState (" overloaded" )
213
216
/**
214
- stalled: The motor is trying to run but is not turning at all.
217
+ * stalled: The motor is trying to run but is not turning at all.
215
218
*/
216
219
val STALLED : MotorState = MotorState (" stalled" )
217
220
218
- val values : Array [MotorState ] = Array (RUNNING ,RAMPING ,HOLDING ,OVERLOADED ,STALLED )
221
+ val values : Array [MotorState ] = Array (RUNNING , RAMPING , HOLDING , OVERLOADED , STALLED )
219
222
}
0 commit comments