Skip to content

Commit 710620a

Browse files
authored
Merge pull request #16 from sphero-inc/feature/send-last-command-before-close
Always delay 200 ms before closing the serial port
2 parents 779ad4b + 9da1387 commit 710620a

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

getting_started/asyncio/magnetometer/magnetometer_calibrate_to_north.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,26 @@
1818
# Flag used to indicate that calibration is complete
1919
calibration_completed = False
2020

21+
# Initial yaw offset to magnetic North
22+
yaw_north = 0
23+
2124
# Handler for completion of calibration
2225
async def on_calibration_complete_notify_handler(response):
2326
global calibration_completed
27+
global yaw_north
2428

2529
print('Calibration complete, response:', response)
30+
yaw_north = response['yaw_north_direction']
2631
calibration_completed = True
32+
2733

2834

2935
async def main():
3036
""" This program demonstrates the magnetometer calibration to find north.
3137
"""
3238

3339
global calibration_completed
40+
global yaw_north
3441

3542
await rvr.wake()
3643

@@ -47,7 +54,33 @@ async def main():
4754
# Wait to complete the calibration. Note: In a real project, a timeout mechanism
4855
# should be here to prevent the script from getting caught in an infinite loop
4956
while not calibration_completed:
50-
await asyncio.sleep(0)
57+
await asyncio.sleep(.1)
58+
59+
print('Turning to face north')
60+
61+
# Turn to face north
62+
await rvr.drive_with_yaw_normalized(
63+
yaw_angle=yaw_north,
64+
linear_velocity=0
65+
)
66+
67+
# Leave some time for it to complete the move
68+
await asyncio.sleep(2)
69+
70+
# Reset yaw to zero. After this, zero heading will be magnetic north
71+
await rvr.reset_yaw()
72+
73+
print('Turning to face east')
74+
75+
# You can now drive along compass headings as follows (adjust linear velocity to drive forward)
76+
await rvr.drive_with_heading(
77+
speed = 0, # This is normalized 0-255. 0 will cause RVR to turn in place
78+
heading=90, # This is the compass heading
79+
flags=0 # Just leave this at zero to drive forward
80+
)
81+
82+
# Allow some time for the move to complete before we end our script
83+
await asyncio.sleep(2)
5184

5285
await rvr.close();
5386

getting_started/observer/magentometer/get_magnetometer_reading.py renamed to getting_started/observer/magnetometer/get_magnetometer_reading.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def main():
2323

2424
rvr.get_magnetometer_reading(handler=get_magnetometer_reading_response_handler)
2525

26+
# Keep the script running briefly so we actually get to report the response.
27+
time.sleep(1)
28+
2629
except KeyboardInterrupt:
2730
print('\nProgram terminated with keyboard interrupt.')
2831

getting_started/observer/magentometer/magnetometer_calibrate_to_north.py renamed to getting_started/observer/magnetometer/magnetometer_calibrate_to_north.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# Flag used to indicate that calibration is complete
1111
calibration_completed = False
1212

13+
# Initial yaw offset to magnetic North
14+
yaw_north = 0
1315

1416
# Handler for completion of calibration
1517
def on_calibration_complete_notify_handler(response):
@@ -20,11 +22,16 @@ def on_calibration_complete_notify_handler(response):
2022

2123

2224
def main():
23-
""" This program demonstrates the magnetometer calibration to find north.
25+
""" This program demonstrates the following:
26+
1. Run magnetometer calibration to find the yaw angle of magnetic north
27+
2. Turn to face magnetic north
28+
3. Reset the yaw angle to zero at north
29+
4. Turn to face a compass heading
2430
"""
2531

2632
try:
2733
global calibration_completed
34+
global yaw_north
2835

2936
rvr.wake()
3037

@@ -43,6 +50,32 @@ def main():
4350
while not calibration_completed:
4451
time.sleep(0)
4552

53+
print('Turning to face north')
54+
55+
# Turn to face north
56+
rvr.drive_with_yaw_normalized(
57+
yaw_angle=yaw_north, # This is the target yaw angle, which RVR will turn to face.
58+
linear_velocity=0, # This is normalized 0-255. 0 will cause RVR to turn in place
59+
)
60+
61+
# Leave some time for it to complete the move
62+
time.sleep(2)
63+
64+
# Reset yaw to zero. After this, zero heading will be magnetic north
65+
rvr.reset_yaw()
66+
67+
print('Turning to face east')
68+
69+
# You can now drive along compass headings as follows (adjust linear velocity to drive forward)
70+
rvr.drive_with_heading(
71+
speed = 0, # This is normalized 0-255. 0 will cause RVR to turn in place
72+
heading=90, # This is the compass heading
73+
flags=0 # Just leave this at zero to drive forward
74+
)
75+
76+
# Allow some time for the move to complete before we end our script
77+
time.sleep(2)
78+
4679
except KeyboardInterrupt:
4780
print('\nProgram terminated with keyboard interrupt.')
4881

sphero_sdk/asyncio/client/toys/sphero_rvr_async.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def request_error_responses_only(self, is_enabled):
6363
async def close(self):
6464
if len(self._sensor_control.enabled_sensors) > 0:
6565
await self._sensor_control.clear()
66-
await asyncio.sleep(.2)
66+
67+
await asyncio.sleep(.2)
6768

6869
await self._dal.close()
6970

sphero_sdk/observer/client/toys/sphero_rvr_observer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def request_error_responses_only(self, is_enabled):
6464
def close(self):
6565
if len(self._sensor_control.enabled_sensors) > 0:
6666
self._sensor_control.clear()
67-
time.sleep(.2)
68-
67+
68+
time.sleep(.2)
6969
self._dal.close()
7070

7171
def echo(self, data, handler, target, timeout=None):

0 commit comments

Comments
 (0)