ANT+

The interface for player control and communications is the ANT+ protocol / data transfer specification. Under the ANT+ protocol, sensor information is communicated wirelessly to various devices include sport watches and computers. In this case, the data streamed wirelessly is collected by a USB "ANT Stick" and made available to the computer which runs the game. The key data obtained for this demonstration is user's Heart Rate and Power produced by the user on the bicycle, and Cadence, or how fast the user spins the bicycle's pedals.

Power

Power in cycling is a measurement of force or torque applied by the cyclist, and is therefore a measurement of cycling effort. This force is measured in a number of different ways depending on the device used. Commonly, strain gauges are used in these measurement devices - called power meters. This information is often combined with the angular velocity to produce the final power value or estimate, which is measured in Watts. The power metes in cycling measure forces at the Crankset, Crank Arm, Pedal, Rear Hub or Chain.

Power is the chosen method for player control because formulas equating power to acceleration, and thus speed are easy derived. These formulas take into account counteracting forces of rolling resistance, air resistance and gravity. Though the current market for power meters is currently changing, the expense associate with one is sometimes considered prohibitory. The cost associate with the above mentioned methods ranges from $600-$2000 USD. For the purpose of estimating power output, the combination of (rear) wheel speed and the power-speed distribution / profile of the trainer used is often used. An ANT+ Rear wheel speed sensor is much more affordable, and with a bit of calculation, a relatively accurate power value is determined. Software such as TrainerRoad has sufficiently demonstrated this capability, but this is not currently developed in this demonstration.

Retrieving ANT+ Data in Unity

The ANT+ alliance provides a C++ library for communication with the USB "ANT+ Stick", making it available to applications. Following are sample methods used to retrieve ANT+ data in a Windows Unity Application / Game.

Unity 5 ( Personal or Professional ) & Unity 4 Pro:

Note: ANT_WrappedLib.dll is a 32bit DLL. For that reason, this will not work in the 64bit editor for Unity 5, but should work if you build a 32bit (x86) application.

  1. Download latest Windows ANT+ library from the downloads page. Registration as an ANT+ Adopter is required to access the documentation and SDK, but is free to test out development.
     
  2. From that packages copy ANT_WrappedLib.dll, DSI_CP210xManufacturing_3_1.dll, DSI_SiUSBXp_3_1.dll into the unity project. I've found these files can be placed in the root of the Unity project folder (outside of Assets).
     
  3. From the package, copy the entire ANT_Managed_Library folder into the Assets Folder of the Unity project
     
  4. From the copied ANT_Managed_Library folder, you can remove the ANT_FS and Properties Subfolders as well as the .csproj file
     
  5. With those in place, Unity will complain about a few things:
    • First, a needed .NET dll: Sytem.Management is missing, This needs to be copied from the .NET framework assembly. Generally, these files are found in C:\Program Files (x86)\Reference Assemblies\Microsoft. This can be copied into the Unity Project - in the Assets or Plugins directory
    • Second, change the API Compatibility Level in the Player Settings from ".NET 2.0 Subset" to ".NET 2.0"
       
  6. We need one modification to the copied ANT_Common.cs file. The checkUnmanagedLibrary method will throw an exception since AppDomain.CurrentDomain.BaseDirectory is null by default (or not provided by Unity.) Add using Unity.Engine to the top of that file to allow it to grab the BaseDirectory.
     
  7. With all that set up, we need to implement the libraries for our needs. This implementation will vary depending on what you are looking for from the ANT+ data. I based this on the source provided in the DEMO_NET project. That code is the best resource for learning how to open a connection and poll data in a .NET environment.
     
  8. Finally, we add some Unity functionality to call these methods at a specified interval to retrieve and display data.

Unity 4 Free

  1. Download latest Windows ANT+ library from the downloads page. Registration as an ANT+ Adopter is required to access the documentation and SDK, but is free to test out development.
     
  2. Create a project in Visual C# that would build a managed DLL (the aforementioned C# wrapper) using files from the ANT+ library and additional code to implement the specific methods you need. This would focus on establishing connection to the necessary device using its profile information and then accessing and possibly formatting that data in a method that can be called in Unity. For my demo, I accessed data from a Power Meter, Speed/Cadence Sensor and HR Strap.
    I’ve found that the DEMO_NET project in the SDK is extremely useful as a starting point since it provides an example on open/closing communication to the device.
     
  3. Compile this project into a (managed) DLL which can be loaded into Unity free.
     
  4. Place DLLs in the unity project folder. This includes The unmanaged DLLs / Interfaces provided by the SDK (DSI_SiUSBXp_3_1.dll, DSI_CP210xManufacturing_3_1.dll, ANT_WrappedLib.dll) as well as any DLLs you build. The precise location varies based on configuration of the Visual C# project. For me, the unmanaged DLLs are in the Unity Project root, and my built DLLs are in the assets folder.
     
  5. When the Unity Project successfully compiles with the new DLL, available methods created in the Visual C# project will become available.
     
  6. Regularly call the available methods created in step 2 from the various Unity scripts to retrieve the data. I’ve found out the hard way that only one program can access an ANT+ Stick at a time, so close any other programs that may be trying to access it when testing.