DriveWorks SDK Reference
3.5.78 Release
For Test and Development only

sensors/canbus/docs/usecase3.md
Go to the documentation of this file.
1 # Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
2 
3 @page canbus_usecase3 CAN Interpreter
4 
5 @note SW Release Applicability: This tutorial is applicable to modules in both **NVIDIA DriveWorks** and **NVIDIA DRIVE Software** releases.
6 
7 ## CAN Interpreter Module workflow
8 
9 ### Initialization
10 
11 The interpreter module for DBC format is initialized as follows:
12 
13 ```{.cpp}
14 dwCANInterpreterHandle_t interpreter;
15 dwCANInterpreter_buildFromDBC(&interpreter, "path/to/definition.dbc", ...);
16 ```
17 
18 The DBC message and signals definition file will be parsed to initialize
19 the interpreter. Please refer to a DBC file format documentation for additional
20 details on the expected syntax.
21 
22 An interpreter for user-defined format is initialized as follows:
23 
24 ```{.cpp}
25 dwCANInterpreterInterface interpreter;
26 
27 // following callbacks to be implemented by user
28 interpreter.addMessage = cb_addMessage;
29 interpreter.getDataf32 = cb_getDataf32;
30 interpreter.getDataf64 = cb_getDataf64;
31 interpreter.getDatai32 = cb_getDatai32;
32 interpreter.getNumAvailableSignals = cb_getNumAvailableSignals;
33 interpreter.getSignalInfo = cb_getSignalInfo;
34 
35 dwCANInterpreter_buildFromCallbacks(&canParser, interpreter, NULL, ...);
36 ```
37 
38 The `dwCANInterpreterInterface` structure is populated with pointers
39 to the callbacks implemented by the user. The expected callback signatures are
40 detailed in Interpreter.h. The callbacks are used as follows:
41 
42 - `dwCANInterpreterInterface.addMessage` gets called with a `dwCANMessage` to be interpreted
43 - `dwCANInterpreterInterface.getNumAvailableSignals` and `dwCANInterpreterInterface.getSignalInfo` are used to extract signals from CAN messages
44 - `dwCANInterpreterInterface.getDataf32`, `dwCANInterpreterInterface.getDataf64` and `dwCANInterpreterInterface.getDatai32` are used to read the corresponding signal type data from each message
45 
46 The following callbacks are required to be implemented:
47 
48 - `addMessage`
49 - `getNumAvailableSignals`
50 - `getSignalInfo`
51 
52 The following callbacks are optional and have to be implemented only
53 if a signal data type requires them:
54 
55 - `getDataf32`
56 - `getDataf64`
57 - `getDatai32`
58 
59 An example implementation of those callbacks is provided in @ref dwx_canbus_message_sample
60 
61 ### Consuming messages
62 
63 CAN messages can be provided to the interpreter for parsing as follows:
64 
65 ```{.cpp}
66 dwCANInterpreter_consume(&msg, interpreter);
67 ```
68 
69 Then used as follows:
70 
71 ```{.cpp}
72 uint32_t num;
73 dwStatus status = dwCANInterpreter_getNumberSignals(&num, interpreter);
74 
75 if (status == DW_SUCCESS && num > 0)
76 {
77  int32_t int_value = 0;
78  float32_t float_value = 0;
79  dwTime_t timestamp = 0;
80  const char* name;
81 
82  // for each signal
83  for (uint32_t i = 0; i < num; ++i)
84  {
85  // get name of signal
86  if (dwCANInterpreter_getSignalName(&name, i, interpreter) == DW_SUCCESS)
87  {
88  // do something based on signal name
89  if (strcmp(name, "some_signal_int") == 0)
90  {
91  if (dwCANInterpreter_geti32(&int_value, &timestamp, i, interpreter) == DW_SUCCESS)
92  std::cout << "some_signal_int value: " << int_value << std::endl;
93  else
94  std::cerr << "error getting value for some_signal_int" << std::endl;
95  }
96  elseif (strcmp(name, "some_signal_float") == 0)
97  {
98  if (dwCANInterpreter_getf32(&float_value, &timestamp, i, interpreter) == DW_SUCCESS)
99  std::cout << "some_signal_float value: " << float_value << std::endl;
100  else
101  std::cerr << "error getting value for some_signal_float" << std::endl;
102  }
103  else std::cerr << "unhandled signal name" << std::endl;
104  }
105  }
106 }
107 ```
108 
109 The above snippet iterates over all signals handled by the intepreter, and
110 attempts to read a value for each signal, in the latest consumed `dwCANMessage`.
111 
112 Note that in general the number of signals as returned by
113 `dwCANInterpreter_getNumberSignals()` is the number of valid signals found
114 in last consumed CAN message. However this does not have to be true in
115 user-provided callback based interpreters, as it is implementation dependent.
116 
117 ### Releasing the interpreter
118 
119 The interpreter can be released as follows:
120 
121 ```{.cpp}
122 dwCANInterpreter_release(&interpreter);
123 ```
124 
125 For more details see @ref dwx_canbus_message_sample