Share this page to your:
Mastodon

Compass and Dalek Detector

Both the Compass app and the Dalek app use the LMS303 IC but for different purposes. The Compass, of course, uses the compass function. The Dalek app makes use of the accelerometer to detect movement. Each movement generates a dalek.

Compass

compass

The bulk of the code here is a bit of trigonometry and graphics library calls, most of which is obvious. I found the most tricky part was drawing the arrow (in the arrow() method). It also calls the Hardware class to get the compass heading. The Hardware class acts as a way to encapsulate the hardware functions away from the logic so there is a compassHeading() method and a getCompassCorrection() method.

The compass correction is interesting because it reminds us that there is a real world away beneath the pure binary logic we normally program in. Each LSM303 will vary a little and they need to be calibrated. My LSM303 values are set in the Hardware.cpp constructor like this:

// Calibration values. Use the Calibrate example program to get the values for
// your compass.
m_lsm303.m_min.x = -880;
m_lsm303.m_min.y = -691;
m_lsm303.m_min.z = -958;
m_lsm303.m_max.x = +262;
m_lsm303.m_max.y = +365;
m_lsm303.m_max.z = -27;

m_lsm303.m_min.x = -495;
m_lsm303.m_min.y = -463;
m_lsm303.m_min.z = +393;
m_lsm303.m_max.x = +495;
m_lsm303.m_max.y = +279;
m_lsm303.m_max.z = +627;
m_compassCorrection = 550;

The best reference I've found for getting your equivalent values is Sparkfun but I have also found that once you get one right the others seem to be about the same, so maybe your LSM303 can use the same values as mine.

Dalek

compass

This app looks a bit like a game, but it is just a display that looks like a radar screen and daleks show up when the LSM303 detects movement, specifically movement on the x axis. The daleks are drawn by the class DalekInstance and that holds an Icon with the dalek picture. This is the same Icon class used by the menu, although in this case the actual image is smaller. The important thing is it knows how to draw itself. We can have as many dalek instances as we want (though most of us prefer fewer daleks to many, of course), and they all know how to draw themselves. If you are not used to Object Oriented programming this is a good example of encapsulation for you.

Summary

This post covered the two apps that use the LSM303: compass and dalek and gave some information on how they were designed.