Skip to content

Commit 80fb8c3

Browse files
author
Angel Campos
committed
Merge branch 'feature/observer-updates' into 'develop'
Observer example updates and API error handling See merge request sdk/v4/convenience/raspberry-pi!74
2 parents 0e7191c + 3ff9abd commit 80fb8c3

File tree

82 files changed

+1898
-1223
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

+1898
-1223
lines changed

getting_started/asyncio/api_and_shell/echo_with_rest_api.py

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
import sys
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+
from sphero_sdk import SpheroRvrTargets
9+
from sphero_sdk import ErrorCode
10+
11+
12+
loop = asyncio.get_event_loop()
13+
14+
rvr = SpheroRvrAsync(
15+
dal=SerialAsyncDal(
16+
loop
17+
)
18+
)
19+
20+
21+
async def main():
22+
""" This sample uses the generate_api_error command to intentionally generate an error response from RVR
23+
with the specified response code. Under normal circumstances certain commands request a response from RVR.
24+
If an error response is detected then it can be handled by the SDK's response logic. However, if a
25+
command does not have expected output, and therefore doesn't request a response (e.g. drive_with_heading),
26+
then RVR does not generate an error response if one occurs. Setting the request_error_responses_only flag
27+
on RVR will enable it to generate error responses even if no output is expected, and give a program the
28+
ability to catch any of the following 10 error response codes:
29+
30+
Response Code 0x00: success
31+
Response Code 0x01: bad_did
32+
Response Code 0x02: bad_cid
33+
Response Code 0x03: not_yet_implemented
34+
Response Code 0x04: restricted
35+
Response Code 0x05: bad_data_length
36+
Response Code 0x06: failed
37+
Response Code 0x07: bad_data_value
38+
Response Code 0x08: busy
39+
Response Code 0x09: bad_tid (bad target id)
40+
Response Code 0x0A: target_unavailable
41+
42+
Note a response code of 0x00 indicates a successful command, and is therefore is not considered an error.
43+
"""
44+
45+
# Since the generate_api_error command doesn't expect any output, the enable_error_responses_only flag must
46+
# be set True in order for RVR to generate an error. This flag will remain true until otherwise specified,
47+
# or when the program terminates.
48+
rvr.request_error_responses_only = True
49+
50+
await rvr.generate_api_error(
51+
error=ErrorCode.target_unavailable, # Specify code 0x01 - 0x0A to receive that specific error response from RVR.
52+
target=SpheroRvrTargets.secondary.value,
53+
timeout=3
54+
)
55+
56+
await rvr.close()
57+
58+
59+
if __name__ == '__main__':
60+
try:
61+
loop.run_until_complete(
62+
main()
63+
)
64+
65+
except KeyboardInterrupt:
66+
print('\nProgram terminated with keyboard interrupt.')
67+
68+
loop.run_until_complete(
69+
rvr.close()
70+
)
71+
72+
finally:
73+
if loop.is_running():
74+
loop.close()

getting_started/asyncio/color_sensor/color_detection_with_rest_api.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

getting_started/asyncio/driving/control_system_selection.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
from sphero_sdk import SpheroRvrAsync
77
from sphero_sdk import SerialAsyncDal
8-
from sphero_sdk import DriveFlagsBitmask
98
from sphero_sdk import ControlSystemTypesEnum
109
from sphero_sdk import ControlSystemIdsEnum
1110

@@ -59,27 +58,27 @@ async def main():
5958
control_system_type = ControlSystemTypesEnum.control_system_type_stop
6059
response = await rvr.get_default_control_system_for_type(control_system_type = control_system_type)
6160
print(response)
62-
controller_id = ControlSystemIdsEnum(response['controllerId'])
61+
controller_id = ControlSystemIdsEnum(response['controller_id'])
6362
print('Default controller for {} is {}'.format(control_system_type.name, controller_id.name))
6463

6564
control_system_type = ControlSystemTypesEnum.control_system_type_rc_drive
6665
response = await rvr.get_default_control_system_for_type(control_system_type = control_system_type)
67-
controller_id = ControlSystemIdsEnum(response['controllerId'])
66+
controller_id = ControlSystemIdsEnum(response['controller_id'])
6867
print('Default controller for {} is {}'.format(control_system_type.name, controller_id.name))
6968

7069

7170
# We are currently stopped. Get the currently active control system.
7271
print('Getting current control system...')
7372
response = await rvr.get_active_control_system_id()
74-
controller_id = ControlSystemIdsEnum(response['controllerId'])
73+
controller_id = ControlSystemIdsEnum(response['controller_id'])
7574
print('Active controller: {}'.format(controller_id.name))
7675

7776

7877
# Drive a bit with RC and check the active control system
7978
print('Driving with RC...')
8079
await rvr.drive_rc_si_units(linear_velocity=1, yaw_angular_velocity=0, flags=0 )
8180
response = await rvr.get_active_control_system_id()
82-
controller_id = ControlSystemIdsEnum(response['controllerId'])
81+
controller_id = ControlSystemIdsEnum(response['controller_id'])
8382
print('Active controller: {}'.format(controller_id.name))
8483

8584
# Delay to allow RVR to drive
@@ -100,7 +99,7 @@ async def main():
10099
print('Driving with RC...')
101100
await rvr.drive_rc_si_units(linear_velocity=1, yaw_angular_velocity=0, flags=0 )
102101
response = await rvr.get_active_control_system_id()
103-
controller_id = ControlSystemIdsEnum(response['controllerId'])
102+
controller_id = ControlSystemIdsEnum(response['controller_id'])
104103
print('Active controller: {}'.format(controller_id.name))
105104

106105
# Delay to allow RVR to drive
@@ -117,7 +116,7 @@ async def main():
117116
print('Driving with RC...')
118117
await rvr.drive_rc_si_units(linear_velocity=1, yaw_angular_velocity=0, flags=0 )
119118
response = await rvr.get_active_control_system_id()
120-
controller_id = ControlSystemIdsEnum(response['controllerId'])
119+
controller_id = ControlSystemIdsEnum(response['controller_id'])
121120
print('Active controller: {}'.format(controller_id.name))
122121

123122
# Delay to allow RVR to drive

getting_started/asyncio/driving/control_system_timeouts.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
from sphero_sdk import SpheroRvrAsync
77
from sphero_sdk import SerialAsyncDal
8-
from sphero_sdk import RawMotorModesEnum
98

109

1110
loop = asyncio.get_event_loop()
@@ -38,15 +37,15 @@ async def main():
3837
# Reset yaw
3938
await rvr.reset_yaw()
4039

41-
# Make sure that we're starting with the default timeout (2 seconds). This is
40+
# Make sure that we're starting with the default timeout (2 seconds). This is
4241
# redundant, unless the timeouts have already been adjusted since boot.
4342
await rvr.restore_default_control_system_timeout()
4443

4544

4645
print("Driving forward with the default 2 second timeout")
4746

4847
await rvr.drive_rc_si_units(
49-
linear_velocity=.3, # Valid velocity values are in the range of [-1.555..1.555] m/s
48+
linear_velocity=.3, # Valid velocity values are in the range of [-2..2] m/s
5049
yaw_angular_velocity=0, # RVR will spin at up to 624 degrees/s. Values outside of [-624..624] will saturate internally.
5150
flags=0
5251
)

getting_started/asyncio/driving/drive_raw_motors.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,49 +36,49 @@ async def main():
3636

3737
await rvr.raw_motors(
3838
left_mode=RawMotorModesEnum.forward.value,
39-
left_speed=128, # Valid speed values are 0-255
39+
left_duty_cycle=128, # Valid duty cycle range is 0-255
4040
right_mode=RawMotorModesEnum.forward.value,
41-
right_speed=0 # Valid speed values are 0-255
41+
right_duty_cycle=0 # Valid duty cycle range is 0-255
4242
)
4343

4444
# Delay to allow RVR to spin motors
4545
await asyncio.sleep(1)
4646

4747
await rvr.raw_motors(
4848
left_mode=RawMotorModesEnum.reverse.value,
49-
left_speed=128, # Valid speed values are 0-255
49+
left_duty_cycle=128, # Valid duty cycle range is 0-255
5050
right_mode=RawMotorModesEnum.reverse.value,
51-
right_speed=0 # Valid speed values are 0-255
51+
right_duty_cycle=0 # Valid duty cycle range is 0-255
5252
)
5353

5454
# Delay to allow RVR to spin motors
5555
await asyncio.sleep(1)
5656

5757
await rvr.raw_motors(
5858
left_mode=RawMotorModesEnum.reverse.value,
59-
left_speed=128, # Valid speed values are 0-255
59+
left_duty_cycle=128, # Valid duty cycle range is 0-255
6060
right_mode=RawMotorModesEnum.forward.value,
61-
right_speed=0 # Valid speed values are 0-255
61+
right_duty_cycle=0 # Valid duty cycle range is 0-255
6262
)
6363

6464
# Delay to allow RVR to spin motors
6565
await asyncio.sleep(1)
6666

6767
await rvr.raw_motors(
6868
left_mode=RawMotorModesEnum.forward.value,
69-
left_speed=128, # Valid speed values are 0-255
69+
left_duty_cycle=128, # Valid duty cycle range is 0-255
7070
right_mode=RawMotorModesEnum.forward.value,
71-
right_speed=0 # Valid speed values are 0-255
71+
right_duty_cycle=0 # Valid duty cycle range is 0-255
7272
)
7373

7474
# Delay to allow RVR to spin motors
7575
await asyncio.sleep(1)
7676

7777
await rvr.raw_motors(
7878
left_mode=RawMotorModesEnum.off.value,
79-
left_speed=128, # Valid speed values are 0-255
79+
left_duty_cycle=128, # Valid duty cycle range is 0-255
8080
right_mode=RawMotorModesEnum.off.value,
81-
right_speed=0# Valid speed values are 0-255
81+
right_duty_cycle=0# Valid duty cycle range is 0-255
8282
)
8383

8484
# Delay to allow RVR to spin motors

getting_started/asyncio/driving/drive_rc_normalized.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
from sphero_sdk import SpheroRvrAsync
77
from sphero_sdk import SerialAsyncDal
8-
from sphero_sdk import RawMotorModesEnum
98

109

1110
loop = asyncio.get_event_loop()
@@ -46,7 +45,7 @@ async def main():
4645
)
4746

4847
# Delay to allow RVR to drive
49-
await asyncio.sleep(1)
48+
await asyncio.sleep(2)
5049

5150
# Continue driving forward, while turning left
5251
await rvr.drive_rc_normalized(
@@ -55,9 +54,19 @@ async def main():
5554
flags=0
5655
)
5756

58-
# Delay to allow RVR to drive
57+
# Delay to allow RVR to turn
5958
await asyncio.sleep(1)
6059

60+
# Drive in new forward direction
61+
await rvr.drive_rc_normalized(
62+
linear_velocity=20, # Valid linear velocity values are -127..127
63+
yaw_angular_velocity=0, # Valid angular velocity values are -127..127
64+
flags=0
65+
)
66+
67+
# Delay to allow RVR to drive
68+
await asyncio.sleep(2)
69+
6170
print("sending stop command")
6271

6372
# Stop driving, with deceleration rate of 2 m/s^2
@@ -71,11 +80,10 @@ async def main():
7180

7281
if __name__ == '__main__':
7382
try:
74-
asyncio.ensure_future(
83+
loop.run_until_complete(
7584
main()
7685
)
77-
loop.run_forever()
78-
86+
7987
except KeyboardInterrupt:
8088
print('\nProgram terminated with keyboard interrupt.')
8189

0 commit comments

Comments
 (0)