These are the slides to my talk "A Deep Dive into QtCanBus" from Qt World Summit 2019. I show how to build the middleware between the CAN bus and the QML HMI.
ENJOY Call Girls In Okhla Vihar Delhi Call 9654467111
A Deep Dive into QtCanBus
1. Copyright 2019, Burkhard Stubert
A Deep Dive into Qt CAN Bus
Burkhard Stubert
I help teams succeed with Qt embedded systems
2. Copyright 2019, Burkhard Stubert 2
Signals per second:
9 50-100
79 10-20
199 2-5
63 <= 2
5 main ECUs with
• 350 signals
• 1800 properties
• 2500 errors
Received in CAN module:
• 1100 / 1700 fps avg / peak
Received in GUI:
• 50 / 300 fps avg / peak
Home Screen of Sugar Beet Harvester
3. Copyright 2019, Burkhard Stubert
Outline – A Deep Dive into Qt CAN Bus
• Basic Concepts of CAN
• Architecture of CAN Middleware
• Connecting to the CAN Bus
• Receiving CAN Frames
• Sending CAN Frames
• Generating Code for CAN Middleware
3
4. Copyright 2019, Burkhard Stubert
CAN Bus Architecture
4
Engine
0x00
Transmission
0x03
Body
0x21
Steering
0x13
Joystick
0x2a
Nav Keys
0x2b
Climate
0x19
Camera
0x1c
Header 1
0xf0
Header 2
0xf1
Terminal
0x28
can0
can1
Communication Principle
• Publish-subscribe
• Signal-slots
5. Copyright 2019, Burkhard Stubert
CAN Frame = Frame ID # Payload
5
auto frameId{0x18ff3203U};
auto payload{QByteArray::fromHex("4cfc4305")};
auto frame{QCanBusFrame{frameId, payload}};
QCOMPARE(frame.frameId(), frameId);
QCOMPARE(frame.payload(), payload);
QVERIFY(frame.isValid());
QVERIFY(frame.hasExtendedFrameFormat());
QVERIFY(!frame.hasFlexibleDataRateFormat());
qDebug() << "frame = " << frame.toString();
// "18FF3203 [4] 4C FC 43 05"
auto invalidType{QCanBusFrame::InvalidFrame};
auto frame2{QCanBusFrame{invalidType}};
QVERIFY(!frame2.isValid());
Extended Frame Format (EFF)
• frame ID: 29 bits
• payload length <= 8 bytes
(if not flexible data rate)
Base Frame Format (BFF)
• frame ID: 11 bits
• payload length <= 8 bytes
(if not flexible data rate)
6. Copyright 2019, Burkhard Stubert
Standardising CAN Frames: SAE J1939
6
Meaning of CAN frame
0cf00400 # a4503871150d031e
• Can mean anything
• Fully defined by OEM
• Communication nightmare
Meaning of J1939 frame
0cf00400 # a4503871150d031e
• Frame EEC1 from Engine about
engine speed and torque
• Standardised communication
• Tractor ó Implements
• Harvester ó MAN or Volvo motor
• Defines 1500+ standard frames,
100+ ECU IDs
8. Copyright 2019, Burkhard Stubert
J1939 Peer-To-Peer Frames (PDU1 Format)
8
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 Priority 0 DP PDU Format (PF) Destination Addr. (DA) Source Address (SA)
Parameter Group Number (PGN)
• Like Qt Properties
• payload[0] determines payload
• Sent from ECU SA to ECU DA
• PDU1 Format
• 0 <= PF < 240 (0xF0)
• proprietary if PF == 239 (0xEF)
Write Property Request:
PR = 0x06 Command = 0x02 (quint8)
PGN = 0xef ID = 0x0203 (quint16)
DA = 0x13 Value = 0x096C61B5 (qint32)
SA = 0x28 Padding = 0x00
J1939Frame: 18 ef 13 28 # 02 0302 B5616C09 00
QCanBusFrame{0x18ef1328U,
QByteArray::fromHex("020302B5616C0900")}
encode decode
9. Copyright 2019, Burkhard Stubert
Outline – A Deep Dive into Qt CAN Bus
• Basic Concepts of CAN
• Architecture of CAN Middleware
• Connecting to the CAN Bus
• Receiving CAN Frames
• Sending CAN Frames
• Generating Code for CAN Middleware
9
10. Copyright 2019, Burkhard Stubert
CanBusRouter and ECU Proxies
10
Body
0x21
Steering
0x13
can0
Terminal
0x28
QCanBusDevice
CanBusRouter
BodyProxySteeringProxy
QML HMI
Number
J1939Frame
QCanBusFrame
QCanBusFrame
Route frames from ECUs to their proxies
Reduce number of frames seen by HMI
Avoid write buffer overflows
Handle DM1 error frames
Responsibilities
11. Copyright 2019, Burkhard Stubert
Outline – A Deep Dive into Qt CAN Bus
• Basic Concepts of CAN
• Architecture of CAN Middleware
• Connecting to the CAN Bus
• Receiving CAN Frames
• Sending CAN Frames
• Generating Code for CAN Middleware
11
12. Copyright 2019, Burkhard Stubert
Setting Up CAN Network Interface
Systemd Service
12
[Unit] Description=can0
[Service]
ExecStartPre=/sbin/ip link set can0 type can
bitrate 250000 restart-ms 100
ExecStart=/sbin/ifconfig can0 up txqueuelen 64
Restart=on-failure
RestartSec=1
[Install]
WantedBy=multi-user.target
Connect Hardware
Terminal on ARM
ECU on PC
m
m
f
f
USB
CAN
Terminated
2-wire serial
cable
CAN
13. Copyright 2019, Burkhard Stubert
Connecting To CAN Network Interface
13
// In canbusrouter.h
QCanBusDevice *m_device;
// In CanBusRouter constructor
m_device = QCanBus::instance()
->createDevice("socketcan", "can0");
m_device->connectDevice();
// In CanBusRouter destructor
if (m_device->state() ==
QCanBusDevice::ConnectedState)
{
m_device->disconnectDevice();
}
14. Copyright 2019, Burkhard Stubert
Outline – A Deep Dive into Qt CAN Bus
• Basic Concepts of CAN
• Architecture of CAN Middleware
• Connecting to the CAN Bus
• Receiving CAN Frames
• Sending CAN Frames
• Generating Code for CAN Middleware
14
19. Copyright 2019, Burkhard Stubert
Filtering in CAN Frame Cache
• Forward fewer frames to HMI
• CanBusRouter notifies Proxies only
every 100-250ms – instead of instantly
• Cache ignores older frames with same
PGN: A1, B1
• Cache ignores frames not passing filter
criterion: D, E
20
Received by CanBusRouter in 100 ms
A1 B1 C A2 E D B2
C A2 B2
Filter
Forwarded to ECU proxies and HMI
20. Copyright 2019, Burkhard Stubert
Raw Filters in SocketCAN
• Block CAN frames from entering
SocketCAN module
• See configuration parameter
QCanBusDevice::RawFilterKey
for implementation details
21
cframe.frameId() & filter.frameIdMask
==
filter.frameId & filter.frameIdMask
• Set source address filter for every
ECU with proxy
• Every EcuProxy sets filter on creation
• Block frames from ECUs without
proxies
0x18ff1021 & 0x000000ff
== 0x00000021 ==
0x1fffff21 & 0x000000ff
• Received CAN frame passes filter:
21. Copyright 2019, Burkhard Stubert
Outline – A Deep Dive into Qt CAN Bus
• Basic Concepts of CAN
• Architecture of CAN Middleware
• Connecting to the CAN Bus
• Receiving CAN Frames
• Sending CAN Frames
• Generating Code for CAN Middleware
22
25. Copyright 2019, Burkhard Stubert
TX Buffer Overflow in SocketCAN
• Write more than txqueuelen frames
to CAN bus without pause
26
• Common non-solutions
• Wait for 20 ms between writes
• Still fails for higher bus loads
• Reduces bandwidth
• Wait 20 ms for response frame
• Not all writes have a response (e.g.,
firmware update)
• Same problems as previous "solution"
• Objectives for good solution
• Adapts dynamically to bus load
• Works for writes without responses
// txqueuelen = 10
for (int i = 0; i < 25; ++i)
{
m_bodyProxy->readProperty(100 + i);
}
// Results in several errors:
// No buffer space available
// (QCanBusDevice::WriteError)
Q1 P1Q2 Q25 P2 P25 Q1 P1 Q2 Q25P2 P25
27. Copyright 2019, Burkhard Stubert
Outline – A Deep Dive into Qt CAN Bus
• Basic Concepts of CAN
• Architecture of CAN Middleware
• Connecting to the CAN Bus
• Receiving CAN Frames
• Sending CAN Frames
• Generating Code for CAN Middleware
28
30. This presentation is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License.
Copyright 2019, Burkhard Stubert
Thank you J
Mail: burkhard.stubert@embeddeduse.com
Web: http://www.embeddeduse.com
Code: https://github.com/bstubert/embeddeduse/tree/master/BlogPosts/CanComm (MIT)