Skip to content

Commit 3a302f5

Browse files
committed
Update asynchronous code and docs for uasyncio V3
1 parent fe72a68 commit 3a302f5

7 files changed

+79
-67
lines changed

Diff for: README.md

+31-27
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ are to use frozen bytecode and to periodically run a garbage collection; the
3939
latter is advisable even on the Pyboard. See the `fusionlcd.py` test program
4040
for an example of this.
4141

42+
## MicroPython firmware dependency
43+
44+
Some modules in this library use asynchronous programming. This uses the
45+
`asyncio` library under CPython, `uasyncio` under MicroPython. The MicroPython
46+
version is much improved after a complete rewrite and is at version 3.0. It is
47+
syntax compatible with CPython 3.8 `asyncio`. All code has been updated to use
48+
this syntax, which is unsupported by older versions.
49+
50+
To run asynchronous modules MicroPython targets should use a daily build of
51+
firmware, or a release build after V1.12: such firmware incorporates `uasyncio`
52+
V3.
53+
54+
Where asynchronous code is run under CPython, this must be V3.8 or later.
55+
4256
## Terminology and units of measurement
4357

4458
I should point out that I'm unfamiliar with aircraft conventions and would
@@ -78,32 +92,19 @@ The issue of the orientation of the sensor is discussed in
7892

7993
# Contents
8094

81-
1. [Modules](./README.md#1-modules)
82-
83-
2. [Fusion module](./README.md#2-fusion-module)
84-
85-
2.1 [Fusion class](./README.md#21-fusion-class)
86-
87-
2.1.1 [Methods](./README.md#211-methods)
88-
89-
2.1.2 [Bound variables](./README.md#212-bound-variables)
90-
91-
3. [Asynchronous version](./README.md#3-asynchronous-version)
92-
93-
3.1 [Fusion class](./README.md#31-fusion-class)
94-
95-
3.1.1 [Methods](./README.md#311-methods)
96-
97-
3.1.2 [Variables](./README.md#312-variables)
98-
99-
4. [Notes for constructors](./README.md#4-notes-for-constructors)
100-
101-
5. [Background notes](./README.md#5-background-notes)
102-
103-
5.1 [Heading Pitch and Roll](./README.md#51-heading-pitch-and-roll)
104-
105-
5.2 [Beta](./README.md#52-beta)
106-
95+
1. [Modules](./README.md#1-modules)
96+
2. [Fusion module](./README.md#2-fusion-module)
97+
2.1 [Fusion class](./README.md#21-fusion-class)
98+
2.1.1 [Methods](./README.md#211-methods)
99+
2.1.2 [Bound variables](./README.md#212-bound-variables)
100+
3. [Asynchronous version](./README.md#3-asynchronous-version)
101+
3.1 [Fusion class](./README.md#31-fusion-class)
102+
3.1.1 [Methods](./README.md#311-methods)
103+
3.1.2 [Variables](./README.md#312-variables)
104+
4. [Notes for constructors](./README.md#4-notes-for-constructors)
105+
5. [Background notes](./README.md#5-background-notes)
106+
5.1 [Heading Pitch and Roll](./README.md#51-heading-pitch-and-roll)
107+
5.2 [Beta](./README.md#52-beta)
107108
6. [References](./README.md#6-references)
108109

109110
# 1. Modules
@@ -124,6 +125,9 @@ Test/demo programs:
124125
5. `fusionlcd.py` Tests the async library with a Hitachi HD44780 2-row LCD
125126
text display to continuously display angle values.
126127

128+
If using InvenSense MPU9150, MPU6050 or MPU9250 IMU's, drivers may be found
129+
[here](https://github.com/micropython-IMU/micropython-mpu9x50).
130+
127131
The directory `remote` contains files and information specific to
128132
[remote mode](./remote/README.md) and to running fusion on standard Python.
129133

@@ -372,4 +376,4 @@ this is the free parameter in the Madgwick filtering and fusion scheme.
372376
[Original Madgwick study](http://sharenet-wii-motion-trac.googlecode.com/files/An_efficient_orientation_filter_for_inertial_and_inertialmagnetic_sensor_arrays.pdf)
373377
[Euler and Tait Bryan angles](http://en.wikipedia.org/wiki/Euler_angles#Tait.E2.80.93Bryan_angles)
374378
[Quaternions to Euler angles](http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles)
375-
[Beta](https://github.com/kriswiner/MPU-9250.git)
379+
[Beta](https://github.com/kriswiner/MPU-9250.git)

Diff for: fusion_async.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# fusion_async.py Asynchronous sensor fusion for micropython targets.
22
# Ported to MicroPython by Peter Hinch, May 2017.
3-
# Released under the MIT License (MIT)
4-
# Copyright (c) 2017, 2018 Peter Hinch
3+
# Released under the MIT License (MIT) See LICENSE
4+
# Copyright (c) 2017-2020 Peter Hinch
55

6+
# Requires:
7+
# uasyncio V3 (Included in daily builds and release builds later than V1.12).
68
# Uses the uasyncio library to enable updating to run as a background coroutine.
79

810
# Supports 6 and 9 degrees of freedom sensors. Tested with InvenSense MPU-9150 9DOF sensor.
@@ -53,11 +55,10 @@ async def calibrate(self, stopfunc):
5355

5456
async def start(self, slow_platform=False):
5557
data = await self.read_coro()
56-
loop = asyncio.get_event_loop()
5758
if len(data) == 2 or (self.expect_ts and len(data) == 3):
58-
loop.create_task(self._update_nomag(slow_platform))
59+
asyncio.create_task(self._update_nomag(slow_platform))
5960
else:
60-
loop.create_task(self._update_mag(slow_platform))
61+
asyncio.create_task(self._update_mag(slow_platform))
6162

6263
async def _update_nomag(self, slow_platform):
6364
while True:

Diff for: fusionlcd.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# fusionlcd.py Test for asynchronous sensor fusion on Pyboard. Uses LCD display and uasyncio.
22
# Author: Peter Hinch
3-
# Released under the MIT License (MIT)
4-
# Copyright (c) 2017 Peter Hinch
5-
# V0.8 16th May 2017 Adapted for uasyncio
6-
# V0.7 25th June 2015 Adapted for new MPU9x50 interface
3+
# Released under the MIT License (MIT) See LICENSE
4+
# Copyright (c) 2017-2020 Peter Hinch
75

86
# Requires:
9-
# uasyncio (official or modified version)
7+
# uasyncio V3 (Included in daily builds and release builds later than V1.12).
108
# MPU9150 on X position
119
# Normally open pushbutton connected between pin Y7 and ground
12-
# LCD driver alcd.py from https://github.com/peterhinch/micropython-async.git
10+
# LCD driver alcd.py uasyncio V3 version from
11+
# https://github.com/peterhinch/micropython-async/blob/master/v3/as_drivers/hd44780/alcd.py
12+
# From https://github.com/micropython-IMU/micropython-mpu9x50:
13+
# imu.py, mpu9150.py, vector3d.py
14+
# From this repo: deltat.py fusion_async.py
15+
1316
# Hitachi HD44780 2 row LCD display wired using 4 bit data bus as follows:
1417

1518
# Name LCD connector Board

Diff for: fusiontest_as.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# fusiontest_as.py Test for asynchronous sensor fusion on Pyboard.
22
# Author: Peter Hinch
3-
# Released under the MIT License (MIT)
4-
# Copyright (c) 2017 Peter Hinch
3+
# Released under the MIT License (MIT) See LICENSE
4+
# Copyright (c) 2017-2020 Peter Hinch
55

66
# Requires:
7-
# uasyncio (official or modified version)
7+
# uasyncio V3 (Included in daily builds and release builds later than V1.12).
8+
# From https://github.com/micropython-IMU/micropython-mpu9x50:
9+
# imu.py, mpu9150.py, vector3d.py
10+
# From this repo: deltat.py fusion_async.py
11+
812
# MPU9150 on X position
913
# Normally open pushbutton connected between pin Y7 and ground
1014

@@ -45,11 +49,8 @@ async def test_task():
4549
await fuse.calibrate(lambda : not switch.value())
4650
print('Mag bias vector: ', fuse.magbias)
4751
await fuse.start() # Start the update task
48-
loop = asyncio.get_event_loop()
49-
loop.create_task(display())
52+
await display()
5053

5154

52-
loop = asyncio.get_event_loop()
53-
loop.create_task(mem_manage())
54-
loop.create_task(test_task())
55-
loop.run_forever()
55+
asyncio.create_task(mem_manage())
56+
asyncio.run(test_task())

Diff for: fusiontest_as6.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# fusiontest_as6.py Test for 6DOF asynchronous sensor fusion on Pyboard.
22
# Author: Peter Hinch
3-
# Released under the MIT License (MIT)
4-
# Copyright (c) 2017 Peter Hinch
3+
# Released under the MIT License (MIT) See LICENSE
4+
# Copyright (c) 2017-2020 Peter Hinch
55

66
# Requires:
7-
# uasyncio (official or modified version)
7+
# uasyncio V3 (Included in daily builds and release builds later than V1.12).
8+
# From https://github.com/micropython-IMU/micropython-mpu9x50:
9+
# imu.py, mpu6050.py, vector3d.py
10+
# From this repo: deltat.py fusion_async.py
811
# MPU9150 on X position
912

1013
from machine import Pin
@@ -38,10 +41,8 @@ async def display():
3841
async def test_task():
3942
await fuse.start() # Start the update task
4043
loop = asyncio.get_event_loop()
41-
loop.create_task(display())
44+
await display()
4245

4346

44-
loop = asyncio.get_event_loop()
45-
loop.create_task(mem_manage())
46-
loop.create_task(test_task())
47-
loop.run_forever()
47+
asyncio.create_task(mem_manage())
48+
asyncio.run(test_task())

Diff for: remote/capture.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# capture.py Data capture for remote operation. Uses LCD display and uasyncio.
22
# Author: Peter Hinch
3-
# Released under the MIT License (MIT)
4-
# Copyright (c) 2018 Peter Hinch
3+
# Released under the MIT License (MIT) See LICENSE
4+
# Copyright (c) 2017-2020 Peter Hinch
55

66
# Requires:
7-
# uasyncio (official or modified version)
7+
# uasyncio V3 (Included in daily builds and release builds later than V1.12).
88
# MPU9150 on X position
99
# Normally open pushbutton connected between pin Y7 and ground
10-
# LCD driver alcd.py from https://github.com/peterhinch/micropython-async.git
10+
# LCD driver alcd.py uasyncio V3 version from
11+
# https://github.com/peterhinch/micropython-async/blob/master/v3/as_drivers/hd44780/alcd.py
1112
# Hitachi HD44780 2 row LCD display wired using 4 bit data bus as follows:
1213

1314
# Name LCD connector Board
@@ -71,6 +72,5 @@ async def lcd_task():
7172
await fuse.start() # Start the update task
7273
await display()
7374

74-
loop = asyncio.get_event_loop()
75-
loop.create_task(mem_manage())
76-
loop.run_until_complete(lcd_task())
75+
asyncio.create_task(mem_manage())
76+
asyncio.run(lcd_task())

Diff for: remote/fusion_r_asyn.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# fusion_r_asyn.py Test for sensor fusion remote device
22
# simulated by captured data mpudata
33
# Author Peter Hinch
4-
# Released under the MIT License (MIT)
5-
# Copyright (c) 2018 Peter Hinch
6-
# Run under MicroPython on Unix or other target, or CPython 3.5 or later
4+
# Released under the MIT License (MIT) See LICENSE
5+
# Copyright (c) 2017-2020 Peter Hinch
6+
7+
# Requires:
8+
# uasyncio V3 (Included in daily builds and release builds later than V1.12).
9+
# Run under MicroPython on Unix or other target, or CPython 3.8 or later
710

811
try:
912
import utime as time
@@ -96,5 +99,4 @@ async def main_task():
9699
await fuse.start() # Start the update task
97100
await display()
98101

99-
loop = asyncio.get_event_loop()
100-
loop.run_until_complete(main_task())
102+
asyncio.run(main_task())

0 commit comments

Comments
 (0)