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.'"
Or instead, people could just implement simple threading and learn something in the process rather than switch to a whole other language just for one problem.
"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?
This just a race condition, which was taught when I was a sophomore in college(and I knew about in high school).
Or try PyMite a.k.a. Python-on-a-chip or p14p if you really must... Also features threads and is a little more mainstream than Occam. And people do actually care about the amount of mental capacity goes to their tools while making the cat door open and close.
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.
Tweet, tweet.
occam iits sh like all lel lanparalguages.
XMOS (http://xmos.com/) has developed a mostly-C language with a few Occam-like extensions, which might also be worth considering. It's called XC (http://www.xmos.com/system/files/xcuser_en.pdf/).
How???
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.... ;)
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
Just curious. When I was an undergrad they had a parallel programming course and the language we used was C*. Basically it was C with this add on called a shape. Really it was just an array (Could be multi-dimentional) of virtual processors and associated data. (Basically a short, long, etc.) Then you'd just do a where statement on this array of processors. So in the where statement you'd just list the instructions you wanted done and each virtual processor would each run those instructions themselves. (Been a long time since I programmed in it.) It was actually pretty neat and worked pretty well. (They had us write a program to solve systems of linear equations. It was cool.)
Did you know 80 to 90% of the moderators on slashdot wouldn't recognize a troll even if one dragged them under a bridge.
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?
Another option is to use the Parallax Propeller microcontroller. It's got 8 cores, 80Mhz clock speed, and 32k of ram, and you can either program in its higher-level Spin language or get right down into assembler. The Arduino is fun to learn on and accessible to people who don't have a strong programming background, but working with the Propeller is like advancing to the varsity squad.
In general, one should try to avoid interrupts whenever possible. I thought years of VB programming and the therac 25 taught people the pitfalls of event driven software. Think about polling, fixed time steps, and state machines. Now we're talking embedded systems and video games. To be fair, interrupts are unavoidable for some things - just try to minimize that and keep the interface between IRQ and non IRQ code minimal too.
I'd support making ardruino more "user-friendly", except I have a feeling it will come at the expense of being "hacker-friendly". See pretty much any hacker project that later decided to become user-friendly as an example.
Oh well, I looked forward to learning how to make a knitted cozy for my board.
Ah you are right, thanks for looking that up. I just saw AVR and jumped to an incorrect conclusion.
They stole their syntay! Argh!
Where is it? I can't find the port. There is a link to: http://www.transterpreter.org/development_download
But that link is for a Mac-only tool chain of some sort. Does this mean the arduino IDE will be replaced with this Transterpreter thing? If they have a library or something drop-in for the arduino IDE (written in java), i would think it would work for any platform.
http://soylentnews.org/~tibman
a cat door that keeps their cat out when the cat comes back with a mouse
sauce?
Google Go seem to have some of the channel communication idioms and select statement semantics similar to occam.
Now, what is really interesting is how so small CPU is capable of handling parallel execution context to begin with...there is only 2KB of RAM!
I recall somebody made a preprocessor&select/break-statement-based "parallel" execution system w/o context switch state preservation (threads)...
What does the Arduino diddly do?
You do not need parallel processing to make the LEDs in your uber-kitsch geek-art project blink on and off. Now GTFO my internets, and go back to reading MAKE.
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.
I'll do it, there is nothing you can do to stop me!
Imagine a Beowulf Clust--- Error 406
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.)
//writes a 1 to PIN12 //writes a 0 to PIN12
/</< 3) : PORTH &= 0b11110111
//reads 1 or 0 into temp depending on PIN13's state
// or
... //reads 1 or 0 into temp depending on PIN13's state
// returns 0 or 0x08
.....
#define SET_PIN12(z) ((z)>0)? PORTH |= (1 << 3) : PORTH &= (0 << 3)
Then usage would simply be like the following:
SET_PIN12(1);
SET_PIN12(0);
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
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);
So we have
(z) = 0;
(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;
I was tempted to simply further to
#define READ_PIN13 (PINL & 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.
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.
Half the battle is accessibility. Arduino does that well. It accomplishes what many want it to do without fuss and esotericisms of "good" code. I'd rather have a set of tools I can work with for a one-off task. It beats waiting for an uppity code jockey who insists that it will take 4 weeks, $14k in developer tools,$2k in class fees, etc., to accomplish what a lot of sixth graders eagerly do in a few evenings from scratch. I've seen it happen- right where I work, and it is frustrating.
I remember Occam I learned it back in 1993, it was designed to power specialized multi-CPU machines, transputers, microprocessor with dedicated high speed serial connects to there peer microprocessors. The company that made the transputer INMOS went burst, and disappeared into Olivetti and the transputer was never seen again. I'm surprised that Occam is still going after all this time. It wasn't a bad language and had parallelism as a very low level, which would make it useful for graphics programming or matrix maths, where you could split a for loop over multiple processors, were the answers where independent of result for other indexes.
If you build tools for idiots, only idiots will use them.
Tools such as these only manage to act as a crutch for incompetent developers. The real solution to the problem lies in proper education, practice, and experience. Everything else is just a shoddy patch, and one reason why most college grads can't code to save their life.
We've been doing parallel programming and multi-threading on the Arduino for years before this.
Absolutely, this sort of behavior is why we have security holes.
A cat door is one of the biggest security holes there is. You never know what kind of cat/burglar might come in through the door. I mean, it could even be a cue:cat, or a /usr/bin/cat(1), or worse yet, a LOLCAT.
--Joe
It's pretty easy, actually, and I've never taken a programming class in my life. Even soldering your own board and programming the AVR directly with bitwise operators (albeit with C, not assembly) is not too tough.
The Occam guys are completely correct. Artists just want to achieve their goals for a project, not learn best practices for software engineering. That's why I don't design high frequency trading platforms for a living! However, I do try to choose frameworks that are written by people who do care about software engineering and abstract that stuff away from me.
I think this is the way of the future. All educated people should know how to program, but only a small number will become engineers.