The Apple Motion Sensor As A Human Interface Device
© Amit Singh. All Rights Reserved.Background
I described the working of the Apple Motion Sensor (AMS), a feature of the PowerBook line of computers, in a previous document called "The PowerBook Sudden Motion Sensor". As I noted earlier, my original reason for investigating the AMS was to fabricate some interesting programming examples for my forthcoming book on Mac OS X internals. The discussion of the AMS generated a substantial amount of interest — perhaps more than I had expected. It is quite interesting to observe how one's fantasy is tickled by abnormal ways of doing the normal.
In the following discussion, I assume familiarity with the concepts and terminology introduced in "The PowerBook Sudden Motion Sensor".
AMS Tools
I provided the following Mac OS X applications that use the orientation data retrieved from the AMS:
- AMSVisualizer A graphical application that displays a 3-D picture of a PowerBook. The picture's orientation is a real-time approximation of the PowerBook's physical orientation. Thus, the on-screen picture moves with the movement of the AMS-equipped PowerBook.
- Stable Window A graphical application that creates a window displaying a bicycle wheel. The window is "stable" in the sense that if you rotate the AMS-equipped PowerBook left or right, the window compensates by rotating itself by an equal amount in the opposite direction.
- AMSTracker A command-line program that retrieves raw orientation data from the AMS hardware.
AMSTracker proved to be the most interesting application. While the others are software "toys", AMSTracker can be used as a fundamental building block to create arbitrary applications that do something interesting based on the computer's orientation.
Still More Interesting Uses
I had also mentioned other ways in which the AMS could be employed, such as for scrolling in applications and for input in video games. The latter was constantly brought up in the feedback I received from "the Internet". Apparently many owners of AMS-equipped PowerBooks wish to experience physical motion-based gaming.
The games most suitable for being played using the AMS would be those that wholly or primarily require directional input. The x and y components of the AMS orientation data can be mapped to the motion of a mouse in a 2D plane, or to specific keys (say, the arrow keys) on a keyboard. Thus, it is conceptually straightforward, both for the programmer and the end-user, to use the AMS as a human interface device (HID).
Using the AMS as a Human Interface Device
While one could modify existing applications, including games, to accept AMS orientation data as input, it would be unnecessary and sub-optimal to do so in most cases. A surprisingly large number of people seemed to think that applications and games must be specially written to be "tilt-sensitive". While such an approach may be justifiable in certain special cases (such as when you need to process the orientation data for a purpose other than human input), doing so would typically be an impulsive reinvention of the proverbial wheel. If the application of interest already receives mouse or keyboard input, it is the AMS that should masquerade as a mouse or a keyboard. After all, one does not create games from scratch for specific models of keyboards, joysticks, or mice. In most cases, the transparent approach would allow the AMS to be used with target applications without any modifications to the latter. This is especially desirable when you do not have access to the source of a target application.
An AMS-based HID could be implemented in two broad ways, as described below.
The Ideal Solution
A particularly seamless solution would be to create a HID driver for a pseudo-device whose input "signals" are the motion sensor's orientation data. Such a software mouse or joystick would then be available to applications simply as yet another input device.
While an AMS-based HID driver is feasible, it is somewhat easier to adopt a relatively "Quicker and Dirtier" approach.
The Pragmatic Solution
Given my utter lack of free time, I had promised myself (and some other people) that I would not experiment with motion sensors beyond what I had already done. There are several justifiable reasons for such a vow:
- It is admittedly frivolous to attempt to connect everything in the Universe to a motion sensor. Sensibility ought to curb motion sensing after a limit.
- I do not have an AMS-equipped PowerBook, which makes it harder to validate ideas. Earlier, I had been simulating the AMS using the mouse pointer's movement. If I were to use a mouse-based or keyboard-based "fake" AMS to masquerade as a fake mouse or a keyboard, it begins to get somewhat messy and cumbersome.
- I have to finish writing a book with an impossible schedule, which leaves no time for arbitrary endeavors.
Now, I had played a game (on real hardware) that was modified (hardwired, for experimentation) to use the motion sensor's data, and it was a very entertaining and unique experience. Moreover, I had received numerous requests for an AMS-based game. In light of all this, I decided to create a user-space "mapper" of AMS data to a HID. While not as clean a solution as a HID driver, it is more flexible and easier to use in many cases. These properties make it particularly suited for experimentation by the end-user.
AMSServer: AMS Over the Network
As alluded to earlier, I still needed a way to simulate the behavior of a user-controlled motion sensor without involving the mouse or the keyboard on the "local" machine. A simple solution was to extend the existing local simulation across the network.
AMSServer is a program that makes the data from a real or fake (mouse-based) AMS available over the network. My AMS API can thereafter use an additional type of AMS: network-based. A client of this API is oblivious of the source of the orientation data, which can now be one of the following:
- A real AMS on the local machine
- A real AMS on a remote machine running AMSServer
- A simulated AMS on the local machine
- A simulated AMS on a remote machine running AMSServer
It would be interesting to come up with creative uses of across-the-network orientation data. Specifically, what could you do with a computer's physical orientation being transmitted to someplace else in near real time?
Let us now look at the program itself, which I call AMS2HID.
AMS2HID
Figure 1. AMS2HID
AMS2HID is a prototype application that translates raw AMS orientation data into input data from a keyboard or a mouse. It presents the translated data to other applications through synthesized keyboard and mouse events. Thus, you can use an AMS-equipped PowerBook as an orientation-driven mouse or keyboard. Perhaps the most interesting and entertaining demonstration of AMS2HID's capabilities is its use for playing a variety of existing games.
AMS2HID performs the AMS-to-HID channeling for a single application at a time. Whenever its target application is active (that is, it is the "front" application), AMS2HID activates its event synthesis. Conversely, when the active application is not AMS2HID's target application, AMS2HID is dormant.
In a pinch, the use of AMS2HID could be described as the following (refer to Figure 1): you select a HID personality (mouse or keyboard), and a target application from a list of currently running applications. The target application will thereafter receive HID events based on the PowerBook's orientation and movement.
Note that unlike AMSTracker, AMSVisualizer, and StableWindow, the version of AMS2HID available on this page requires an AMS-equipped PowerBook: it will not simulate the AMS. I may make AMSServer available in the future. An important benefit of AMSServer is that AMS2HID can reside on any Mac OS X computer, and may be remotely driven by another computer. The driving computer may or may not be AMS-equipped. The latter case is akin to a "remote mouse".
The AMS2HID User Interface
Let us look at the tunable parameters available in AMS2HID for mouse-event synthesis, with reference to Figure 2. Corresponding parameters and their semantics for keyboard-event synthesis are identical except when otherwise noted.
Figure 2. AMS2HID (Mouse)
- Select a HID personality (such as "Keyboard" or "Mouse") using this pop-up button. AMS data will be translated to events corresponding to the selected device.
- Select a target application. HID events will only be synthesized for this application, and only when it is the "front" application.
- Select whether to run in "Normal" or "Test" mode. Although unlikely in practice, it is possible under certain circumstances for the user-interface to become unresponsive to events from the physical keyboard or mouse. This may happen if the combination of AMS2HID settings, the PowerBook's orientation, and available resources is such that more events than what can be handled are being generated. In such a case, you may not be able to interact with AMS2HID, its target application, or perhaps even the rest of the user interface. If you have a sane "cold zone" defined (see item 11), keeping the PowerBook level and steady will quiesce AMS2HID. Alternatively, logging in from another computer, say via SSH, should allow you to terminate AMS2HID from the command line. Finally, Test mode allows you to use AMS2HID with a 60-second auto-reset timer. Regardless of what state AMS2HID and its target application, if any, are in, AMS2HID's state will reset to no HID personality being selected. It is recommended that you test AMS2HID in each new scenario before running it normally.
- When AMS2HID is running in Test mode, a countdown timer appears in this area.
- This area shows the raw orientation data retrieved from the AMS.
- Select this button to cause the mouse pointer to visually (in addition to logically) move on the screen in response to AMS-caused events. De-selecting the button causes the pointer to remain visually stationary. The corresponding setting for the keyboard personality is "repeat keys", which simulates a rapid sequence of key presses and depresses, as opposed to a key being held continuously.
- This specifies the frequency at which the AMS is sampled, and thus represents the maximum number of events that can be generated in a given time interval. 60 Hz is an appropriate frequency for a mouse.
- This setting is used to reverse the logical direction of the x-axis. It can be used when AMS2HID's sense of direction along the x-axis is the opposite of that of the AMS.
- This slider, initially at zero, can be moved left or right to correct the "zero-error" of the AMS, if it has any. Zero error can be determined by keeping the PowerBook on an approximately perfectly level surface, and noting the raw orientation values. If the x and y values are not zero, then correction values with the same magnitudes but opposite signs can be specified. However, even if you do have a minor zero-error, you should try AMS2HID with its default settings before correcting the error.
- The sensitivity value represents the minimum "delta" for which AMS2HID would generate an event. The maximum sensitivity value corresponds to the minimum delta.
- A "cold zone" along an axis is a line segment that defines a range of AMS values for which AMS2HID will not generate any events. It corresponds to the center of the display screen in case of the mouse, and a no-keypress zone in case of the keyboard. For example, an x-axis cold zone specification of 10 means that unless the AMS x value is more than 10, or less than -10, no events will be generated for that x value. Thus, the combined x and y cold zone values define a rectangle. This feature is useful since it is normal for the AMS values to fluctuate by minor amounts even when the PowerBook is perceptibly still.
- Settings 8 through 11 are y-axis settings corresponding to those discussed above for the x-axis.
- Since the ranges of possible AMS x and y values are different from typical screen dimensions, transforms must be applied to scale AMS values to arrive at the locations of the synthesized events. If you have multiple displays, only the resolution of the primary display (the one with the menu bar) is used in these calculations. This area shows the resolution in use.
- This area shows the currently active application.
Experimenting With AMS2HID
Let us briefly discuss two uses of AMS2HID: first, to play video games, and second, to scroll in an application.
Playing Games
Figure 3. Neverball Like Never Before
AMS2HID allows you to play games using the PowerBook itself as a controller — the computer's motion in physical space provides input to games. As noted earlier, any game whose primary controls are directional should work well with AMS2HID. This includes many car-racing or driving games, where "left"/"right", "up", and "down" can be used for steering, acceleration and braking, respectively. Flight simulators and maze-based puzzle games are other likely candidates.
A game extremely well-suited to AMS2HID is Neverball, in which you "tilt the floor to roll the ball through an obstacle course before time runs out". In this case, the physical tilting of the computer closely matches the virtual tilting required in the game to result in a very immersive and enjoyable gaming experience.
Neverball is available for Mac OS X, among other platforms. Its source code is available under the terms of the GNU General Public License (GPL).
The following is a rough sequence of steps needed to play Neverball through AMS2HID:
- Ensure that the PowerBook is stationary and level, ideally on a flat surface. It is desirable that the PowerBook is stationary and level whenever you need to generate events from a physical mouse or a keyboard while AMS2HID's target application is the "front" application. Once you get used to AMS2HID, you may find that this is not a requirement, and you can normally use the physical HIDs in most cases.
- Launch Neverball.
- Launch AMS2HID. Note that Neverball automatically suspends gameplay when it is not the current application.
- Select the keyboard personality using the "Select HID" pop-up button.
You should also try Neverball with the mouse personality. It is possible that you find that the mouse offers finer-grained control in the game.
- Select Neverball as the target application using the "Select An Application" pop-up-button.
- Optionally, adjust any keyboard settings. If this is the first time that you are attempting to use AMS2HID with Neverball, select "Test Mode" using the pop-up button that says "Normal Mode" by default. At this point, switching to Neverball will activate AMS2HID's event generation core.
- Switch to Neverball, and select any game options if necessary (as you normally would). Finally, start a new game.
- Pick the PowerBook up and start playing, using the PowerBook itself as the game controller.
Movies
This movie (QuickTime format, no audio, 2.96 MB) depicts the use of AMS2HID for playing Neverball.
This movie (QuickTime format, with audio, 5.44 MB) depicts the use of AMS2HID for playing several games, such as a flight simulator, car-racing games, and so on.
Scrolling In Safari
You may use the following steps to scroll horizontally or vertically in an application, say Safari, by tilting the PowerBook appropriately:
- Ensure that the PowerBook is stationary and level, ideally on a flat surface.
- Launch Safari and have it display a page that can be scrolled.
- Launch AMS2HID, and select the "Keyboard" personality using the "Select HID" pop-up button.
- Select Safari as the target application using the "Select An Application" pop-up button.
- Depending on the content being displayed, you may need to enable the "repeat keys" option to be able to scroll the content.
- Switch to Safari. Pick up the PowerBook and start scrolling.
Movie
This movie (QuickTime format, no audio, 5.03 MB) depicts the use of AMS2HID for scrolling the content of the Google Maps web site in Safari.
Tips and Miscellaneous Notes
The out-of-the-box experience while using AMS2HID will differ based on various factors such as the calibration and sensitivity of the specific AMS unit, the nature and behavior of the target application, and individual user skills and preferences. This section contains information that may be helpful while experimenting with AMS2HID.
- For a game that accepts directional from either the mouse of the keyboard, you may find that one or the other personality provides a better experience. In particular, some games may not work well with the mouse personality. This has to do with the mouse pointer warping behavior of the game. In cases where warping the physical mouse would "work", warping the AMS-based mouse may not, because unlike a physical mouse, the AMS-based mouse is dependent on the absolute physical orientation of the PowerBook. To put it differently, logically warping the AMS-based mouse would desynchronize it with the PowerBook's physical orientation.
- You can experiment by making the keyboard sampling frequency higher. While the default of 60 Hz should be appropriate in most cases, a much higher frequency (for example, 300 Hz) may work better for certain games. The maximum possible frequency is 1 kHz (999 Hz to be precise).
- Observe the "raw" readings when your PowerBook is still and level (preferably placed on a flat surface). It is normal for the readings to fluctuate in tiny amounts. Ensure that the x and y cold zones are somewhat larger than the fluctuations along the corresponding axes. In most cases, the default cold zone values should suffice. An appropriately sized cold zone rectangle will cause AMS2HID to stop generating events when the PowerBook is placed in a level position. This is particularly useful when you wish to use the physical keyboard or mouse, say, to switch to an application other than the AMS2HID target application, to perform mouse clicks, and so on.
- Try decreasing the sensitivity values, perhaps in decrements of 1, if you find that mouse is "jumping" around when you expect it to be stationary.
- In general, if you must change any AMS2HID settings from their default values, do so in small amounts, rather than changing one or more settings drastically.
- Before trying any combination of AMS2HID settings (including default settings) with an application, run AMS2HID in its Test mode so that you can recover from potential problems that may prevent you from using the physical keyboard or mouse. Alternatively, enable remote login on the PowerBook and have another machine available from which you can log on to the PowerBook in case of a problem.
Some Limitations
- AMS2HID does not currently synthesize mouse clicks. An option could be to have a certain amount of change in the z value trigger a mouse click.
- When the keyboard personality is in effect, the tilting of the PowerBook along the x and y axes is translated only to the arrow keys. If a game insists upon using some other keys for directional control, the current version of AMS2HID will not work. However, it would be trivial to make this translation configurable.
- The current mechanism used by AMS2HID to detect application switching is not foolproof.
Download
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Please understand that AMS2HID is currently being made available as a prototype-level preview version that is functional for ten minutes, after which it must be restarted. AMS2HID may not be redistributed.
Do you agree to these terms and conditions?
Acknowledgments
I would like to thank Ted Bonkenburg for his invaluable feedback on AMS2HID. Ted, a longtime friend, is one of the finest minds I know in Computer Science. I have never had a fruitless discussion on a technology-related matter with him, and this time was no different.
Discuss this article on Kernelthread Forums
>>>
Amit Singh