-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from kscalelabs/sim-protos
Protobufs for kos-sim
- Loading branch information
Showing
11 changed files
with
349 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../Cargo.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
"""KOS Python client.""" | ||
|
||
__version__ = "0.3.0" | ||
|
||
from pykos.client import KOS | ||
__version__ = "0.4.1" | ||
|
||
from . import services | ||
from .client import KOS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"""Sim service client.""" | ||
|
||
from typing import NotRequired, TypedDict, Unpack | ||
|
||
import grpc | ||
from google.protobuf.empty_pb2 import Empty | ||
|
||
from kos_protos import common_pb2, sim_pb2, sim_pb2_grpc | ||
|
||
|
||
class DefaultPosition(TypedDict): | ||
qpos: list[float] | ||
|
||
|
||
class ResetRequest(TypedDict): | ||
initial_state: NotRequired[DefaultPosition] | ||
randomize: NotRequired[bool] | ||
|
||
|
||
class StepRequest(TypedDict): | ||
num_steps: int | ||
step_size: NotRequired[float] | ||
|
||
|
||
class SimulationParameters(TypedDict): | ||
time_scale: NotRequired[float] | ||
gravity: NotRequired[float] | ||
initial_state: NotRequired[DefaultPosition] | ||
|
||
|
||
class SimServiceClient: | ||
def __init__(self, channel: grpc.Channel) -> None: | ||
self.stub = sim_pb2_grpc.SimulationServiceStub(channel) | ||
|
||
def reset(self, **kwargs: Unpack[ResetRequest]) -> common_pb2.ActionResponse: | ||
"""Reset the simulation to its initial state. | ||
Args: | ||
**kwargs: Reset parameters that may include: | ||
initial_state: DefaultPosition to reset to | ||
randomize: Whether to randomize the initial state | ||
Example: | ||
>>> client.reset( | ||
... initial_state={"qpos": [0.0, 0.0, 0.0]}, | ||
... randomize=True | ||
... ) | ||
Returns: | ||
ActionResponse indicating success/failure | ||
""" | ||
initial_state = None | ||
if "initial_state" in kwargs: | ||
pos = kwargs["initial_state"] | ||
initial_state = sim_pb2.DefaultPosition(qpos=pos["qpos"]) | ||
|
||
request = sim_pb2.ResetRequest(initial_state=initial_state, randomize=kwargs.get("randomize")) | ||
return self.stub.Reset(request) | ||
|
||
def set_paused(self, paused: bool) -> common_pb2.ActionResponse: | ||
"""Pause or unpause the simulation. | ||
Args: | ||
paused: True to pause, False to unpause | ||
Returns: | ||
ActionResponse indicating success/failure | ||
""" | ||
request = sim_pb2.SetPausedRequest(paused=paused) | ||
return self.stub.SetPaused(request) | ||
|
||
def step(self, num_steps: int, step_size: float | None = None) -> common_pb2.ActionResponse: | ||
"""Step the simulation forward. | ||
Args: | ||
num_steps: Number of simulation steps to take | ||
step_size: Optional time per step in seconds | ||
Returns: | ||
ActionResponse indicating success/failure | ||
""" | ||
request = sim_pb2.StepRequest(num_steps=num_steps, step_size=step_size) | ||
return self.stub.Step(request) | ||
|
||
def set_parameters(self, **kwargs: Unpack[SimulationParameters]) -> common_pb2.ActionResponse: | ||
"""Set simulation parameters. | ||
Example: | ||
>>> client.set_parameters( | ||
... time_scale=1.0, | ||
... gravity=9.81, | ||
... initial_state={"qpos": [0.0, 0.0, 0.0]} | ||
... ) | ||
Args: | ||
**kwargs: Parameters that may include: | ||
time_scale: Simulation time scale | ||
gravity: Gravity constant | ||
initial_state: Default position state | ||
Returns: | ||
ActionResponse indicating success/failure | ||
""" | ||
initial_state = None | ||
if "initial_state" in kwargs: | ||
pos = kwargs["initial_state"] | ||
initial_state = sim_pb2.DefaultPosition(qpos=pos["qpos"]) | ||
|
||
params = sim_pb2.SimulationParameters( | ||
time_scale=kwargs.get("time_scale"), gravity=kwargs.get("gravity"), initial_state=initial_state | ||
) | ||
request = sim_pb2.SetParametersRequest(parameters=params) | ||
return self.stub.SetParameters(request) | ||
|
||
def get_parameters(self) -> sim_pb2.GetParametersResponse: | ||
"""Get current simulation parameters. | ||
Returns: | ||
GetParametersResponse containing current parameters and any error | ||
""" | ||
return self.stub.GetParameters(Empty()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ module = [ | |
"google.*", | ||
"kos.*", | ||
"kos_protos.*", | ||
"version", | ||
] | ||
|
||
ignore_missing_imports = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
syntax = "proto3"; | ||
|
||
package kos.sim; | ||
|
||
import "google/protobuf/empty.proto"; | ||
import "kos/common.proto"; | ||
|
||
option go_package = "kos/sim;sim"; | ||
option java_package = "com.kos.sim"; | ||
option csharp_namespace = "KOS.Sim"; | ||
|
||
// Service to control the simulation environment. | ||
service SimulationService { | ||
// Resets the simulation to its initial state. | ||
rpc Reset(ResetRequest) returns (kos.common.ActionResponse); | ||
|
||
// Pauses/unpauses the simulation. | ||
rpc SetPaused(SetPausedRequest) returns (kos.common.ActionResponse); | ||
|
||
// Steps the simulation forward by a specified amount. | ||
rpc Step(StepRequest) returns (kos.common.ActionResponse); | ||
|
||
// Sets various simulation parameters. | ||
rpc SetParameters(SetParametersRequest) returns (kos.common.ActionResponse); | ||
|
||
// Gets the current simulation parameters. | ||
rpc GetParameters(google.protobuf.Empty) returns (GetParametersResponse); | ||
} | ||
|
||
// Default position for the simulation (initial state) | ||
message DefaultPosition { | ||
repeated float qpos = 1; | ||
} | ||
|
||
// Request to reset the simulation to initial state | ||
message ResetRequest { | ||
// If provided, reset to this specific state, otherwise use default | ||
optional DefaultPosition initial_state = 1; | ||
// If true, randomize the initial state within pre-set bounds | ||
optional bool randomize = 2; | ||
} | ||
|
||
// Request to pause or resume the simulation | ||
message SetPausedRequest { | ||
bool paused = 1; | ||
} | ||
|
||
// Request to step the simulation forward | ||
message StepRequest { | ||
// Number of simulation steps to take | ||
uint32 num_steps = 1; | ||
// Time per step in seconds | ||
optional float step_size = 2; | ||
} | ||
|
||
message SetParametersRequest { | ||
SimulationParameters parameters = 1; | ||
} | ||
|
||
message GetParametersResponse { | ||
SimulationParameters parameters = 1; | ||
kos.common.Error error = 2; // Error details if any | ||
} | ||
|
||
// Controllable parameters for the simulation | ||
message SimulationParameters { | ||
// Time scale for the simulation | ||
optional float time_scale = 1; | ||
// Strength of gravity for the simulation | ||
optional float gravity = 2; | ||
// Initial state for the simulation | ||
optional DefaultPosition initial_state = 3; | ||
} |
Oops, something went wrong.