Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Thursday, 4 September 2014

Running Sniff on the Lego EV3

In a previous post we discussed how to get a C program running on the Lego EV3, as a working C toolchain is prerequisite for running Sniff. It's quite a hassle to set up, and to get the EV3 running nicely is a bit more of a hassle, but once its set up it's a pretty nice platform for running Sniff on.

The new release (Beta 9) supports the EV3, though so far it only supports the buttons, LED's and the screen. Support for motors and sensors will be forthcoming in the next releases, as and when I can get my hands on the relevant devices.

Once you've installed the C compiler, and the new Sniff release on your host machine, there's a couple of things you need to do to make everything work.

The first thing to do is make sure the bricks WiFi is enabled, and connected. Then on your host machine, initialise Sniff, and run "ev3finder". This prints out the ip address of your Brick. In the shell type:

export EV3HOST=

or whatever the IP address reported is, to tell the system how to find the Brick.

Then connect to the brick and enable ssh:

telnet $EV3HOST

login as root (no password!)

and type


First time setup:

If this is the first time you've used Sniff on the Brick you'll need to make  a folder for it:

mkdir /media/card/Sniff

Now go back to your Mac or Linux machine and you should be able to compile programs using ev3-sniff, just as you would for any other platform. When you compile a program it will be placed in the Sniff folder on the memory card, and you can run it from the telnet session.

However if you want to use the keypad or the screen your program will "fight" the LMS/EV3 user interface, and it'll be a bit of a mess. It also means you have to have another machine, and a WiFi connection which might not always be possible.

The solution to this is based on a trick by RobotNav. In the Sniff/Examples/EV3/SniffMenu folder is a Lego VM program: the source is Sniff.lms, but you just need the compiled version Sniff.rbf. Place this in the Sniff folder with

scp Sniff.rbf root@$EV3HOST:/media/card/Sniff

You can run this from the Bricks on-screen menus. When you do it will try and run a Sniff program called sniffMenu. You'll find this in the same folder. If you simply compile that it will install on the Brick. sniffMenu lists all of the installed programs in the Sniff folder, and lets you choose one to run, so now you can compile the program in your desktop machine, and run it easily on the Brick. SniffMenu is pretty primitive at the moment, but its written in Sniff, so you can make a better version!

EV3 Devices

Currently there are two EV3 specific devices: ev3lcd and ev3Keypad. There are examples of using them both in the Sniff/examples/EV3 folder. Basically ev3lcd behaves in the same way as all the other display devices. ev3Keypad mimics the Gambuino keypad. However the setBacklight uses values 0-9 to represent different patterns and colours, rather than adjusting brightness. Fortunatly 0 is off and 1 is sold green so the results work fairly consistently cross platform.

To test these there's a version of Bounce Out - our favourite game written in Sniff, running on the EV3. The only changes from the Arduino version is to comment out the Sound player, replace Arduino devices with their EV3 equivalent, and add a scale factor so that the game is sized to fill the EV3's larger screen. Things are a little hard to see on the small screen (and the "standard" 5x7 font is a bit small!), but it generally works great.

The one thing to remember if you're running from the EV3 menu (rather than telnet'ing in from your computer) is that your program needs to end, otherwise it will run forever and hang the EV3 (you can still connect by telnet, use ps to find the offending program and kill it)! There's an example in the EV3 examples folder which shows how you can make your program quit when you hold the back button for 1 second. Also remember that "say" and "ask" will only work from the telnet session.

No comments:

Post a Comment