Skip to content

Commit

Permalink
#20 finalize necessary can messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyton-McKee committed Jan 11, 2024
1 parent 61e6098 commit 611ca2c
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 108 deletions.
23 changes: 10 additions & 13 deletions oxy/RustSynth.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
from structs.CANField import CANField
from structs.CANMsg import CANMsg
from structs.Decoding import Decoding

from typing import Optional

class RustSynth:
'''
A class to synthesize Rust from a given CANMsg spec.
'''

inst_hashmap: str = " let mut result = HashMap::new();"
return_type: str = "Vec<Data>"
inst_hashmap: str = f" let mut result = {return_type}::new();"
closing: str = " result\n}"

def synthesize(self, msg: CANMsg) -> str:
signature: str = self.signature(msg.desc)
generated_lines: list[str] = []
for field in msg.fields:
generated_lines.append(self.finalize_line(field.id, f"({self.parse_decoders(field)}){self.correcting_factor(field)}"))
generated_lines.append(self.finalize_line(field.name, field.id, f"({self.format_data(field, self.parse_decoders(field))})"))
total_list: list[str] = [signature, self.inst_hashmap] + generated_lines + [self.closing]
return "\n".join(total_list)

def signature(self, to_decode: str) -> str:
return f"pub fn decode_{to_decode.replace(' ', '_')}(data: &[u8]) -> HashMap<u8, f32> {{"
return f"pub fn decode_{to_decode.replace(' ', '_')}(data: &[u8]) -> {self.return_type} {{"

def finalize_line(self, id: int, val: str) -> str:
return f" result.insert({id}, {val});"
def finalize_line(self, topic: str, id: int, val: str) -> str:
return f" result.push(Data::new({id}, {val}, \"{topic}\"));"

def parse_decoders(self, field: CANField) -> str:
if isinstance(field.decodings, type(None)):
Expand All @@ -35,8 +32,8 @@ def parse_decoders(self, field: CANField) -> str:
base = f"pd::{decoder.repr}({base}, {decoder.bits}) as {decoder.final_type}"
return base

def correcting_factor(self, field:CANField) -> str:
cf: str = ""
if field.correcting_factor:
cf = f" {field.correcting_factor.op} {field.correcting_factor.const}"
def format_data(self, field:CANField, decoded_data: str) -> str:
cf = decoded_data
if field.format:
cf = f"fd::{field.format}({decoded_data})"
return cf
8 changes: 3 additions & 5 deletions oxy/YAMLParser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from io import TextIOWrapper
from ruamel.yaml import YAML, Any

from structs.CANMsg import CANMsg
from structs.CANField import CANField
from structs.CorrectingFactor import CorrectingFactor
import structs.Decoding
from structs.Format import Format
from structs.Decoding import Decoding

class YAMLParser:
'''
Expand All @@ -16,8 +15,7 @@ def __init__(self):
self.yaml = YAML()
self.yaml.register_class(CANMsg)
self.yaml.register_class(CANField)
self.yaml.register_class(CorrectingFactor)
for decoding in structs.Decoding.Decoding.__subclasses__():
for decoding in Decoding.__subclasses__():
self.yaml.register_class(decoding)


Expand Down
178 changes: 164 additions & 14 deletions oxy/mapping.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#BMS BROADCAST
!CANMsg
id: 1
id: "0x80"
desc: "accumulator status"
fields:
- !CANField
id: 1
name: Pack Inst Voltage
name: "BMS/Pack/Voltage"
units: "V"
size: 2
decodings:
- !BigEndian
bits: 8
final_type: "f32"
correcting_factor:
!CorrectingFactor
const: 10.0
op: "/"
format: "high_voltage"
- !CANField
id: 2
name: "Pack Current"
name: "BMS/Pack/Current"
units: "A"
size: 2
decodings:
Expand All @@ -27,13 +25,10 @@ fields:
- !TwosComplement
bits: 16
final_type: "f32"
correcting_factor:
!CorrectingFactor
const: 10.0
op: "/"
format: "current"
- !CANField
id: 3
name: "Pack Amp-hours"
name: "BMS/Pack/Amp-hours"
units: "Ah"
size: 2
decodings:
Expand All @@ -42,13 +37,168 @@ fields:
final_type: "f32"
- !CANField
id: 4
name: "Pack SOC"
name: "BMS/Pack/SOC"
units: "%"
size: 1
final_type: "f32"
- !CANField
id: 5
name: "Pack Health"
name: "BMS/Pack/Health"
units: "%"
size: 1
final_type: "f32"

!CANMsg
id: "0x81"
desc: "BMS Status"
fields:
- !CANField
id: 106
name: "BMS/State"
units: ""
size: 1
final_type: "f32"
- !CANField
id: 107
name: "BMS/Faults"
unit: ""
size: 4
decodings:
- !LittleEndian
bits: 8
final_type: "f32"
- !CANField
id: 10
name: "BMS/Temps/Average"
units: "C"
size: 1
decodings:
- !TwosComplement
bits: 8
final_type: "f32"
- !CANField
id: 11
name: "BMS/Temps/Internal"
size: 1
decodings:
- !TwosComplement
bits: 8
final_type: "f32"
- !CANField
id: 143
name: "BMS/Cells/BurningStatus"
size: 1

!CANMsg
id: "0x82"
desc: "Shutdown Control"
fields:
- !CANField
id: 12
name: "BMS/Shutdown/MPE"
size: 1
final_type: "f32"

!CANMsg
id: "0x83"
desc: "Cell Data"
fields:
- !CANField
id: 13
name: "BMS/Cells/Volts/High/Value"
size: 2
decodings:
- !LittleEndian
bits: 8
final_type: "i32"
format: "cell_voltage"
- !CANField
id: 121
name: "BMS/Cells/Volts/High/Chip"
size: 1
decodings:
- !Half
bits: 4
final_type: "f32"
- !CANField
id: 122
name: "BMS/Cells/Volts/High/Cell"
index: 2
size: 1
decodings:
- !Half
bits: 0
final_type: "f32"
- !CANField
id: 15
name: "BMS/Cells/Volts/Low/Value"
size: 2
index: 3
decodings:
- !LittleEndian
bits: 8
final_type: i32
format: "cell_voltage"
- !CANField
id: 123
name: "BMS/Cells/Volts/Low/Chip"
index: 5
size: 1
decodings:
- !Half
bits: 4
final_type: "f32"
- !CANField
id: 124
name: "BMS/Cells/Volts/Low/Cell"
index: 5
size: 1
- !Half
bits: 0
final_type: "f32"
- !CANField
id: 17
name: "BMS/Cells/Volts/Ave/Value"
size: 2
index: 6
decodings:
- !LittleEndian
bits: 8
final_type: i32
format: "cell_voltage"

!CANMsg
id: "0x84"
desc: "Cell Temperatures"

!CANMsg
i: "0x85"
desc: "Segment Temperatures"



# MPU BROADCAST
!CANMsg
id: "0x500"
desc: "NERduino Acceleromter"
fields:
- !CANField
id: 91
name: "MPU/Accel/X"
size:

!CANMsg
id: "0x501"
desc: "MPU Status"



#STEERINGWHEEL
!CANMsg
id: "0x680"
desc: "Wheel State"





48 changes: 0 additions & 48 deletions oxy/poc_translator.py

This file was deleted.

4 changes: 2 additions & 2 deletions oxy/structs/CANField.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations
from .CorrectingFactor import CorrectingFactor
from .Decoding import *
from ruamel.yaml import Optional
from dataclasses import dataclass
from .Format import Format

@dataclass
class CANField:
Expand All @@ -16,5 +16,5 @@ class CANField:
units: str
size: int
index: int = -1
correcting_factor: Optional[CorrectingFactor] = None
decodings: Optional[list[Decoding]] = None
format: Optional[str] = None
9 changes: 4 additions & 5 deletions oxy/structs/CANmsg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from __future__ import annotations
from ruamel.yaml import Optional, MappingNode
from structs.CorrectingFactor import CorrectingFactor
from .CANField import CANField
from dataclasses import dataclass

Expand All @@ -9,15 +7,16 @@ class CANMsg:
'''
Represents a CAN message. Has an id, a description, and a number of individual fields.
'''
id: int
id: str
desc: str
fields: list[CANField]

def __post_init__(self) -> None:
idx: int = 0
for field in self.fields:
field.index = idx
idx += field.size
if (field.index is not None):
field.index = idx
idx += field.size


def __setstate__(self, state):
Expand Down
9 changes: 0 additions & 9 deletions oxy/structs/CorrectingFactor.py

This file was deleted.

Loading

0 comments on commit 611ca2c

Please sign in to comment.