Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-8714: add Gantry wrappers #63

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.viam.common.v1.Common.ResourceName;
import com.viam.component.board.v1.BoardServiceGrpc;
import com.viam.component.camera.v1.CameraServiceGrpc;
import com.viam.component.gantry.v1.GantryServiceGrpc;
import com.viam.component.generic.v1.GenericServiceGrpc;
import com.viam.component.gripper.v1.GripperServiceGrpc;
import com.viam.component.motor.v1.MotorServiceGrpc;
Expand All @@ -17,6 +18,9 @@
import com.viam.sdk.core.component.camera.Camera;
import com.viam.sdk.core.component.camera.CameraRPCClient;
import com.viam.sdk.core.component.camera.CameraRPCService;
import com.viam.sdk.core.component.gantry.Gantry;
import com.viam.sdk.core.component.gantry.GantryRPCClient;
import com.viam.sdk.core.component.gantry.GantryRPCService;
import com.viam.sdk.core.component.generic.Generic;
import com.viam.sdk.core.component.generic.GenericRPCClient;
import com.viam.sdk.core.component.generic.GenericRPCService;
Expand Down Expand Up @@ -116,6 +120,13 @@ public class ResourceManager implements Closeable {
ServoRPCClient::new
));

Registry.registerSubtype(new ResourceRegistration<>(
Gantry.SUBTYPE,
GantryServiceGrpc.SERVICE_NAME,
GantryRPCService::new,
GantryRPCClient::new
));

// SERVICES
Registry.registerSubtype(new ResourceRegistration<>(
DataManager.SUBTYPE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.viam.sdk.core.component.gantry

import com.google.protobuf.Struct
import com.viam.common.v1.Common.ResourceName
import com.viam.sdk.core.component.Component
import com.viam.sdk.core.resource.Resource
import com.viam.sdk.core.resource.Subtype
import com.viam.sdk.core.robot.RobotClient

abstract class Gantry(name: String) : Component(SUBTYPE, Gantry.named(name)) {
companion object {
@JvmField
val SUBTYPE = Subtype(Subtype.NAMESPACE_RDK, Subtype.RESOURCE_TYPE_COMPONENT, "gantry")

/**
* Get the ResourceName of the component
* @param name the name of the component
* @return the component's ResourceName
*/
@JvmStatic
fun named(name: String): ResourceName {
return Resource.named(SUBTYPE, name)
}

/**
* Get the component with the provided name from the provided robot.
* @param robot the RobotClient
* @param name the name of the component
* @return the component
*/
@JvmStatic
fun fromRobot(robot: RobotClient, name: String): Gantry {
return robot.getResource(Gantry::class.java, named(name))
}
}

/**
* Get the positions of the axes of the gantry in millimeters.
* @return the list of the position of the axes of the gantry in millimeters
*/
abstract fun getPosition(extra: Struct): List<Double>

/**
* Get the positions of the axes of the gantry in millimeters.
* @return the list of the position of the axes of the gantry in millimeters
*/
fun getPosition(): List<Double> {
return getPosition(Struct.getDefaultInstance())
}

/**
* Move the axes of the gantry to the desired positions (mm) at the requested speeds (mm/sec).
* @param positions the list of positions for the axes of the gantry to move to, in millimeters
* @param speeds the list of speeds in millimeters per second for the gantry to move at respective to each axis
*/
abstract fun moveToPosition(positions: List<Double>, speeds: List<Double>, extra: Struct)

/**
* Move the axes of the gantry to the desired positions (mm) at the requested speeds (mm/sec).
* @param positions the list of positions for the axes of the gantry to move to, in millimeters
* @param speeds the list of speeds in millimeters per second for the gantry to move at respective to each axis
*/
fun moveToPosition(positions: List<Double>, speeds: List<Double>) {
return moveToPosition(positions, speeds, Struct.getDefaultInstance())
}

/**
* Run the homing sequence of the gantry to re-calibrate the axes with respect to the limit switches.
* @return whether the gantry has run the homing sequence successfully
*/
abstract fun home(extra: Struct): Boolean

/**
* Run the homing sequence of the gantry to re-calibrate the axes with respect to the limit switches.
* @return whether the gantry has run the homing sequence successfully
*/
fun home(): Boolean {
return home(Struct.getDefaultInstance())
}

/**
* Get the lengths of the axes of the gantry in millimeters.
* @return the list of the lengths of the axes of the gantry in millimeters
*/
abstract fun getLengths(extra: Struct): List<Double>

/**
* Get the lengths of the axes of the gantry in millimeters.
* @return the list of the lengths of the axes of the gantry in millimeters
*/
fun getLengths(): List<Double> {
return getLengths(Struct.getDefaultInstance())
}

/**
*Stop all motion of the gantry. It is assumed that the gantry stops immediately.
*/
abstract fun stop(extra: Struct)

/**
* Stop all motion of the gantry. It is assumed that the gantry stops immediately.
*/
fun stop() {
stop(Struct.getDefaultInstance())
}

/**
* Get if the gantry is currently moving.
* @returns if the gantry is moving
*/
abstract fun isMoving(): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.viam.sdk.core.component.gantry

import com.google.protobuf.Struct
import com.google.protobuf.Value
import com.viam.common.v1.Common
import com.viam.common.v1.Common.GetGeometriesRequest
import com.viam.component.gantry.v1.Gantry.*
import com.viam.component.gantry.v1.GantryServiceGrpc
import com.viam.component.gantry.v1.GantryServiceGrpc.GantryServiceBlockingStub
import com.viam.sdk.core.rpc.Channel
import java.util.*
import kotlin.jvm.optionals.getOrDefault

class GantryRPCClient(name: String, channel: Channel) : Gantry(name) {
private val client: GantryServiceBlockingStub

init {
val client = GantryServiceGrpc.newBlockingStub(channel)
if (channel.callCredentials.isPresent) {
this.client = client.withCallCredentials(channel.callCredentials.get())
} else {
this.client = client
}
}

override fun getPosition(extra: Struct): List<Double> {
val request = GetPositionRequest.newBuilder().setName(this.name.name).setExtra(extra).build()
val response = this.client.getPosition(request)
return response.positionsMmList
}

override fun moveToPosition(positions: List<Double>, speeds: List<Double>, extra: Struct) {
val request =MoveToPositionRequest.newBuilder().setName(this.name.name).setExtra(extra).addAllPositionsMm(positions).addAllSpeedsMmPerSec(speeds).build()
this.client.moveToPosition(request)
}

override fun home(extra: Struct): Boolean {
val request = HomeRequest.newBuilder().setName(this.name.name).setExtra(extra).build()
val response = this.client.home(request)
return response.homed
}

override fun getLengths(extra: Struct): List<Double> {
val request = GetLengthsRequest.newBuilder().setName(this.name.name).setExtra(extra).build()
val response = this.client.getLengths(request)
return response.lengthsMmList
}

override fun stop(extra: Struct) {
val request = StopRequest.newBuilder().setName(this.name.name).setExtra(extra).build()
this.client.stop(request)
}

override fun isMoving(): Boolean {
val request = IsMovingRequest.newBuilder().setName(this.name.name).build()
val response = this.client.isMoving(request)
return response.isMoving
}

override fun doCommand(command: Map<String, Value>?): Struct {
val request = Common.DoCommandRequest.newBuilder().setName(this.name.name)
.setCommand(Struct.newBuilder().putAllFields(command).build()).build()
val response = this.client.doCommand(request)
return response.result
}

override fun getGeometries(extra: Optional<Struct>): List<Common.Geometry> {
val request = GetGeometriesRequest.newBuilder().setName(this.name.name)
.setExtra(extra.getOrDefault(Struct.getDefaultInstance())).build()
val response = this.client.getGeometries(request)
return response.geometriesList
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.viam.sdk.core.component.gantry

import com.viam.common.v1.Common.*
import com.viam.component.gantry.v1.Gantry.*
import com.viam.component.gantry.v1.GantryServiceGrpc
import com.viam.sdk.core.component.motor.Motor
import com.viam.sdk.core.resource.ResourceManager
import com.viam.sdk.core.resource.ResourceRPCService
import io.grpc.stub.StreamObserver
import java.util.*

internal class GantryRPCService(private val manager: ResourceManager) : GantryServiceGrpc.GantryServiceImplBase(),
ResourceRPCService<Gantry> {

override fun getPosition(
request: GetPositionRequest, responseObserver: StreamObserver<GetPositionResponse>
) {
val gantry = getResource(Gantry.named(request.name))
val position = gantry.getPosition(request.extra)
responseObserver.onNext(GetPositionResponse.newBuilder().addAllPositionsMm(position).build())
responseObserver.onCompleted()
}

override fun moveToPosition(
request: MoveToPositionRequest, responseObserver: StreamObserver<MoveToPositionResponse>
) {
val gantry = getResource(Gantry.named(request.name))
gantry.moveToPosition(request.positionsMmList, request.speedsMmPerSecList, request.extra)
responseObserver.onNext(MoveToPositionResponse.newBuilder().build())
responseObserver.onCompleted()
}

override fun home(request: HomeRequest, responseObserver: StreamObserver<HomeResponse>) {
val gantry = getResource(Gantry.named(request.name))
val homed = gantry.home(request.extra)
responseObserver.onNext(HomeResponse.newBuilder().setHomed(homed).build())
responseObserver.onCompleted()
}

override fun getLengths(request: GetLengthsRequest, responseObserver: StreamObserver<GetLengthsResponse>) {
val gantry = getResource(Gantry.named(request.name))
val lengths = gantry.getLengths(request.extra)
responseObserver.onNext(GetLengthsResponse.newBuilder().addAllLengthsMm(lengths).build())
responseObserver.onCompleted()
}

override fun stop(request: StopRequest, responseObserver: StreamObserver<StopResponse>) {
val gantry = getResource(Gantry.named(request.name))
gantry.stop(request.extra)
responseObserver.onNext(StopResponse.newBuilder().build())
responseObserver.onCompleted()
}

override fun isMoving(request: IsMovingRequest, responseObserver: StreamObserver<IsMovingResponse>) {
val gantry = getResource(Gantry.named(request.name))
val bool = gantry.isMoving()
responseObserver.onNext(IsMovingResponse.newBuilder().setIsMoving(bool).build())
responseObserver.onCompleted()
}

override fun doCommand(
request: DoCommandRequest, responseObserver: StreamObserver<DoCommandResponse>
) {
val gantry = getResource(Gantry.named(request.name))
val result = gantry.doCommand(request.command.fieldsMap)
responseObserver.onNext(DoCommandResponse.newBuilder().setResult(result).build())
responseObserver.onCompleted()
}

override fun getGeometries(
request: GetGeometriesRequest, responseObserver: StreamObserver<GetGeometriesResponse>
) {
val motor = getResource(Gantry.named(request.name))
val result = motor.getGeometries(Optional.of(request.extra))
responseObserver.onNext(GetGeometriesResponse.newBuilder().addAllGeometries(result).build())
responseObserver.onCompleted()
}


override fun getResourceClass(): Class<Gantry> {
return Gantry::class.java
}

override fun getManager(): ResourceManager {
return this.manager
}

}
Loading