Slashdot Mirror


Parallel Programming For the Arduino

blackbearnh writes "As more non-traditional programmers start playing around with embedded platforms like the Arduino, the limitations and complications of interrupt-driven event handling can become an annoying barrier to entry. Now a group of academics have ported the parallel-processing language Occam to the Arduino. In an interview on O'Reilly Answers, Matt Jadud of Allegheny College describes how Occam helps artists using the Arduino in their installations, and how the advent of low-cost computing platforms is changing the educational experience for proto-makers in school. 'Basically, an artist or a tinkerer or a hacker has a goal. They don't really care about learning Occam. They don't care about how this language is different from C. They just want to make a cat door that keeps their cat out when the cat comes back with a mouse. Or they want to make some kind of installation piece. Trying to focus as much on the user and the possible goals they might have is what's motivating our work right now.'"

31 of 140 comments (clear)

  1. That kind of thinking... by Anonymous Coward · · Score: 3, Funny

    "They don't really care about learning Occam. They don't care about how this language is different from C. They just want to make a cat door that keeps their cat out when the cat comes back with a mouse. Or they want to make some kind of installation piece. Trying to focus as much on the user and the possible goals they might have is what's motivating our work right now."

    Isn't this kind of thinking that lead us to why we have the security holes, shoddy programming, and bloat-ware today? People just want to code and not to learn the ins and outs required to craft a well-heeled, tuned, and functioning program or application?

    1. Re:That kind of thinking... by skelterjohn · · Score: 2, Insightful

      Arduinos are allowed to have security holes.

    2. Re:That kind of thinking... by Trepidity · · Score: 5, Insightful

      Next up I suppose you're going to complain about how Legos don't force you to learn proper civil engineering before building things?

    3. Re:That kind of thinking... by Bigjeff5 · · Score: 4, Funny

      I've been pushing for that for years! All children should have a proper engineering degree before playing with legos! I mean, have you seen what some kids come up with? Totally unworkable.

      --
      Security is mostly a superstition... Avoiding danger is no safer in the long run than outright exposure. - Helen Keller
    4. Re:That kind of thinking... by bb5ch39t · · Score: 3, Funny

      I agree with you. In the extreme, it might lead to gas pedals sticking in computer controlled cars. Oh, wait, that's already been done.

    5. Re:That kind of thinking... by John+Whitley · · Score: 4, Interesting

      Isn't this kind of thinking that lead us to why we have the security holes, shoddy programming, and bloat-ware today? People just want to code and not to learn the ins and outs required to craft a well-heeled, tuned, and functioning program or application?

      Repeat after me: programming languages and frameworks do not make developers dumber. It's this kind of thinking that forces every developer-user of a complicated system to be continually faced with issues outside of their domain of expertise, or even just the current problem focus. *That's* what causes these problems.

      For example, when doing embedded programming some years back, I noted that team members working on codec optimization were starting to crank out bad, broken ad-hoc synchronization logic to take advantage of some unique parallel hardware. Their specialties ran into numerical analysis and implementing low-level numerical optimizations, not into synchronization algorithms. I could take these folks and run them through an OS class, and walk them through the inevitable sea of mistakes...

      Or I could do what I did: I created a framework that abstracted away all of the platform synchronization concerns. They did their jobs neatly and cleanly by writing a class that contained some shared state and implementing just two virtual methods that embodied the parallel work. They were much happier, and the whole team was much happier because there was now *one* place to look for synchronization bugs. This was quickly hammered out into a very stable foundation for the other teams' work.

      Allowing our programming languages, libraries, and frameworks to do the heavy lifting so we humans can focus on the real problems we want to solve pretty much describes the history of real progress in software development.

    6. Re:That kind of thinking... by jd · · Score: 2, Interesting

      I was thinking more that all Engineering degrees require a combined combined LEGO/Mecchano device as a final-year project (to demonstrate interoperability), with internship at LEGOLand.

      --
      It's a small world and it smells funny; I'd buy another if it wasn't for the money; Take back what I paid (SoM)
    7. Re:That kind of thinking... by John+Whitley · · Score: 4, Insightful

      Repeat after me: people who don't want to learn programming will make lousy programmers.

      Fine then: the statement above is garden-variety developer egocentric stupidity. TFS' statement is right, these folks want to get their work done, but the specific tools are irrelevant. The qualities of those tools for the task are the only things that matter.

      It's insulting and stupid to propose that those looking to program and leverage an *Arduino* for their personal projects are somehow slackers uninterested in learning. Maybe they're just interested in learning what they want to, not what you want them to. I've walked the path of deep knowledge of programming, CS, etc. I've put my thousands and thousands of hours in. It's a good one for those who enjoy it, but it's not the end-all, be-all for all people.

      Lots of people "want to learn", but at the same time they don't have that "ten thousand hours to mastery" to invest in a new domain (here, programming/CS). There's a spectrum here: on one end, the deep knowledge of an experienced programmer. On the other, those who want and need to access the power of programming but don't want to be burdened with oceans of complexity and specialized domain knowledge. I'll apply an existing term, "end-user programming", for this.

      The most successful end-user programming environment by far is the spreadsheet. It provides simple, tabular model and some fairly rich programming capabilities within its scope. Another great example is the Max/MSP/Jitter environment for real-time audio/video signal processing -- very popular in the computer music and visuals world. Labview-based systems (which includes the Lego Mindstorms stuff) are another great example. Each of these environments is rich enough to allow programming, learning, and exploration. And all provide environments that are tailored to specific problem domains.

      There's a place in the middle, often called by programmers "tools for the task", where a programmer doesn't have to bend over backwards to address certain hairy problem domains. Libraries, frameworks, and programming languages can all meet these needs in their various ways -- even to the extreme that it transforms what some people consider the nature of "programming".

    8. Re:That kind of thinking... by ultranova · · Score: 2, Insightful

      Repeat after me: programming languages and frameworks do not make developers dumber.

      But they do make promises that they cannot keep. The abstractions they offer leak, so when a developer works with these abstractions, the code develops weird bugs and slowdowns.

      It's this kind of thinking that forces every developer-user of a complicated system to be continually faced with issues outside of their domain of expertise, or even just the current problem focus. *That's* what causes these problems.

      No, what forces developers face issues outside their domain of experience is that modern computers still don't have artificial intelligence. Consequently, when the frameworks start leaking, they start leaking hard. You end up acquiring a lot of knowlede and habits just to work around the oddities caused by the mess.

      Frameworks and high-level languages are a good thing, but using them requires more, not less, expertize, since you need to know not only them but the underlaying system too - because that system will find a way to peek in.

      Allowing our programming languages, libraries, and frameworks to do the heavy lifting so we humans can focus on the real problems we want to solve pretty much describes the history of real progress in software development.

      Perhaps, but it also describes the history of weird little bugs that keep on creeping in from the leaky abstractions in every framework. Sooner or later you end up fighting the framework because it's limited in some way; fixing every such instance results in unbelievably convoluted frameworks that are really their own programming languages. At that point it would probably be better to just scrap the whole thing and start over with a newer, higher-level language.

      --

      Forget magic. Any technology distinguishable from divine power is insufficiently advanced.

  2. "simple" threading by weston · · Score: 5, Insightful

    just implement simple threading

    Sure, and they could just learn to fly too, instead of relying on some convenient form of transportation that solves the problem for them.

    Threads are the famed "simple, clean and wrong" general solution to parallel programming tasks. The *concept* and *implementation* of threads can be simple, sure, but if you're working on anything other than simple problems, the trouble of keeping track of everything that's going on can become very challenging very quickly.

    a whole other language just for one problem.

    It's a big problem. Learning another language is generally a smaller problem. Particularly if you're the kind of Real Programmer(TM) that we're probably going to hear can manage with threads just fine.

     

  3. occam by Anonymous Coward · · Score: 4, Funny

    occam iits sh like all lel lanparalguages.

  4. Re:Threads by Monkeedude1212 · · Score: 5, Interesting

    In my limitted experience, threads are one of the more difficult things for... people to understand. I find it difficult to describe their position, which I think Matt Jadud had a tough time too, (See how he said "an artist or a tinkerer or a hacker"). In my situation, I have a friend who is taking an engineering major at the local university. Now, a little background information; I don't know how it is in other cities across the world, but here, Engineering at the university is considered one of the hardest courses. You know, really ridiculously high drop out rates, cause most people can't handle it. Opening orientation, they say look to your left, look to your right. 2 out of the 3 of you won't make it past second year. So anyone who manages to make it through the first 2 years of Engineering gets this perception that they know to do stats as well as a stats major or know how to program as good as a programming major.

    Anyways, so my buddy is in engineering, and he knows enough C++ to essentially do any calculation he wants through the command line. He hasn't had to work with GUI's or anything like that. The most he did was a turn based Star Trek game where the command prompt simply reprints the "game board" everytime you make a move or perform an attack, prompting the player what to do at the end of each turn.

    So he tends to be the kind of user that they target with these kinds of ports. He's already loaded with a bunch of information in some other field. Be it engineering, arts, hacking, radio signals, whatever. They don't have a whole lot of time to run through the tutorials to learn threading and how its supposed to be done properly. There's no telling how long it'll be before they get into an issue with threading and they won't have enough knowledge on how to fix it and it'll be a big headache if they went and built their entire code that revolves around this segfault they created.

    So thats where these other languages come in. They are similar enough to a common language like C that anyone who does a beginner course can pick them up. They offer the features that users WANT without all the complications that come with learning how its done.

    I know, I know, teach a man to fish, right? But what if he only ever needs 1 fish in his entire lifetime?

  5. Re:Threads by AlecC · · Score: 2, Insightful

    Occam was intended as a reply to all the problems that can happen with threading, The ides with Occam is that a lot of the things that can go wrong with threads simply cannot happen in Occam. Think of it as Java to threading's C. Just as you cannot create random pointers in Java, you cannot lock random mutexes in Occam (which doesn't have mutexes),

    Traditional threading really is assembler level coding for parallelism; Occam tries to move to a high level language.

    --
    Consciousness is an illusion caused by an excess of self consciousness.
  6. Interrupt Service Routines by TwineLogic · · Score: 3, Interesting

    There is no limit to the functionality of Interrupt Service Routines (ISR) and the interrupt-driven "event model," as the OP put it.

    Programming an ISR may be difficult, but even the topic of this post, a parallel environment running on the Arduino, will be based upon ISR routines. The user-level programs will not interact with ISRs, but the Ocaml implementation will abstract them.

    Fundamentally, the hardware will continue to use interrupts to signal the availability of data from human input devices. Therefore, the fastest and most direct way to access this data is to write an assembly language ISR. The difficulty is that embedded systems programming such as this requires specialized technical knowledge on the part of the programmer.

    Clearly the Ocaml solution posted will ease the burden on the coder, and that is a good thing. But the way it works is not that it no longer uses ISRs. It almost certainly implements its own ISRs and polling routines. In this way, it will be like a library. The beneficial result is that individual programmers do not have to reimplement the ISRs. But there is no benefit in, and no possibility of, eliminating the very needed ISR itself.

    Personally, I question whether the MCUs selected for the Arduino are appropriate for the "cute tech" market that the Arduino-series-PCB-module (a.k.a. "shield") manufacturers seem to be going for. Possibly the availability of Ocaml will bring the platform more in line with, e.g., the BasicStamp or similar. Overall, I see an impedence mismatch between what people would like to do (make costumes) and what they get (asking their friends to write code for them).

    The fundamental first step will be describing to the Ocaml environment how it is that the peripherals have been wired to the chip. Then the Ocaml environment can, presumably, service these interfaces either through ISRs or polling. We'll see what transpires in simplifying the Arduino software landscape.... ;)

  7. Re:Why do people struggle with this so much? by Bigjeff5 · · Score: 3, Insightful

    A race condition between two processes is easy. A race condition between three is several times harder. Race conditions between a half dozen processes? Forget about it, at least for the hobbyist.

    Race conditions can be notoriously difficult to program around. You can go from 20 lines of code to 200 in a heartbeat with just one or two of them. Get five or six, and your 20 line program needs a thousand lines to deal with it all. That's pretty ridiculous, especially for hobbyists.

    If you've got a tool to eliminate the problem completely, why would you poopoo it?

    --
    Security is mostly a superstition... Avoiding danger is no safer in the long run than outright exposure. - Helen Keller
  8. Parallax Propeller by BasilBrush · · Score: 5, Funny

    The best option for people who want to learn about parallel programming on an embedded processor is the Parallax Propeller. Genuine 8 core system on a chip, programmed to the bare metal. And so much fun.

    http://en.wikipedia.org/wiki/Parallax_Propeller

    1. Re:Parallax Propeller by BasilBrush · · Score: 2, Interesting

      I'm somewhat familiar with the Propeller. Parallelizes quite well up to eight simultaneous tasks. Nineth? Well, turn back around and back to hell.

      Very true. I've done 2 games with the Propeller, and hit the 8 core ceiling both times. So a lot of people are now doing projects with 2 (or more) propellers.

      Maybe not a great choice for production electronics. But great fun for tinkering and one off projects.

    2. Re:Parallax Propeller by Jerrry · · Score: 2, Interesting

      "I'm somewhat familiar with the Propeller. Parallelizes quite well up to eight simultaneous tasks. Nineth? Well, turn back around and back to hell."

      In that case take a look at the XMOS chips. Each core supports eight hardware threads and there are 1, 2, and 4 core versions available. Each core runs at 400 MHz. With the 4-core chip, you have 32 hardware threads to work with. Need more? No problem, just add more chips and connect them using the built-in Links hardware. XMOS sells a development board that has 16 of the 4-core chips for a total of 512 hardware threads.

      The development tools (IDE, compiler, debugger) for Windows, Linux, and OS X are free downloads from the XMOS site. XMOS has added parallel processing capabilities to C (calling it XC), but the development tools also support C, C++, and assembly. JTAG units are US$50, which is quite reasonable.

      Check it out: www.xmos.com www.xcore.com

      Disclaimer: I have no relationship with XMOS except as a satisfied customer.

  9. Re:Keep a cat out when it has a mouse?? by ari_j · · Score: 4, Funny

    You embed RFID tags in the mice and then keep the door locked if one is detected.

  10. Reasonable enough. by Animats · · Score: 3, Interesting

    Good idea. I'm impressed that they were able to cram Occam into an Atmel ATMega. Occam syntax is rather clunky by modern standards, but it gets the job done. It has a sane concurrency model and is safer than C.

    Next, Ada?

  11. Re:Keep a cat out when it has a mouse?? by JoelMartinez · · Score: 2, Informative

    I read this story a *long* time ago, but I remember that someone built a cat door that used a webcam to capture the silhouette of his cat as he entered the cat door. The software would look at the shape, and use a computer learning algorithm to "recognize" the cat ... that way, when he tried to enter with a mouse in his mouth, it would block him. It also had the effect of keeping out raccoons because it obviously wouldn't fit the profile

  12. Re:Is that this language that looks like Python? by Egelmex · · Score: 2, Informative

    Python came out in 1991 but occam in 1983....

  13. "Arduino" sounds like Ned Flanders word by walterbyrd · · Score: 2, Funny

    What does the Arduino diddly do?

  14. Artists are NOT going to be programming AVRs by Simonetta · · Score: 2, Interesting

    I'm an AVR programmer. I prefer to work with assembler, because I come from an electronics background and assembler is closer to the electrons. I can, and occasionally do, work in C on the AVR and Visual BASIC on the PC.

        Let me say, this stuff is hard. It's hard for programmers and electronic technicians. It's really hard for hobbyists and people who have little technical background. Artists are not going to be programming AVRs to make cool performance art projects with Arduinos. OK, maybe one or two, but not many.

        Even rock-bottom beginning simple stuff like blinking an LED or making a beep when a button is pressed can be challenging on a microcontroller. It's not hard to know what to do; it's hard to actually do it and make it work 100% all the time.

        Your average guy or performance artist is NOT going to be making a cat door that won't let the cat in the house with a mouse. Let's see, the cat pushes on the door with its nose. This flips a sensor that activates a camera that relays an image of the cat's face to a microcontroller. The MCU parses the pixels to determine that the image sector of the mouth of the cat is significantly different from the analysis of previous images of the cat's face. The door won't open.

        Now if you're reading this, then yes, you can program something that might be able to do this. You're a Slashdaughter, for Christ's sake, you can do anything technical, and you know it.
    But you wouldn't be able to do it on a $1.59 microcontroller. And you sure wouldn't be able to do it if you didn't have thousands of hours of programming experience and technical training.

        It doesn't matter what language or integrated development environment that you use, it's just not going to happen.

        And frankly, most of the cool projects that performance artists want to do with computers would require a real gigahertz/gigabyte/advanced_OS PC to do, not an 8-bit microcontroller with 1K of RAM that can just barely run a microwave oven, let alone a telephone.

        Performance artists want professional-level programming ability and talent at bargain-basement artists prices. But if you're not a beautiful woman into performance art who has the ability to hook up her beautiful friends to nerdy techno-geeks who actually do the programming, it's unlikely to happen.

  15. Re:Why do people struggle with this so much? by Haxamanish · · Score: 2, Funny

    This just a race condition, which was taught when I was a sophomore in college(and I knew about in high school).

    Are you sure you were not a semaphore?

  16. Re:Propeller Chip by vlm · · Score: 2, Informative

    After all, it's a microcontroller

    You italicized the wrong syllables. Should have said microcontroller as it can only parallelize separate hardware threads. You can't, for example, do more than eight software threads.

    Here's a mixed model fail for an four person soccer video game:

    one cog runs the video out (hardware, OK)
    one cog runs the sound out (hardware, OK)
    one cog each for each human player, reading each joystick or bluetoothed wiimote or whatever (hardware-ish, OK)
    one cog each for each computer controlled AI player (software, danger! danger! danger!)

    That adds up to 10 cogs. And success or hard failure depends on a user configurable number of players due to inherent hardware architectural limitations.

    A better architecture in this situation would be scrap the hardware accelerated threads and go pure software threading, since none of the threads (well, except video) are terribly computationally difficult.

    Also note that lynxmotion sells numerous little robots with up to 32 little servos. Easy if your threads are done in software, not so easy if they are only done in hardware and you only get 8 or whatever.

    --
    "Science flies us to the moon. Religion flies us into buildings." - Victor Stenger
  17. Re:Keep a cat out when it has a mouse?? by smellsofbikes · · Score: 2, Informative

    Here's the hackaday entry about the feline facial recognition project. The actual project itself is located on a pretty slow server, so you'll have to just go with that, but the idea (from 2003) is what you say: it lets in cats that aren't carrying stuff in their mouths, but doesn't let in raccoons or skunks, and since he's captured pictures of them trying to get in, that's pretty useful.

    --
    Nostalgia's not what it used to be.
  18. Re:Threads by TheTrueScotsman · · Score: 2, Interesting

    I did Occam in the 80s and server-side Java now. Java is way more powerful for parallel processing and the simplification in Occam (essentially a built-in parallel functional transform aka the 'threaded for loop') can be replicated with a fairly simple Java library that almost any experienced programmer has written for themselves. I literally use one every day. Only newbies are wrestling with Thread.start() and mutex locking. Everybody else has abstracted this long ago.

  19. C can do you wrong by electrostatic · · Score: 2, Interesting

    I've been playing with the Arduino and ran into these examples last night. The objective of the macro below is to set (1) or reset (0) a single bit in an 8-bit register. Register PORTH is mapped to 8 pins I/O pins on the Arduino and we want to control one of them: pin 12. This is the code I found. It's very helpful in that it shows register-to-pin mapping. (Pin 12 has previously been set as an output pin.)

       #define SET_PIN12(z)  ((z)>0)? PORTH |= (1 << 3) : PORTH  &= (0 << 3)

       Then usage would simply be like the following:
       SET_PIN12(1); //writes a 1 to PIN12
       SET_PIN12(0); //writes a 0 to PIN12

    There are some problems with this. ((z)>0) will not do what the programmer intended if z, an int, is negative. And there's no need to test for Z being non-zero. The expression should be replaced with z alone.

    The first statement of the conditional if, (1 << 3) : PORTH, works fine as it sets the desired bit to 1 while leaving the other bits as-is. But PORTH  &= (0 << 3) resets ALL 8 bits to zero. I suspect he was thinking that (0<<3)  is 11110111.
    In any case, PORTH  &= 0b11110111 functions properly. (The Arduino language is a subset of C++ with a few additions.) A simpler version is

       #define SET_PIN12(z)  (z ? PORTH |= (1 /</< 3) : PORTH  &= 0b11110111

    Another example. This is their code to read an input on pin 13. (Pin 13 has been set as an input pin.)

       #define READ_PIN13(z) ((PINL & 0x08) > 0) ? (z) = 1 : (z) = 0
       Usage would be
       READ_PIN13(temp); //reads 1 or 0 into temp depending on PIN13's state

    So we have
       (z) = 0;  // or
       (z) = 1;
    being executed. I didn't realize this would compile.

    The macro functions correctly but I simplified it and changed its usage from a conditional if to to a simple assignment
       #define READ_PIN13 ((PINL & 0x08) > 0)
       // ...
       temp =  READ_PIN13; //reads 1 or 0 into temp depending on PIN13's state

    I was tempted to simply further to

       #define READ_PIN13 (PINL & 0x08)  // returns 0 or 0x08

    But since there was a lot of this kind of (proper) code I didn't go that far.

       #define   HIGH   1
       // .....
       temp =  READ_PIN13;
       if (temp == HIGH) {...   // tests temp == 1, versus "true" (non-zero)

    After looking through lots of code on the web sites it got me thinking about how easy it is produce C code with unintended consequences.

  20. Interesting, by inhuman_4 · · Score: 2, Interesting

    This is interesting and I hope that it helps bring in new people to the embedded field. Having easy tools to introduce people to a system can make a big difference in the learning curve. Once they get hooked they can start to learn how to do things manually.

    For things like the ARM, Blackfin, etc. having multitasking is a huge benefit. But on lower end systems like PIC, AVR, etc. it's really just for show and tell.

    I have a fair bit of experience programing these low end devices and the golden rule is ISRs (Interrupt Service Routines) for everything. Everything should be done via ISRs, and when not running an ISR the chip should be in low power mode. A lot of embedded systems are battery powered and they simply don't have the power to waste on things like polling. If you have no choice but to poll (and there are very, very few cases for this) then use a timer ISR. Additionally ISRs give you interrupt priority and hard-realtime responses, something that many applications (especially safety) require.

    Putting occam on Arduinos should help people get started on these devices, but I seriously doubt it will see any use in the professional world.

  21. Re:Threads by Pinky's+Brain · · Score: 2, Insightful

    PAR is not the important part of Occam. The fact that all message passing is synchronous and there is zero aliasing is what is important (it prevents a ton of errors, makes automatic reasoning about the programming much easier too). You can do synchronous message passing in any language, you can try to program aliasing free in any language too ... but few people do. They'd rather create abstractions for mutex locking and then rely on programming convention and programmer infallibility for their correct use.

    Modern Occam isn't 80's Occam by the way, creating complex data structures is much easier and you can pass data by reference for instance (the local reference gets destroyed when passed).