Skip to content

Commit

Permalink
Add bidirectional communications to Calypso (#44)
Browse files Browse the repository at this point in the history
* initial bidirectional config

* remove generated files

* fix clippy

* add msbs, format, and clippy

* misc error fixes, cleanup, release performance improvements

* remove unneeded result, fix up docs

* remove uneeded let

* bump and upgrade bitstream to const eval, msrv 1.79

* make clippy happy

* bump
  • Loading branch information
jr1221 authored Aug 2, 2024
1 parent 3f9dcf7 commit c2fb31c
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 352 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustflags = ["-Ctarget-cpu=native"]
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@ __pycache__/
build/
dist/
logs/
master_mapping.rs

# protobuf
mod.rs
command_data.rs
serverdata.rs

# cangen files generaed by calypsogen.py
decode_master_mapping.rs
decode_data.rs
encode_data.rs
encode_master_mapping.rs
format_data.rs
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "calypso"
version = "0.1.0"
edition = "2021"
rust-version = "1.79"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -10,4 +11,13 @@ socketcan = "3.3.0"
paho-mqtt = "0.12.5"
protobuf-codegen = "3.3.0"
protobuf = "3.3.0"
bitstream-io = "2.3.0"
bitstream-io = "2.5.0"


[build-dependencies]
protobuf-codegen = "3.3.0"

[profile.release]
lto = true
codegen-units = 1
panic = "abort"
11 changes: 11 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ fn main() {
std::process::exit(1);
}
}

protobuf_codegen::Codegen::new()
.pure()
// All inputs and imports from the inputs must reside in `includes` directories.
.includes(["src/proto"])
// Inputs must reside in some of include paths.
.input("src/proto/command_data.proto")
.input("src/proto/serverdata.proto")
// Specify output directory relative to Cargo output directory.
.out_dir("src")
.run_from_script();
}
26 changes: 23 additions & 3 deletions calypsogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,44 @@
spec.loader.exec_module(cangen)

decode_data = open("./src/decode_data.rs", "w")
master_mapping = open("./src/master_mapping.rs", "w")
decode_master_mapping = open("./src/decode_master_mapping.rs", "w")

encode_data = open("./src/encode_data.rs", "w")
encode_master_mapping = open("./src/encode_master_mapping.rs", "w")

format_data = open("./src/format_data.rs", "w")


bms_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/bms.yaml", "r"))
mpu_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/mpu.yaml", "r"))
wheel_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/wheel.yaml", "r"))
dti_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/dti.yaml", "r"))
calypso_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/calypso_cmd.yaml", "r"))
charger_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/charger.yaml", "r"))
msb_messages = cangen.YAMLParser().parse(open(f"{EMBEDDED_BASE_PATH}/{module_name}/can-messages/msb.yaml", "r"))


bms_messages.msgs.extend(mpu_messages.msgs)
bms_messages.msgs.extend(wheel_messages.msgs)
bms_messages.msgs.extend(dti_messages.msgs)
bms_messages.msgs.extend(charger_messages.msgs)
bms_messages.msgs.extend(calypso_messages.msgs)
bms_messages.msgs.extend(msb_messages.msgs)

result = cangen.RustSynth().parse_messages(bms_messages.msgs)

decode_data.write(result.decode_data)
decode_data.close()

master_mapping.write(result.master_mapping)
master_mapping.close()
decode_master_mapping.write(result.decode_master_mapping)
decode_master_mapping.close()

encode_data.write(result.encode_data)
encode_data.close()

encode_master_mapping.write(result.encode_master_mapping)
encode_master_mapping.close()

format_data.write(result.format_data)
format_data.close()

47 changes: 43 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
/**
* Wrapper Class for Data coming off the car
*/
pub struct Data {
pub struct DecodeData {
pub value: Vec<f32>,
pub topic: String,
pub unit: String,
Expand All @@ -12,7 +12,7 @@ pub struct Data {
/**
* Implementation for the format of the data for debugging purposes
*/
impl fmt::Display for Data {
impl fmt::Display for DecodeData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Overrides the string representation of the class.

Expand All @@ -25,9 +25,9 @@ impl fmt::Display for Data {
}

/**
* Implementation fo the Data Structs' methods
* Implementation fo the DecodeData methods
*/
impl Data {
impl DecodeData {
/**
* Constructor
* @param id: the id of the data
Expand All @@ -42,3 +42,42 @@ impl Data {
}
}
}

/**
* Wrapper Class for data going into the car
*/
pub struct EncodeData {
pub value: Vec<u8>,
pub id: u32,
pub is_ext: bool,
}

/**
* Implementation for the format of the data for debugging purposes
*/
impl fmt::Display for EncodeData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Overrides the string representation of the class.

write!(
f,
"{}#{:?} (extended: {})",
self.id, self.value, self.is_ext
)
}
}

/**
* Implementation fo the DecodeData methods
*/
impl EncodeData {
/**
* Constructor
* @param id: the id of the can message
* @param value: the can message payload
* @param is_ext: whether the can message is extended format ID
*/
pub fn new(id: u32, value: Vec<u8>, is_ext: bool) -> Self {
Self { id, value, is_ext }
}
}
16 changes: 8 additions & 8 deletions src/message.rs → src/decodable_message.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use super::data::Data;
use super::data::DecodeData;

use super::master_mapping::get_message_info;
use super::decode_master_mapping::get_message_info;
/**
* Wrapper class for an individual message.
*/
pub struct Message {
pub struct DecodableMessage {
id: u32,
data: Vec<u8>,
}

/**
* Implementation of Message.
*/
impl Message {
impl DecodableMessage {
/**
* Creates a new message with the given timestamp, id, and data.
*/
Expand All @@ -23,8 +23,8 @@ impl Message {
/**
* Decodes the message and returns a vector of Data objects.
*/
pub fn decode(&self) -> Vec<Data> {
Message::decode_message(&self.id, &self.data)
pub fn decode(&self) -> Vec<DecodeData> {
DecodableMessage::decode_message(&self.id, &self.data)
}

/**
Expand All @@ -35,9 +35,9 @@ impl Message {
* param data: The data of the message.
* return: A vector of Data objects.
*/
fn decode_message(id: &u32, data: &[u8]) -> Vec<Data> {
fn decode_message(id: &u32, data: &[u8]) -> Vec<DecodeData> {
let decoder = get_message_info(id).decoder;
let mut decoded_data: Vec<Data> = Vec::new();
let mut decoded_data: Vec<DecodeData> = Vec::new();
let result = decoder(data);
for data in result {
decoded_data.push(data);
Expand Down
41 changes: 41 additions & 0 deletions src/encodable_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::data::EncodeData;

use super::encode_master_mapping::get_message_info;
/**
* Wrapper class for an individual encodable message.
*/
pub struct EncodableMessage {
key: String,
data: Vec<f32>,
}

/**
* Implementation of Message.
*/
impl EncodableMessage {
/**
* Creates a new message with the given string key and value
*/
pub fn new(key: String, data: Vec<f32>) -> Self {
Self { key, data }
}

/**
* Decodes the message and returns a struct which defines a CAN packet
*/
pub fn encode(self) -> EncodeData {
EncodableMessage::encode_message(self.key, self.data)
}

/**
* Decodes the message and returns a vector of Data objects.
* Achieves this by calling the decoder function associated with the message key.
* param key: The key of the message.
* param data: The data of the message.
* return: A vector of Data objects.
*/
fn encode_message(key: String, data: Vec<f32>) -> EncodeData {
let encoder = get_message_info(key).encoder;
encoder(data)
}
}
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
pub mod command_data;
pub mod data;
pub mod decodable_message;
pub mod decode_data;
pub mod master_mapping;
pub mod message;
pub mod decode_master_mapping;
pub mod encodable_message;
pub mod encode_data;
pub mod encode_master_mapping;
pub mod format_data;
pub mod mqtt;
pub mod serverdata;
Loading

0 comments on commit c2fb31c

Please sign in to comment.