Matrix manipulation modules (umatrix, ulinalg)

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
jlawson
Posts: 8
Joined: Sat Jun 07, 2014 7:12 pm

Matrix manipulation modules (umatrix, ulinalg)

Post by jlawson » Sun Nov 22, 2015 11:25 pm

Matrix representation and manipulation module.

https://github.com/jalawson/ulinalg

Provides for "Numpy-like" 2D-array manipulation.

Jamie

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: matrix module added

Post by dhylands » Sun Nov 22, 2015 11:57 pm

Very Nice.

I just finished an online course called "Introduction to Robotics" https://moocs.qut.edu.au/learn/introduc ... ugust-2015 which was mostly about forward and inverse kinematics for robot arms.
I did the course project using some bioloid servos driven by MATLAB:
https://www.youtube.com/watch?v=tZNaBf91Quk
https://www.youtube.com/watch?v=Npi_SXHF_LM
https://www.youtube.com/watch?v=DsrhCSLmoww

and one of my goals is redo the same stuff in MicroPython. I'm currently working on the bioloid servo stuff. So the matrix stuff will definitely come in handy.

jlawson
Posts: 8
Joined: Sat Jun 07, 2014 7:12 pm

Re: matrix module added

Post by jlawson » Mon Nov 23, 2015 4:46 am

I hadn't heard of that course. I actually started thinking about this code as I was taking the following course:

https://www.edx.org/course/autonomous-n ... autonavx-0

They offered quite a neat little web based simulator to test your Python code on.
This course was also very good:

https://www.coursera.org/course/conrob

I don't have a flying robot of any sort but I do have a few wheeled robots behind me waiting for the controls to be finished.
Something like your arm mechanism in the videos seems a good way to get a feel for control algorithms.

Those are some serious looking servos.

Jamie

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: matrix module added

Post by dhylands » Mon Nov 23, 2015 5:10 am

Hi Jamie,

I'm also taking a followon course, called Robotic Vision (I'm about half way through that one).

I'll definitely take a look at those courses you mentioned.

The bioloid servos are pretty cool, and they have some big brothers as well.
http://www.trossenrobotics.com/robot-servos
The smallest ones are the AX series and they have some bigger ones as well. They are a bit pricey (about $45 for the AX-12A). I see thay they have some smaller ones now too (the XL-320 for $22)

The really cool thing about them is that they're intelligent. You communicate with them using commands sent over a 1 Mbit/sec half-duplex serial bus.

You can control, position, speed, torque, compliance and query a ton of information from the servo (temperature, position, current speed, etc). Because they use a half-duplex protocol, you can daisy chain many servos on a single 3-wire bus (power, ground, and signal).

I've written a bunch of code for the AVRs so that you can create your own bioloid bus devices and I plan on adding that capability to MicroPython as well.

jlawson
Posts: 8
Joined: Sat Jun 07, 2014 7:12 pm

Re: matrix module added

Post by jlawson » Tue Nov 24, 2015 4:14 am

Hi Dave,

Took a look at the servos. Thanks. I've stayed away from servos in the past as they did not appear robust enough for the platforms I was building (for drive motors) but these look good. I'd like to get one of the big ones just to see what it can do.
I currently do something similar by combining the motor, drive electronics and encoder with a PIC and talk to them via I2C bus.

I'd like to change at least one system over to something better and using MicroPython makes all this much easier to play with.

Jamie

User avatar
workless
Posts: 13
Joined: Sat Mar 24, 2018 7:03 pm
Location: Vancouver, BC

Re: Matrix manipulation modules (umatrix, ulinalg)

Post by workless » Sat Mar 24, 2018 8:37 pm

As far as I know this is the only attempt to add matrix manipulation capabilities to Micropython.

What would it take to convert the key parts of this module to use Micropython arrays and assembler for the inner loops? (Not sure I have the skills or time but willing to help if someone more knowledgable can advise on whether this is a good idea and how to best do it).

User avatar
workless
Posts: 13
Joined: Sat Mar 24, 2018 7:03 pm
Location: Vancouver, BC

Re: Matrix manipulation modules (umatrix, ulinalg)

Post by workless » Sun Mar 25, 2018 5:58 am

workless wrote:
Sat Mar 24, 2018 8:37 pm
What would it take to convert the key parts of this module to use Micropython arrays and assembler for the inner loops?
So, I had a go at writing some functions in assembler to do some basic array computations (add, subtract, negative, multiply, divide, etc.). Take a look and feel free to give me some advice/correct any mistakes you notice (the last time I ever did any assembly language was in 1982 on a BBC Micro...):

https://github.com/billtubbs/array_funcs

@jlawson, do you think these functions (or something similar) could be built into your matrix manipulation module to speed it up?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Matrix manipulation modules (umatrix, ulinalg)

Post by pythoncoder » Sun Mar 25, 2018 10:48 am

workless wrote:
Sun Mar 25, 2018 5:58 am
...@jlawson, do you think these functions (or something similar) could be built into your matrix manipulation module to speed it up?
The assembler code looks good to me. One obvious issue here is that your solution is, of necessity, based on arrays while that of @jlawson uses a list. Changing that may be tougher than it looks. One issue that comes to mind is that he supports matrices of type complex. The array module does not (alas). There may be other reptiles lurking below the surface...

More fundamentally yours is a high performance solution while his aims for generality - it's based on NumPy which is not targeted at realtime applications. Both are laudable aims but they aren't necessarily compatible. A 350ms time to invert a 3x3 matrix is going to be problematic if you're programming a robot.

If it were my project I'd be tempted to write a performance-oriented matrix class providing only those functions and methods used in typical realtime applications and discarding functionality which can't readily be adapted (e.g. complex support). I think support for 2D matrices is probably essential.

Views on vital functionality from potential users would be good.
Peter Hinch
Index to my micropython libraries.

User avatar
workless
Posts: 13
Joined: Sat Mar 24, 2018 7:03 pm
Location: Vancouver, BC

Re: Matrix manipulation modules (umatrix, ulinalg)

Post by workless » Mon Mar 26, 2018 10:07 pm

Peter, thanks for your comments. I agree, implementing arrays into the existing module might be a lot of work and put some constraints on the data types and functionality. Maybe it's best to keep this 'high performance' array functionality as a separate project. It would still take a lot more work though so I think we need to have more discussion about what this would be used for, key functionality needed and what else is out there or going on in this area before we spend too much time on it.

As well as:
- 2D arrays (matrices) and associated methods (shape, reshape, ravel, etc.)

I would add:
- Building vectorized versions of the main math functions (sin, cos, tan, ...etc, pow, exp, log, sum, max, min, mean, std, random)
- Selected linear algebra operations (e.g. dot, det, norm, inv)
- Other data types (double, bool, byte/char)
- Memory views into arrays
- Potentially n-d arrays
- ...

Constraining it to a bare minimum that is compatible with the MicroPython concept might be the hardest part here. I.e. what's the MicroPython equivalent of numpy.ndarray?

Other users please comment.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Matrix manipulation modules (umatrix, ulinalg)

Post by OutoftheBOTS_ » Tue Mar 27, 2018 8:10 am

I like building robots. I am going to be porting all my robots across from RPi to ESP32 running Micropython. I am right at the moment poritng across a quadruped with 3 joints per leg. Coordination of the joints of all the legs requires lots of maths (especially lots of trig fuctions) and it is very easy to bog down.

I do also remember reading on another thread that MicroPythpon maths isn't optimized for speed but rather optimized for low memory usage. I am using the ESP32 with the extended psRAM so it isn't as constrained by memory like all the other hardware that MicroPython runs on atm

I am still a learner so contributing to the development of this project is over my head but I am keen to follow it and learn as much as possible.

Post Reply