Skip to content

Commit

Permalink
Motor (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored May 14, 2024
1 parent 4016099 commit 5fb1010
Show file tree
Hide file tree
Showing 10 changed files with 802 additions and 44 deletions.
1 change: 1 addition & 0 deletions core/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation "io.grpc:grpc-testing:1.63.0"
testImplementation "io.grpc:grpc-inprocess:1.63.0"
testImplementation 'org.mockito:mockito-core:5.12.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typealias Tick = StreamTicksResponse
*/
abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
companion object {
@JvmStatic
val SUBTYPE =
Subtype(Subtype.NAMESPACE_RDK, Subtype.RESOURCE_TYPE_COMPONENT, "board")

Expand All @@ -28,6 +29,7 @@ abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
* @param name the name of the component
* @return the component's ResourceName
*/
@JvmStatic
fun named(name: String): ResourceName {
return Resource.named(SUBTYPE, name)
}
Expand All @@ -38,6 +40,7 @@ abstract class Board(name: String) : Component(SUBTYPE, named(name)) {
* @param name the name of the component
* @return the component
*/
@JvmStatic
fun fromRobot(robot: RobotClient, name: String): Board {
return robot.getResource(Board::class.java, named(name))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.viam.sdk.core.component.board
import com.viam.common.v1.Common
import com.viam.common.v1.Common.DoCommandResponse
import com.viam.common.v1.Common.GetGeometriesResponse
import com.viam.component.board.v1.Board.*
import com.viam.component.board.v1.BoardServiceGrpc
import com.viam.sdk.core.resource.ResourceManager
import com.viam.sdk.core.resource.ResourceRPCService
Expand All @@ -12,110 +13,108 @@ import java.util.*
import kotlin.time.DurationUnit
import kotlin.time.toDuration

class BoardRPCService(private val manager: ResourceManager) :
BoardServiceGrpc.BoardServiceImplBase(), ResourceRPCService<Board> {
class BoardRPCService(private val manager: ResourceManager) : BoardServiceGrpc.BoardServiceImplBase(),
ResourceRPCService<Board> {

override fun setGPIO(
request: com.viam.component.board.v1.Board.SetGPIORequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.SetGPIOResponse>
request: SetGPIORequest,
responseObserver: StreamObserver<SetGPIOResponse>
) {
val board = getResource(Board.named(request.name))
board.setGpioState(request.pin, request.high, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.SetGPIOResponse.newBuilder().build()
SetGPIOResponse.newBuilder().build()
)
responseObserver.onCompleted()
}

override fun getGPIO(
request: com.viam.component.board.v1.Board.GetGPIORequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.GetGPIOResponse>
request: GetGPIORequest,
responseObserver: StreamObserver<GetGPIOResponse>
) {
val board = getResource(Board.named(request.name))
val state = board.getGpioState(request.pin, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.GetGPIOResponse.newBuilder().setHigh(state).build()
GetGPIOResponse.newBuilder().setHigh(state).build()
)
responseObserver.onCompleted()
}

override fun setPWM(
request: com.viam.component.board.v1.Board.SetPWMRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.SetPWMResponse>
request: SetPWMRequest,
responseObserver: StreamObserver<SetPWMResponse>
) {
val board = getResource(Board.named(request.name))
board.setPwm(request.pin, request.dutyCyclePct, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.SetPWMResponse.newBuilder().build()
SetPWMResponse.newBuilder().build()
)
responseObserver.onCompleted()
}

override fun pWM(
request: com.viam.component.board.v1.Board.PWMRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.PWMResponse>
request: PWMRequest,
responseObserver: StreamObserver<PWMResponse>
) {
val board = getResource(Board.named(request.name))
val pwm = board.getPwm(request.pin, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.PWMResponse.newBuilder().setDutyCyclePct(pwm).build()
PWMResponse.newBuilder().setDutyCyclePct(pwm).build()
)
responseObserver.onCompleted()
}

override fun setPWMFrequency(
request: com.viam.component.board.v1.Board.SetPWMFrequencyRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.SetPWMFrequencyResponse>
request: SetPWMFrequencyRequest,
responseObserver: StreamObserver<SetPWMFrequencyResponse>
) {
val board = getResource(Board.named(request.name))
board.setPwmFrequency(request.pin, request.frequencyHz.toInt(), Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.SetPWMFrequencyResponse.newBuilder().build()
SetPWMFrequencyResponse.newBuilder().build()
)
responseObserver.onCompleted()
}

override fun pWMFrequency(
request: com.viam.component.board.v1.Board.PWMFrequencyRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.PWMFrequencyResponse>
request: PWMFrequencyRequest,
responseObserver: StreamObserver<PWMFrequencyResponse>
) {
val board = getResource(Board.named(request.name))
val freq = board.getPwmFrequency(request.pin, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.PWMFrequencyResponse.newBuilder()
.setFrequencyHz(freq.toLong()).build()
PWMFrequencyResponse.newBuilder().setFrequencyHz(freq.toLong()).build()
)
responseObserver.onCompleted()
}

override fun writeAnalog(
request: com.viam.component.board.v1.Board.WriteAnalogRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.WriteAnalogResponse>
request: WriteAnalogRequest,
responseObserver: StreamObserver<WriteAnalogResponse>
) {
val board = getResource(Board.named(request.name))
board.writeAnalog(request.pin, request.value, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.WriteAnalogResponse.newBuilder().build()
WriteAnalogResponse.newBuilder().build()
)
responseObserver.onCompleted()
}

override fun readAnalogReader(
request: com.viam.component.board.v1.Board.ReadAnalogReaderRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.ReadAnalogReaderResponse>
request: ReadAnalogReaderRequest,
responseObserver: StreamObserver<ReadAnalogReaderResponse>
) {
val board = getResource(Board.named(request.boardName))
val value = board.getAnalogReaderValue(request.analogReaderName, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.ReadAnalogReaderResponse.newBuilder().setValue(value)
.build()
ReadAnalogReaderResponse.newBuilder().setValue(value).build()
)
responseObserver.onCompleted()
}

override fun streamTicks(
request: com.viam.component.board.v1.Board.StreamTicksRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.StreamTicksResponse>
request: StreamTicksRequest,
responseObserver: StreamObserver<StreamTicksResponse>
) {
val board = getResource(Board.named(request.name))
val ticksStream = board.streamTicks(request.pinNamesList, Optional.of(request.extra))
Expand All @@ -126,8 +125,8 @@ class BoardRPCService(private val manager: ResourceManager) :
}

override fun setPowerMode(
request: com.viam.component.board.v1.Board.SetPowerModeRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.SetPowerModeResponse>
request: SetPowerModeRequest,
responseObserver: StreamObserver<SetPowerModeResponse>
) {
val board = getResource(Board.named(request.name))
board.setPowerMode(
Expand All @@ -136,28 +135,26 @@ class BoardRPCService(private val manager: ResourceManager) :
Optional.of(request.extra)
)
responseObserver.onNext(
com.viam.component.board.v1.Board.SetPowerModeResponse.newBuilder().build()
SetPowerModeResponse.newBuilder().build()
)
responseObserver.onCompleted()
}

override fun getDigitalInterruptValue(
request: com.viam.component.board.v1.Board.GetDigitalInterruptValueRequest,
responseObserver: StreamObserver<com.viam.component.board.v1.Board.GetDigitalInterruptValueResponse>
request: GetDigitalInterruptValueRequest,
responseObserver: StreamObserver<GetDigitalInterruptValueResponse>
) {
val board = getResource(Board.named(request.boardName))
val value =
board.getDigitalInterruptValue(request.digitalInterruptName, Optional.of(request.extra))
val value = board.getDigitalInterruptValue(request.digitalInterruptName, Optional.of(request.extra))
responseObserver.onNext(
com.viam.component.board.v1.Board.GetDigitalInterruptValueResponse.newBuilder()
.setValue(value.toLong()).build()
GetDigitalInterruptValueResponse.newBuilder().setValue(value.toLong())
.build()
)
responseObserver.onCompleted()
}

override fun doCommand(
request: Common.DoCommandRequest,
responseObserver: StreamObserver<DoCommandResponse>
request: Common.DoCommandRequest, responseObserver: StreamObserver<DoCommandResponse>
) {
val board = getResource(Board.named(request.name))
val result = board.doCommand(request.command.fieldsMap)
Expand All @@ -166,8 +163,7 @@ class BoardRPCService(private val manager: ResourceManager) :
}

override fun getGeometries(
request: Common.GetGeometriesRequest,
responseObserver: StreamObserver<GetGeometriesResponse>
request: Common.GetGeometriesRequest, responseObserver: StreamObserver<GetGeometriesResponse>
) {
val board = getResource(Board.named(request.name))
val result = board.getGeometries(Optional.of(request.extra))
Expand Down
Loading

0 comments on commit 5fb1010

Please sign in to comment.