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

Added CAN Equation #328

Merged
merged 11 commits into from
Dec 26, 2024
70 changes: 70 additions & 0 deletions scripts/cangen/cangen/traffic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Author: Manush Patel
Date: 2024-11-24
https://macformula.github.io/racecar/firmware/can-traffic/
"""
from dataclasses import dataclass

@dataclass
class CANMessage:
data_length: int
frequency: int

def frame_length(data_bytes:int) -> float:
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
"""
Calculate the total length of a single CAN frame in bits, this will include bit stuffing
:param frame_data is tuple containing the bytes of data, and frames per second
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
:return: Bus load as percentage
"""

total_message_length = 0
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

fixed_length = 44 #fixed length of CAN frame

variable_length = data_bytes * 9.6 #accounted for bit stuffing

total_frame_length = variable_length + fixed_length

return total_frame_length

def calculate_bits(message_length, frequency):
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
"""
Calculate total bits by multiplying with frequency of message
:param message_length is float representing the message length
:param frequency is an int representing how often a message is transmitted per second
"""

total_bits = message_length * frequency

return (total_bits)
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

def calculate_bus_load(total_bits, can_speed):
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
"""
Calculate bus load by dividing total bits by can_speed (default: 500,000bits)
Return Bus Load percentage
"""

return (total_bits/ (can_speed))*100 #bus load formula
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__": #TEST CASE USED

total_bits = 0
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

can_speed = 500000 #set to 500kbaud
BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved

messages = [
CANMessage(data_length=8, frequency=100), # 8 bytes, 200 frames/sec
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
CANMessage(data_length=4, frequency=50), # 4 bytes, 100 frames/sec
CANMessage(data_length=5, frequency=500), # 5 bytes, 500 frames/sec
]

# Initialize the calculator
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved
for message in messages:
ManushPatell marked this conversation as resolved.
Show resolved Hide resolved

message_length = frame_length(message.data_length) #collect frame length of message

total_bits +=(calculate_bits(message_length, message.frequency)) #add bits per second of message to bits_persec

bus_load = calculate_bus_load(total_bits, can_speed)

print(f"Calculated Bus Load is: {bus_load:.2f}%")
Loading