Skip to content

Commit

Permalink
Rust synth encoding (#134)
Browse files Browse the repository at this point in the history
* initial bidirectionality

* fix diff of dti and mpu by hand bc git sucks

* misc improvements

* add docs

* improve const evals, add hacky formatting, fix example
  • Loading branch information
jr1221 authored Aug 2, 2024
1 parent 0ff91be commit fdfdca7
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 66 deletions.
5 changes: 5 additions & 0 deletions cangen/CANField.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from typing import Optional
from dataclasses import dataclass
import math

@dataclass
class CANPoint:
Expand All @@ -13,6 +14,7 @@ class CANPoint:
endianness : str = "big"
final_type: str = "f32"
format: Optional[str] = None
default: Optional[float] = 0

def get_size_bytes(self):
# Calculate max number of bytes to represent value
Expand All @@ -25,6 +27,9 @@ def get_size_bytes(self):
def get_size_bits(self):
return self.size

def get_size_min_bytes(self):
return math.ceil(self.size / 8.0) * 8

@dataclass
class NetField:
'''
Expand Down
19 changes: 17 additions & 2 deletions cangen/CANMsg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
from .CANField import NetField

@dataclass
Expand All @@ -11,5 +12,19 @@ class CANMsg:
desc: str # Brief name of CAN message, used for generating function names
fields: list[NetField] # List of CAN fields in the message

def __setstate__(self, state):
self.__init__(**state)
@dataclass
class EncodableCANMsg(CANMsg):
'''
Represents a CAN message that can also be encoded to using the command_data protobuf spec.
Has all properties of a decodable message, but also has a key and optional is_ext field.
IMPORTANT: Use the default flag in each CANPoint to specify a default value to be sent before a command is sent.
'''
'''
The name to be used to look up command data over mqtt
'''
key: str
'''
Whether the CAN ID is extended or standard
'''
is_ext: Optional[bool] = False
34 changes: 25 additions & 9 deletions cangen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ Messages should follow these rules:
3. Wherever possible, bit-wise decoding and byte-wise decoding should happen in seperate bytes to avoid confusion.
Ex. If there are 5 messages of size one (booleans), add a 3 bit filler before adding a 16 bit number
4. Message totals should be byte aligned, meaning total bit size of a message should be a power of 2
5. **Signed messages must be 8,16,or 32 bits!**
6. **Little endian messages must be 8,16, or 32 bits!**
5. **Signed messages must be 8,16,or 32 bits and byte aligned!**
6. **Little endian messages must be 8,16, or 32 bits and byte aligned!**
7. Maximum size of a sent message (default, aka send=True), is 32 bits
8. Unsent messages should only contain the size parameter
9. Make the topic of an EncodableCANMsg be "Calypso/Bidir/State/{key}/{field_name}"
10. The description field must be only letters and spaces, upper or lowercase

Message guide:
1. Use previous examples for most things
Expand All @@ -22,7 +25,10 @@ Message guide:
Note: Single bit messages are memcpy-ed wrong by default, you may need to use `reverse_bits` in `c_utils.h`
Note: Please use big endian whenever possible, as that is the standard at which our MC, Charger Box, etc. expect it. Use `endian_swap` in `c_utils.h`

YAML info.
## YAML Spec.

### Decodable messages

```
# all files start with this yaml
!Messages
Expand All @@ -42,7 +48,22 @@ msgs:
signed: false # (optional) whether the number is in signed twos complement form, default is false
endianness: "big" # (optional) the byte endianness of the bits being read, "big" or "false", "big" is default
format: "formatter_name" # (optional) the name of the formatter to use, default is no formatting, see above for formatter info
final_type: "f32" # (optional, not recommended) the final type of the data
final_type: "f32" # (optional, not recommended) the final type of the data
```

### Encodable messages
Occassionally you may want Calypso to also send a message on the CAN network. Use the above fields, with these modifications/additions:
It is recommended that the decoding of the message be done to the topic "Calypso/Bidir/State/{key}/{field_name}". Note decoding works exactly the same with these messages, so serves as an accurate representation of what Calypso is current sending out to the car.

```
- !EncodableCANMsg # use this instead of CANMsg
is_ext: false # (optional) whether the CAN ID of the message is extended or standard, default is false
key: # the key to index the encodable message to, so it would be sent to Calypso on "Calypso/Bidir/Command/{key}"
- !CANPoint:
default: 0 # (optional) the default value to be sent before a command is recieved or when an empty command is recieved, default is 0. This is ignored when decoding the point
```

### Directory Structure
Expand All @@ -58,11 +79,6 @@ msgs:
|───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
|
Expand Down
12 changes: 9 additions & 3 deletions cangen/Result.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ class Result:
master_mapping is the synthesized Rust code for the master_mapping.rs file.
"""
decode_data: str
master_mapping: str
decode_master_mapping: str
encode_data: str
encode_master_mapping: str
format_data: str

def __init__(self, decode_data: str, master_mapping: str):
def __init__(self, decode_data: str, master_mapping: str, encode_data: str, encode_master_mapping: str, format_data: str):
self.decode_data = decode_data
self.master_mapping = master_mapping
self.decode_master_mapping = master_mapping
self.encode_data = encode_data
self.encode_master_mapping = encode_master_mapping
self.format_data = format_data
Loading

0 comments on commit fdfdca7

Please sign in to comment.