Pages: [1] 2   Go Down
Author Topic: FORTH environment for Arduino  (Read 7646 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written a pretty simple FORTH environment for the Arduino platform. So far, I've tested it only on a ATMega168 Arduino, and simple things, like the LED blinker example, works fine. I'm releasing the source code under GPL2 and putting it on my Github account here: http://github.com/lpereira/finf; so please fork it and help me improve it, specially if you're able to cut down the memory usage smiley

Called FINF (FINF Is Not Forth), it weighs at the moment of writing, 6.5kB of program memory, and was written using the standard Arduino IDE. It is different from amforth however because words are not saved to the program memory; they stay in RAM and a simple virtual machine takes care of executing it.

As for the LED blinker example, try pasting this into Arduino's Serial Monitor:

1 13 pinmode
: led 13 pinwrite;
: on 1 led;
: off 0 led;
: w 100 delay;
: blink 0 begin on w off w 1 + dup 10 = negate until;
blink


This will blink the LED on pin 13 ten times, in 100ms intervals. What this does is, line by line:

1. Set pin 13 to digital output
2. Create a new word "led" that writes the value on stack on pin 13
3. Create a new word "on" that pushes "1" on the stack and call the word "led"
4. Does the same, except it pushes "0" on the stack
5. Creates a new word "w" that delays program execution by 100ms
6. Creates a new word "blink" that calls "on", "off", and "w", 10 times
7. Call the word "blink" -- the LED on your Arduino should be blinking now smiley

It may look weird, but FORTH is pretty fun actually. You have some pre-defined words (type "words " on the Monitor to see what you've defined and what comes with FINF; type "dis " to see the disassembly of user-defined words) and end up creating your program by defining new words and gluing them together.

There are a few things missing from FINF, however:
  • Stability smiley
  • Ability to save/load things from a non-volatile memory
  • Testing (specially conditionals and loops)
  • Arrays bound checks, etc
  • Did I mention stability?
Well, anyway. Give it a try and shout here.

Cheers
Logged

Cumming, GA
Offline Offline
Edison Member
*
Karma: 18
Posts: 1628
Ultimate DIY: Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Neat.

Peanut Gallery Comment:  Forth never really caught on... did it.
Logged

London, GB
Offline Offline
Sr. Member
****
Karma: 7
Posts: 332
Nothing works.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I downloaded it, and inside a finf folder there's a file called “finf.pde” which I double clicked on and it opened the Arduino application (v22), and then did nothing else, so I wondered how to make it work and found no information on this on your “github” website. Later, however, I double clicked on it again and it then appeared in the already open Arduino application. That's a relief. However, upon setting the board to “Lilypad Arduino with Atmega 328” and my appropriate serial port “/dev/tty.usbserial-A700eXhz” and pressing the upload button, I get the following:


core.a(main.cpp.o): In function `main':
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/main.cpp:7: undefined reference to `setup'
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/main.cpp:10: undefined reference to `loop'


What do I do next?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Ian

Try removing the "inline" keyword of setup() and loop() functions; they're the last two functions in the finf.pde sketch. I don't know why it's not working, but I've used Arduino 0017 (I'm on a flaky connection and that's the one I could find lying around my hard drive).

@pwillard

Yeah, it didn't. But this is more of a programming exercise than anything else. smiley
Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Very neat. FORTH may not be #1 on anyone's list of languages but it is still a great choice for interactive control on a highly-embedded system. For a flavor of what interactivity is like without the "strangeness" of FORTH give Bill Roy's Bitlash a try:

http://bitlash.net/wiki/start

I think the "strangeness" of FORTH is charming and look forward to this FORTH project evolving (can't do much without looping smiley-sad )

--
The Rugged Motor Driver: two H-bridges, more power than an L298, fully protected

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bitlash is news to me; I liked it. Currently reading its source code to gather some ideas on how to conserve memory.

BTW, RuggedCircuits, non-nested loops works fine under FINF; but as is the case with if..else..then constructs, it only works inside a defined word. I'll fix this later.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wrote a serial terminal mode for FINF, so it works also without Arduino's Serial Monitor. The provided shell works as you might expect, with common shortcuts such as Ctrl+L to clear the screen, Ctrl+W to delete the last word, etc. It also offers some colored output to help understand better the output. Here's a nice screenshot of a terminal program running on my computer:



(BTW, the terminal code can be easily reused if one needs this kind of thing; it's pretty small, uses only about 500bytes of Flash and 32 bytes of RAM (for the line buffer))
« Last Edit: January 02, 2011, 10:15:08 am by lafp » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Forth never really caught on... did it.
It did with me.
I wrote a PCB layout program in it on the Mac in the late 80's, I also did a lot of image processing research with it at my University. I also taught it for about 8 years at the University.

About 8 years ago I went to an access control company and a lot of there stuff was written in Forth. I was the only one in the company who could understand it.
A great little language.
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16548
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was the only one in the company who could understand it.

Might that say something about forth in itself? Or not.  ;D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, FORTH is pretty obscure, specially by today's standards. But it is simple, short, powerful and -- once you get the hang out of it -- easy to program on.

Of course, it is so different from popular languages that finding examples for it is pretty difficult; and I assume most people that uses Arduinos are learning to program, and this is a problem for them.

Being a programmer for almost 20 years (if typing BASIC programs at age 8 copied from an user's manual count as programming), I can say however that learning non-mainstream languages -- the weird ones, not only on their syntax but on how you're supposed to do things on them -- will greatly improve your problem solving abilities.

FORTH is great as in there is practically no syntax: it's like playing with LEGO, but you often have to create your own bricks. And being interactive, it remembers me the fun it was to program on BASIC on a MSX computer.

FINF is my third FORTH interpreter (it is actually a rewrite of my first FORTH environment), and so far, the one I had the most fun writing. I'll probably try something along the same line later; perhaps a dialect of BASIC, why not?

Anyway. The last couple of days I've sent some bug fixes and new features. FINF is now pretty stable and uses a lot of less memory (didn't know about the PROGMEM trick). Things that were previously not working, such as nested if..then and begin..until blocks are working; they also work while typing the program interactively, which is quite nice. The only thing really needed is a way to store your program; I might use the EEPROM for that, or add support for SD cards in the future.

So, if you've tried FINF the first time I've announced it here, try it again. Be sure to uncomment the "#define TERMINAL 1" line and use a terminal program like GNU screen (Mac / Linux) or Putty (Windows).

Have fun! smiley
Logged

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 3
Posts: 348
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"FORTH never really caught on"!?!?

I guess Adobe NEVER gives any credit to Forth and they should because Postscript IS Forth!
If you ever had an HP programmable calculator, getting into Forth was easy.

Some of my favourite applications are written in Forth, Voyager 1 & 2, the desktop planetarium programme I have running on my Mac (plus!) is a conspicuous example.

Logged

Halifax, Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Don't anthropomorphize computers. They hate that.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Some of my favourite applications are written in Forth, Voyager 1 & 2, the desktop planetarium programme I have running on my Mac (plus!) is a conspicuous example.

I played with Forth on a Sinclair ZX81, back in the day. I recall reading somewhere that Forth was popular with astronomers who used it to control large telescopes. I suppose that enthusiasm has migrated to astronomy software today.

As an artist and not a mathematician, I always thought the phrase 'reverse polish notation' was pretty cool.
Logged

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 3
Posts: 348
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lot's of observatories use Forth.
The Arecibo radio telescope and the VLA radio telescope in New Mexico, this is not surprising, Charles Moore worked at the National Radio Astronomy Observatory (NRAO), he was a freelance programmer working mainly on control and data acquisition, which is where Forth really shines.
The contempt that C programmers show Forth (and just about any other language) is amazing!
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16548
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The contempt that C programmers show Forth (and just about any other language) is amazing!

Not me. I have equal contempt for all software languages. Solder is the best programming language.  smiley-wink

Lefty
Logged

New Zealand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
arduino: far too much like electrical engineering.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's pretty impressive... I wonder if you could fit a PHP implementation into an Arduino, that'd take an Arduino webserver to a whole new level.
Logged


-- Alec

Pages: [1] 2   Go Up
Jump to: