Skip to content

Commit 734d33c

Browse files
author
Anthony Vizcarra
committed
Merge branch 'angel/streaming-control-refactor' into 'develop'
Streaming control refactor to stay consistent with node.js interface See merge request sdk/v4/convenience/raspberry-pi!53
2 parents 5f1820f + 0562b4a commit 734d33c

File tree

82 files changed

+1367
-1137
lines changed

Some content is hidden

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

82 files changed

+1367
-1137
lines changed

getting_started/asyncio/api_and_shell/echo.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import asyncio
21
import os
32
import sys
4-
5-
63
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
74

5+
import asyncio
86
from sphero_sdk import SpheroRvrAsync
97
from sphero_sdk import SerialAsyncDal
108
from sphero_sdk import SpheroRvrTargets
@@ -26,14 +24,20 @@ async def main():
2624

2725
await rvr.wake()
2826

29-
# give RVR time to wake up
27+
# Give RVR time to wake up
3028
await asyncio.sleep(2)
3129

3230
echo_response = await rvr.echo(
33-
data=[0, 2, 4, 8, 16, 32, 64, 128, 255],
31+
data=[0, 1, 2],
3432
target=SpheroRvrTargets.primary.value
3533
)
36-
print('Echo response: ', echo_response)
34+
print('Echo response 1: ', echo_response)
35+
36+
echo_response = await rvr.echo(
37+
data=[4, 5, 6],
38+
target=SpheroRvrTargets.secondary.value
39+
)
40+
print('Echo response 2: ', echo_response)
3741

3842
await rvr.close()
3943

@@ -45,12 +49,12 @@ async def main():
4549
)
4650

4751
except KeyboardInterrupt:
48-
print('Program terminated with keyboard interrupt.')
52+
print('\nProgram terminated with keyboard interrupt.')
4953

50-
finally:
5154
loop.run_until_complete(
5255
rvr.close()
5356
)
5457

58+
finally:
5559
if loop.is_running():
5660
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
import sys
21
import os
2+
import sys
33
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
44

55
import asyncio
66
from sphero_sdk import SpheroRvrAsync
77
from sphero_sdk import RestfulAsyncDal
8-
from sphero_sdk.common.log_level import LogLevel
8+
from sphero_sdk import SpheroRvrTargets
9+
910

1011
loop = asyncio.get_event_loop()
1112

1213
rvr = SpheroRvrAsync(
1314
dal=RestfulAsyncDal(
14-
prefix="RV", # RVR's prefix is RV
15-
domain="10.211.2.21", # Add your raspberry-pi's IP address here
15+
domain='10.211.2.21', # Add your raspberry-pi's IP address here
1616
port=2010 # The port opened by the npm server is always 2010
17-
),
18-
log_level=LogLevel.Debug_Verbose
17+
)
1918
)
2019

2120

2221
async def main():
22+
""" This program demonstrates how to use the echo command, which sends data to RVR and has RVR returns
23+
the same data. Echo can be used to check to see if RVR is connected and awake. In order to test it,
24+
a node.js server must be running on the raspberry-pi connected to RVR. This code is meant to be
25+
executed from a separate computer.
2326
"""
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.
2827

29-
"""
30-
response = await rvr.echo([1], target=2)
31-
print(response)
28+
await rvr.wake()
3229

33-
await asyncio.sleep(1)
30+
# Give RVR time to wake up
31+
await asyncio.sleep(2)
3432

35-
response = await rvr.echo([2], target=2)
36-
print(response)
33+
echo_response = await rvr.echo(
34+
data=[0, 1, 2],
35+
target=SpheroRvrTargets.primary.value
36+
)
37+
print('Echo response 1: ', echo_response)
3738

38-
await asyncio.sleep(1)
39+
echo_response = await rvr.echo(
40+
data=[4, 5, 6],
41+
target=SpheroRvrTargets.secondary.value
42+
)
43+
print('Echo response 2: ', echo_response)
44+
45+
await rvr.close()
3946

4047

4148
if __name__ == '__main__':
@@ -45,12 +52,12 @@ async def main():
4552
)
4653

4754
except KeyboardInterrupt:
48-
print('Program terminated with keyboard interrupt.')
55+
print('\nProgram terminated with keyboard interrupt.')
4956

50-
finally:
5157
loop.run_until_complete(
5258
rvr.close()
5359
)
5460

61+
finally:
5562
if loop.is_running():
5663
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import asyncio
21
import os
32
import sys
4-
5-
63
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
74

5+
import asyncio
86
from sphero_sdk import SpheroRvrAsync
97
from sphero_sdk import SerialAsyncDal
8+
from sphero_sdk import RvrStreamingServices
109

1110

1211
loop = asyncio.get_event_loop()
@@ -18,8 +17,8 @@
1817
)
1918

2019

21-
async def sensor_data_handler(sensor_data):
22-
print('Sensor data response: ', sensor_data)
20+
async def color_detected_handler(color_detected_data):
21+
print('Color detection data response: ', color_detected_data)
2322

2423

2524
async def main():
@@ -29,27 +28,37 @@ async def main():
2928

3029
await rvr.wake()
3130

32-
# give RVR time to wake up
31+
# Give RVR time to wake up
3332
await asyncio.sleep(2)
3433

3534
await rvr.enable_color_detection(is_enabled=True)
36-
await rvr.sensor_control.add_sensor_data_handler(handler=sensor_data_handler)
37-
await rvr.sensor_control.enable('ColorDetection') # TODO: is there a constant available for this?
35+
await rvr.sensor_control.add_sensor_data_handler(
36+
service=RvrStreamingServices.color_detection,
37+
handler=color_detected_handler
38+
)
39+
await rvr.sensor_control.start(interval=250)
40+
41+
while True:
42+
await asyncio.sleep(1)
3843

3944

4045
if __name__ == '__main__':
4146
try:
42-
loop.run_until_complete(
47+
asyncio.ensure_future(
4348
main()
4449
)
50+
loop.run_forever()
4551

4652
except KeyboardInterrupt:
47-
print('Program terminated with keyboard interrupt.')
53+
print('\nProgram terminated with keyboard interrupt.')
4854

49-
finally:
5055
loop.run_until_complete(
51-
rvr.close()
56+
asyncio.gather(
57+
rvr.enable_color_detection(is_enabled=False),
58+
rvr.close()
59+
)
5260
)
5361

62+
finally:
5463
if loop.is_running():
5564
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
1-
import sys
21
import os
2+
import sys
33
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
44

55
import asyncio
66
from sphero_sdk import SpheroRvrAsync
77
from sphero_sdk import RestfulAsyncDal
8+
from sphero_sdk import RvrStreamingServices
9+
810

911
loop = asyncio.get_event_loop()
1012

1113
rvr = SpheroRvrAsync(
1214
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+
domain='10.211.2.21', # Add your raspberry-pi's IP address here
1516
port=2010 # The port opened by the npm server is always 2010
1617
)
1718
)
1819

1920

20-
21-
async def on_color_detected(response):
22-
print('Response data for color detected:',response)
21+
async def color_detected_handler(color_detected_data):
22+
print('Color detection data response: ', color_detected_data)
2323

2424

2525
async def main():
2626
""" This program uses the color sensor on RVR (located on the down side of RVR, facing the floor) to report colors detected.
2727
To exit program, press <CTRL-C>
28-
2928
"""
30-
# Wake up RVR
29+
3130
await rvr.wake()
3231

3332
# Give RVR time to wake up
34-
await asyncio.sleep(1)
33+
await asyncio.sleep(2)
3534

36-
# This enables the color sensor on RVR
3735
await rvr.enable_color_detection(is_enabled=True)
36+
await rvr.sensor_control.add_sensor_data_handler(
37+
service=RvrStreamingServices.color_detection,
38+
handler=color_detected_handler
39+
)
40+
await rvr.sensor_control.start(interval=250)
3841

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")
42+
while True:
43+
await asyncio.sleep(1)
4444

4545

4646
if __name__ == '__main__':
4747
try:
48-
loop.run_until_complete(
48+
asyncio.ensure_future(
4949
main()
5050
)
51+
loop.run_forever()
5152

5253
except KeyboardInterrupt:
53-
print('Program terminated with keyboard interrupt.')
54+
print('\nProgram terminated with keyboard interrupt.')
5455

55-
finally:
5656
loop.run_until_complete(
57-
rvr.close()
57+
asyncio.gather(
58+
rvr.enable_color_detection(is_enabled=False),
59+
rvr.close()
60+
)
5861
)
5962

63+
finally:
6064
if loop.is_running():
6165
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import asyncio
21
import os
32
import sys
4-
5-
63
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
74

5+
import asyncio
86
from sphero_sdk import SpheroRvrAsync
97
from sphero_sdk import SerialAsyncDal
108
from sphero_sdk import RawMotorModesEnum
@@ -25,59 +23,59 @@ async def main():
2523

2624
await rvr.wake()
2725

28-
# give RVR time to wake up
26+
# Give RVR time to wake up
2927
await asyncio.sleep(2)
3028

3129
await rvr.reset_yaw()
3230

3331
await rvr.raw_motors(
3432
left_mode=RawMotorModesEnum.forward.value,
35-
left_speed=128,
33+
left_speed=128, # Valid speed values are 0-255
3634
right_mode=RawMotorModesEnum.forward.value,
37-
right_speed=128
35+
right_speed=128 # Valid speed values are 0-255
3836
)
3937

40-
# delay to allow RVR to drive
38+
# Delay to allow RVR to drive
4139
await asyncio.sleep(1)
4240

4341
await rvr.raw_motors(
4442
left_mode=RawMotorModesEnum.reverse.value,
45-
left_speed=64,
43+
left_speed=64, # Valid speed values are 0-255
4644
right_mode=RawMotorModesEnum.reverse.value,
47-
right_speed=64
45+
right_speed=64 # Valid speed values are 0-255
4846
)
4947

50-
# delay to allow RVR to drive
48+
# Delay to allow RVR to drive
5149
await asyncio.sleep(1)
5250

5351
await rvr.raw_motors(
5452
left_mode=RawMotorModesEnum.reverse.value,
55-
left_speed=128,
53+
left_speed=128, # Valid speed values are 0-255
5654
right_mode=RawMotorModesEnum.forward.value,
57-
right_speed=128
55+
right_speed=128 # Valid speed values are 0-255
5856
)
5957

60-
# delay to allow RVR to drive
58+
# Delay to allow RVR to drive
6159
await asyncio.sleep(1)
6260

6361
await rvr.raw_motors(
6462
left_mode=RawMotorModesEnum.forward.value,
65-
left_speed=128,
63+
left_speed=128, # Valid speed values are 0-255
6664
right_mode=RawMotorModesEnum.forward.value,
67-
right_speed=128
65+
right_speed=128 # Valid speed values are 0-255
6866
)
6967

70-
# delay to allow RVR to drive
68+
# Delay to allow RVR to drive
7169
await asyncio.sleep(1)
7270

7371
await rvr.raw_motors(
7472
left_mode=RawMotorModesEnum.off.value,
75-
left_speed=0,
73+
left_speed=0, # Valid speed values are 0-255
7674
right_mode=RawMotorModesEnum.off.value,
77-
right_speed=0
75+
right_speed=0 # Valid speed values are 0-255
7876
)
7977

80-
# delay to allow RVR to drive
78+
# Delay to allow RVR to drive
8179
await asyncio.sleep(1)
8280

8381
await rvr.close()
@@ -90,12 +88,12 @@ async def main():
9088
)
9189

9290
except KeyboardInterrupt:
93-
print('Program terminated with keyboard interrupt.')
91+
print('\nProgram terminated with keyboard interrupt.')
9492

95-
finally:
9693
loop.run_until_complete(
9794
rvr.close()
9895
)
9996

97+
finally:
10098
if loop.is_running():
10199
loop.close()

0 commit comments

Comments
 (0)