Skip to content

Instantly share code, notes, and snippets.

@robofoundry
Created April 30, 2023 02:24
Show Gist options
  • Save robofoundry/1fc37f9f2e32927c3746154814b63381 to your computer and use it in GitHub Desktop.
Save robofoundry/1fc37f9f2e32927c3746154814b63381 to your computer and use it in GitHub Desktop.
mock_robot_python_code
import asyncio
from viam.robot.client import RobotClient
from viam.rpc.dial import Credentials, DialOptions
from viam.components.arm import Arm
from viam.components.motor import Motor
from viam.components.board import Board
from viam.components.arm import ArmClient, JointPositions
import random
import asyncio
# Gets a random position for each servo on the arm that is within the safe range of motion of the arm. Returns a new array of safe joint positions.
def getRandoms():
return [random.randint(-90, 90),
random.randint(-120, -45),
random.randint(-45, 45),
random.randint(-45, 45),
random.randint(-45, 45)]
# Moves the arm into a new random position every second
async def randomMovement(arm: ArmClient):
while (True):
randomPositions = getRandoms()
newRandomArmJointPositions = JointPositions(values=randomPositions)
await arm.move_to_joint_positions(newRandomArmJointPositions)
print(await arm.get_joint_positions())
await asyncio.sleep(1)
return
async def connect():
creds = Credentials(
type='robot-location-secret',
payload='YOUR_SECRET_HERE')
opts = RobotClient.Options(
refresh_interval=0,
dial_options=DialOptions(credentials=creds)
)
return await RobotClient.at_address('YOUR_ROBOT_ADDR_HERE', opts)
async def main():
robot = await connect()
print('Resources:')
print(robot.resource_names)
# myArm
my_arm = Arm.from_robot(robot, "myArm")
my_arm_return_value = await my_arm.get_end_position()
print(f"myArm get_end_position return value: {my_arm_return_value}")
# myMotor
my_motor = Motor.from_robot(robot, "myMotor")
my_motor_return_value = await my_motor.is_moving()
print(f"myMotor is_moving return value: {my_motor_return_value}")
# Note that the pin supplied is a placeholder. Please change this to a valid pin you are using.
# myPi
my_pi = Board.from_robot(robot, "myPi")
my_pi_return_value = await my_pi.gpio_pin_by_name("16")
print(f"myPi gpio_pin_by_name return value: {my_pi_return_value}")
arm = ArmClient.from_robot(robot=robot, name='myArm')
await randomMovement(arm)
# Don't forget to close the robot when you're done!
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