Skip to content

Commit

Permalink
Merge branch 'main' into max7314-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz authored Apr 13, 2024
2 parents 7070d2b + 19dbb72 commit 6c355e8
Show file tree
Hide file tree
Showing 30 changed files with 1,222 additions and 681 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_cerberus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
run-build:
runs-on: ubuntu-latest
container:
image: nwdepatie/ner-gcc-arm:latest
image: ghcr.io/northeastern-electric-racing/embedded-base:docker-compose
timeout-minutes: 10
steps:
- name: Clone Cerberus
Expand Down
42 changes: 37 additions & 5 deletions cangen/CANField.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
from __future__ import annotations
from .Decoding import *
from ruamel.yaml import Optional
from dataclasses import dataclass

@dataclass
class CANField:
'''
Represents a field in a CAN message. Has an id, a name, a unit, a size,
and an optional Format and Decodings. Also knows its own
and an optional Format and encodings. Also knows its own
index within its parent CANMsg, which is assigned at load from YAML.
Think 1 MQTT Topic per 1 CAN Field
'''

name: str
unit: str
size: int
index: int = -1
size: int # in bits
signed : bool = False
endianness : str = "little"
field_type: str = "*"*42
final_type: str = "f32"
decodings: Optional[list[Decoding]] = None
format: Optional[str] = None

def get_size_bytes(self):
# Calculate max number of bytes to represent value
num_bytes = self.size / 8
if (self.size % 8):
num_bytes += 1

return int(num_bytes)

@dataclass
class DiscreteField(CANField):
"""
Represents a discrete field inside a CAN message in the sense that
it will make sense if it is published to MQTT and timestamped on its own
(think State of Charge or Temperature)
"""

field_type = "discrete"

@dataclass
class CompositeField(CANField):
"""
Represents a composite field inside a CAN message in the sense that
it will only make sense with other data points in the same message and
can't be timestamped on its own (think XYZ acceleration or Roll, Pitch, Yaw)
"""

num_points: int = 0
field_type = "composite"
16 changes: 5 additions & 11 deletions cangen/CANMsg.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
from __future__ import annotations
from dataclasses import dataclass
from .NetworkEncoding import NetworkEncoding
from ruamel.yaml import Optional
from .CANField import CANField

@dataclass
class CANMsg:
'''
Represents a CAN message. Has an id, a description, and a number of individual fields.
'''
id: str
desc: str
networkEncoding: list[NetworkEncoding]

def __post_init__(self) -> None:
idx: int = 0
for field in self.networkEncoding[0].fields:
if (field.index is not None):
field.index = idx
idx += field.size
id: str # Hex value of CAN ID, i.e. `0x88`
desc: str # Brief name of CAN message, used for generating function names
fields: list[CANField] # List of CAN fields in the message (Optional to allow for only networkEncoding)

def __setstate__(self, state):
self.__init__(**state)
32 changes: 0 additions & 32 deletions cangen/Decoding.py

This file was deleted.

2 changes: 1 addition & 1 deletion cangen/Format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Torque(Format):

@dataclass
class HighVoltage(Format):
repr: str = "high_voltage"
repr: str = "high_voltage"

@dataclass
class Current(Format):
Expand Down
2 changes: 1 addition & 1 deletion cangen/Messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
@dataclass
class Messages:
'''
Represents a list of CAN messages. Has a list of CANMsgs.
Represents a list of CAN messages, used to create a container for each YAML
'''
msgs: list[CANMsg]
23 changes: 0 additions & 23 deletions cangen/NetworkEncoding.py

This file was deleted.

33 changes: 33 additions & 0 deletions cangen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# CANGEN
> Definition of all CAN messages to be sent across the car with Python scripts to generate code to encode and decode
Here is a custom Python module built to generate embedded code for encoding and decoding CAN messages.

### Directory Structure
```
|
|───CANField.py:
| |───DiscreteField # Single data point to stand alone
| └───CompositeField # Group of related data points that won't make sense alone
|
|───CANMsg.py:
| └───CANMsg # Represents a full CAN message
|
|───Messages.py:
| └───Messages # Container for all messages related to a node
|
|───Decoding.py:
| |───LittleEndian
| |───BigEndian
| └───TwosComplement
|
|───YAMLParser.py:
| └───YAMLParser # Parses a YAML file into CAN message structures
|
|───RustSynth.py:
| └───RustSynth # Generates Rust code from a CAN messages structure
|
|───CSynth.py:
| └───CSynth # Generates code from a CAN messages structure
|
└───README.md # This file!
Loading

0 comments on commit 6c355e8

Please sign in to comment.