Skip to content

Commit 939ca15

Browse files
author
Angel Campos
committed
Merge branch 'feature/anthony/refactor_getting_started_samples' into 'develop'
Feature/anthony/refactor getting started samples See merge request sdk/v4/convenience/raspberry-pi!51
2 parents d7d1a70 + 693aa4b commit 939ca15

File tree

78 files changed

+1913
-1702
lines changed

Some content is hidden

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

78 files changed

+1913
-1702
lines changed
+24-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import sys
1+
import asyncio
22
import os
3-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
3+
import sys
44

5-
import asyncio
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
67

78
from sphero_sdk import SpheroRvrAsync
89
from sphero_sdk import SerialAsyncDal
10+
from sphero_sdk import SpheroRvrTargets
11+
912

1013
loop = asyncio.get_event_loop()
1114

@@ -17,21 +20,30 @@
1720

1821

1922
async def main():
20-
""" This program demonstrates how to use the echo command, which sends data to RVR and has RVR returns
23+
""" This program demonstrates how to use the echo command, which sends data to RVR and RVR returns
2124
the same data. Echo can be used to check to see if RVR is connected and awake.
22-
2325
"""
26+
2427
await rvr.wake()
28+
29+
# give RVR time to wake up
30+
await asyncio.sleep(2)
2531

26-
response = await rvr.echo([0,2,4,8,16,32,64,128,255], target=1)
27-
print("Response data for echo: ",response)
32+
echo_response = await rvr.echo(
33+
data=[0, 2, 4, 8, 16, 32, 64, 128, 255],
34+
target=SpheroRvrTargets.primary.value
35+
)
36+
print('Echo response: ', echo_response)
2837

38+
await rvr.close()
2939

30-
loop.run_until_complete(
31-
asyncio.gather(
40+
41+
if __name__ == '__main__':
42+
loop.run_until_complete(
3243
main()
3344
)
34-
)
3545

36-
loop.stop()
37-
loop.close()
46+
if loop.is_running():
47+
loop.stop()
48+
49+
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
1-
import sys
1+
import asyncio
22
import os
3-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
3+
import sys
44

5-
import time
65

7-
import asyncio
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
87

98
from sphero_sdk import SpheroRvrAsync
109
from sphero_sdk import SerialAsyncDal
1110

12-
# Get a reference to the asynchronous program loop
11+
1312
loop = asyncio.get_event_loop()
1413

15-
# Create an AsyncSpheroRvr object and pass in a SerialAsyncDal object, which in turn takes a reference to the program loop
1614
rvr = SpheroRvrAsync(
1715
dal=SerialAsyncDal(
1816
loop
1917
)
2018
)
2119

2220

23-
async def on_color_detected(response):
24-
print('Response data for color detected:',response)
21+
async def sensor_data_handler(sensor_data):
22+
print('Sensor data response: ', sensor_data)
2523

2624

2725
async def main():
28-
""" This program uses the color sensor on RVR (located on the down side of RVR, facing the floor) to report colors detected.
29-
To exit program, press <CTRL-C>
30-
26+
""" This program demonstrates how to use the color sensor on RVR (located on the down side of RVR, facing the floor)
27+
to report colors detected. To exit program, press <CTRL-C>
3128
"""
32-
# Wake up RVR
29+
3330
await rvr.wake()
3431

35-
# Give RVR time to wake up
36-
await asyncio.sleep(1)
32+
# give RVR time to wake up
33+
await asyncio.sleep(2)
3734

38-
# This enables the color sensor on RVR
3935
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?
38+
4039

41-
# Register a handler to be called when a color detection notification is received
42-
await rvr.sensor_control.add_sensor_data_handler(on_color_detected)
40+
if __name__ == '__main__':
41+
try:
42+
asyncio.ensure_future(
43+
main()
44+
)
4345

44-
# Enable the color detection sensor stream
45-
await rvr.sensor_control.enable("ColorDetection")
46+
loop.run_forever()
4647

48+
except KeyboardInterrupt:
49+
print('Program terminated with keyboard interrupt.')
4750

48-
try:
49-
asyncio.ensure_future(main())
50-
loop.run_forever()
51+
loop.run_until_complete(
52+
rvr.close()
53+
)
5154

52-
except KeyboardInterrupt:
53-
loop.run_until_complete(rvr.close())
55+
finally:
56+
if loop.is_running():
57+
loop.stop()
5458

55-
loop.close()
59+
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import sys
1+
import asyncio
22
import os
3-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
3+
import sys
44

5-
import asyncio
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
67

78
from sphero_sdk import SpheroRvrAsync
89
from sphero_sdk import SerialAsyncDal
10+
from sphero_sdk import RawMotorModesEnum
11+
912

10-
# Get a reference to the asynchronous program loop
1113
loop = asyncio.get_event_loop()
1214

13-
# Create an AsyncSpheroRvr object, and pass in a SerialAsyncDal object, which in turn takes a reference
14-
# to the asynchronous program loop
1515
rvr = SpheroRvrAsync(
1616
dal=SerialAsyncDal(
1717
loop
@@ -20,41 +20,75 @@
2020

2121

2222
async def main():
23+
""" This program has RVR drive around in different directions.
2324
"""
24-
This program has RVR drive around in different directions using the function raw_motors.
25-
26-
Note:
27-
To give RVR time to drive, we call asyncio.sleep(...); if we did not have these calls, the program would
28-
go on and execute all the statements and exit without the driving ever taking place.
29-
"""
25+
3026
await rvr.wake()
3127

32-
# Drive straight for one second at speed 128
33-
await rvr.raw_motors(1, 128, 1, 128)
28+
# give RVR time to wake up
29+
await asyncio.sleep(2)
30+
31+
await rvr.reset_yaw()
32+
33+
await rvr.raw_motors(
34+
left_mode=RawMotorModesEnum.forward.value,
35+
left_speed=128,
36+
right_mode=RawMotorModesEnum.forward.value,
37+
right_speed=128
38+
)
39+
40+
# delay to allow RVR to drive
3441
await asyncio.sleep(1)
3542

36-
# Drive backwards for one second at speed 64
37-
await rvr.raw_motors(2, 64, 2, 64)
43+
await rvr.raw_motors(
44+
left_mode=RawMotorModesEnum.reverse.value,
45+
left_speed=64,
46+
right_mode=RawMotorModesEnum.reverse.value,
47+
right_speed=64
48+
)
49+
50+
# delay to allow RVR to drive
3851
await asyncio.sleep(1)
3952

40-
# Turn right
41-
await rvr.raw_motors(2, 128, 1, 128)
53+
await rvr.raw_motors(
54+
left_mode=RawMotorModesEnum.reverse.value,
55+
left_speed=128,
56+
right_mode=RawMotorModesEnum.forward.value,
57+
right_speed=128
58+
)
59+
60+
# delay to allow RVR to drive
4261
await asyncio.sleep(1)
4362

44-
# Drive forward for 1 second at speed 128
45-
await rvr.raw_motors(1, 128, 1, 128)
63+
await rvr.raw_motors(
64+
left_mode=RawMotorModesEnum.forward.value,
65+
left_speed=128,
66+
right_mode=RawMotorModesEnum.forward.value,
67+
right_speed=128
68+
)
69+
70+
# delay to allow RVR to drive
4671
await asyncio.sleep(1)
4772

48-
# Stop RVR
49-
await rvr.raw_motors(0, 0, 0, 0)
73+
await rvr.raw_motors(
74+
left_mode=RawMotorModesEnum.off.value,
75+
left_speed=0,
76+
right_mode=RawMotorModesEnum.off.value,
77+
right_speed=0
78+
)
5079

80+
# delay to allow RVR to drive
81+
await asyncio.sleep(1)
5182

52-
# Run event loop until the main function has completed
53-
loop.run_until_complete(
54-
main()
55-
)
83+
await rvr.close()
84+
85+
86+
if __name__ == '__main__':
87+
loop.run_until_complete(
88+
main()
89+
)
90+
91+
if loop.is_running():
92+
loop.stop()
5693

57-
# Stop the event loop
58-
loop.stop()
59-
# Close the event loop
60-
loop.close()
94+
loop.close()
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import sys
1+
import asyncio
22
import os
3-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
3+
import sys
44

5-
import asyncio
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
67

78
from sphero_sdk import SpheroRvrAsync
89
from sphero_sdk import SerialAsyncDal
10+
from sphero_sdk import DriveFlagsBitmask
11+
912

10-
# Get a reference to the asynchronous program loop
1113
loop = asyncio.get_event_loop()
1214

13-
# Create an AsyncSpheroRvr object, and pass in a SerialAsyncDal object, which in turn takes a reference
14-
# to the asynchronous program loop
1515
rvr = SpheroRvrAsync(
1616
dal=SerialAsyncDal(
1717
loop
@@ -21,47 +21,69 @@
2121

2222
async def main():
2323
""" This program has RVR drive around in different directions using the function drive_with_heading.
24-
25-
Note:
26-
To have RVR drive, we call asyncio.sleep(...); if we did not have these calls, the program would
27-
go on and execute all statements and exit without the driving ever taking place.
2824
"""
25+
2926
await rvr.wake()
3027

31-
# Reset yaw such that the heading will be set compared to the direction RVR is currently facing
28+
# give RVR time to wake up
29+
await asyncio.sleep(2)
30+
3231
await rvr.reset_yaw()
3332

34-
# Drive straight for one second at speed 128
35-
await rvr.drive_with_heading(128, 0, 0)
33+
await rvr.drive_with_heading(
34+
speed=128,
35+
heading=0,
36+
flags=DriveFlagsBitmask.none.value
37+
)
38+
39+
# delay to allow RVR to drive
3640
await asyncio.sleep(1)
3741

38-
# Drive backwards for one second at speed 128
39-
# Note that the flag is set to 1 for reverse
40-
await rvr.drive_with_heading(128, 0, 1)
42+
await rvr.drive_with_heading(
43+
speed=128,
44+
heading=0,
45+
flags=DriveFlagsBitmask.drive_reverse.value
46+
)
47+
48+
# delay to allow RVR to drive
4149
await asyncio.sleep(1)
4250

43-
# Go right for a second (relative to original yaw)
44-
await rvr.drive_with_heading(128, 90, 0)
51+
await rvr.drive_with_heading(
52+
speed=128,
53+
heading=90,
54+
flags=DriveFlagsBitmask.none.value
55+
)
56+
57+
# delay to allow RVR to drive
4558
await asyncio.sleep(1)
4659

47-
# Go left for a second (relative to original yaw)
48-
await rvr.drive_with_heading(128, 270, 0)
60+
await rvr.drive_with_heading(
61+
speed=128,
62+
heading=270,
63+
flags=DriveFlagsBitmask.none.value
64+
)
65+
66+
# delay to allow RVR to drive
4967
await asyncio.sleep(1)
5068

51-
# Turn facing the original direction
52-
await rvr.drive_with_heading(0, 0, 0)
69+
await rvr.drive_with_heading(
70+
speed=0,
71+
heading=0,
72+
flags=DriveFlagsBitmask.none.value
73+
)
74+
75+
# delay to allow RVR to drive
5376
await asyncio.sleep(1)
5477

55-
# Stop RVR
56-
await rvr.raw_motors(0, 0, 0, 0)
78+
await rvr.close()
5779

5880

59-
# Run event loop until the main function has completed
60-
loop.run_until_complete(
61-
main()
62-
)
81+
if __name__ == '__main__':
82+
loop.run_until_complete(
83+
main()
84+
)
85+
86+
if loop.is_running():
87+
loop.stop()
6388

64-
# Stop the event loop
65-
loop.stop()
66-
# Close the event loop
67-
loop.close()
89+
loop.close()

0 commit comments

Comments
 (0)