There is a saying that a house is not a home with out a dog, as it happens I have three dogs and two cats. The three dogs are two Cavalier King Charles Spaniels and a Samoyed, two more different breeds you could not imagine.
Sadly as the oldest cavalier called Harry has aged (he is 9) he has started to develop heart failure a common medical compliant with Cavalier King Charles Spaniels. Lucky he is on tablets which control this and is under the care of the Royal Veterinary College here in the UK.
One of the things the cardiac specialist requested, was that his sleeping respiration rate was monitored. This did not need to be continuous however, the change in his respiration rate from circa 20 breaths per second to greater than 30 will indicate fluid is beginning to build up in his lungs and new medications and approaches are required.
Of course, the earlier this can be detected the better.
As such I thought I would see what I could create that would enable me to monitor Harry's respiration rate and most importantly log it to the cloud so I can see trends and of course I can share it with the cardiac specialist.
ApproachMeasuring respiration can be achieved using either a sensor which works by the patient wearing a monitor, or using RF detection.
As this monitoring does not need to be continuous I selected a RF monitoring method. Using a XETHRU X4M200 sensor.
This sensor is able to monitor motion, distance and respiration which makes it very useful. The sensor is optimized to monitor respiration rate when the subject is at rest, for example sleeping which makes it perfect.
Even better the module can be powered and controlled over a USB connection, which means we can connect it easily to our PYNQ or PetaLinux boards.
For this application I am going to use the PYNQ-Z2 board.
Updating the X4M200To get the X4M200 to work with the communication framework on our PYNQ board we need to first ensure it's firmware is up to date.
To do check we have the latest firmware on the X4M200 we can use the XETHRU explorer which is available for Windows and Linux.
Once the XETHRU explorer starts if the X4M200 module is connected you will see the option to connect to it.
Once you connect to the X4M200 if an update is available, you will be presented the option of updating.
Clicking on the update button will show the current version of firmware and the updated version. Once you click on the install, the X4M200 is updated and upon completion you will see the image below.
To use the X4M200, XETRHU provide a communications framework module which will run on the Arm A9 cores provided by the Zynq. This is communication framework is intended to be used with a Raspberry PI however, we can adapt it to run on the PYNQ.
Our approach will be the following
- Download the Module Communication framework for the RPI from here
- Set up the Python environment to work with the Module Communication framework.
- Configure a number of links for static libraries
When we download the Module Communication framework, you receive a zip file. To install this file the first thing we need to do is upload the zip file to our PYNQ board.
In the jupyter create a new folder and upload the ModuleConnector-RPI-1.6.2.zip file.
Once this is uploaded in a jupyter terminal window enter the following command to unzip the file
unzip ModuleConnector-rpi-1.6.2.zip
This will unzip the contents into the directory just created.
Under directory python35-arm-linux-gnueabhif you will find a setup.py we can install this using the command.
python3 setup.py install
This will install the ModuleConnector framework.
However, before we start working with the ModuleConnector and that means we need to set it up properly. ModuleConnector is designed to work with Python3 version 3.5 as such the version of Python3 we are using on the PYNQ is 3.6 as such we need to install Python 3.5 on the PYNQ board.
To do this we actually need to build python 3.5 on the PYNQ-Z2 and make sure it it's libraries are shareable and available as an alternate installation of Python3
This will take quite a while, in my case it took approximately 6 hours.
To build and install Python 3.5 we can use the command sequence below.
wget https://www.python.org/ftp/python/3.5.7/Python-3.5.7.tgz
tar zxvf Python-3.5.7.tgz
cd Python-3.5.7
./configure --enable-shared --enable-optimizations
make
sudo make altinstall
sudo ldconfig
Once the configuration has been set we can build Python 3.5 using the make command.
Once the build is complete the final stage is to install Python 3.5
With Python 3.5 installed we are nearly ready to get going however we need to make a few links so we can use newer libraries when the ModuleConnector expects an older version.
sudo ln -s /usr/lib/arm-linux-gnueabihf/libboost_filesystem.so.1.65.1 /usr/lib/arm-linux-gnueabihf/libboost_filesystem.so.1.62.0
sudo ln -s /usr/lib/arm-linux-gnueabihf/libboost_system.so.1.65.1 /usr/lib/arm-linux-gnueabihf/libboost_system.so.1.62.0
Once these links have been created if you observed the usr/lib/arm-linux-gnueabhif directory you will see the newly created links
We also need to install pyserial which we can do using the jupyter terminal
pip3 install pyserial
Finally we need to install the adafruit cloud python packages
pip3 install adafruit.io
ApplicationWith our environment all set up we are ready to develop our application and communicate with the X4M200
The first application I created was to test the X4M200 module.
import sys
from optparse import OptionParser
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from pymoduleconnector import ModuleConnector
import pymoduleconnector
from time import sleep
zstart = 0.5
zend = 5.0
XTS_ID_APP_RESPIRATION_2 = int("0x47fabeba",16)
XTS_ID_RESP_STATUS = int("0x2375fe26",16)
XTS_ID_RESPIRATION_DETECTIONLIST = int("0x064e57ad", 16)
XTID_SM_RUN = int("0x01",16)
XTID_SM_MANUAL = int("0x12",16)
XTID_SM_STOP = int("0x13",16)
XTID_OUTPUT_CONTROL_ENABLE = int(1)
FPS = 10
mc = ModuleConnector("/dev/ttyACM0")
x4m200 = mc.get_x4m200()
x4m200.module_reset()
mc.close()
sleep(3)
mc = ModuleConnector("/dev/ttyACM0")
x4m200 = mc.get_x4m200()
try:
x4m200.set_sensor_mode(0x01, 0)
except RuntimeError:
pass
x4m200.load_profile(XTS_ID_APP_RESPIRATION_2)
try:
x4m200.set_sensor_mode(0x01,0)#RUN mode
except RuntimeError:
pass
x4m200.set_output_control(0x610a3b00,1)
while True:
rdata = x4m200.read_message_respiration_sleep()
print ("Frame: {} RPM: {} Distance: {} Movement Slow: {} Movement Fast: {}" .forma t(rdata.frame_counter, rdata.respiration_rate, rdata.distance, rdata.movement_slow, rdata.movement_fast))
sleep(0.2)
When this application was run in jupyter you can see after a few frames the information was output.
The sensor will stake about 2 minutes to start outputting data once the application starts.
The respiration rate will be output once the target is stationary and not moving, When I pointed this at my Harry as he slept for the first time I recorded the
Below are the initial recordings for Harrys' Respiration
Of course, we want to be able to see trends in the data and timestamp the data so we know the date when the reading was taken.
To do this I updated the application to output the data to the Adafruit.io cloud service. This is simple and free to do and provides me with an easily accessible backlog of respiration data for Harry.
aio = Client('name', 'key')
try:
x4m200.set_sensor_mode(0x01, 0) #Make sure no profile is running
except RuntimeError:
pass
x4m200.load_profile(XTS_ID_APP_RESPIRATION_2)
try:
x4m200.set_sensor_mode(0x01,0)#RUN mode
except RuntimeError:
pass
x4m200.set_output_control(0x610a3b00,1)
while True:
rdata = x4m200.read_message_respiration_sleep()
print ("Frame: {} RPM: {} Distance: {} Movement Slow: {} Movement Fast: {}" .forma t(rdata.frame_counter, rdata.respiration_rate, rdata.distance, rdata.movement_slow, rdata.movement_fast))
sleep(10)
aio.send('harry-resp-rate', rdata.respiration_rate)
When ran the application you can see the below the graphical representation of his respiration rate. In this instance I was the test target.
Along with the graphical view, I can also see the time stamped data.
This data can be shared easily with the cardiac specialist at Harry's next appointment.
Wrap UpI know have a simple system that will allow me to monitor and log Harry's respiration rate which can be mounted in the lounge while he sleeps. It will also be further useful too as the youngest Cavalier Sam is also showing signs of heart issues.
See previous projects here.
Additional Information on Xilinx FPGA / SoC development can be found weekly on MicroZed Chronicles.
Comments