-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrive.py
49 lines (37 loc) · 1.45 KB
/
drive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
from viam.components.base import Base
from viam.robot.client import RobotClient
from viam.rpc.dial import Credentials, DialOptions
SECRET = '' # make sure to place the secret value inside of the single quotes
REMOTE_ADDRESS = '' # likewise for the remote address
"""
The connect function establishes a network connection between Codespace host and the robot.
"""
async def connect():
creds = Credentials(type='robot-location-secret', payload=SECRET)
opts = RobotClient.Options(refresh_interval=0, dial_options=DialOptions(credentials=creds))
return await RobotClient.at_address(REMOTE_ADDRESS, opts)
"""
The move_in_square function contains the code that moves the robot, executing a sequence
of spin and move_straight commands that drives the robot in a square.
"""
async def move_in_square(base):
for _ in range(4):
# moves the rover forward 500mm at 500mm/s
await base.move_straight(velocity=500, distance=500)
print("move straight")
# spins the rover 90 degrees at 100 degrees per second
await base.spin(velocity=100, angle=90)
print("spin 90 degrees")
"""
The main function is the one called upon script execution. It sets up and calls the
two other functions.
"""
async def main():
robot = await connect()
base = Base.from_robot(robot, 'viam_base')
await move_in_square(base)
# Don't forget to close the robot when you're done!
await robot.close()
if __name__ == '__main__':
asyncio.run(main())