Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Using Timestamps

This example shows you how to use Vehicle Network Toolbox™ with the InitialTimestamp CAN channel property to work with relative and absolute timestamps for CAN messages. It also uses MathWorks Virtual CAN channels connected in a loopback configuration.

Open the Database File

Open the database file to access the definitions.

db = canDatabase('VehicleInfo.dbc')
db = 

  Database with properties:

             Name: 'VehicleInfo'
             Path: '\\central-mi\home\jpyle\Documents\MATLAB\examples\vnt-ex13648766\VehicleInfo.dbc'
            Nodes: {}
         NodeInfo: [0×0 struct]
         Messages: {'WheelSpeeds'}
      MessageInfo: [1×1 struct]
       Attributes: {'BusType'}
    AttributeInfo: [1×1 struct]
         UserData: []

Create the CAN Channels

Create CAN channels on which you can send and receive information.

txCh = canChannel('MathWorks','Virtual 1',1)
rxCh = canChannel('MathWorks','Virtual 1',2)
rxCh.Database = db;
txCh = 

Summary of CAN Channel using 'MathWorks' 'Virtual 1' Channel 1.

  Channel Parameters:  Bus Speed is 500000.
                       Bus Status is 'N/A'.
                       Transceiver name is 'N/A'.
                       Serial Number of this device is 0.
                       Initialization access is allowed.
                       No database is attached.

              Status:  Offline - Waiting for start.
                       0 messages available to receive.
                       0 messages transmitted since last start.
                       0 messages received since last start.

      Filter History:  Standard ID Filter: Allow All | Extended ID Filter: Allow All


rxCh = 

Summary of CAN Channel using 'MathWorks' 'Virtual 1' Channel 2.

  Channel Parameters:  Bus Speed is 500000.
                       Bus Status is 'N/A'.
                       Transceiver name is 'N/A'.
                       Serial Number of this device is 0.
                       Initialization access is allowed.
                       No database is attached.

              Status:  Offline - Waiting for start.
                       0 messages available to receive.
                       0 messages transmitted since last start.
                       0 messages received since last start.

      Filter History:  Standard ID Filter: Allow All | Extended ID Filter: Allow All

Create the CAN Message

You can create CAN messages to send on the network.

msg = canMessage(db,'WheelSpeeds')
msg = 

  Message with properties:

           ID: 1200
     Extended: 0
         Name: 'WheelSpeeds'
     Database: [1×1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0
         Data: [0 0 0 0 0 0 0 0]
      Signals: [1×1 struct]
     UserData: []

Start the CAN Channels

To begin using channels for transmit and receive operations, place them online.

start(rxCh)
start(txCh)

Send CAN Messages

The transmit function sends messages onto the network. Use pause to delay the transmits. Update the signal data each transmit.

msg.Signals.LF_WSpeed = 10;
transmit(txCh,msg)
pause(1);
msg.Signals.LF_WSpeed = 20;
transmit(txCh,msg)
pause(2);
msg.Signals.LF_WSpeed = 30;
transmit(txCh,msg)
pause(3);
msg.Signals.LF_WSpeed = 40;
transmit(txCh,msg)
pause(1);
msg.Signals.LF_WSpeed = 50;
transmit(txCh,msg)

Receive the CAN Messages

The receive function retrieves information from the channel representing messaging that occurred on the network.

stop(rxCh)
stop(txCh)
msgRx = receive(rxCh,Inf,'OutputFormat','timetable')
msgRx =

  5×8 timetable array

        Time        ID     Extended        Name            Data        Length      Signals       Error    Remote
    ___________    ____    ________    _____________    ___________    ______    ____________    _____    ______

    0.44104 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     1.4501 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     3.4551 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     6.4623 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     7.4743 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 

Inspect Signal Data

View details of the received messages, including the timestamps and the signal value. Note that timestamp values are relative from the start time of the CAN channel.

signalTimetable = canSignalTimetable(msgRx)
plot(signalTimetable.Time,signalTimetable.LF_WSpeed,'x')
title('Signal Data with Relative Time','FontWeight','bold')
xlabel('Relative Timestamp')
ylabel('Signal Value')
ylim([0 60])
signalTimetable =

  5×4 timetable array

        Time       LR_WSpeed    RR_WSpeed    RF_WSpeed    LF_WSpeed
    ___________    _________    _________    _________    _________

    0.44104 sec    0            0            0            10       
     1.4501 sec    0            0            0            20       
     3.4551 sec    0            0            0            30       
     6.4623 sec    0            0            0            40       
     7.4743 sec    0            0            0            50       

Inspect InitialTimestamp Property

View the InitialTimestamp property of the receiving CAN channel. It is a datetime value that gives the absolute time of when the channel was started.

rxCh.InitialTimestamp
ans = 

  datetime

   29-Nov-2016 10:28:47

Analyze Data with Absolute Timestamps

The relative timestamps of each message and the InitialTimestamp property combine to apply absolute time values to each message or signal contained within.

msgRx.Time = msgRx.Time + rxCh.InitialTimestamp
signalTimetable = canSignalTimetable(msgRx)
figure
plot(signalTimetable.Time,signalTimetable.LF_WSpeed,'x')
title('Signal Data with Absolute Time','FontWeight','bold')
xlabel('Absolute Timestamp')
ylabel('Signal Value')
ylim([0 60])
msgRx =

  5×8 timetable array

            Time             ID     Extended        Name            Data        Length      Signals       Error    Remote
    ____________________    ____    ________    _____________    ___________    ______    ____________    _____    ______

    29-Nov-2016 10:28:48    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    29-Nov-2016 10:28:49    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    29-Nov-2016 10:28:51    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    29-Nov-2016 10:28:54    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    29-Nov-2016 10:28:55    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 


signalTimetable =

  5×4 timetable array

            Time            LR_WSpeed    RR_WSpeed    RF_WSpeed    LF_WSpeed
    ____________________    _________    _________    _________    _________

    29-Nov-2016 10:28:48    0            0            0            10       
    29-Nov-2016 10:28:49    0            0            0            20       
    29-Nov-2016 10:28:51    0            0            0            30       
    29-Nov-2016 10:28:54    0            0            0            40       
    29-Nov-2016 10:28:55    0            0            0            50       

Was this topic helpful?