-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into max7314-fixes
- Loading branch information
Showing
30 changed files
with
1,222 additions
and
681 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
Oops, something went wrong.