Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

You are here: Forum Home → ANT+ Forums → ANT+ Simulation Tools → Thread

   

SimulANT+ Heart rate display script

Rank

Total Posts: 2

Joined 2013-11-13

PM

Hi,

I'm fairly new to both ANT+ and scripting, especially with IronPython that SimulANT+ uses. I've managed to pair my CycleOps heart rate monitor to an ANT+ dongle and get a live feed of data using the Heart Rate Display device profile. I have also managed to export a .csv file of a collected heart rate using the RUN and STOP commands, however, I would like to be able to write heart rate data to a .txt file using a 'pending to file' command where the text file is constantly updating when new data is recorded. I've tried scouring the forums and googling for scripts that show this but I feel as though I'm banging my head against a wall now trying to figure this out.

If anyone could help I would hugely appreciate it.

Regards,
Tom      
Avatar
Rank

Total Posts: 19

Joined 2012-05-08

PM

Hello Tom,

I'd like to start by double checking that I've understood your need properly. So I think you'd like to create a new custom script that records heart rate data into a .txt file every time new HRM data is received. You'll likely even be able to use the hr_DisplayCSV.py file as a template (you can find the SimulANT+ scripts directory in the same folder where you installed SimulANT+.exe).

Luckily, manipulating text files in ironpython is easy enough. All you'll really need is to create a file object using the open() function. Look here for its documentation.

You might end up using something like...
open('filename.txt''w'

... to create your file object.

Then in your DataPageReceived event handler, you might write something like...
f.write('Heart Rate: ' page.ComputedHeartRate '\n'


Don't forget to close your file object when you're done with it, and please share what you come up with! I'll be happy to respond if you need any further help.      
Rank

Total Posts: 2

Joined 2013-11-13

PM

Hi,

Finally got around to having a look at this again recently. A friend who is more scripting savvy took a look at the script and made a few changes which now means the .txt file is constantly receiving and recording live data, exactly what I was after! Here is the code which only has some minor adjustments to the original "hr_Display" python script.

EDIT: Please compare this code to the edited script found in the next post - laprairie

# This script logs raw data received from a heart rate sensor to a CSV file
# It needs to access IronPython modules to use the CSV writer
import clr
clr
.AddReferenceToFile('IronPython.Modules.dll')
from IronPython
.Modules.PythonCsvModule import *

open('hr_log.txt''a'# file stored in same directory as main simulator executable
##logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
#logger.writerow(['Event Time', 'Event Count', 'Heart Rate'])
f.close()

simulator.TurnOn()

# This function gets called everytime data is received; will be logging raw data
def DataPageReceived(pagekey):   
 
open('C:/hr_log.txt''a'# file stored in same directory as main simulator executable
 
logger writer(fdelimiter=','quotechar='|'quoting=QUOTE_MINIMAL)
    
logger.writerow([page.HeartBeatEventTimepage.HeartBeatCountpage.ComputedHeartRate])
 
f.close()

# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
   
simulator.TurnOff()
   
f.close() 


My next step (which is now my next problem), is running two seperate heart rate monitors, on two seprate individuals which export 2 seperate hr data files. I'm assuming I can just run assign 2 scripts which export 2 different file names?
I'm using a CycleOps dongle with a CycleOps heart rate monitor, I also have an ANT+USBm dongle and I was hoping that each HR monitor would pair to a seperate dongle but both dongles pick up the same monitor or nothing at all. Is this a channel pairing issue or have I totally missed something out?
Again, any help is appreciated!

Cheers,
Tom      
Avatar
Rank

Total Posts: 19

Joined 2012-05-08

PM

Hi there! Thanks for posting your code.

I have some small tweaks to suggest in your script, and I can confirm for you that you will be able to receive from both HRMs, and log them separately.

In your posted script, be careful of indenting in the DataPageReceived function, and make sure that the calls to open() point to the same file. At the moment, the open() call inside DataPageReceived points to C:/hr_log.txt instead of a text file in the same folder as the SimulANT+ executable. To more easily manage this, I would suggest storing the file path in a string variable, and using that string in the open() call.

Here's my suggested edits...

# This script logs raw data received from a heart rate sensor to a CSV file
# It needs to access IronPython modules to use the CSV writer
import clr
clr
.AddReferenceToFile('IronPython.Modules.dll')
from IronPython
.Modules.PythonCsvModule import *

# EDIT: this variable will be used for every call to open()
# when creating scripts that will create output to different files, this is the only line that will need to be edited
fileName 'hr_log.txt'

open(fileName'a'# file stored in same directory as main simulator executable
##logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
#logger.writerow(['Event Time', 'Event Count', 'Heart Rate'])
f.close()

simulator.TurnOn()

# This function gets called everytime data is received; will be logging raw data
# EDIT: indenting
def DataPageReceived(pagekey):   
    
open(fileName'a'# file stored in same directory as main simulator executable
    
logger writer(fdelimiter=','quotechar='|'quoting=QUOTE_MINIMAL)
    
logger.writerow([page.HeartBeatEventTimepage.HeartBeatCountpage.ComputedHeartRate])
    
f.close()

# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
    
simulator.TurnOff()
    
f.close() 


After the above modification, I was able to collect data the way you described. I used a single USBm, and configured two Heart Rate Displays on two of its channels to listen to two different HRMs. I then made a copy of your script, changed the output filename, and changed the name of the script file. I ran the two different scripts on each of the Heart Rate Displays, and found two separate log files in my SimulANT+ folder.

Don't hesitate to post any further questions you may have!

Jules      
Rank

Total Posts: 4

Joined 2015-10-29

PM

Hallo,


Well can anyone guide as how to do the same through C#.I need to fetch the data in the log file and then need to decode .It would be nice if anyone can share the code as I am new to all these things....

Thanks



laprairie - 30 January 2014 10:39 AM
Hi there! Thanks for posting your code.

I have some small tweaks to suggest in your script, and I can confirm for you that you will be able to receive from both HRMs, and log them separately.

In your posted script, be careful of indenting in the DataPageReceived function, and make sure that the calls to open() point to the same file. At the moment, the open() call inside DataPageReceived points to C:/hr_log.txt instead of a text file in the same folder as the SimulANT+ executable. To more easily manage this, I would suggest storing the file path in a string variable, and using that string in the open() call.

Here's my suggested edits...

# This script logs raw data received from a heart rate sensor to a CSV file
# It needs to access IronPython modules to use the CSV writer
import clr
clr
.AddReferenceToFile('IronPython.Modules.dll')
from IronPython
.Modules.PythonCsvModule import *

# EDIT: this variable will be used for every call to open()
# when creating scripts that will create output to different files, this is the only line that will need to be edited
fileName 'hr_log.txt'

open(fileName'a'# file stored in same directory as main simulator executable
##logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
#logger.writerow(['Event Time', 'Event Count', 'Heart Rate'])
f.close()

simulator.TurnOn()

# This function gets called everytime data is received; will be logging raw data
# EDIT: indenting
def DataPageReceived(pagekey):   
    
open(fileName'a'# file stored in same directory as main simulator executable
    
logger writer(fdelimiter=','quotechar='|'quoting=QUOTE_MINIMAL)
    
logger.writerow([page.HeartBeatEventTimepage.HeartBeatCountpage.ComputedHeartRate])
    
f.close()

# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
    
simulator.TurnOff()
    
f.close() 


After the above modification, I was able to collect data the way you described. I used a single USBm, and configured two Heart Rate Displays on two of its channels to listen to two different HRMs. I then made a copy of your script, changed the output filename, and changed the name of the script file. I ran the two different scripts on each of the Heart Rate Displays, and found two separate log files in my SimulANT+ folder.

Don't hesitate to post any further questions you may have!

Jules
     
Rank

Total Posts: 1

Joined 2017-10-05

PM

I am new to scripting much like the original poster.

I have a Garmin HR chest strap and an ANT+ usb - all working through simulANT+

I would like to get the HR data into another software to create a GUI with the live HR data, this other software is called Touchdesigner and it can read xmls and can run Python scripts within itself.

I tried the posted script to record to a .txt file - I recieved an error that says "Error: DataPageReceived() takes exactly 2 arguments (1 given)" and the .txt file has no data in it.

Can anyone help me capture the data in a way that I can get it into Touchdesigner OR does anyone know of a script that I can run in Python to capture the data (and therefore I can run that entirely within Touchdesigner) ?

Thank You!      
RankRankRankRank

Total Posts: 120

Joined 2013-05-07

PM

Hi rrmallin,

The script should work with the following modification:
def DataPageReceived(pagekey): 

to
def DataPageReceived(page):