Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equation for Can Traffic #241

Merged
merged 11 commits into from
Nov 7, 2024
59 changes: 25 additions & 34 deletions docs/docs/firmware/can-traffic/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Calculating CAN Traffic

# Calculating CAN traffic

In order to calculate the CAN load, we must declare a few variables and their importance within the CAN protocol.
In order to calculate the CAN load, we must define a few variables related to the CAN protocol.

## Baud

Expand All @@ -22,60 +21,52 @@ The message length in a CAN frame can be calculated as a function of the data le
<figcaption>CAN Bus Frame Diagram. Source: <a href="https://www.csselectronics.com/pages/can-bus-simple-intro-tutorial">CSS Electronics CAN Bus Tutorial</a></figcaption>
</figure>

Each CAN frame consists of several fields with fixed bit lengths, plus a variable-length data field. Here’s the breakdown:

- **Start of Frame (SOF)**: 1 bit
- **Identifier (ID)**: 11 bits for standard frames or 29 bits for extended frames
- **Remote Transmission Request (RTR)**: 1 bit
- **Control (DLC)**: 6 bits
- **Data**: Variable length, 8 bits per byte, depending on data length \( x \)
- **CRC**: 16 bits
- **ACK**: 2 bits
- **End of Frame (EOF)**: 7 bits

**Calculation for fixed portion(\(a\))** : (\( 1 + 11 + 1 + 6 + 16 + 2 + 7 = 44\)) bits
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:

**Variable Portion (b)**: The data field adds 8 bits for each byte of data. So, \( b = 8 \).

**Bit Stuffing**: To ensure synchronization, CAN adds around 20% bit stuffing to the frame length. Including bit stuffing, the approximate formulas are:

- **For a Standard Frame**: \( M(x) \approx 44 + 10x \) bits
|Frame Element| Length(bits)|
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
|--------------|------------|
|Start of Frame (SOF) | 8 |
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
|Identifier (ID)| 11 |
|Remote Transmission Request (RTR)| 1|
| Control (DLC)| 6|
|Cyclic Redundancy Check (CRC) | 16 |
|Acknowledgement (ACK)| 2 |
|End of Frame (EOF)| 7|
|Data Length| 8 bits per byte with 20% bit stuffing = 9.6x|
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

### Final Formula

A message with data length of \( x \) bytes requires up to \( a + bx \) bits.
The maximum total frame length is: (\( 1 + 11 + 1 + 6 + 16 + 2 + 7 + 9.6x = 44 + 9.6x\)) bits
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

--------------------------------------

## Sample calculation

**Given**:

Baud Rate: 500 kbaud (500,000 bits transferred per second)

|Message Length| Data Length| Frequency (Hz)|
|--------------|------------|---------------|
|Battery Status| 8 | 100 |
|Motor Control| 5 | 50 |

Let \(M_1\) represent data length in bytes:
|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|

$$
\text{Total Bits Per Second} = \sum_{i=1}^n \left(\text{Frequency}_i \times \text{Message Length}_i\right)
\text{Total Bits Per Second} = \sum_{i=1}^n \left(\text{Frequency}_i \times \text{Message Length}_i \right)
$$

$$
\text{Total Bits Per Second} = (M_1 \times 8 \, \text{bits/byte} \times F_1) + (M_2 \times 8 \, \text{bits/byte} \times F_2)
$$

$$
\text{Total Bits Per Second} = (8 \times 8 \times 100) + (5 \times 8 \times 50) = 6400 + 2000 = 8400 \, \text{bits per second}
\text{Total Bits Per Second} = (121 \times 100) + (92 \times 50) = 12100 + 4600 = 16700 \, \text{bits per second}
$$

## Calculating Bus Load

### 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\%
$$

$$
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
\text{Bus Load} = \left(\frac{8400}{500,000}\right) \times 100\% = 1.7\%
\text{Bus Load} = \left(\frac{16700}{500,000}\right) \times 100\% = 3.34\%
$$