CO_trace.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * CANopen trace object for recording variables over time.
  3. *
  4. * @file CO_trace.h
  5. * @ingroup CO_trace
  6. * @author Janez Paternoster
  7. * @copyright 2016 - 2020 Janez Paternoster
  8. *
  9. * This file is part of CANopenNode, an opensource CANopen Stack.
  10. * Project home page is <https://github.com/CANopenNode/CANopenNode>.
  11. * For more information on CANopen see <http://www.can-cia.org/>.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #ifndef CO_TRACE_H
  26. #define CO_TRACE_H
  27. #include "301/CO_driver.h"
  28. #include "301/CO_SDOserver.h"
  29. /* default configuration, see CO_config.h */
  30. #ifndef CO_CONFIG_TRACE
  31. #define CO_CONFIG_TRACE (0)
  32. #endif
  33. #if ((CO_CONFIG_TRACE) & CO_CONFIG_TRACE_ENABLE) || defined CO_DOXYGEN
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * @defgroup CO_trace Trace
  39. * @ingroup CO_CANopen_extra
  40. * @{
  41. *
  42. * CANopen trace object for recording variables over time.
  43. *
  44. * In embedded systems there is often a need to monitor some variables over time.
  45. * Results are then displayed on graph, similar as in oscilloscope.
  46. *
  47. * CANopen trace is a configurable object, accessible via CANopen Object
  48. * Dictionary, which records chosen variable over time. It generates a curve,
  49. * which can be read via SDO and can then be displayed in a graph.
  50. *
  51. * CO_trace_process() runs in 1 ms intervals and monitors one variable. If it
  52. * changes, it makes a record with timestamp into circular buffer. When trace is
  53. * accessed by CANopen SDO object, it reads latest points from the the circular
  54. * buffer, prints a SVG curve into string and sends it as a SDO response. If a
  55. * SDO request was received from the same device, then no traffic occupies CAN
  56. * network.
  57. */
  58. /**
  59. * Start index of traceConfig and Trace objects in Object Dictionary.
  60. */
  61. #ifndef OD_INDEX_TRACE_CONFIG
  62. #define OD_INDEX_TRACE_CONFIG 0x2301
  63. #define OD_INDEX_TRACE 0x2401
  64. #endif
  65. /**
  66. * structure for reading variables and printing points for specific data type.
  67. */
  68. typedef struct {
  69. /** Function pointer for getting the value from OD variable. **/
  70. int32_t (*pGetValue) (void *OD_variable);
  71. /** Function pointer for printing the start point to trace.plot */
  72. uint32_t (*printPointStart)(char *s, uint32_t size, uint32_t timeStamp, int32_t value);
  73. /** Function pointer for printing the point to trace.plot */
  74. uint32_t (*printPoint)(char *s, uint32_t size, uint32_t timeStamp, int32_t value);
  75. /** Function pointer for printing the end point to trace.plot */
  76. uint32_t (*printPointEnd)(char *s, uint32_t size, uint32_t timeStamp, int32_t value);
  77. } CO_trace_dataType_t;
  78. /**
  79. * Trace object.
  80. */
  81. typedef struct {
  82. bool_t enabled; /**< True, if trace is enabled. */
  83. CO_SDO_t *SDO; /**< From CO_trace_init(). */
  84. uint32_t *timeBuffer; /**< From CO_trace_init(). */
  85. int32_t *valueBuffer; /**< From CO_trace_init(). */
  86. uint32_t bufferSize; /**< From CO_trace_init(). */
  87. volatile uint32_t writePtr; /**< Location in buffer, which will be next written. */
  88. volatile uint32_t readPtr; /**< Location in buffer, which will be next read. */
  89. uint32_t lastTimeStamp; /**< Last time stamp. If zero, then last point contains last timestamp. */
  90. void *OD_variable; /**< Pointer to variable, which is monitored */
  91. const CO_trace_dataType_t *dt; /**< Data type specific function pointers. **/
  92. int32_t valuePrev; /**< Previous value of value. */
  93. uint32_t *map; /**< From CO_trace_init(). */
  94. uint8_t *format; /**< From CO_trace_init(). */
  95. int32_t *value; /**< From CO_trace_init(). */
  96. int32_t *minValue; /**< From CO_trace_init(). */
  97. int32_t *maxValue; /**< From CO_trace_init(). */
  98. uint32_t *triggerTime; /**< From CO_trace_init(). */
  99. uint8_t *trigger; /**< From CO_trace_init(). */
  100. int32_t *threshold; /**< From CO_trace_init(). */
  101. } CO_trace_t;
  102. /**
  103. * Initialize trace object.
  104. *
  105. * Function must be called in the communication reset section.
  106. *
  107. * @param trace This object will be initialized.
  108. * @param SDO SDO server object.
  109. * @param enabled Is trace enabled.
  110. * @param timeBuffer Memory block for storing time records.
  111. * @param valueBuffer Memory block for storing value records.
  112. * @param bufferSize Size of the above buffers.
  113. * @param map Map to variable in Object Dictionary, which will be monitored. Same structure as in PDO.
  114. * @param format Format of the plot. If first bit is 1, above variable is unsigned. For more info see Object Dictionary.
  115. * @param trigger If different than zero, trigger time is recorded, when variable goes through threshold.
  116. * @param threshold Used with trigger.
  117. * @param value Pointer to variable, which will show last value of the variable.
  118. * @param minValue Pointer to variable, which will show minimum value of the variable.
  119. * @param maxValue Pointer to variable, which will show maximum value of the variable.
  120. * @param triggerTime Pointer to variable, which will show last trigger time of the variable.
  121. * @param idx_OD_traceConfig Index in Object Dictionary.
  122. * @param idx_OD_trace Index in Object Dictionary.
  123. *
  124. * @return 0 on success, -1 on error.
  125. */
  126. void CO_trace_init(
  127. CO_trace_t *trace,
  128. CO_SDO_t *SDO,
  129. uint8_t enabled,
  130. uint32_t *timeBuffer,
  131. int32_t *valueBuffer,
  132. uint32_t bufferSize,
  133. uint32_t *map,
  134. uint8_t *format,
  135. uint8_t *trigger,
  136. int32_t *threshold,
  137. int32_t *value,
  138. int32_t *minValue,
  139. int32_t *maxValue,
  140. uint32_t *triggerTime,
  141. uint16_t idx_OD_traceConfig,
  142. uint16_t idx_OD_trace);
  143. /**
  144. * Process trace object.
  145. *
  146. * Function must be called cyclically in 1ms intervals.
  147. *
  148. * @param trace This object.
  149. * @param timestamp Timestamp (usually in millisecond resolution).
  150. *
  151. * @return 0 on success, -1 on error.
  152. */
  153. void CO_trace_process(CO_trace_t *trace, uint32_t timestamp);
  154. /** @} */ /* CO_trace */
  155. #ifdef __cplusplus
  156. }
  157. #endif /*__cplusplus*/
  158. #endif /* (CO_CONFIG_TRACE) & CO_CONFIG_TRACE_ENABLE */
  159. #endif /* CO_TRACE_H */