Wp502 Python
Wp502 Python
ABSTRACT
The Python open-source programming language has become a de facto standard in
applications ranging from engineering, scientific, data science, machine learning, information
technology, and artificial intelligence.
Modern systems-on-a-chip (SoCs), when used in embedded applications, make it possible to
run Python to execute complex analytical algorithms with performance close to that of a
desktop workstation—but with a much smaller form factor and significantly lower power
requirements. By pre-processing the data read from sensors, the Xilinx® Zynq portfolio
provides a much higher level of performance and determinism as well as lower latency.
This approach, referred to as the PYNQ framework, effectively offloads many critical but
repetitive operations that consume processor bandwidth unnecessarily from the application
processor. The offloading capability is critical to meeting the need for increased intelligence in
I IoT edge applications.
© Copyright 2018 Xilinx, Inc. Xilinx, the Xilinx logo, Artix, ISE, Kintex, Spartan, Virtex, Vivado, Zynq, and other designated brands included herein are trademarks of Xilinx
in the United States and other countries. AMBA, AMBA Designer, ARM, ARM1176JZ-S, CoreSight, Cortex, and PrimeCell are trademarks of ARM in the EU and other
countries. All other trademarks are the property of their respective owners.
Python and its associated frameworks enable the development of complex algorithms used across
data analytics, machine learning (ML), and artificial intelligence (AI) applications. Of course, these
applications are hot topics within embedded computing and are driving the adoption of Python,
especially within Industrial IoT (I I oT) at the edge.
C, C++, and Python are closely interlinked, as Python itself relies upon both C and C++ for its
innermost libraries. However, unlike C and C++, which are compiled languages and can be
executed using a bare-metal approach, Python is an interpreted language. This difference provides
its own challenges in embedded computing: for example, Python requires an operating system,
typically Linux, along with volatile and nonvolatile memory resources.
1. A digital twin is a digital replica of physical assets, processes, and systems, allowing real-world behavior to be modeled providing
monitoring, prognostics, and diagnostics.
FPGA
FPGA
FPGA
FPGA
Barrier
I I oT solution applications benefit from the use of ML, especially those applications where
traditional methods do not result in acceptable performance or require significant human
intervention, like re-callibration, maintenance, diagnostic, and fail-safe operation. These
applications include:
This tight coupling of PS and PL allows for the creation of a system that is more responsive,
reconfigurable, and power efficient when compared to a traditional approach. Traditional CPU-
based approaches require the use of external memory to share large data structures, e.g., images.
This reduces determinism and increases both power dissipation and latency, as arbitration and
communication off-chip are required. Heterogeneous system-on-chip devices like those in the
Zynq portfolio allow the designer to accelerate functionality within the PL of the device. This
provides a deterministic response time with reduced latency and an optimal power solution. See
Figure 2.
X-Ref Target - Figure 2
Sensor
Image Sensor DSP/GPU Image Sensor
Fusion
Programmable Logic
Ultrasonic Ultrasonic
Radar Radar Sensor Critical Motor
Motor Fusion Decision Control IP
Lidar Control IP Lidar
IMU IMU
CPUs CPUs
Motoring Critical
Monitor / UI
/ UI Decision
DDR DDR
WP502_07_061218
The use of the PL features a wider interfacing capability than traditional CPU approaches, which
come with fixed interfaces. The flexible nature of PL I/O structures allows for any-to-any
connectivity, enabling industry standard, proprietary, or legacy interfaces to be implemented.
Thanks to the high-level synthesis tools SDSoC™ and Vivado® HLS, developers are able to turn C
and C++ compiled code directly in hardware.
Along with acceleration of functions in the programmable logic, it is also possible to instantiate
one or more MicroBlaze™ 32-bit soft-core processors. The MicroBlaze core allows execution of
real-time critical applications. These features allow the Zynq portfolio to be used to meet all the
requirements of a Python-enabled embedded platform.
The PYNQ framework enables Python to work with the Zynq portfolio, taking advantage of the
acceleration provided by programmable logic. To do this, PYNQ employs PL overlays packaged as
hybrid libraries. A hybrid library is a new form of library that includes an overlay bitstream, along
with its associated hardware-dependent C code, and Python APIs. Hybrid libraries are a key
mechanism for enabling reuse and can be installed easily using PIP Install (package management
system used to install and manage software packages written in Python).
The PYNQ framework therefore provides the necessary abstraction levels required for all
developers: data scientists, embedded engineers, hardware engineers, and system engineers. See
Figure 3.
X-Ref Target - Figure 3
Data
Analytics Quick
Prototyping
Statistical
Machine
Learing
Data
Analytics CFFI
Sensors
Embedded Hardware
Acceleration Acceleration
Robots
Discrete
Event
Simulation
WP502_02_052318
PYNQ is the first framework to combine the following elements, which simplify and improve the
Zynq portfolio based designs:
The data scientist can immediately use the system Xilinx created in the Python framework with
familiar packages. Figure 3 demonstrates a possible stack where some of the standard packages
can be used. Panda, Scikit-learn, and NumPy are at the top, with other packages providing more
specialized functions like ROS for robots and SimPy for simulation.
Python can import one or more ready-to-use pre-configured hardware modules, enabling the
acceleration of the application and removing bottlenecks. All the packages then can be explored
and used within the Jupyter environment and interfaced to the programmable logic using
hardware libraries based on FPGA overlays.
The PYNQ framework is designed around an open-source community where overlays are created
and shared between developers. Using this method, developers are not required to build overlay(s)
until they see the need for a new one—that is, when an overlay performing the desired function
does not already exist. Each new overlay should ideally follow an established “design pattern” for
I I oT. These design patterns include (but are not limited to) the following:
• Accelerators expedite computation-sharing and/or exchanging data with the main processors.
Exchange can happen in PL memory (block RAM), in on-chip memory (OCM), in the L2 cache,
and in the DDR memory as function of data size and performance.
• Loggers acquire data—in general, raw data—that is shared with the main processors. Logging
can happen in PL memory (block RAM), in on-chip memory (OCM), in the L2 cache, and in DDR
memory as a function of data size and performance. The acquisition process is started by
explicit triggering of an event from the main processors or by capturing an external event.
• Sequencers generate automatic sequences of logical values; depending upon the complexity,
these can include instances of programmable generators to implement Boolean functions,
FSMs and/or arbitrary digital patterns.
• Workers provide real-time control based on the MicroBlaze 32-bit soft-core processor,
programmable in C/C++ with the purpose of offloading repetitive tasks from the main
processors to ensure determinism.
• It is possible to use workers as Safety Modules. MicroBlaze processors can be configured for
lock-step operation. While the full Zynq portfolio has been designed for functional safety, this
capability is important when the product is going to move from the initial prototype into
something that is production-ready.
Sensor Diagnosis
Sensor performance changes throughout the operating lifetime due to aging. This is especially true
if sensors are used in harsh environments, where aging affects reliability and introduces drift and
deviation. The ability to diagnose a sensor is also extremely useful if the sensor is used in safety
applications; in this case, proper diagnostic processes are also part of the safety system.
Using ML, the designer can create a sensor model—or digital twin, as it is more commonly known—
to predict sensor output while also continuously monitoring actual sensor output. At nominal
conditions, sensor signals follow some known patterns with a specific degree of uncertainty due to
system and measurement noise. However, when sensor failures occur, the observable outputs
deviate from the predicted values, and sensor failure can be declared when the deviation exceeds
specified timing or value thresholds. Analytical redundancy techniques allow information from the
sensors at nominal conditions to be used to create models for the dynamic system, so the “digital
twin” does not age or fail, and lives forever.
Ball bearings ensure free rotation of the shaft or the axle with minimum friction and hold a shaft or
axle in its correct position. If a ball bearing of a rotating system fails, its consequences can be
catastrophic. There are many causes of bearing damage, including:
• Bearing misalignment
• Fretting wear, a type of corrosive damage of the bearings’ contact surfaces
• Variable-frequency drive, generating shaft currents that cause pitting, fluting, and fusion
craters
• Improper bearing lubrication
• Bearing corrosion
• Bearing fatigue
• High temperature and other factors
Detection of such faults before a failure occurs is of paramount importance to reduce operational
and maintenance costs. Thankfully, the availability of low-cost accelerometers that provide high
bandwidth measurements enable very powerful vibration data acquisition systems. When these are
deployed with advanced motor control, it is possible to deliver a system that can detect possible
failures in real time, based upon the PYNQ framework.
For this application, the PYNQ framework is deployed on an ARTY-Z7 board. This board supports
the PYNQ base overlay, but it is just one of the possible incarnations of the PYNQ framework. In
this example, the system setup contains a high-performance motor control system and an
acquisition system formed by five Kionix(1) three-axis accelerometers.
There are several possible techniques for vibration monitoring, including vibration measurement,
acoustic measurement, temperature measurement, and wear analysis. The use of silicon
accelerometers provides an economical way to sense vibration in all three axes, making it possible
to get a full picture of all possible vibration modes happening in the system. This is illustrated in
Figure 4.
To ensure accurate measurement, accelerometers are positioned close to the ball bearings in the
motor, gearbox, and main pulley.
System parameters, such as motor currents, stator voltages, and the position of the shaft angle, are
monitored. A motor model is used to estimate angle and internal voltages.
X-Ref Target - Figure 4
=
Z
Wideband Accelerator 3 Axes X
y
Z
Z X
y
X
y
Z
X
y
High-Speed Zynq-7000 SoC
Motor Z I2C
Brushless y
X
Motor Control
Power Stage
Ethernet
WP502_05_052318
1. Kionix Inc., www.kionix.com, manufacturer of microelectromechanical (MEMS) systems, including accelerometers, gyroscopes, and
sensor fusion inertial sensor technology.
As can be seen in Figure 5, a new overlay is created for the PYNQ environment using the following
elements:
Accelerometers
Linux Logging Logging Z
DDR3 Segment Vector Motor Vector Segment Zynq-7000 SoC Z X
Z y
X X
y y
PS HP0 HP1 PL Z
Linux y
X
Block RAM
DPR Mechanical
1.6µs Motor Sensors
FFT 6.4µs 3.2µs 1.6µs 1.6µs Load
Module
Power
Positioning Speed Torque
Modulation
1.6µs
Logger
8 Channels Currents
To enable the new overlay to work with the PYNQ framework, the Python C Foreign Function
Interface (CFFI) is used. This interface allows interaction with almost any C code when using Python.
Within the PYNQ framework are programmable logic acceleration functions, and the framework’s
C/C++ based software drivers are accessed. The PYNQ framework also provides additional
functions that are helpful when developing applications. Two examples of PYNQ core functions are
(a) mmio, which allows memory-mapped I/O, and (b) xlnk, which allows allocation of DDR memory
as buffer space visible to NumPy (the fundamental package for scientific computing with Python).
xlnk is responsible for getting the virtual and physical addresses for the PL where the logged
vectors are mapped.
As a small example, the vibration signals from the accelerometer can be correlated with the
vibration signals detected by looking at the currents through the motor, so that correlation is
possible by selecting the traditional vibration analysis or the more sophisticated signature analysis.
The descriptions of the measurements and of the overall system are worthy of a separate white
paper and are not included here. For illustrative purposes, Figure 6 shows the limit of a ball-
bearing frequency exceeding the threshold. The signal is extracted from the apparently good
current waveforms of Figure 7.
X-Ref Target - Figure 6
-2
-4
-6
-8
-10
1.0
0.5
0.0
-0.5
-1.0
Figure 7: Waveforms of the 3-phase Motor (the Ball Bearing Signature is Buried in the Noise)
The above example demonstrates that with proper understanding of the problem, as well as of the Zynq
portfolio, advanced diagnostics are possible and practical, opening the door to more intelligent
approaches to machine learning-enabled IIoT systems.
Conclusion
This white paper covers only some of the basics of the PYNQ framework, which binds Python and
the Zynq portfolio together. The PYNQ framework offers new and immediate possibilities for
creating systems capable of exploiting the full capabilities of the I I oT and the emergent
capabilities offered by Machine Learning.
Machine Learning at the edge is in its infancy, and Xilinx is committed to deliver best-in-class products
and design frameworks to expedite and simplify the design, deployment, and maintenance of intelligent
and adaptive assets. Being just at the start of a very exciting journey, many of the topics described in this
white paper are to be detailed in further white papers, application notes, and examples. Some of them are
already available in the PYNQ GitHub repository.
Getting Started
• The PYNQ framework is available at http://www.pynq.io.
• The Arty Z7 board described in this white paper is available from Digilent Inc. at:
store.digilentinc.com/arty-z7-apsoc-zynq-7000-development-board-for-makers-and-hobbyists/
• A bundle featuring the Arty Z7 board, the power module for motor control, and a BLDC motor
is available at: https://shop.trenz-electronic.de/en/TEC0053-04-K1-EDDP-Motor-Control-Kit-
with-Motor-Power-Supplies
Revision History
The following table shows the revision history for this document:
Disclaimer
The information disclosed to you hereunder (the “Materials”) is provided solely for the selection and use of Xilinx
products. To the maximum extent permitted by applicable law: (1) Materials are made available “AS IS” and with all faults,
Xilinx hereby DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING BUT NOT
LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;
and (2) Xilinx shall not be liable (whether in contract or tort, including negligence, or under any other theory of liability)
for any loss or damage of any kind or nature related to, arising under, or in connection with, the Materials (including your
use of the Materials), including for any direct, indirect, special, incidental, or consequential loss or damage (including loss
of data, profits, goodwill, or any type of loss or damage suffered as a result of any action brought by a third party) even
if such damage or loss was reasonably foreseeable or Xilinx had been advised of the possibility of the same. Xilinx
assumes no obligation to correct any errors contained in the Materials or to notify you of updates to the Materials or to
product specifications. You may not reproduce, modify, distribute, or publicly display the Materials without prior written
consent. Certain products are subject to the terms and conditions of Xilinx’s limited warranty, please refer to Xilinx’s
Terms of Sale which can be viewed at http://www.xilinx.com/legal.htm#tos; IP cores may be subject to warranty and
support terms contained in a license issued to you by Xilinx. Xilinx products are not designed or intended to be fail-safe
or for use in any application requiring fail-safe performance; you assume sole risk and liability for use of Xilinx products
in such critical applications, please refer to Xilinx’s Terms of Sale which can be viewed at http://www.xilinx.com/
legal.htm#tos.