|
| 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() |
0 commit comments