Skip to content

Commit 1e52007

Browse files
committed
Merge branch 'feature/RVR-1520-drive-target-slewing-example' into 'develop'
Feature/rvr 1520 drive target slewing example See merge request sdk/v4/convenience/raspberry-pi!77
2 parents 5833487 + 57badb1 commit 1e52007

34 files changed

+416
-64
lines changed

getting_started/asyncio/driving/control_system_selection.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async def main():
8686

8787
# Stop the robot
8888
print('Stopping...')
89-
await rvr.stop_active_controller()
89+
await rvr.drive_stop()
9090

9191
# We don't actually need it to stop, so no delay here. We just needed the RC drive command to be inactive
9292

@@ -107,7 +107,7 @@ async def main():
107107

108108
# Stop the robot
109109
print('Stopping...')
110-
await rvr.stop_active_controller()
110+
await rvr.drive_stop()
111111

112112
# Restore initial default control systems
113113
print('Restore initial default control systems')

getting_started/asyncio/driving/control_system_timeouts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def main():
3232
await asyncio.sleep(2)
3333

3434
# Register the handler for the stopped notification
35-
await rvr.on_active_controller_stopped_notify(handler=stopped_handler)
35+
await rvr.on_robot_has_stopped_notify(handler=stopped_handler)
3636

3737
# Reset yaw
3838
await rvr.reset_yaw()

getting_started/asyncio/driving/drive_rc_normalized.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def main():
3232
await asyncio.sleep(2)
3333

3434
# Register the handler for the stopped notification
35-
await rvr.on_active_controller_stopped_notify(handler=stopped_handler)
35+
await rvr.on_robot_has_stopped_notify(handler=stopped_handler)
3636

3737
await rvr.reset_yaw()
3838

@@ -70,7 +70,7 @@ async def main():
7070
print("sending stop command")
7171

7272
# Stop driving, with deceleration rate of 2 m/s^2
73-
await rvr.stop_active_controller(2.0)
73+
await rvr.drive_stop(2.0)
7474

7575
# Delay to allow RVR to stop
7676
await asyncio.sleep(1)

getting_started/asyncio/driving/drive_rc_si_units.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def main():
3131
await asyncio.sleep(2)
3232

3333
# Register the handler for the stopped notification
34-
await rvr.on_active_controller_stopped_notify(handler=stopped_handler)
34+
await rvr.on_robot_has_stopped_notify(handler=stopped_handler)
3535

3636
await rvr.reset_yaw()
3737

@@ -64,7 +64,7 @@ async def main():
6464
print("sending stop command")
6565

6666
# Stop early, with a custom deceleration rate of 2 m/s^2.
67-
await rvr.stop_active_controller(2.0)
67+
await rvr.drive_stop(2.0)
6868

6969
# Restore the default control system timeout to keep things more normal after this.
7070
await rvr.restore_default_control_system_timeout()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 LinearVelocitySlewMethodsEnum
9+
10+
11+
loop = asyncio.get_event_loop()
12+
13+
rvr = SpheroRvrAsync(
14+
dal=SerialAsyncDal(
15+
loop
16+
)
17+
)
18+
19+
# Flag for tracking position move completion
20+
move_completed = False
21+
22+
# Handler for completion of XY position drive moves.
23+
# In this script it is only used for returning to our starting position
24+
async def on_xy_position_drive_result_notify_handler(response):
25+
global move_completed
26+
27+
move_completed = True
28+
print('Move completed, response:', response)
29+
30+
31+
# This wrapper function implements a simple way to return to the start position
32+
async def return_to_start():
33+
global move_completed
34+
35+
await rvr.drive_stop()
36+
37+
# Allow RVR some time to come to a stop
38+
await asyncio.sleep(1.5)
39+
40+
print("Driving to (0,0) at 0 degrees")
41+
42+
# Clear the completion flag
43+
move_completed = False
44+
45+
# Send the drive command, passing the wrapper function parameters into the matching
46+
# parameters in rvr.drive_to_position_normalized (which sends the actual drive command)
47+
await rvr.drive_to_position_normalized(
48+
yaw_angle=0, # 0 degrees is straight ahead, +CCW (Following the right hand rule)
49+
x=0, # Target position X coordinate in meters
50+
y=0, # Target position Y coordinate in meters
51+
linear_speed=64, # Max speed in transit to target. Normalized in the range [0..127]
52+
flags=0, # Option flags (none applied here)
53+
)
54+
55+
# Wait to complete the move. Note: In a real project, a timeout mechanism
56+
# should be here to prevent the script from getting caught in an infinite loop
57+
while not move_completed:
58+
await asyncio.sleep(0)
59+
60+
61+
# This function gives us an easily repeated driving sequence to compare different target slewing configurations
62+
async def drive_demo():
63+
await rvr.drive_with_yaw_si(
64+
linear_velocity=.5,
65+
yaw_angle=0 # Valid yaw values are traditionally [-179..+180], but will continue wrapping outside of that range
66+
)
67+
68+
await asyncio.sleep(1)
69+
70+
await rvr.drive_with_yaw_si(
71+
linear_velocity=1,
72+
yaw_angle=-90 # Valid yaw values are traditionally [-179..+180], but will continue wrapping outside of that range
73+
)
74+
75+
await asyncio.sleep(2)
76+
77+
# Drive back to the starting position using the XY position controller, which also uses yaw target slewing
78+
await return_to_start()
79+
80+
81+
async def main():
82+
""" This program demonstrates RVR's adjustable drive target slewing feature.
83+
84+
Yaw and linear velocity slewing behaviors can be adjusted independently.
85+
86+
Note: Make sure you have plenty of space ahead and to the right of RVR from the starting position.
87+
88+
See control system documentation at sdk.sphero.com for more details
89+
on the meaning of each parameter. This example will demonstrate some example
90+
configurations from the docs.
91+
"""
92+
93+
await rvr.wake()
94+
95+
# Give RVR time to wake up
96+
await asyncio.sleep(2)
97+
98+
await rvr.reset_yaw()
99+
100+
await rvr.reset_locator_x_and_y();
101+
102+
# Register for the async on completion of xy position drive commands
103+
await rvr.on_xy_position_drive_result_notify(handler=on_xy_position_drive_result_notify_handler)
104+
105+
await rvr.restore_default_control_system_timeout()
106+
107+
# Make sure that we're starting with the default slew parameters.
108+
# This can be used at any time to restore the default slewing behavior
109+
await rvr.restore_default_drive_target_slew_parameters()
110+
111+
# Get the default parameters and print them
112+
response = await rvr.get_drive_target_slew_parameters()
113+
print(response)
114+
115+
# Drive with the default slew parameters.
116+
await drive_demo()
117+
118+
# Set RVR to always turn slowly regardless of linear velocity
119+
await rvr.set_drive_target_slew_parameters(
120+
a=0,
121+
b=0,
122+
c=60,
123+
linear_acceleration=.500, # This will set a linear acceleration of .5 m/s^2
124+
linear_velocity_slew_method=LinearVelocitySlewMethodsEnum.constant
125+
)
126+
127+
# Set an extra long timeout to accomodate additional turning time
128+
await rvr.set_custom_control_system_timeout(3000)
129+
130+
# Drive with the updated parameters
131+
await drive_demo()
132+
133+
# Set RVR to turn faster as linear speed increases.
134+
# Also give it some snappier linear acceleration.
135+
await rvr.set_drive_target_slew_parameters(
136+
a=-30,
137+
b=400,
138+
c=100,
139+
linear_acceleration=3, # This will set a linear acceleration of 3 m/s^2
140+
linear_velocity_slew_method=LinearVelocitySlewMethodsEnum.constant
141+
)
142+
143+
# Drive with the updated parameters
144+
await drive_demo()
145+
146+
await rvr.close()
147+
148+
149+
if __name__ == '__main__':
150+
try:
151+
loop.run_until_complete(
152+
main()
153+
)
154+
155+
except KeyboardInterrupt:
156+
print('\nProgram terminated with keyboard interrupt.')
157+
158+
loop.run_until_complete(
159+
rvr.close()
160+
)
161+
162+
finally:
163+
if loop.is_running():
164+
loop.close()

getting_started/asyncio/driving/drive_with_stop_controller.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def main():
6969
await asyncio.sleep(2)
7070

7171
# Register the handler for the stopped notification
72-
await rvr.on_active_controller_stopped_notify(handler=stopped_handler)
72+
await rvr.on_robot_has_stopped_notify(handler=stopped_handler)
7373

7474
# Register the handler for the xy position drive result notification
7575
await rvr.on_xy_position_drive_result_notify(
@@ -101,7 +101,7 @@ async def main():
101101
# This will request a stop with default deceleration.
102102
# Control of the motors is handed off to the stop controller,
103103
# which linearly ramps down the velocity targets of both treads.
104-
await rvr.stop_active_controller()
104+
await rvr.drive_stop()
105105

106106
# Wait until we receive the notification that the robot has stopped.
107107
while not rvr_has_stopped_from_handler:
@@ -130,8 +130,8 @@ async def main():
130130
# This will request a stop with slower than normal deceleration.
131131
# Control of the motors is handed off to the stop controller,
132132
# which linearly ramps down the velocity targets of both treads.
133-
await rvr.stop_active_controller_custom_decel(
134-
deceleration_rate=0.25 # Decelerate both treads toward 0 velocity at 0.25 m/s^2
133+
await rvr.drive_stop_custom_decel(
134+
deceleration_rate=0.5 # Decelerate both treads toward 0 velocity at 0.5 m/s^2
135135
)
136136

137137
# In addition to the notification, it is also possible to poll whether the robot
@@ -167,7 +167,7 @@ async def main():
167167
# This will request a stop with faster than normal deceleration.
168168
# Control of the motors is handed off to the stop controller,
169169
# which linearly ramps down the velocity targets of both treads.
170-
await rvr.stop_active_controller_custom_decel(
170+
await rvr.drive_stop_custom_decel(
171171
deceleration_rate=10 # Decelerate both treads toward 0 velocity at 10 m/s^2
172172
)
173173

getting_started/asyncio/sensor_streaming/multi_sensor_stream.py

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ async def accelerometer_handler(accelerometer_data):
3232
async def ambient_light_handler(ambient_light_data):
3333
print('Ambient light data response: ', ambient_light_data)
3434

35+
async def encoder_handler(encoder_data):
36+
print('Encoder data response: ', encoder_data)
37+
3538

3639
async def main():
3740
""" This program demonstrates how to enable multiple sensors to stream.
@@ -58,6 +61,10 @@ async def main():
5861
service=RvrStreamingServices.ambient_light,
5962
handler=ambient_light_handler
6063
)
64+
await rvr.sensor_control.add_sensor_data_handler(
65+
service=RvrStreamingServices.encoders,
66+
handler=encoder_handler
67+
)
6168

6269
await rvr.sensor_control.start(interval=250)
6370

getting_started/observer/driving/control_system_selection.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def main():
9292

9393
# Stop the robot
9494
print('Stopping...')
95-
rvr.stop_active_controller()
95+
rvr.drive_stop()
9696

9797
# We don't actually need it to stop, so no delay here. We just needed the RC drive command to be inactive
9898

@@ -113,7 +113,7 @@ def main():
113113

114114
# Stop the robot
115115
print('Stopping...')
116-
rvr.stop_active_controller()
116+
rvr.drive_stop()
117117

118118
# Restore initial default control systems
119119
print('Restore initial default control systems')

getting_started/observer/driving/control_system_timeouts.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main():
2424
time.sleep(2)
2525

2626
# Register the handler for the stopped notification
27-
rvr.on_active_controller_stopped_notify(handler=stopped_handler)
27+
rvr.on_robot_has_stopped_notify(handler=stopped_handler)
2828

2929
# Reset yaw
3030
rvr.reset_yaw()

getting_started/observer/driving/drive_rc_normalized.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def main():
2525
time.sleep(2)
2626

2727
# Register the handler for the stopped notification
28-
rvr.on_active_controller_stopped_notify(handler=stopped_handler)
28+
rvr.on_robot_has_stopped_notify(handler=stopped_handler)
2929

3030
rvr.reset_yaw()
3131

@@ -63,7 +63,7 @@ def main():
6363
print("sending stop command")
6464

6565
# Stop driving, with deceleration rate of 2 m/s^2
66-
rvr.stop_active_controller(2.0)
66+
rvr.drive_stop(2.0)
6767

6868
# Delay to allow RVR to stop
6969
time.sleep(1)

getting_started/observer/driving/drive_rc_si_units.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def main():
2525
time.sleep(2)
2626

2727
# Register the handler for the stopped notification
28-
rvr.on_active_controller_stopped_notify(handler=stopped_handler)
28+
rvr.on_robot_has_stopped_notify(handler=stopped_handler)
2929

3030
rvr.reset_yaw()
3131

@@ -58,7 +58,7 @@ def main():
5858
print("sending stop command")
5959

6060
# Stop early, with a custom deceleration rate of 2 m/s^2.
61-
rvr.stop_active_controller(2.0)
61+
rvr.drive_stop(2.0)
6262

6363
# Restore the default control system timeout to keep things more normal after this.
6464
rvr.restore_default_control_system_timeout()

0 commit comments

Comments
 (0)