Lego Mindstorm Ev3 Python

0 views
Skip to first unread message

Athina Dollison

unread,
Aug 4, 2024, 4:05:32 PM8/4/24
to textergfareast
Im new to mindstorms and I'm trying to do some first programming attemps, but I have a problem. I prefer usage of python over scratch, but I don't know how to handle multi thread (parallel) processes using python. What I'm talking about is following:

In above mentioned case both color sensor is able to stop the motor in case of red color, but also distance sensor in case of distance smaller than 20cm. Program is always checking both these sensors and waiting for data from them. But how can I do the same thing in python? If I do something like following, then it won't work for obvious reason because all those "waiting" functions are blocking and therefore if code is waiting for distance sensor, it won't react fo colors and vice versa:


But I dont like it, it will be not usable in all cases and it doesn't allow to perform parallel activities (e.g. if I will request to move motor A on red and move motor B when distance is I expect that I'm looking for something like define new methods via DEF where each method will be similar to one of those scratch blocks (probably having infinite loop inside) and somehow run them as an independent processes (something like fork?) so all methods will be working at the same time and I will be able to handle multiple events at the same time.


Is there some possibility to do this? Can you please help me with some example or link me to some resource where is described this micropython in better detail than in Mindstorms knowledge base? Thank you very much.


I quickly made one with one of my program variables, you will need to change them to yours, and try to understand how it works. You can "def"ine some routines, and just call them in your program once, or as in the example make a thread that you can start, it then will start it and continue with the rest of the program.


The Python that runs on SPIKE Prime and MINDSTORMS Robot Inventor is actually MicroPython rather than regular Python. And it doesn't have threading enabled, so threads are not an option for the MINDSTORMS Robot Inventor hub.


Thank you for additional info guys. It's pretty bad that those threads are not supported as they are more understandable for me than those yields. Though I think I was able to use them for what I need (or needed when I was asking this question).


1) When I'm using this way for creating parallelism, then it probably doesn't make sense to use those "wait_for_...." methods of objects at all right? Because it would block program execution inside generator. If I understand it correctly, then this "workaround" with generators and yields just uses yield to be able to stop one method for a while, run another for a while, then stop it and continue for a while with next one etc. So it makes kind of multitasking for those generators and allows each of them to run periodically and so it's needed to avoid long-running block of codes there as it will break this paralelism (because in fact it's still running sequentially and it only seems to be parallel because each block until yield is performed very quickly


2) Is there some possibility how to use some other IDE for development of Mindstorm programs (such as MS Visual Studio Code or IntelliJ Idea)? I really miss autocomplete functionality (why shoud I look into knowledge base if IDE can offer me all available options) and linting etc.


Your understanding as described in 1) is correct. You will have to make your own replacements for blocking (long-running) functions that use yield instead. For example, wait_for_... is replaced with while not ...: yield.


Writing code this way does take some getting used to, but it does have one nice advantage over threads. Threads can switch at any point, so extra locking mechanisms are often needed synchronize threads to avoid concurrency issues. But with generator functions, we know that a function will only pause at the yield keyword, so many of these concurrency issues are never a problem to begin with.


I had the same issue than Biermann. I was trying to replicate Charlie's "Drum solo" using (the vanilla version of) MicroPython, in which the arm motors move simultaneously, but asynchronously. After breaking my head and quite some Googling, I came across this thread. Maybe there is a better, simple, and/or cleaner solution for my problem. However, I am very happy with how @David Lechner's works. Thank you!


I finally figured out how to use Yield to run parallel threads in Micro Python (Spike Prime).

I know this is a quite old thread, I do feel however that the topic is important enough and still relevant to justify its revival.


Please see my sample Spike Prime program below which shows a simple way to run parallel activities using Yield.

I am not a programmer so there might be a more "correct" way to do this - it does however work for me.


This module allows you to operate the LEGO mindstorms hub fromyour computer. This means that instead of sending entire pythonfiles to the hub and letting it run them, you can run the commandone after another from your computer. This allows easy experimentingfrom a python shell on your computer, showing you completions andAPI documentation on the way. This also allows you to use theregular debugging facilities you're used to.


Hello, I would like to use the pixy2 with mindstorms ev3, but not with the original lego firmware. I would like to use it with micropython distribution. So my question is what is the best connection and communication for it? How can I use it the easyest and the most productive way?


As I researched the product I discovered that the EV3 brick, which is at the core of the Mindstorms robots, runs on Linux. Despite that, all of the coding software distributed with the project runs on Windows, OS X, iOS, and Android. I found an open source project that allowed the EV3 brick software to be modified, ev3dev. ev3dev is an open source project that allows the Mindstorm user to create a Debian-based operating system (OS) that boots from a microSD card. Once the EV3 brick boots the Debian image, the user can interact with the EV3 via SSH using the command line in a terminal window.


David Lechner is the lead developer for the project, and I had a chance to ask him a few questions. David's background is a bachelor's degree in electrical engineering from Oklahoma State, then he spent the next eight years doing industrial automation in the water and wastewater industry. Recently, for the last four years he's been doing freelance computer programming for primarily open source projects, such as ev3dev.


I asked him how the ev3dev project got started and he said his inspiration came from a blog post by Ralph Hempel, the founder of the EV3dev project. Being a tinkerer at heart, David couldn't resist. He said, "Even though I had no idea what I was doing, I jumped right in and started learning how to write kernel drivers (I had to start with "Hello World" in C because I didn't have any experience with the language) and how to maintain Debian packages and all sorts of other things that are needed to make an operating system." Fortunately for him he was in a position that allowed him to focus his efforts on that task. It wasn't long before Ralph realized that David had more time to devote and he made him a co-maintainer of the project.


I asked David how many people are involved with the project and he said, "It's hard to say exactly. Since LEGO robots are just a hobby for most people, we have quite a few people that show up for a weekend or two and then disappear. However, I would say that we have somewhere between 5 and 10 contributors that have been with us long term." I asked him if the project would consider allowing younger people to be involved since Mindstorm robots are used in so many schools. He said, "Of course. Unfortunately, at this point in time, we don't have much to offer that is very kid-friendly for younger students. Hopefully some day. On the other hand, it would be great to work with high school and/or college students that have just a little bit of programming experience already." He said that a potential developer in the project should not be frightened by lack of experience as he himself was very inexperienced when he got involved with ev3dev.


David said he enjoys working with students and has taught some after-school NXT robotics classes. He is currently serving as a coach of a First LEGO League team. I asked him what his most pressing needs are in the short term, and he said, "I would really like to see some other contributors that are willing to spend a significant amount of time making ev3dev better before the next generation LEGO Mindstorms robot comes along and everyone moves on to that. Perhaps some university students could make some aspect of ev3dev their senior design project, or companies that develop Mindstorms-compatible products could adopt ev3dev as a platform instead of developing their own."


When asked about his longterm goals, he said, "I would like to get our hardware drivers into the mainline Linux kernel. Jason Kridner (of BeagleBone) and Greg Kroah-Hartman (of The Linux Foundation) have been pushing me to do this in the short term. But, there is quite a bit to be done to even get the EV3 bootable from the mainline Linux kernel before we even get to the ev3dev-specific bits." David encourages users of ev3dev to try it and let the project know how they can improve, and let them know ideas that will yield iterations to the project.


To get started with your own ev3dev experience you need a LEGO Mindstorms Home or Education kit, the EV3 brick, and a 2GB or larger microSD card. Go to the ev3dev website and download the latest Debian Jessie image file. Download Etcher for your particular operating system to transfer the image to your microSD card, then after the image is on your card insert it into your EV3 and power on the unit. Follow the directions on the ev3dev website. Connect to the EV3 with the USB cable that comes with your kit and start having some fun.

3a8082e126
Reply all
Reply to author
Forward
0 new messages