Desktop Sense HAT emulator

If this post gives you a sense of déjà-vu it’s because, last month, we announced a web-based Sense HAT emulator in partnership with US-based startup Trinket.

Today, we’re announcing another Sense HAT emulator designed to run natively on your Raspberry Pi desktop, instead of inside a browser. Developed by Dave Jones, it’s intended for people who own a Raspberry Pi but not a Sense HAT. In the picture below, the sliders are used to change the values reported by the sensors while your code is running.

sense-emu

So, why do we need two versions?

  • For offline use, possibly the most common way Raspberry Pis are used in the classroom.
  • To accommodate the oldest 256 MB models of Raspberry Pi which cannot run the web version.
  • To allow you to integrate your Sense HAT program with any available Python modules, or other Raspberry Pi features such as the Camera Module.

The emulator will come pre-installed in the next Raspbian release but, for now, you can just install it by typing the commands below into a terminal window:

sudo apt-get update
sudo apt-get install python-sense-emu python3-sense-emu python-sense-emu-doc sense-emu-tools -y

You can then access it from the Desktop menu, under Programming.

The emulator closely simulates the Sense HAT hardware being attached to your Pi. You can read from the sensors or write to the LED matrix using multiple Python processes, for example.

sense-idle

Write your code in IDLE as before; there are also a number of examples that can be opened from the emulator’s built-in menu. If you then want to port your code to a physical Sense HAT, you just need to change

sense_emu

to

sense_hat

at the top of your program. Reverse this if you’re porting a physical Sense HAT program to the emulator, perhaps from one of our educational resources; this step isn’t required in the web version of the emulator.

sense-emu-prefs

There are a number of preferences that you can adjust to change the behaviour of the emulator, most notably sensor simulation, otherwise known as jitter. This costs some CPU time, and is disabled by default on the low-end Raspberry Pis, but it provides a realistic experience of how the hardware sensors would behave. You’ll see that the values being returned in your code drift according to the known error tolerances of the physical sensors used on the Sense HAT.

This emulator will allow more Raspberry Pi users to participate in future Astro Pi competitions without having to buy a Sense HAT: ideal for the classroom where 15 Sense HATs may be beyond the budget.

So, where do you start? If you’re new to the Sense HAT, you can just copy and paste many of the code examples from our educational resources, like this one. You can also check out our e-book Sense HAT Essentials. For a complete list of all the functions you can use, have a look at the Sense HAT API reference here.

You can even install this emulator on other types of Linux desktop, such as Ubuntu! For more information on how to do this, please visit the emulator documentation pages here.

29 comments

Avatar

Excellent ! Could this be also used in conjunction with a real SenseHat ?

Avatar

Yes, you can have a Sense HAT plugged in and both the python-sense-hat and python-sense-emu packages can be installed simultaneously (this was rather useful during development of the emulator!).

I should also mention that there’s a couple of command line utilities included, one of which is called “sense_rec”. This allows you to record sensor values from a real Sense HAT, then replay the recording in the emulator. I’ve got a bit more work to do on this tool (and the emulator for that matter), but it’s already reasonably functional.

Avatar

sounds good ! Hopefully this will ship directly with Raspbian at some point – sounds like a useful thing to have available !

Avatar

Actually, this is already mentioned in the blog post:

“The emulator will come pre-installed in the next Raspbian release but, for now, …”

Avatar

I’m planning to run sense_rec on the Astro Pis on board the International Space Station during some interesting events like when they fire the maneuvering thrusters. Then we’ll be able to let students play these recordings back in the emulator and test that their code can detect those events.

Avatar

Great Idea

Avatar

Well done Dave ‘picamera’ Jones :)

Avatar

Great thanks! My model A will be still usefull :-)

Avatar

I’ve been using an A+ and a 3B (opposite ends of the spectrum) during development to get an idea of how things run. On the A+ it works, but with the full simulation enabled it does eat at least half the processor time (hence why the full error simulation defaults to disabled on BCM2835 based platforms like the A and A+).

Without the full simulation it runs acceptably. However, the recording replay function I mentioned above (re: sense_rec) is *far* too heavy for the A/A+ (updating all the sliders as the recording plays is too much work). To that end, there’s a command line replay utility (sense_play) included, specifically for use on these platforms (you can use it on any platform, but it was built to work around the performance bottleneck on the slower platforms).

Avatar

Running the install command I am getting the result : Unable to locate package. For all of the packages. I am currently running on a Pi B+ on Raspian Wheezy

Avatar

You have a couple of options, either upgrade to Raspbian Jessie (see https://www.raspberrypi.org/downloads/raspbian/) and then you’ll be able to install it through apt-get or, if you prefer to stay on Wheezy, you can install it through pip (see https://sense-emu.readthedocs.io/en/v1.0/install.html).

Avatar

The .deb packages *should* be Wheezy compatible (I deliberately built them on Wheezy for that purpose) but they may only be uploaded to the Jessie repos.

However, I should warn that I haven’t tested the system on Wheezy; I try and write code that’s compatible with Python 3.2 (the ancient Py3 version that’s available on Wheezy), but I’ve only got enough time to test on a few platforms. In this case, the emulator’s been tested on Raspbian Jessie (under Py2.7 and 3.4), Ubuntu (Py2.7, and 3.5), and Windows (Py3.4; joystick emulation partially broken due to GTK3 bugs).

If you do give it a try and run into bugs, do file a report on the GitHub repo (https://github.com/RPi-Distro/python-sense-emu) and I’ll see what I can do, but I’m afraid Wheezy support isn’t likely to be a particularly high priority.

Avatar

Seems to work on my Pi-Top laptop easily enough. Just have to ensure I have enough room for the Pi-Top Proto-Board and see if that will run a Sense HAT module.

Avatar

Excellent, I had noticed it there a few weeks back but thought nothing of it. Assumed it was something to do with the web based version.

anyway, this lets me send the kids off testing at home without the need to also given them one of the few SenseHats’s we have at school.

Many thanks.

Avatar

Is there a way to loop through a read Sense HATs data? I think that could be very useful for testing to switch to the real hardware with just the click of a button instead of changing and restarting your code (even though it is just a small code change). Also, it would be great to have recording and playback functionalities like in sense_rec directly inside the program.

Avatar

We’re planning to run sense_rec on the Astro Pis on board the ISS so you can all have some in-space sensor measurements to play back and run your code against. But I don’t think these kinds of enhancements are planned currently. Feel free to raise an enhancement request here: https://github.com/RPi-Distro/python-sense-emu/issues

Avatar

Playback functionality is obviously built-in but recording is a bit trickier as that then means loading both the sense_emu and sense_hat libraries into a single process (should be possible, but have to be careful about that). I should also mention there’s a “sense_play” command line utility so you can script both recording and playback if you want.

Avatar

Oh, and before I forget there’s “sense_csv” too which can convert a recording into CSV format. This is largely intended for debugging / analysis but it might prove useful in ways I haven’t thought of yet.

The idea of making a pass-thru to the real Sense HAT is intriguing. I’m not sure how easy/hard that is off the top of my head, but I’ll have a look at it in future.

Avatar

What sense could a Sense HAT make if an emulated Sense HAT could be sensed?

Try saying that after beer ;)

Avatar

Quick Question on this.
How would I get this working on a Windows(10) setup, since that’s much easier to use in a school with all the children.
Then they can be let loose on the Pi’s when they want to transfer it across.
I have had Python installed from python.org installed (v3.5 iirc, whatever it gives as default).

Avatar

Are these offline PCs? If they have Internet access the easiest option for you may be to just use the web browser version of the emulator here: https://trinket.io/sense-hat

The desktop emulator can be made to run on Windows but we’re not officially supporting it because there are bugs in the GTK library under windows which cause problems with the joystick buttons.

However if you still want to attempt it you would have to install GTK+ http://www.gtk.org/download/windows.php and then you should be able to install the emulator through pip. More info on that here: https://sense-emu.readthedocs.io/en/v1.0/install.html

Avatar

They are online since they are school computers used for teaching.
We have plenty of these and few Pi’s, hence it is much better to learn and test on these desktops and move and test on the Pi ‘in the real world’.

Triket seems quite restricted it what it can actually do. The mainstay astropi/sensehat of datalogging does not work, errors instantly https://www.raspberrypi.org/learning/sense-hat-data-logger/worksheet2/ (The download there)
Trinket gives no useful information for debugging.
It’s fine for some simple LED tinkering, but nothing actually useful beyond that. Unless I’m missing something.

I assumed a full python install would be more flexible. Shame the cross compatibility is not there with python.

Will send it to the IT techs to see if they’ll download more things :)

Luckily Joysticks are not something we’re bothered with.

Avatar

See my comment below. Basically, cross compatibility *is* there with Python … not so much with GTK3 though which is what the GUI portion of the emulator is built upon.

Avatar

GTK3 is also painful to install on Windows. Here’s what I did to try things out on my Windows 10 setup:

1. Install Python 3.4.4. PyGObject isn’t packaged for 3.5, so you can’t use that, and Python 3.4.5 doesn’t have a binary build on Windows (for no apparent reason) so you’re stuck on the old version. Make sure there’s no other python versions installed or the PyGObject installer (below) gets confused, and ensure 3.4.4 is included in the PATH when installed.

Link: https://www.python.org/downloads/release/python-344/

(you probably want the x86-64 MSI installer unless you’re on truly ancient hardware)

2. Install PyGObject for Windows. Windows seems to be a distinctly second-class citizen in Gnome (in marked contrast to Qt) so the builds are from some random Sourceforge page. You want the latest build which at the moment appears to be 3.18.2 rev9. When the installer presents an enormous list of packages to you, make sure Gtk and Gdk are selected (I don’t recall needing anything else – all the other dependencies of those were automatically pulled in).

Link: https://sourceforge.net/projects/pygobjectwin32/files/?source=navbar

3. All the other dependencies (numpy and PIL basically) should be handled automatically by pip. Fire up a command line window (cmd) and run the following to install the emulator and all other dependencies:

pip install sense_emu

With a bit of luck everything should now be installed and you can run the following to start the graphical portion of the emulator:

sense_emu_gui

It doesn’t get added to the Start menu as that would require plenty more work (basically, that gets us into the realm of building a full Windows installer and uninstaller). As you’ll discover, various bits of GTK3 are flaky under Windows (the joystick buttons toggle rather than pressing and releasing, the keyboard support for joystick control doesn’t work at all, the sliders don’t smoothly follow mouse control when dragged, etc.) which is part of the reason we don’t want to get into building a Windows installer; that gives the impression that Windows is a supported platform.

With the current state of GTK3 on Windows, it’s simply not realistic for us to support the platform officially. Which is not to say we actively work against it: I’ve tried to be careful when building the emulator to ensure I’m not doing anything which’ll outright fail on Windows, but support is very much “unofficial best effort”.

Avatar

“Python 3.4.5 doesn’t have a binary build on Windows (for no apparent reason)”

https://www.python.org/downloads/release/python-345/ says:
“Python 3.4 has now entered “security fixes only” mode, …. Python 3.4.5 has only been released in source code form; no more official binary installers will be produced.”
:-/

Avatar

Ah, there’s the reason. It’s a little harsh though (reads a bit like “we’ll release security fixes for this, but to strongly encourage you to just upgrade to another version we won’t build the binaries for you”).

Still, 3.4 is pretty outdated at this point and I can understand their desire not to expend resources supporting outdated versions. The issue is more that Gnome’s Windows support seems to be lagging, hence forcing the outdated version of Python.

Avatar

Looks like a job for me to try at home and see if it is usable, before getting the school to have a go.
/me hoping they’ve not installed and need the latest python with the Computing/IT classes.

Many thanks for the detailed explanation.

Avatar

Hello,

nice work, man!

can I also write a program in C to connect to this emulator, or must it be Python?

Avatar

Can I use sense_rec for recording every second and not at 60 hertz?

Sorry for my English!

I want to use for recording 11h and at 60Hz is too much data

Leave a Comment

Comments are closed