Skip to content

Commit d7d1a70

Browse files
author
Anthony Vizcarra
committed
Merge branch 'angel/new-sensor-streaming' into 'develop'
New sensor streaming, straight out of the oven. See merge request sdk/v4/convenience/raspberry-pi!50
2 parents 07e248b + f66063a commit d7d1a70

Some content is hidden

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

45 files changed

+2689
-496
lines changed

getting_started/asyncio/color_sensor/color_detection.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ async def main():
3939
await rvr.enable_color_detection(is_enabled=True)
4040

4141
# Register a handler to be called when a color detection notification is received
42-
await rvr.on_color_detection_notify(handler=on_color_detected)
42+
await rvr.sensor_control.add_sensor_data_handler(on_color_detected)
4343

44-
# Enable the color detection notifications with the given parameters
45-
await rvr.enable_color_detection_notify(is_enabled=True, interval=250, minimum_confidence_threshold=0, timeout=5)
44+
# Enable the color detection sensor stream
45+
await rvr.sensor_control.enable("ColorDetection")
4646

4747

4848
try:
4949
asyncio.ensure_future(main())
5050
loop.run_forever()
5151

5252
except KeyboardInterrupt:
53-
loop.stop()
53+
loop.run_until_complete(rvr.close())
5454

55-
time.sleep(1)
5655
loop.close()

getting_started/asyncio/sensor_streaming/__init__.py

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 SerialAsyncDal
8+
9+
loop = asyncio.get_event_loop()
10+
11+
rvr = SpheroRvrAsync(
12+
dal=SerialAsyncDal(
13+
loop
14+
)
15+
)
16+
17+
18+
async def on_sensor_streaming_data(response):
19+
print(response)
20+
21+
22+
async def get_single_sensor_stream():
23+
"""This program demonstrates how to update sensor streaming parameters at runtime.
24+
25+
"""
26+
# Wake up RVR
27+
await rvr.wake()
28+
29+
# Give RVR time to wake up
30+
await asyncio.sleep(2)
31+
print("----------", "Enabling IMU at 100 ms", "----------")
32+
33+
# Add a callback to receive sensor stream data
34+
await rvr.sensor_control.add_sensor_data_handler(on_sensor_streaming_data)
35+
36+
# Set a slow streaming interval (every 500 ms)
37+
rvr.sensor_control.streaming_interval = 100
38+
39+
# Enable a single sensor. Supported sensors are:
40+
# "ColorDetection"
41+
# "AmbientLight"
42+
# "Quaternion"
43+
# "IMU"
44+
# "Accelerometer"
45+
# "Gyroscope"
46+
# "Locator"
47+
# "Velocity"
48+
# "Speed"
49+
# "CoreTime"
50+
await rvr.sensor_control.enable("IMU")
51+
52+
# Pause this function for 5 seconds
53+
await asyncio.sleep(5)
54+
print("----------", "Updating interval to 1000 ms", "----------")
55+
56+
# Update the streaming interval to 100ms
57+
rvr.sensor_control.streaming_interval = 1000
58+
59+
# Sleep for 5 seconds before calling next function
60+
await asyncio.sleep(5)
61+
print("----------", "Adding color detection and velocity sensor streams", "----------")
62+
63+
# Add 2 more services
64+
await rvr.sensor_control.enable("AmbientLight", "Velocity")
65+
66+
# Sleep for another 5 seconds
67+
await asyncio.sleep(5)
68+
print("----------", "Disabling IMU sensor stream and updating interval to 100", "----------")
69+
70+
# Disable IMU service
71+
await rvr.sensor_control.disable("IMU")
72+
73+
# Sleep for another 5 seconds
74+
await asyncio.sleep(5)
75+
print("----------", "Disabling all services", "----------")
76+
77+
# Disable all services
78+
await rvr.sensor_control.disable_all()
79+
80+
try:
81+
loop.run_until_complete(
82+
get_single_sensor_stream()
83+
)
84+
except KeyboardInterrupt:
85+
print("Program terminated with keyboard interrupt.")
86+
87+
# Need to make sure we close rvr properly to terminate all streams.
88+
loop.run_until_complete(
89+
rvr.close()
90+
)
91+
finally:
92+
# stop the loop
93+
loop.close()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 SerialAsyncDal
8+
9+
loop = asyncio.get_event_loop()
10+
11+
rvr = SpheroRvrAsync(
12+
dal=SerialAsyncDal(
13+
loop
14+
)
15+
)
16+
17+
18+
async def on_sensor_streaming_data(response):
19+
print(response)
20+
21+
22+
async def get_single_sensor_stream():
23+
"""This program enables multiple sensors streams that will be printed to the console.
24+
25+
"""
26+
# Wake up RVR
27+
await rvr.wake()
28+
29+
# Add a callback to receive sensor stream data
30+
await rvr.sensor_control.add_sensor_data_handler(on_sensor_streaming_data)
31+
32+
# Enable a multiple sensors. Supported sensors are:
33+
# "ColorDetection"
34+
# "AmbientLight"
35+
# "Quaternion"
36+
# "IMU"
37+
# "Accelerometer"
38+
# "Gyroscope"
39+
# "Locator"
40+
# "Velocity"
41+
# "Speed"
42+
# "CoreTime"
43+
await rvr.sensor_control.enable("IMU","ColorDetection","Accelerometer","AmbientLight")
44+
45+
# Allow this program to run until a keyboard interrupt is detected
46+
while True:
47+
await asyncio.sleep(1)
48+
49+
50+
async def close_rvr():
51+
await rvr.close()
52+
53+
54+
try:
55+
loop.run_until_complete(
56+
get_single_sensor_stream()
57+
)
58+
except KeyboardInterrupt:
59+
print("Program terminated with keyboard interrupt.")
60+
61+
# Need to make sure we close rvr properly to terminate all streams.
62+
loop.run_until_complete(
63+
rvr.close()
64+
)
65+
finally:
66+
# stop the loop
67+
loop.close()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 SerialAsyncDal
8+
9+
loop = asyncio.get_event_loop()
10+
11+
rvr = SpheroRvrAsync(
12+
dal=SerialAsyncDal(
13+
loop
14+
)
15+
)
16+
17+
async def on_sensor_streaming_data(response):
18+
print(response)
19+
20+
async def get_single_sensor_stream():
21+
"""This program enables a single sensor stream that will be printed to the console.
22+
23+
"""
24+
# Wake up RVR
25+
await rvr.wake()
26+
27+
# Add a callback to receive sensor stream data
28+
await rvr.sensor_control.add_sensor_data_handler(on_sensor_streaming_data)
29+
30+
# Enable a single sensor. Supported sensors are:
31+
# "ColorDetection"
32+
# "AmbientLight"
33+
# "Quaternion"
34+
# "IMU"
35+
# "Accelerometer"
36+
# "Gyroscope"
37+
# "Locator"
38+
# "Velocity"
39+
# "Speed"
40+
# "CoreTime"
41+
await rvr.sensor_control.enable("Accelerometer")
42+
43+
# Allow this program to run until a keyboard interrupt is detected
44+
while True:
45+
await asyncio.sleep(1)
46+
47+
async def close_rvr():
48+
await rvr.close()
49+
50+
try:
51+
loop.run_until_complete(
52+
get_single_sensor_stream()
53+
)
54+
except KeyboardInterrupt:
55+
print("Program terminated with keyboard interrupt.")
56+
57+
# Need to make sure we close rvr properly to terminate all streams.
58+
loop.run_until_complete(
59+
rvr.close()
60+
)
61+
finally:
62+
# stop the loop
63+
loop.close()

getting_started/observer/color_sensor/color_detection.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,32 @@ def main():
1717
""" This program uses the color sensor on RVR (located on the down side of RVR, facing the floor) to report colors detected.
1818
1919
"""
20-
# Wake up RVR
21-
rvr.wake()
20+
try:
21+
# Wake up RVR
22+
rvr.wake()
2223

23-
# Give RVR time to wake up
24-
time.sleep(2)
24+
# Give RVR time to wake up
25+
time.sleep(2)
2526

26-
# This enables the color sensor on RVR
27-
rvr.enable_color_detection(is_enabled=True)
27+
# This enables the color sensor on RVR
28+
rvr.enable_color_detection(is_enabled=True)
2829

29-
# Register a handler to be called when a color detection notification is received
30-
rvr.on_color_detection_notify(handler=on_color_detected)
30+
# Register a handler to be called when a color detection notification is received
31+
rvr.sensor_control.add_sensor_data_handler(on_color_detected)
3132

32-
# Enable the color detection notifications with the given parameters
33-
rvr.enable_color_detection_notify(is_enabled=True, interval=250, minimum_confidence_threshold=0, timeout=5)
33+
# Enable the color detection sensor stream
34+
rvr.sensor_control.enable("ColorDetection")
3435

35-
# Allow this program to run for 10 seconds
36-
time.sleep(10)
37-
38-
# Properly shuts down the RVR serial port.
39-
rvr.close()
36+
# Allow this program to run for 10 seconds
37+
time.sleep(10)
4038

39+
# Disable all services
40+
rvr.sensor_control.disable_all()
41+
except KeyboardInterrupt:
42+
print("Program terminated with keyboard interrupt.")
43+
finally:
44+
# Properly shuts down the RVR serial port.
45+
rvr.close()
4146

4247
if __name__ == "__main__":
4348
main()

getting_started/observer/sensor_streaming/__init__.py

Whitespace-only changes.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import time
2+
import sys
3+
import os
4+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
5+
6+
from sphero_sdk import SpheroRvrObserver
7+
8+
rvr = SpheroRvrObserver()
9+
10+
11+
def on_sensor_streaming_data(response):
12+
print(response)
13+
14+
15+
def run_sample_sequence():
16+
"""This program demonstrates how to update sensor streaming parameters at runtime.
17+
18+
"""
19+
try:
20+
# Wake up RVR
21+
rvr.wake()
22+
23+
# Give RVR time to wake up
24+
time.sleep(2)
25+
print("----------", "Enabling IMU at 100 ms", "----------")
26+
27+
# Add a callback to receive sensor stream data
28+
rvr.sensor_control.add_sensor_data_handler(on_sensor_streaming_data)
29+
30+
# Set a slow streaming interval (every 500 ms)
31+
rvr.sensor_control.streaming_interval = 100
32+
33+
# Enable a single sensor. Supported sensors are:
34+
# "ColorDetection"
35+
# "AmbientLight"
36+
# "Quaternion"
37+
# "IMU"
38+
# "Accelerometer"
39+
# "Gyroscope"
40+
# "Locator"
41+
# "Velocity"
42+
# "Speed"
43+
# "CoreTime"
44+
rvr.sensor_control.enable("IMU")
45+
46+
# Pause this function for 5 seconds
47+
time.sleep(5)
48+
print("----------", "Updating interval to 1000 ms", "----------")
49+
50+
# Update the streaming interval to 100ms
51+
rvr.sensor_control.streaming_interval = 1000
52+
53+
# Sleep for 5 seconds before calling next function
54+
time.sleep(5)
55+
print("----------", "Adding color detection and velocity sensor streams", "----------")
56+
57+
# Add 2 more services
58+
rvr.sensor_control.enable("AmbientLight", "Velocity")
59+
60+
# Sleep for another 5 seconds
61+
time.sleep(5)
62+
print("----------", "Disabling IMU sensor stream and updating interval to 100", "----------")
63+
64+
# Disable IMU service
65+
rvr.sensor_control.disable("IMU")
66+
67+
# Sleep for another 5 seconds
68+
time.sleep(5)
69+
print("----------", "Disabling all services", "----------")
70+
71+
# Disable all services
72+
rvr.sensor_control.disable_all()
73+
74+
except KeyboardInterrupt:
75+
print("Program terminated with keyboard interrupt.")
76+
finally:
77+
# Properly shuts down the RVR serial port.
78+
rvr.close()
79+
80+
81+
if __name__ == "__main__":
82+
run_sample_sequence()

0 commit comments

Comments
 (0)