MATLAB Examples

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.

Contents

Open the Database File

Open the database file to access the definitions.

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

  Database with properties:

             Name: 'VehicleInfo'
             Path: 'C:\TEMP\Bdoc18a_805738_11608\ib4C766E\15\tp75927dcd\ex13648766\VehicleInfo.dbc'
            Nodes: {}
         NodeInfo: [0x0 struct]
         Messages: {'WheelSpeeds'}
      MessageInfo: [1x1 struct]
       Attributes: {'BusType'}
    AttributeInfo: [1x1 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 = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 1
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 0
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: [0x0 datetime]
           FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: []
                UserData: []


rxCh = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 0
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: [0x0 datetime]
           FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: []
                UserData: []

Create the CAN Message

You can create CAN messages to send on the network.

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

  Message with properties:

   Message Identification
    ProtocolMode: 'CAN'
              ID: 1200
        Extended: 0
            Name: 'WheelSpeeds'

   Data Details
       Timestamp: 0
            Data: [0 0 0 0 0 0 0 0]
         Signals: [1x1 struct]
          Length: 8

   Protocol Flags
           Error: 0
          Remote: 0

   Other Information
        Database: [1x1 can.Database]
        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 =

  5x8 timetable

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

    0.033549 sec    1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    1.0385 sec      1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    3.0486 sec      1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    6.0529 sec      1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    7.0559 sec      1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 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 =

  5x4 timetable

        Time        LR_WSpeed    RR_WSpeed    RF_WSpeed    LF_WSpeed
    ____________    _________    _________    _________    _________

    0.033549 sec      -100         -100         -100          10    
    1.0385 sec        -100         -100         -100          20    
    3.0486 sec        -100         -100         -100          30    
    6.0529 sec        -100         -100         -100          40    
    7.0559 sec        -100         -100         -100          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

   10-Feb-2018 18:44:09

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 =

  5x8 timetable

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

    10-Feb-2018 18:44:10    1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    10-Feb-2018 18:44:11    1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    10-Feb-2018 18:44:13    1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    10-Feb-2018 18:44:16    1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 
    10-Feb-2018 18:44:17    1200     false      'WheelSpeeds'    [1x8 uint8]      8       [1x1 struct]    false    false 


signalTimetable =

  5x4 timetable

            Time            LR_WSpeed    RR_WSpeed    RF_WSpeed    LF_WSpeed
    ____________________    _________    _________    _________    _________

    10-Feb-2018 18:44:10      -100         -100         -100          10    
    10-Feb-2018 18:44:11      -100         -100         -100          20    
    10-Feb-2018 18:44:13      -100         -100         -100          30    
    10-Feb-2018 18:44:16      -100         -100         -100          40    
    10-Feb-2018 18:44:17      -100         -100         -100          50