diff --git a/docs/docs/firmware/can-traffic/img/canbusdiagram.svg b/docs/docs/firmware/can-traffic/img/canbusdiagram.svg new file mode 100644 index 00000000..b5f857c7 --- /dev/null +++ b/docs/docs/firmware/can-traffic/img/canbusdiagram.svg @@ -0,0 +1 @@ +Standard CAN frameSOFIDRTRControlDataCRCACKEOF111160-641627Start ofFrameStandardIdentifierRemote Trans-mission RequestCyclic RedundancyCheckAcknow-ledgementEnd of Frame#bits \ No newline at end of file diff --git a/docs/docs/firmware/can-traffic/index.md b/docs/docs/firmware/can-traffic/index.md new file mode 100644 index 00000000..f6ef6803 --- /dev/null +++ b/docs/docs/firmware/can-traffic/index.md @@ -0,0 +1,87 @@ +# Calculating CAN Traffic + +In order to calculate the CAN load, we must define a few variables related to the CAN protocol. + +## Baud + +The baud rate is the rate at which data is transfered in a communication channel (like CAN). For example, 9600 Baud rate is 9600 bits per second + +## Frequency + +The frequency of a message, expressed in (Hz), is how often a message is transmitted per second. Different messages can have different frequencies. + +This variation affects the total bits transmitted and, in turn, the bus load. To calculate bits per second, consider each message’s frequency and whether their lengths differ. + +## Message Length + +The message length in a CAN frame can be calculated as a function of the data length. This calculation includes several fixed fields and a variable data field, which changes based on the message's data content. + +
+![](img/canbusdiagram.svg){ width="100%" } +
CAN Bus Frame Diagram. Source: CSS Electronics CAN Bus Tutorial
+
+ +Each CAN frame consists of several fields with fixed bit lengths, plus a variable-length data field. Let \(x\) be the number of bytes. Here’s the breakdown: + +|Frame Element| Length (bits)| +|--------------|------------| +|Start of Frame (SOF) | 1 | +|Identifier (ID)| 11 | +|Remote Transmission Request (RTR)| 1| +| Control (DLC)| 6| +|Data| 8 bits per byte with 20% bit stuffing = 9.6x| +|Cyclic Redundancy Check (CRC) | 16 | +|Acknowledgement (ACK)| 2 | +|End of Frame (EOF)| 7| + +### Final Formula + +The maximum total frame length is: + +\[ 1 + 11 + 1 + 6 + 16 + 2 + 7 + 9.6x = 44 + 9.6x \text{ bits}\] + +where \(x\) represents the number of bytes in the variable data field. + +-------------------------------------- + +## Total CAN Traffic + +\begin{align} +\text{Total Bits Per Second} &= \sum_{i=1}^n \left(\text{Frequency}_i \times \text{Message Length}_i \right)\\ +\end{align} + +## Total Bus Load + +\begin{align} +\text{Bus Load}\%= \frac{\text{Total Bits per Second}}{\text{Baud Rate}} \times 100\% +\end{align} + +-------------------------------------- + +## Sample calculation + +**Given**: + +Baud Rate: 500 kbaud (500,000 bits transferred per second) + +|Message Type | Data Length| Frequency (Hz)| Message Length (Bits)| +|--------------|------------|---------------|---------------------| +|Battery Status| 8 | 100 | 44 + 9.6 x 8 = 121| +|Motor Control| 5 | 50 | 44 + 9.6 x 5 = 92| + +\begin{align} +\text{Total Bits Per Second} &= \sum_{i=1}^n \left(\text{Frequency}_i \times \text{Message Length}_i \right)\\ +\text{Total Bits Per Second} &= (100\text{ Hz} \times 121\text{ bits}) + (50\text{ Hz}\times 92\text{ bits})\\ +&=(12100 + 4600)\text{ bits per second}\\ +&= 16700\text{ bits per second} +\end{align} + +The bus load is the previous example can be calculated as: + +$$ +\text{Bus Load}\%= \frac{\text{Total Bits per Second}}{\text{Baud Rate}} \times 100\% +$$ + +$$ +\text{Bus Load} = \left(\frac{16700}{500,000}\right) \times 100\% = 3.34\% +$$ diff --git a/docs/javascripts/katex.js b/docs/javascripts/katex.js new file mode 100644 index 00000000..3828300a --- /dev/null +++ b/docs/javascripts/katex.js @@ -0,0 +1,10 @@ +document$.subscribe(({ body }) => { + renderMathInElement(body, { + delimiters: [ + { left: "$$", right: "$$", display: true }, + { left: "$", right: "$", display: false }, + { left: "\\(", right: "\\)", display: false }, + { left: "\\[", right: "\\]", display: true } + ], + }) + }) \ No newline at end of file diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 35236fb6..f4dcf97e 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -32,6 +32,12 @@ theme: extra_css: - stylesheets/extra.css + - https://unpkg.com/katex@0/dist/katex.min.css + +extra_javascript: + - https://unpkg.com/mathjax@3/es5/tex-mml-chtml.js + - https://unpkg.com/katex@0/dist/katex.min.js + - https://unpkg.com/katex@0/dist/contrib/auto-render.min.js copyright: Copyright © McMaster Formula Electric @@ -91,6 +97,8 @@ markdown_extensions: base_path: - !relative - !relative $docs_dir/../ + - pymdownx.arithmatex: + generic: true repo_url: https://github.com/macformula/racecar repo_name: macformula/racecar @@ -127,6 +135,7 @@ nav: - firmware/compile-project.md - firmware/flashing/index.md - firmware/hw-debug.md + - firmware/can-traffic/index.md - Glossary: glossary.md watch: