Skip to content

Commit 5f1820f

Browse files
author
Anthony Vizcarra
committed
Merge branch 'angel/rest-dals' into 'develop'
Implementation of restful asyncio dal See merge request sdk/v4/convenience/raspberry-pi!52
2 parents 939ca15 + 31e43b1 commit 5f1820f

File tree

76 files changed

+1614
-847
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1614
-847
lines changed

getting_started/asyncio/api_and_shell/ping.py renamed to getting_started/asyncio/api_and_shell/echo.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def main():
2828

2929
# give RVR time to wake up
3030
await asyncio.sleep(2)
31-
31+
3232
echo_response = await rvr.echo(
3333
data=[0, 2, 4, 8, 16, 32, 64, 128, 255],
3434
target=SpheroRvrTargets.primary.value
@@ -39,11 +39,18 @@ async def main():
3939

4040

4141
if __name__ == '__main__':
42-
loop.run_until_complete(
43-
main()
44-
)
42+
try:
43+
loop.run_until_complete(
44+
main()
45+
)
46+
47+
except KeyboardInterrupt:
48+
print('Program terminated with keyboard interrupt.')
4549

46-
if loop.is_running():
47-
loop.stop()
50+
finally:
51+
loop.run_until_complete(
52+
rvr.close()
53+
)
4854

49-
loop.close()
55+
if loop.is_running():
56+
loop.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
4+
5+
import asyncio
6+
from sphero_sdk import SpheroRvrAsync
7+
from sphero_sdk import RestfulAsyncDal
8+
from sphero_sdk.common.log_level import LogLevel
9+
10+
loop = asyncio.get_event_loop()
11+
12+
rvr = SpheroRvrAsync(
13+
dal=RestfulAsyncDal(
14+
prefix="RV", # RVR's prefix is RV
15+
domain="10.211.2.21", # Add your raspberry-pi's IP address here
16+
port=2010 # The port opened by the npm server is always 2010
17+
),
18+
log_level=LogLevel.Debug_Verbose
19+
)
20+
21+
22+
async def main():
23+
"""
24+
This program demonstrates how to use the echo command, which sends data to RVR and has RVR returns
25+
the same data. Echo can be used to check to see if RVR is connected and awake. In order to test it,
26+
a node.js server must be running on the raspberry-pi connected to RVR. This code is meant to be
27+
executed from a separate computer.
28+
29+
"""
30+
response = await rvr.echo([1], target=2)
31+
print(response)
32+
33+
await asyncio.sleep(1)
34+
35+
response = await rvr.echo([2], target=2)
36+
print(response)
37+
38+
await asyncio.sleep(1)
39+
40+
41+
if __name__ == '__main__':
42+
try:
43+
loop.run_until_complete(
44+
main()
45+
)
46+
47+
except KeyboardInterrupt:
48+
print('Program terminated with keyboard interrupt.')
49+
50+
finally:
51+
loop.run_until_complete(
52+
rvr.close()
53+
)
54+
55+
if loop.is_running():
56+
loop.close()

getting_started/asyncio/color_sensor/color_detection.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,17 @@ async def main():
3939

4040
if __name__ == '__main__':
4141
try:
42-
asyncio.ensure_future(
42+
loop.run_until_complete(
4343
main()
4444
)
4545

46-
loop.run_forever()
47-
4846
except KeyboardInterrupt:
4947
print('Program terminated with keyboard interrupt.')
5048

49+
finally:
5150
loop.run_until_complete(
5251
rvr.close()
5352
)
5453

55-
finally:
5654
if loop.is_running():
57-
loop.stop()
58-
59-
loop.close()
55+
loop.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
4+
5+
import asyncio
6+
from sphero_sdk import SpheroRvrAsync
7+
from sphero_sdk import RestfulAsyncDal
8+
9+
loop = asyncio.get_event_loop()
10+
11+
rvr = SpheroRvrAsync(
12+
dal=RestfulAsyncDal(
13+
prefix="RV", # RVR's prefix is RV
14+
domain="10.211.2.21", # Add your raspberry-pi's IP address here
15+
port=2010 # The port opened by the npm server is always 2010
16+
)
17+
)
18+
19+
20+
21+
async def on_color_detected(response):
22+
print('Response data for color detected:',response)
23+
24+
25+
async def main():
26+
""" This program uses the color sensor on RVR (located on the down side of RVR, facing the floor) to report colors detected.
27+
To exit program, press <CTRL-C>
28+
29+
"""
30+
# Wake up RVR
31+
await rvr.wake()
32+
33+
# Give RVR time to wake up
34+
await asyncio.sleep(1)
35+
36+
# This enables the color sensor on RVR
37+
await rvr.enable_color_detection(is_enabled=True)
38+
39+
# Register a handler to be called when a color detection notification is received
40+
await rvr.sensor_control.add_sensor_data_handler(on_color_detected)
41+
42+
# Enable the color detection sensor stream
43+
await rvr.sensor_control.enable("ColorDetection")
44+
45+
46+
if __name__ == '__main__':
47+
try:
48+
loop.run_until_complete(
49+
main()
50+
)
51+
52+
except KeyboardInterrupt:
53+
print('Program terminated with keyboard interrupt.')
54+
55+
finally:
56+
loop.run_until_complete(
57+
rvr.close()
58+
)
59+
60+
if loop.is_running():
61+
loop.close()

getting_started/asyncio/driving/drive_raw_motors.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,18 @@ async def main():
8484

8585

8686
if __name__ == '__main__':
87-
loop.run_until_complete(
88-
main()
89-
)
87+
try:
88+
loop.run_until_complete(
89+
main()
90+
)
91+
92+
except KeyboardInterrupt:
93+
print('Program terminated with keyboard interrupt.')
9094

91-
if loop.is_running():
92-
loop.stop()
95+
finally:
96+
loop.run_until_complete(
97+
rvr.close()
98+
)
9399

94-
loop.close()
100+
if loop.is_running():
101+
loop.close()

getting_started/asyncio/driving/drive_with_heading.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,18 @@ async def main():
7979

8080

8181
if __name__ == '__main__':
82-
loop.run_until_complete(
83-
main()
84-
)
82+
try:
83+
loop.run_until_complete(
84+
main()
85+
)
86+
87+
except KeyboardInterrupt:
88+
print('Program terminated with keyboard interrupt.')
8589

86-
if loop.is_running():
87-
loop.stop()
90+
finally:
91+
loop.run_until_complete(
92+
rvr.close()
93+
)
8894

89-
loop.close()
95+
if loop.is_running():
96+
loop.close()

getting_started/asyncio/driving/drive_with_heading_reverse_mode.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,18 @@ async def main():
5252

5353

5454
if __name__ == '__main__':
55-
loop.run_until_complete(
56-
main()
57-
)
55+
try:
56+
loop.run_until_complete(
57+
main()
58+
)
59+
60+
except KeyboardInterrupt:
61+
print('Program terminated with keyboard interrupt.')
5862

59-
if loop.is_running():
60-
loop.stop()
63+
finally:
64+
loop.run_until_complete(
65+
rvr.close()
66+
)
6167

62-
loop.close()
68+
if loop.is_running():
69+
loop.close()

getting_started/asyncio/driving/drive_with_helper.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ async def main():
5959

6060

6161
if __name__ == '__main__':
62-
loop.run_until_complete(
63-
main()
64-
)
62+
try:
63+
loop.run_until_complete(
64+
main()
65+
)
66+
67+
except KeyboardInterrupt:
68+
print('Program terminated with keyboard interrupt.')
6569

66-
if loop.is_running():
67-
loop.stop()
70+
finally:
71+
loop.run_until_complete(
72+
rvr.close()
73+
)
6874

69-
loop.close()
75+
if loop.is_running():
76+
loop.close()

getting_started/asyncio/driving/drive_with_helper_roll.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,18 @@ async def main():
4646

4747

4848
if __name__ == '__main__':
49-
loop.run_until_complete(
50-
main()
51-
)
49+
try:
50+
loop.run_until_complete(
51+
main()
52+
)
53+
54+
except KeyboardInterrupt:
55+
print('Program terminated with keyboard interrupt.')
5256

53-
if loop.is_running():
54-
loop.stop()
57+
finally:
58+
loop.run_until_complete(
59+
rvr.close()
60+
)
5561

56-
loop.close()
62+
if loop.is_running():
63+
loop.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
4+
5+
import asyncio
6+
from sphero_sdk import SpheroRvrAsync
7+
from sphero_sdk import RestfulAsyncDal
8+
9+
loop = asyncio.get_event_loop()
10+
11+
rvr = SpheroRvrAsync(
12+
dal=RestfulAsyncDal(
13+
prefix="RV", # RVR's prefix is RV
14+
domain="10.211.2.21", # Add your raspberry-pi's IP address here
15+
port=2010 # The port opened by the npm server is always 2010
16+
)
17+
)
18+
19+
20+
async def main():
21+
"""
22+
This program has RVR drive using the Rest API. In order to test it, a node.js server must be running on the
23+
raspberry-pi connected to RVR. This code is meant to be executed from a separate computer.
24+
25+
Note:
26+
To give RVR time to drive, we call asyncio.sleep(...); if we did not have these calls, the program would
27+
go on and execute all the statements and exit without the driving ever taking place.
28+
"""
29+
await rvr.wake()
30+
31+
await rvr.reset_yaw()
32+
33+
# drive forward
34+
await rvr.drive_with_heading(speed=128, heading=0, flags=0)
35+
36+
await asyncio.sleep(3)
37+
38+
# drive reverse
39+
await rvr.drive_with_heading(speed=128, heading=0, flags=1)
40+
41+
await asyncio.sleep(3)
42+
43+
44+
if __name__ == '__main__':
45+
try:
46+
loop.run_until_complete(
47+
main()
48+
)
49+
50+
except KeyboardInterrupt:
51+
print('Program terminated with keyboard interrupt.')
52+
53+
finally:
54+
loop.run_until_complete(
55+
rvr.close()
56+
)
57+
58+
if loop.is_running():
59+
loop.close()

getting_started/asyncio/infrared/broadcast_ir.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,18 @@ async def main():
5151

5252

5353
if __name__ == '__main__':
54-
loop.run_until_complete(
55-
main()
56-
)
54+
try:
55+
loop.run_until_complete(
56+
main()
57+
)
5758

58-
if loop.is_running():
59-
loop.stop()
59+
except KeyboardInterrupt:
60+
print('Program terminated with keyboard interrupt.')
61+
62+
finally:
63+
loop.run_until_complete(
64+
rvr.close()
65+
)
6066

61-
loop.close()
67+
if loop.is_running():
68+
loop.close()

0 commit comments

Comments
 (0)