Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

Problem on Unity With ANT+

Rank

Total Posts: 4

Joined 2018-10-13

PM

Hello,

I am a student from Sun-Yat-sen University, China.I am trying to receive speed data from a magnet-less speed sensor in Unity.

Now, I have :
·a ant+ usb stick,
·a speed sensor,
·a x64 win10 PC
·some dll from the sdk

Problem

In the ANT+ SDK 3.5, I modify the c# sub-project "DEMO_NET" which is a c# console app and run , I can receive the speed data from my speed sensor. However, I use the same c# script in Unity, but it doesn't works, saying that


Demo failed with exception:
Could not connect to any device.
Detials:
Arguments cannot be null


I found that the code below doesn't work, in Unity. The device0 here refers to the ANT+ USB STICK in c# sub-project "DEMO_NET", but in Unity, it's null.

device0 = new ANT_Device(); // Create a device instance using the automatic constructor (automatic detection of USB device number and baud rate)



Additonally, the Unity support .Net frame most up to 3.5, so I recompiled the source code in ANT+ SDK. And the needed .dll are placed in the same file, so they should refer each other.
Can somebody help me?

Some Threads in the Forum

the thread 6709 https://www.thisisant.com/forum/viewthread/6709/ says that, there is an unity ant+ asset called ANT+ FOR UNITY, in https://assetstore.unity.com/packages/tools/network/ant-for-unity-57137.But it is unavailable now.

There is also a ant+ library in unity, call "Advanced ANT+", https://assetstore.unity.com/packages/tools/network/advanced-ant-71980 But i.t cost $60, which is too expansive to a student like me.I just develop ant+ with unity for learn instead of commerce.

ATTACHMENT
The attchment is my unity project and the output scene.

ahhhhh, I uploaded the attachments, but can not found them in my post., so I upload they in a cloud-disk, and share.
·the output scene in Unity https://pan.baidu.com/s/1A5eqd-A3CcFqEAgbtVOBDA
·PluginTest.rar: the unity project :https://pan.baidu.com/s/19fdaDXXdKFXid4pgffba9g

if the page is in Chinese, click the button "下载(xxxKB)" for "download(xxxKB)".      
Rank

Total Posts: 2

Joined 2019-03-12

PM

Hi there, wondering if you managed to get this sorted. I'm trying to get data from Ant into Unity as well.      
Rank

Total Posts: 4

Joined 2018-10-13

PM

Hi, dmen.
I did it, howerver, in a ugly way. I start a thread in the C# program to listen a tcp connecttion which respon with the speed once on request. And in the unity, I start a thread to request the speed every 200ms.
It works and it's enough for me to play.If you are developing a stable commercial app, you'd better find a more integrated and more beautiful way.
     
Rank

Total Posts: 2

Joined 2019-03-12

PM

Thanks, I think I will pick up the Advanced ANT asset from the store. Seems like it should work really well.      
Rank

Total Posts: 4

Joined 2020-06-11

PM

Although this is an old post, I'd like to second that Advanced Ant is a great tool for getting Ant+ sensor data into Unity.

We also managed to recognize and store specific sensors via PlayerPrefs in the computer registry to have multiple devices in the same room without interference.

But the code needs some modification to work well in Unity in our case e.g. maxing out timeout to prevent that the sensor goes into sleep mode

Add 255 in setLowPrioritySearchTimeout to 255 in the AntChannel.cs in ConfigureAnt()

setLowPrioritySearchTimeout(255)      
Rank

Total Posts: 12

Joined 2017-01-13

PM

I have managed to link separate ANT interface programme and Unity in C# to link to a turbo and heart rate monitor. It uses a Memory Mapped File. Because the link is just data in the MMF, it doesn't matter that ANT is X86 and Unity is X64. It isn't an elegant solution but it works. Here's the relevant code in the ANT programme. The code in Unity is pretty similar. I pass gradient (to control the turbo) one way and power, heart rate and cadence the other way. Hopefully this is enough to get anyone who wants to try this method started. I always start the ANT programme from Unity before trying to read data in Unity. The hardest part was working out that it could be done this way!

As a side issue. When I first did this, I found my link to the Elite Turbo kept dropping out. The trick to prevent this was to send something to the turbo every few seconds minimum because anything over 10 seconds between talking to the turbo and it would drop the connection. I use 293 milliseconds, ie something out of sync with ANT's messages.

using System.IO.MemoryMappedFiles;
using System.Threading;

static MemoryMappedFile mmf;
static Mutex mutex;


static bool mutexCreated;

static void StartMemoryMappedFile()
{
try
{
mmf = MemoryMappedFile.OpenExisting("testmap");
}
catch
{
mmf = MemoryMappedFile.CreateNew("testmap", 30);
}

try
{
mutex = new Mutex(true, "testmapmutex", out mutexCreated);
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(1);
}
mutex.ReleaseMutex();
}
catch { }
}

static void WriteToMappedFile(float heartRate, float powerW, float cadence, float gradientPct)
{
mutex.WaitOne();
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(heartRate);
writer.Write(powerW);
writer.Write(cadence);
writer.Write(gradientPct);
}
mutex.ReleaseMutex();

}

static void ReadFromMappedFile(ref float heartRate, ref float powerW, ref float cadence, ref float gradientPct)
{
mutex.WaitOne();
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryReader reader = new BinaryReader(stream);
heartRate = reader.ReadSingle();
powerW = reader.ReadSingle();
cadence = reader.ReadSingle();
gradientPct = reader.ReadSingle();
}
mutex.ReleaseMutex();
}

static void WriteHeartRateToMappedFile(float newHheartRate)
{
float heartRate = 0;
float powerW = 0;
float cadence = 0;
float gradientPct = 0;
ReadFromMappedFile(ref heartRate, ref powerW, ref cadence, ref gradientPct);
WriteToMappedFile(newHheartRate, powerW, cadence, gradientPct);
}

static void WriteTurboToMappedFile(float newPowerW, float newCadence)
{
float heartRate = 0;
float powerW = 0;
float cadence = 0;
float gradientPct = 0;
ReadFromMappedFile(ref heartRate, ref powerW, ref cadence, ref gradientPct);
WriteToMappedFile(heartRate, newPowerW, newCadence, gradientPct);
}
     
Rank

Total Posts: 3

Joined 2023-04-12

PM

I have the same issue. Thank you, Steve.

coreball