Latest / Talk Python To Me / #17: Python on bare metal with MicroPython
Transcript
- 0:00How many layers of abstraction and indirection are between your Python code and machine instructions?
- 0:05What if that number could be one and Python itself was the operating system?
- 0:08That would be so amazing, right?
- 0:10In fact, it is amazing, and it's called MicroPython.
- 0:13This is show number 17.
- 0:15It's MicroPython with guest Damian George.
- 0:18You're listening to Talk Python To Me, and this was recorded on Thursday, June 25, 2015.
- 0:24I'm a developer in many senses of the word, because I make these applications, but I also use these verbs to make this music.
- 0:35I construct it line by line, just like when I'm coding another software design.
- 0:39In both cases, it's about design patterns.
- 0:42Anyone can get the job done.
- 0:44It's the execution that matters.
- 0:45I have many interests.
- 0:47Sometimes it can flit.
- 0:48My creativity can usually be...
- 0:50Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 0:57This is your host, Michael Kennedy.
- 0:59Follow me on Twitter, where I'm @mkennedy.
- 1:01Keep up with the show and listen to past episodes at talkpythontome.com.
- 1:06And follow the show on Twitter, where we're at Talk Python.
- 1:09This show will be talking to Damian George about rethinking the Python runtime for embedded microcontrollers and his implementation, MicroPython.
- 1:18This episode is brought to you by CodeShip and Hired.
- 1:21Thank them for supporting the show on Twitter via at CodeShip and at Hired underscore HQ.
- 1:27Before we get to the interview with Damian, I'd like to give a shout out to Ganesh Kupan.
- 1:32Ganesh suggested this topic for the show, and I think it's a great one.
- 1:37Ganesh, thank you for sending that in and making this happen.
- 1:40Now, let me introduce Damian.
- 1:43Damian George is a theoretical physicist at the University of Cambridge and the creator of MicroPython.
- 1:48Damian, welcome to the show.
- 1:50Hi, Michael.
- 1:52Thanks.
- 1:52Yeah, this is really great.
- 1:53We've talked a lot about software, and we've even talked a little bit about robots on the show before.
- 1:58And now we're going to talk about sort of the most low-level stuff you can do with Python that I know of.
- 2:05We're going to talk about running Python directly on microchips and microcontrollers, right?
- 2:10Yes, that's right.
- 2:12Yeah.
- 2:12Your project, MicroPython, at micropython.org is really cool.
- 2:16Thank you.
- 2:17Yeah, you're welcome.
- 2:18And some of the listeners said, hey, you really need to have Damian on your show and talk about this stuff because what you guys are doing, it's really amazing.
- 2:24And so we're definitely going to do that.
- 2:26We're going to talk all about that.
- 2:28But before we do, maybe you could tell me a bit about your background, how you got into programming, how you got into Python, that kind of thing.
- 2:33Okay.
- 2:33Well, I mean, I've always liked computers and using computers and programming since I can remember.
- 2:41I know that I've written many languages, toy languages, ever since I could start programming.
- 2:47It's always been sort of a passion.
- 2:50I think anyone who gets into programming at some stage tries writing their own language.
- 2:55That's why there's so many of them.
- 2:57But it's a really great way to understand how a computer works is by inventing your own language or at least implementing something that already exists.
- 3:07That's sort of something that I did in my spare time.
- 3:11But actually, formally, I also did a computer engineering degree at university, which was based around microcontrollers and designing computers and designing instruction sets and writing operating systems and those kinds of low-level things.
- 3:30So that was really good to get an actual formal training so that you're forced to be exposed to all the things that maybe you didn't want to study.
- 3:40Absolutely.
- 3:41Was that mostly in C or C++?
- 3:43And what languages and stuff did you use in that field?
- 3:47We learned a lot of languages like Haskell and Java and C and C++.
- 3:53So, yeah, but I mean, it was a bit about learning languages, but also you learn algorithms like hash tables and things like that.
- 4:02And then, you know, you learn, I mean, you learn how to write a compiler.
- 4:07And it's not so much about the language.
- 4:09It's about the concepts and the algorithms.
- 4:12Right, like the abstract syntax trees and things like this, yeah?
- 4:15Yeah, that's right.
- 4:17That's right.
- 4:17So, I mean, this is what I studied.
- 4:20I actually also studied theoretical physics and pure math.
- 4:25So, I did a lot of extra stuff.
- 4:29And all these things, I think, sort of tie in, in the end, to the same kind of way of thinking, is that you think very methodically and you try and solve problems.
- 4:40And you use computers a lot in maths and physics to solve problems because they're so difficult nowadays that you can't just do it with a pen and paper.
- 4:49Yeah, I think you're totally right.
- 4:50My background is I was working on my PhD in math as well, and I got into computers to trying to do research with math.
- 4:57And, like, you're going to have to learn to program this thing.
- 4:59And it turned out I liked computers better, so I never did get a PhD.
- 5:03But I totally know where you're coming from.
- 5:05That's awesome.
- 5:05In physics, if you're an experimental physicist, a lot of the time you end up doing just a lot of programming, like Monte Carlo simulations and data analysis and things like that.
- 5:16And, I mean, yeah, if you like the kind of stuff, it's really good.
- 5:20But if you don't, you know, maybe if you want to build an actual apparatus, then you probably need to be an engineer.
- 5:27I think, yeah, my background is all these things combined.
- 5:31And, yeah, I just really like the technical side of things.
- 5:35That's cool.
- 5:37Both programming and hardware.
- 5:38Yeah, so how did you get into Python?
- 5:40You started out in this sort of what to me feels more like, you know, the majority of the work is done in C, C++, Fortran, that kind of space with physics and operating systems and so on.
- 5:51And then somehow you ended up in Python, right?
- 5:53Yeah, I mean, I think it's good if you know more than one language or more than two or more than five languages.
- 6:01And Python is such a popular language and for a good reason.
- 6:04It's a very nice language.
- 6:07And so, yeah, it's just one of the ones that you will, I think, eventually learn if you're into programming.
- 6:13And, yeah, I mean, yeah, I just, I guess just stumbled across it like all the other languages that people learn, like C and C++.
- 6:25Because, yeah, I mean, C is a great language and I love writing code in C.
- 6:31I mean, I know it's a very old language, but it allows you to be really close to the hardware.
- 6:36I mean, a line of C translates to a few lines of assembly and you sort of know what's going on.
- 6:42And, yeah, that means you have to write more code to do the same sort of thing as you would in a higher level language.
- 6:47But it gives you more power and when you need it, speed.
- 6:51But then, of course, you want something like Python to say when you don't, you're not so worried about the efficiency and you're more worried about writing something quickly and expressing your idea in a very quick way and sort of a very clean and clear way.
- 7:09So a Python program is, you know, you think it and then you sort of put your thoughts on it in code and it's sort of the same thing.
- 7:18Yeah, that's a lovely thing about Python, isn't it?
- 7:21Yeah, yeah, exactly.
- 7:22You just, you know, you don't have to allocate lists and make links in the list.
- 7:27You know, it just works having a list or it just works having a hash table, a dictionary.
- 7:31And, you know, you have lambdas and you have list comprehension and you have generators.
- 7:36And these things allow you to think in a certain way.
- 7:40And if you're writing in C, you don't think about generators and you don't think about sort of closures because they're really hard.
- 7:48But they're really good concepts.
- 7:49And a language that allows you to express those concepts easily is a good language.
- 7:55I agree.
- 7:56And when I think of, you know, microcontrollers and embedded devices, I typically think of people using C because they want to squeeze that extra 5% of energy or performance or, you know, those are more or less the same thing in some sense.
- 8:10Right.
- 8:11You've taken the opposite perspective.
- 8:13You've made Python suitable for microcontrollers rather than saying we're going to use a language that already sort of matched to the metal.
- 8:21That's really awesome.
- 8:21So you call this project MicroPython, right?
- 8:24Yeah, that's right.
- 8:25MicroPython.
- 8:26I think the name says it all.
- 8:29That's awesome.
- 8:31If you want Python on a microcontroller, then that's what you need.
- 8:36I did it as a sort of, to begin with, it was more of a can it be done question.
- 8:41Because, you know, I just, it's technically interesting to try and put a language as big and as high level and as dynamic and as memory hungry as Python on a really small microcontroller, which has very, very, very little memory.
- 8:58So that was kind of what started it for me was just the technical interest.
- 9:03But at the same time, I also thought, you know, why not pick another language or why not just, I mean, C is already on microcontrollers and stuff, but why even choose a high level language to start with?
- 9:16And my reason is that because microcontrollers now are a little, you know, are more powerful than they were a few years ago when they had almost no memory at all, you know, like 100 bytes or something.
- 9:28Now they have 100 kilobytes.
- 9:31And that's starting to be interesting.
- 9:35They also have really sophisticated peripherals.
- 9:38So you buy, you know, a $1 chip and it has all these UART and SPI and I2C and ADCs and DACs and all these peripherals that do great things and are very, very powerful.
- 9:49It has DMA to do quick transfers and this kind of stuff.
- 9:54When you start writing it in C to control all of these peripherals, it gets really, really hard really quickly because you've got to, you've got so many configuration options.
- 10:04You've got so many things to think about, interrupts and interrupt levels and it becomes very quickly sort of, I mean, it can become a bit of a mess to get something to run quickly and easily.
- 10:18So if you just want to flash an LED and read some sensor, you don't need to sort of write really efficient code that you would need to or C would allow you to.
- 10:31So to do a simple thing, but a low level simple thing, like turn a light on, I mean, what you're doing is putting a voltage, five volts or three volts on a pin.
- 10:44It's a pretty basic operation.
- 10:46It's something that you can do in Python quite efficiently.
- 10:50And then once you, you can do, you know, turn up an LED on with a Python command, you can think about having a list or a dictionary of pins and then you can iterate over them or you can pass them along as objects.
- 11:03So you can start to really abstract the low level hardware.
- 11:09And it's still relatively efficient because, well, the way MicroPython is implemented, you still have running relatively efficiently on the microcontroller.
- 11:18You know, I've seen your API and it looks super clean.
- 11:22I saw your Kickstarter video and the one that you have on micropython.org is sort of the opener video to introduce people.
- 11:28That's really cool.
- 11:29Maybe let's take a step back and just real quickly tell people what MicroPython is and tell them about your board and how it came into existence.
- 11:36Sure.
- 11:37Well, so MicroPython is a rewrite, a complete re-implementation from scratch of Python sort of version three, as it were, 3.4, actually.
- 11:51So complete rewrite, no components whatsoever that are in common with CPython.
- 11:59And the aim in rewriting it was to make sure that every operation was done as efficiently and used as little and, if possible, no RAM at all.
- 12:10So that was sort of the guiding principle is use as little RAM as possible.
- 12:15So MicroPython, the software, is this re-implementation of Python.
- 12:21And it includes all of your favorites, lists and dictionaries and sets.
- 12:26And, I mean, it's complete Python 3.4 in terms of the language specification.
- 12:31Probably not the same standard library, though.
- 12:34Probably things like dbapi2 is not there or something like that.
- 12:37Right, yeah.
- 12:38So there's the language in terms of the syntax and the semantics, you know, classes and function definitions.
- 12:44And then there's the library.
- 12:46So import numpy or whatever.
- 12:48Import this, import that.
- 12:51Python famously comes with batteries included with all of the standard library and everything you need, you know, statistics to compute the mean of something and things like that.
- 13:02And these things, some of them are not sensible to run on a microcontroller and some of them won't fit on a microcontroller.
- 13:10And so not all, well, not that many of the standard libraries are available.
- 13:16But the ones that are useful are there, like JSON string parsing, for example, and regular expressions are there in a sort of cut down form.
- 13:25And, I mean, we're always looking to add more.
- 13:27It's just that the manpower to write all of these things in a micro way is not that easy.
- 13:33Do you have to re-implement every standard library package or module that you're using?
- 13:39Some of them we have, for example, JSON printing and parsing is rewritten because you want to make it efficient.
- 13:48But other things actually import directly.
- 13:52We can just copy over the CPython standard library and use that code directly.
- 13:58So, yeah, it depends what you want to do.
- 14:01I mean, the idea really is to provide micropython as the core language.
- 14:06And then if people need some extra feature, they can copy it from normal CPython and just see if it works and it should work.
- 14:15And, you know, if not, maybe it needs a few little tweaks here and there.
- 14:21But, yeah, we concentrate mostly on the core language and providing a very robust and hopefully efficient core language.
- 14:33So that's, but that's the software side of things.
- 14:36Then there's the hardware side of things, which is, so the Pyboard, which is this little microcontroller that runs micropython.
- 14:45And all the peripherals on this Pyboard are well supported, like the I2C bus or the UART, for example.
- 14:53And the aim with this was, well, to be able to sort of kickstart this whole idea.
- 15:00We went to Kickstarter.
- 15:01Yeah.
- 15:03Why don't you tell everybody a little bit about your Kickstarter?
- 15:05It was super, super successful.
- 15:07I saw you originally were going out for 15,000 pounds.
- 15:10Yes.
- 15:11And you raised 97,000 pounds or 153,000 US dollars.
- 15:16That's, you must have been super thrilled.
- 15:18Yeah.
- 15:19Well, it was, it was a great reception.
- 15:21We had a lot of people get on board.
- 15:24And yeah, I mean, it was, it was really good.
- 15:27I didn't expect it to be that big.
- 15:29I mean, as I said, it started out as a sort of technical interest to be like, you know, it'd be really great to be able to see if this would fit.
- 15:36And then, you know, getting it running, making a little board.
- 15:40So the idea with the board was that I could give something to the Kickstarter backers.
- 15:45If they back, you know, it's nice to actually have something physical as a backer instead of a piece of free software, which is going to be free for everyone.
- 15:53So the hardware was a way to sort of, to give an incentive to say, look, there's actually something physical it can run on and you can do cool stuff with it.
- 16:01That's cool.
- 16:02The work of creating it, was it mostly actually writing the reimplementation of Python or was it, how much of it was hardware work and how much of it was software work?
- 16:11I'd say it was about 50, 50 to begin with.
- 16:15Up until when I launched the campaign, it was about 50, 50.
- 16:19The software, I mean, yeah, I didn't write the entire thing before the Kickstarter campaign started.
- 16:25I wrote enough to be able to convince myself and others that it would actually work.
- 16:29Like you have a REPL and you can turn lights on and off and so on.
- 16:33So there were bits of the language missing at the Kickstarter stage.
- 16:37And probably 50% of the time was spent actually building the hardware.
- 16:42So I had to learn about these microcontrollers and how they work.
- 16:47The hardest thing is getting a tool chain up and running to cross compile stuff and download it and then, you know, debug it.
- 16:52And why doesn't my USB thing work and so on?
- 16:55There's a lot of really technical and difficult things to sort of get your head around when you first get into microcontrollers.
- 17:04And I mean, these days with their 32-bit ARM chips, they're very powerful things.
- 17:09And there's lots of little things that can go wrong.
- 17:14So, yeah, I liked it.
- 17:16It was a good balance of software and hardware.
- 17:18Then after the Kickstarter, when the hardware was finalized, it shifted more into software.
- 17:23And since then, it's been, you know, pretty much purely just software, improving the software both for the core language and also to control the Pyboard in a nice way and support all the features.
- 17:37Yeah, it looks really rich.
- 17:39Maybe you could tell everyone a little bit about the features of the board because when I learned about it, I was surprised at all the things it has.
- 17:45You know, an accelerometer, for example, storage, all sorts of stuff.
- 17:50It seems pretty much like I could take that, put it into something with a power source and use it.
- 17:57Yeah, well, for sure.
- 17:58I mean, the aim is you plug it into USB and it works straight away.
- 18:01I just need to power source and it's got so that the accelerometer is not built into the microcontroller.
- 18:07That's a separate little chip that's on there.
- 18:09But the idea of putting that on was so that people could, you know, have sort of some input to play with straight away instead of buttons or switches or something.
- 18:19An accelerometer is like a lot more fun to play with.
- 18:23On your video, you have a little app you wrote where there's a robot that can lean forwards, backwards, left, right.
- 18:30And as you tilt the board, the robot kind of leans in the way.
- 18:33And you're doing that with an accelerometer and Python.
- 18:37That's right.
- 18:37That was a really cool example.
- 18:38Yeah, well, I mean, that's only, you know, a handful of lines.
- 18:43It's just an infinite loop while true.
- 18:45And then you read the accelerometer, which is like one line in Python.
- 18:50And then you set the servo angle based on that accelerometer reading.
- 18:54So, you know, it really is just a few lines to do that stuff.
- 18:58Okay, cool.
- 18:59So maybe you could walk the listeners through what it's like from going, you know, file new project to having, what was it?
- 19:06A main.py that runs when you turn on the board?
- 19:09Yeah.
- 19:10What's the lifecycle look like there?
- 19:12So there's actually a few different ways of using it.
- 19:16Okay.
- 19:18So the simplest way is you just plug it in to USB and then it becomes a serial port, like a COM port, which you can log into.
- 19:29It depends if you use, you know, Linux or Windows or Mac.
- 19:33But you just use a terminal program and then you can connect to the serial port of the board and you have a Python prompt, a REPL right there.
- 19:43And that runs directly on the board.
- 19:45So you can type commands in straight away and they will be executed and do whatever you ask on the board.
- 19:52So that's a good way to sort of just play around with things, debug things, test things.
- 19:57The other way of programming it is to write your code, your script in a file and call it main.py.
- 20:07And you copy that to the little mini file system that's on the board.
- 20:11And then whenever it turns on, whenever it gets power, it runs that main.py script straight away and just does whatever you have in there.
- 20:20So you could have an infinite loop, you know, reading a sensor and outputting some LEDs or servo angle or something like that.
- 20:26And, of course, it's not just a single file.
- 20:29You've got the full import machinery of Python.
- 20:32So if you also copy some libraries to the little file system on the board, then you can import them as well.
- 20:39So you can do lots and lots of different things.
- 20:42That's cool.
- 20:42And how much storage space do I have?
- 20:44CodeShip is a hosted continuous delivery service focused on speed, security, and customizability.
- 21:03You can set up continuous integration in a matter of seconds and automatically deploy when your tests have passed.
- 21:09CodeShip supports your GitHub and Bitbucket projects.
- 21:12You can get started with CodeShip's free plan today.
- 21:15Should you decide to go with a premium plan, Talk Python listeners can save 20% off any plan for the next three months by using the code TALKPYTHON.
- 21:23All caps, no spaces.
- 21:25Check them out at CodeShip.com and tell them thanks for sponsoring the show on Twitter where they're at CodeShip.
- 21:38It has about just under 200 or 100 kilobytes file system, which is not very big.
- 21:46So you can put a few scripts and maybe log some data.
- 21:50But it does have expansion with an SD card.
- 21:54So you can put a little micro SD card in.
- 21:56For a few dollars, you can buy something that has a gigabyte of storage and then you can run scripts from there.
- 22:01So you can put everything there and log lots of data, do whatever you like.
- 22:06So it's expandable in that sense.
- 22:10Yeah, that's excellent.
- 22:11So you should be able to put a lot of code on there for 200K.
- 22:17Yeah, yeah.
- 22:17And yeah, the other, there's actually, well, the third way to use it is you can actually sort of, you can write your script on your PC and then run it remotely by using a small, actually another Python script, which sort of downloads your script onto the board and runs it straight away.
- 22:37So that's a quick way to sort of prototype your code.
- 22:41Yeah, yeah, that's cool.
- 22:42How do you get files on there other than this prototyping way?
- 22:46Can I, does it like show up as a drive when I connect it or?
- 22:50Yeah, it shows up as a USB drive and you just, it's just like a USB stick.
- 22:54That's it.
- 22:57Yeah, cool.
- 22:58Can I set up networking if I want to do a little internet of thing type stuff?
- 23:01Yes.
- 23:03So the Pyboard itself has drivers for a wireless chip.
- 23:09So you have to buy an extra wireless chip and connect it up and then it will work as a, it'll work as sort of a Wi-Fi extension.
- 23:18So you have, you have your normal Python sockets.
- 23:21So you can create a socket and you can, you bind it and connect it to a server.
- 23:26You can either be a server or a client.
- 23:28So you can do that kind of, the same Python stuff you can do on your PC on, on the board.
- 23:34It's a bit restricted.
- 23:36Yeah.
- 23:36Could I run a web server on it?
- 23:37Yes, you can.
- 23:39Yes, you can.
- 23:39It's like probably one of the micro, yeah, like one of the micro frameworks, like a Flask or Pyramid or something like that might be better.
- 23:45Yeah.
- 23:46Flask, even Flask might be a little bit big.
- 23:48I mean, you'd be surprised.
- 23:50Flask is, I mean, these, when you load up a full Python app,
- 23:54there's a lot of stuff that actually gets imported that you don't think about.
- 23:58So in micro Python, which we have to think about all that and, and really strip it back.
- 24:04But the other thing, so there, there's been a few other Kickstarters based around micro Python since mine,
- 24:12a year and a half, yeah, almost two years ago now started.
- 24:18So just recently there was this Wi-Fi Kickstarter, which is actually a Wi-Fi module that runs micro Python.
- 24:29So it's one ship and it has Wi-Fi built into it.
- 24:32So it really is an Internet of Things toy.
- 24:34Well, it's more than a toy.
- 24:35It's a cool Internet of Things device that you, that is like the Pyboard in that you, it runs micro Python.
- 24:44And you can tell net in to get a REPL prompt and you can FTP in to copy your main dot pyscript across.
- 24:52So it's, it's really a proper wireless from the, from the ground micro Python device.
- 24:59And on that you can make a server or a client and you, you can sort of attach a battery, put it behind your sofa.
- 25:05And then there's this little Python wireless thing running behind your sofa.
- 25:10That is really cool.
- 25:11Yeah.
- 25:11Just Google WIPY Kickstarter.
- 25:15Yeah.
- 25:16Wi-Fi and you'll find it.
- 25:17That's awesome.
- 25:17And it looks like it, it also really exceeded its, its Kickstarter goal.
- 25:23You, they were going for 30, 30,000 pounds and they got 75,000 pounds.
- 25:27That's great.
- 25:27Yeah.
- 25:28I think they're in euros there.
- 25:29Yes.
- 25:30Oh yeah, that's right.
- 25:31It's euros.
- 25:31So yeah, more like dollars.
- 25:32Yeah.
- 25:33Right now with the exchange rate.
- 25:34Okay.
- 25:34Very cool.
- 25:35Several times I've had the thought as you're talking about this, you're like,
- 25:40Oh, we've really got to squeeze the performance out of this framework or that framework is actually kind of heavy in this way.
- 25:45Or I rewrote these things to be more efficient.
- 25:48Is there a place for micro Python in the data center?
- 25:53Well, maybe, maybe in the future.
- 25:56Yeah.
- 25:57So what I'm thinking is, you know, if you've got like these little Docker containers and if you're looking for server density, you know, maybe having something that was focused on a micro controller that uses so few things.
- 26:09So few resources, you might be able to run really, really dense sort of cloud computing systems and do interesting things with it.
- 26:16What are your thoughts?
- 26:17Yeah.
- 26:17No, definitely.
- 26:19That's actually one of the directions of the project as well.
- 26:23So it's so that the soft micro Python software runs on the Pyboard and the Wi-Fi and other pieces of hardware, but it also runs on your PC, Linux, Mac or Windows.
- 26:35And it's basically Python, but unbloated.
- 26:39So it tries to be a really efficient version of Python and as compatible as it can be.
- 26:45So it's very, very quick to start up and it uses, you know, almost no memory.
- 26:49And I've actually tried sort of spawning, you know, a thousand micro Pythons on my computer and it's fine.
- 26:56It runs a thousand micro Pythons.
- 26:58And I tried spawning a thousand Python, normal Pythons, and then my computer just grounds to a halt.
- 27:04So, okay, that's just a sort of a fun game.
- 27:09But it shows you that, yeah, as you were saying, if you wanted to, you know, have a container and put in some app and you needed it to be efficient because you wanted to run lots of them, then maybe Macro Python can do what you need.
- 27:23So, I mean, there is a movement.
- 27:26There are some people who are helping to, there's quite a lot of people on GitHub working on Macro Python, which is really great.
- 27:34Yeah, that's cool.
- 27:35And that's just github.com/micropython, all our case, right?
- 27:39Yes, that's right.
- 27:39Yep.
- 27:40But one of the things they're doing is implementing as much of the Python standard library as can be done in order to run some real-life applications like a web server, for example.
- 27:56And, yeah, I mean, there's some other interesting applications as well.
- 28:03Running a web server is sort of, yeah, the number one thing that you think about when you're thinking about, you know, Python and running lots of instances.
- 28:11So that's definitely something that we're working towards.
- 28:15I mean, it already can run web server for sure.
- 28:20But I don't know of anyone who's doing that in production just yet.
- 28:25Yeah, actually doing it.
- 28:26Well, maybe we're giving some people some ideas out there.
- 28:28Yeah.
- 28:29It seems like an interesting thing to be the sort of foundation of some lightweight, restful services that you want to just have lots of horizontal scale on.
- 28:39Yeah, yeah.
- 28:40Yeah, there's definitely lots of scope for this.
- 28:43Lots and lots of scope.
- 28:44Okay.
- 28:44So the actual operating system on the microcontroller, that's written in Python?
- 28:49Python is the operating system.
- 28:51That's what – I think that's a good way to describe it.
- 28:56That's really awesome.
- 28:56On the microcontroller, there is no operating system.
- 29:01It's just bare metal.
- 29:02So the first thing that happens when it boots up is you have to set the stack pointer and you have to copy the data from ROM to RAM.
- 29:10And then you've got sort of free reign.
- 29:14You can do whatever you like.
- 29:15So, you know, it jumps into some initialization, loads up the Python runtime, and then jumps to the REPL.
- 29:21So Python essentially is the operating system, and it's giving you full control over the microcontroller.
- 29:28There's nothing – there's no context switching.
- 29:31I mean, every cycle is yours to spend on Python code.
- 29:35That's fantastic.
- 29:36Yeah.
- 29:38So you can – I mean, you can do things like disable interrupts with a Python call, and then, you know, everything's nice and quiet.
- 29:45You can do things in cycle count and then turn them back on again.
- 29:49You can set interrupts to trigger, so you can have a timer which counts and say, you know, 10 times a second fires an interrupt.
- 29:57And when that interrupt fires, you can call a Lambda function in Python to do something, and then it will return.
- 30:03So you can call Python on an interrupt.
- 30:06That's pretty awesome to hook a Lambda function straight to like a hardware interrupt.
- 30:11That's super cool.
- 30:11Yeah.
- 30:12So you can do that.
- 30:14And the other, I think, very nice thing – well, the thing that I think is quite cool is you can write inline assembler code within Python.
- 30:22So just like in C, how you can say, you know, inline assembler, and you write your functions there.
- 30:27In Python, you can write a function, and you use the inline assembler decorator for that function.
- 30:34It's a special decorator.
- 30:36And then the compiler knows that instead of compiling Python, it compiles the, you know, the arm assembly.
- 30:43It assembles it.
- 30:45And then it turns that into a normal function that you can call from Python.
- 30:49You can even pass arguments to that function.
- 30:53And then it can do whatever you want to do in assembler, and it can even return stuff back to Python land.
- 30:57So this way, if you really need to do some low-level things, then it's at your fingertips.
- 31:04That's very cool.
- 31:05That's kind of your escape hatch from – if you haven't thought of it in Python, if you guys haven't pre-made this feature available, that's the way to do it, right?
- 31:15That's right.
- 31:16Yeah.
- 31:17You can access the direct hardware however you like.
- 31:21Now, I'm not sure that I would be super comfortable doing much in direct assembler, but I would be in C.
- 31:26Is there a possibility of having, like, inline C in the future?
- 31:30There already is such a thing.
- 31:33It's not inline C, but it's a mode of a compiler, the Python compiler, that is.
- 31:42So the Python compiler in MicroPython is a bytecode compiler.
- 31:46It reads your script, and it turns it into a parse tree, and then it compiles the parse tree into some bytecode.
- 31:54But there's a sort of an option to – instead of emitting bytecodes, it can emit direct machine code.
- 32:01So this is kind of like just-in-time compiling, although it's ahead of time compiling.
- 32:06But this is how the V8 JavaScript engine works.
- 32:09Instead of targeting a virtual machine with bytecode, it just targets the direct hardware by emitting.
- 32:16So if I wanted to load an integer onto the stack, then instead of calling a bytecode, I can just do the actual machine code.
- 32:24And then if I wanted to call a function or call a method, then I just implement the machine code to actually do that.
- 32:32So this makes your code run faster, but it also allows you to declare types to say, okay, using Python's type annotations, I'm going to say that this argument is an integer and this one's an integer.
- 32:43And then MicroPython can optimize that when you do an addition.
- 32:46And instead of calling lots and lots of calls to do an addition to these two numbers, it knows that it can use a single hardware add, and it just adds them.
- 32:57And in this way, you essentially turn your Python code into sort of C-like code.
- 33:06But it still looks Python, but it's compiled to something that would have happened if it was written in C.
- 33:13So you've got pointers, and you've got integers, and you can do loops.
- 33:17So you can write much faster code that way if you really need to.
- 33:23Yeah, that sounds really interesting.
- 33:26One thing I'd like to sort of think about with you is what's the tradeoff in terms of performance if I were to, say, work with embedded C on a regular, like, say, embedded Linux or something versus Python on MicroPython on this board?
- 33:44So versus, say, Python on a regular, like, Adreno or something in terms of relative performance.
- 33:52What's the tradeoffs here?
- 33:55So, okay, yeah, if you want a number, it's something like a factor of 100 slower when you're running Python's bytecode.
- 34:02So each, so, yeah, this is a very rough number.
- 34:06So, you know, if you wanted to do a loop, which was flashing an LED on and off, then if you've written that loop in Python, you can get maybe 100 kilohertz out of that loop.
- 34:19But if you're writing it in directly in C, you'll probably get 10 megahertz out of the loop.
- 34:24Now, I mean, yeah, look, MicroPython is not meant to replace C in any form.
- 34:33It's, I mean, it's supposed to be there for doing high-level control of all these sophisticated components.
- 34:40And if you really need to run a 10 megahertz loop, then, well, you could use the inline assembler, maybe, or you could use this other mode I was talking about before.
- 34:50Or you can write your own C function and then recompile MicroPython and use that C function as sort of an extension to MicroPython if you need to.
- 34:59So you have lots of different options.
- 35:03But there is, of course, going to be some kind of overhead in terms of the speed.
- 35:07I mean, you can't escape that.
- 35:08Yeah, no, that's totally reasonable.
- 35:10I guess one of the things I was trying to get a sense of is how does MicroPython compare to CPython on the same hardware?
- 35:18Ah, I see.
- 35:30This episode is brought to you by Hired.
- 35:32Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
- 35:39Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
- 35:48Typically, candidates receive five or more offers in just the first week, and there are no obligations ever.
- 35:54Sounds pretty awesome, doesn't it?
- 35:56Well, did I mention there's a signing bonus?
- 35:59Everyone who accepts a job from Hired gets a $2,000 signing bonus.
- 36:02And as Talk Python listeners, it gets way sweeter.
- 36:07Use the link Hired.com slash Talk Python To Me, and Hired will double the signing bonus to $4,000.
- 36:14Opportunity's knocking.
- 36:16Visit Hired.com slash Talk Python To Me and answer the call.
- 36:20On the same hardware, it's impossible to measure because C-Python won't run on a Pyboard.
- 36:36That's really not working.
- 36:38If you want to try and compare apples with apples, you can run MicroPython on your PC, and you can run C-Python on your PC, and you can compare the two using some benchmark that you like.
- 36:51Actually, MicroPython is slightly faster than C-Python for a reasonable range of benchmarks that we have.
- 37:01For example, the Pystone benchmark, MicroPython runs a little bit faster than C-Python.
- 37:06And that's because it uses less resources, and the way it handles integers is a bit more efficient.
- 37:11So, you know, not having to allocate RAM to do certain things makes it faster.
- 37:16But, of course, there are other things that C-Python is quicker at.
- 37:19But, actually, on par, they're about on par when you take your own overall sort of benchmark.
- 37:27Okay, that's very interesting.
- 37:29So, it's more like they're on par, but C-Python is kind of unsuitable for these microcontrollers because it's so big and memory hungry, yeah?
- 37:38One thing this might be worth talking about is where is MicroPython used?
- 37:43Are there some notable, I guess, I'm not sure if deployment's the right word, but products or devices with MicroPython?
- 37:50Well, it's been used, I think, in quite a lot of places.
- 37:55Some of them that I don't know about and some of them I do know about.
- 37:59I think it's used a lot in robotics.
- 38:04There's been a few other Kickstarters as well.
- 38:07To do with MicroPython.
- 38:10There was this Robocore Kickstarter where they made a little sort of robot brain that ran MicroPython so you could make robots.
- 38:17And then the Wi-Fi, obviously, for Internet of Things.
- 38:20It's been used in teaching.
- 38:23There's some people that will be using it for teaching university classes because it's sort of a good mix of, you know,
- 38:31in university course you don't have all this time to go into the details of getting cross-compilers working and all this stuff.
- 38:37You just want to teach some basics about low-level interrupts and controlling an I2C bus.
- 38:43And doing that in Python, yeah, it seems like a really great way because Python is so simple.
- 38:47You totally understand what's going on and so it lets you focus on the concept, right?
- 38:52Yeah, exactly.
- 38:52And turn an LED on and off and sort of get some hands-on use of some hardware straight away.
- 39:00So, I mean, in the UK here there's a big push for students at primary school even to start learning languages.
- 39:09So, they have to learn two languages and mostly Python is one of the languages they learn.
- 39:15And they also do sort of some hardware in their sort of design or science class.
- 39:21So, if these two things go together, if MicroPython, because they already know Python and they need to do some sort of simple exercises,
- 39:30it's a very easy path for them to take.
- 39:35So, it's been used in education and I think it will grow a bit more.
- 39:40One other very exciting thing is that the European Space Agency has actually expressed interest in using MicroPython for space-based applications.
- 39:50That's awesome.
- 39:51Because the hardware that runs in space is very limited in resources because it has to be proven that, you know,
- 40:00it takes a long time to prove that a piece of hardware is actually run in space given its radiation hardening and so on.
- 40:09So, yes, the European Space Agency has agreed to fund some further development of MicroPython to see if it can be made robust and deterministic
- 40:21so that it can be put in a space-based application.
- 40:26So, there's more than, yeah, more than just the Internet of Things.
- 40:30Yeah, that's, yeah, space.
- 40:32That is really cool.
- 40:34I guess space agencies, like the European one and NASA and so on,
- 40:40they're traditionally not really thought of as rapid, agile-type places.
- 40:45And anything they can do to move away from these languages like Fortran and C that make it really hard to build reliable, tested stuff,
- 40:54and something simpler like Python, it seems like a really big win for them.
- 40:58So, that'd be amazing.
- 40:59I mean, we look forward to the developments to see what will happen if it's happening.
- 41:03It's very exciting.
- 41:04Yeah.
- 41:05Maybe some amazing picture of galaxies will be pulled down someday and it'll be, have taken, been taken by Python.
- 41:12That'd be cool.
- 41:13So, speaking of space and robots and things like that, another area where we've had people on the show,
- 41:23and I know Python is used, is robot operating system, ROS and ROSPy.
- 41:27Is there anything going on between those two projects?
- 41:32With MicroPython, not that, no, we don't have direct communication with them yet.
- 41:40But, I mean, ROS is more of a, ROS is sort of a very, it's sort of a large operating system in the sense that it needs sort of a PC to run on it.
- 41:51It doesn't run directly on a microcontroller.
- 41:53Yeah, it's more like an i7 or something like that, right?
- 41:56So, yeah, so you'd need, so, MicroPython could fit in the picture there if they wanted to run an efficient Python implementation.
- 42:03So, instead of running CPython, they could run MicroPython, but it would be sort of the PC version of MicroPython.
- 42:11It wouldn't be the Pyboard version because the ROS has already taken over the system and it is controlling it.
- 42:18Yeah.
- 42:20Okay, that's very cool.
- 42:21Good to know.
- 42:22But there are other real-time operating systems that have MicroPython sort of already built in.
- 42:33So, NutX is one of them.
- 42:34And Riot is another real-time operating system that we're working to get MicroPython into.
- 42:40So, there's, yeah, these things are good, interesting avenues.
- 42:46Yeah, that's really nice.
- 42:48There's a lot of stuff going out there and the more you guys can work together, the better, I guess.
- 42:52Yeah, well, I mean, with an open-source project, it is an open-source project.
- 42:57It's got an MIT license and anyone is free to contribute.
- 43:01I mean, yeah, it's not something that's going to move super quickly unless it has help from external or from the community.
- 43:11Of course.
- 43:12You also said that the hardware was open hardware.
- 43:16I don't do much with hardware, so that doesn't mean anything to me, but it sounds cool.
- 43:19Can you talk about that?
- 43:20It just means that the schematics are available for people to download and they can make their own board.
- 43:26So, the idea with the PyBoard was that it's not supposed to be a sophisticated piece of hardware that's impossible for anyone to make themselves.
- 43:36It was supposed to be simple enough that you could solder one by hand.
- 43:40I mean, I made the first prototypes by hand just with a soldering iron.
- 43:45So, it's not an overly complicated piece of hardware.
- 43:49It's just there so that you can run MicroPython and connect it to some sensors and some motors and get things working straight away.
- 43:57And then, if the PyBoard isn't exactly what you need because it doesn't have enough IO or you want to use a different microcontroller or you need more LEDs or something like that, you can make your own board very easily.
- 44:09And MicroPython is sort of configurable enough that you can put it on your own chip.
- 44:14That's really excellent.
- 44:15And it runs on ARM chips, right?
- 44:18Yes, it runs on ARM, but it also runs on other things as well.
- 44:22It doesn't just have to be in ARM.
- 44:25There are certain parts that are optimized for ARM, but there are other chips we also have it running on, like a microchip pick and this expressive Wi-Fi chip as well.
- 44:41And these architectures are all different.
- 44:43So, it's not just an ARM architecture that it's relying on.
- 44:47Oh, that's really cool.
- 44:48So, when I think of what I do during the day, I go and I sit down and I plug my MacBook into a larger monitor and have my keyboard and that's pretty much how I work.
- 44:58But you're talking about things like hardware and soldering irons and all sorts of stuff.
- 45:04You must have an interesting place where you work or an interesting setup.
- 45:09Yeah, well, I think it's a mess like everyone else has set up, right?
- 45:14There's just lots of stuff laying around.
- 45:16But yeah, you're right.
- 45:17There's soldering irons and there's oscilloscopes and, well, there's big screen and keyboards and everything.
- 45:23And then lots and lots of pieboards just lying everywhere.
- 45:26How many revisions did you go through the pieboards?
- 45:29Is what you're shipping kind of what you started with or is it really different?
- 45:33No, it's the same controller and pretty much the same.
- 45:37It started out with the same features.
- 45:40Accelerometer, SD card, a few LEDs and switches.
- 45:44And it just sort of got a little bit smaller and all the components went on to the top side to make it cheaper to manufacture.
- 45:50But otherwise, it's pretty much stayed the same.
- 45:53I think this is the final version was the fifth revision.
- 45:57Okay, excellent.
- 45:58So if people want to get started working with MicroPython and the Pyboard, how do they do that?
- 46:05Can they come and buy that from you?
- 46:07Yes, they can.
- 46:08They have different options.
- 46:10They can buy it directly from us.
- 46:12And it's, well, it's 23 pounds if you buy outside of the EU.
- 46:18But if you're inside the EU, you've got to add 20% tax.
- 46:21So it's 28.
- 46:22We also have a distributor in America, Adafruit, who stocks them.
- 46:26So you can buy it directly from them.
- 46:28We have some also distributors in Australia and in Italy and Belgium.
- 46:33And on the website, there are links to these places.
- 46:38So you can buy from a few different places.
- 46:39And, yeah, I mean, if you just want to try out the software, you can run it on your PC.
- 46:43You can go to GitHub and download the code and compile it and see what it's like.
- 46:47You can browse through the code.
- 46:49It'll also run on some other boards that maybe some people already have and they just want to try it out, like a Teensy or a STMicroelectronics discovery kit.
- 47:02So there's a few different options.
- 47:04If you go to the GitHub page, there's quite a lot of explanation there.
- 47:10There's also the forum, which has a lot of people you can talk to there and ask questions.
- 47:16And these are all linked from micropython.org.
- 47:18Okay, excellent.
- 47:19And I recommend just to see what's going on that you go to micropython.org and just play the video.
- 47:25It's like nine minutes or something.
- 47:26And it'll give you – it's a good video and it really gives you a quick visual overview of what you guys are all about.
- 47:32Yes, great.
- 47:33Yeah, and I'll put, of course, all that stuff, links to all those things in the show notes.
- 47:37So, Damien, I think this might be a good place to kind of wrap up the show.
- 47:40So, a question I usually ask my guests on the way out the door is, you know, there's a ton of stuff on PyPI, the package index, that's really amazing.
- 47:50And everybody kind of has their own favorite packages they work with and things they've discovered.
- 47:54Do you have any favorites you want to give a shout-out to?
- 47:57Favorite PyPI packages?
- 47:59Well, there's a whole bunch of MicroPython PyPI packages.
- 48:04Because MicroPython also uses PyPI and has a built-in package manager, MicroPip, so you can download lots of things from PyPI there.
- 48:16But as for myself, in normal Python, wow, I mean, there's so many things that I use.
- 48:23I mean, in my work in physics, I use a lot of scientific computing like NumPy, of course, and SciPy is very good.
- 48:31Yeah, I don't know if I could narrow it down to one particular thing.
- 48:35Yeah, it's kind of like a candy sword a little bit.
- 48:36It's very nice.
- 48:37A lot of stuff that looks good.
- 48:40Awesome.
- 48:41Okay, anything else you want to let the listeners know about that I haven't asked you about or we haven't had a chance to talk about?
- 48:47Only that if they're interested, then please go to GitHub and take a look.
- 48:53And there's lots of coding that can be done from, you know, beginner stuff to more advanced stuff.
- 49:00You can write tests or you can just implement some module or some feature that's missing right up to, you know, trying to fix some nasty bugs or help improve, make it more efficient and reduce memory usage.
- 49:15So there's lots of stuff that can be worked on.
- 49:19And I think, I mean, for me, it's a lot of fun.
- 49:22So maybe someone else would also find it fun to join in the coding experience.
- 49:28Yeah, that's great.
- 49:28And you guys have 63 contributors on GitHub.
- 49:32So I'm guessing you're pretty open to having people jump in and help out and add to that.
- 49:36Yeah, sure, sure.
- 49:37We are very open.
- 49:39Okay, great.
- 49:39Damien, it's been a really good conversation.
- 49:41I've really learned a lot.
- 49:42Yeah, well, thanks for having me, Michael.
- 49:44It's been great.
- 49:45Yeah, you bet.
- 49:46Talk to you later.
- 49:47Okay, thank you.
- 49:48Bye.
- 49:49Bye.
- 49:49This has been another episode of Talk Python To Me.
- 49:53Today's guest was Damien George.
- 49:55And this episode has been sponsored by CodeChip and Hired.
- 49:58Thank you guys for supporting the show.
- 49:59Check out CodeChip at CodeChip.com and thank them on Twitter via at CodeChip.
- 50:04Don't forget the discount code for listeners.
- 50:06It's easy.
- 50:07Talk Python.
- 50:08All caps.
- 50:09No spaces.
- 50:10Hired wants to help you find your next big thing.
- 50:13Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity
- 50:18presented right up front and a special lister signing bonus of $4,000.
- 50:22You'll find the links from today's show at Talk Python To Me.com slash episodes slash show slash 17.
- 50:29Be sure to subscribe to the show.
- 50:32Open your favorite podcatcher and search for Python.
- 50:35We should be right at the top.
- 50:36You can also find the iTunes and direct RSS feed links in the footer of the website.
- 50:42Our theme music is the song Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 50:47You can hear the entire song on our website.
- 50:49This is your host, Michael Kennedy.
- 50:52Thanks for listening.
- 50:53Smix, take us out of here.
- 50:56Staying with my voice.
- 50:57There's no norm that I can fill within.
- 50:59Haven't been sleeping.
- 51:00I've been using lots of rest.
- 51:02I'll pass the mic back to who rocked it best.
- 51:04I'm first developers.
- 51:06I'm first developers.
- 51:13Developers, developers, developers, developers.