Skip to content

Instantly share code, notes, and snippets.

@maximpertsov
Created May 25, 2023 21:13
Show Gist options
  • Save maximpertsov/acd5d9ddaaa835a000a310341cdfb1de to your computer and use it in GitHub Desktop.
Save maximpertsov/acd5d9ddaaa835a000a310341cdfb1de to your computer and use it in GitHub Desktop.
Test script for viam python sdk sessions
import asyncio
from aioconsole import ainput
from viam.components.base import Base
from viam.logging import DEBUG
from viam.proto.common import Vector3
from viam.robot.client import RobotClient
from viam.rpc.dial import Credentials, DialOptions
URI = "<CHANGE ME>"
SECRET = "<CHANGE ME>"
BASE_NAME = "<CHANGE ME>"
POWER = 0.5
async def connect():
creds = Credentials(type="robot-location-secret", payload=SECRET)
opts = RobotClient.Options(refresh_interval=0, dial_options=DialOptions(credentials=creds), log_level=DEBUG) # , log_level=DEBUG)
return await RobotClient.at_address(URI, opts)
async def main():
robot = await connect()
base = Base.from_robot(robot, BASE_NAME)
while True:
cmd = await ainput("Move in a direction (WASD) or Q to exit\n")
if not cmd:
continue
match cmd.upper():
case "W":
await base.set_power(Vector3(x=0, y=POWER, z=0), Vector3(x=0, y=0, z=0))
print("going forward")
case "A":
await base.set_power(Vector3(x=0, y=0, z=0), Vector3(x=0, y=0, z=POWER))
print("going left")
case "S":
await base.set_power(Vector3(x=0, y=-POWER, z=0), Vector3(x=0, y=0, z=0))
print("going back")
case "D":
await base.set_power(Vector3(x=0, y=0, z=0), Vector3(x=0, y=0, z=-POWER))
print("going right")
case "Q":
break
await asyncio.sleep(1)
await base.stop()
await robot.close()
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment