Skip to content

Commit

Permalink
Add a way to simulate messages (#61)
Browse files Browse the repository at this point in the history
* cangen simulation structure

* moved simulate into a separate binary

* added sim code genreation

* improved new value generation

* bug fixes

* added struct for param passing, removed format and signed

* fixed value initialization for boolean values

* added rounding for nicer numbers and boolean cases

* updated submodule pointer

* ok cargo clippy

* fixed NaN values

* limit retry attempts; refactoring and cleanup

* update embedded-base pointer

* bump submodule

---------

Co-authored-by: Jack Rubacha <[email protected]>
  • Loading branch information
tszwinglitw and jr1221 authored Oct 17, 2024
1 parent 0a2cf21 commit bcc1a76
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ decode_data.rs
encode_data.rs
encode_master_mapping.rs
format_data.rs
simulate_data.rs

# my dumb script
./format
/privatetest/
78 changes: 78 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "calypso"
version = "0.1.0"
edition = "2021"
rust-version = "1.79"
default-run = "calypso"

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

Expand All @@ -13,6 +14,7 @@ protobuf-codegen = "3.5.1"
protobuf = "3.5.1"
bitstream-io = "2.5.3"
clap = { version = "4.5.20", features = ["derive", "env"] }
rand = "0.8"


[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion Embedded-Base
Submodule Embedded-Base updated 72 files
+19 −0 .github/workflows/format-check.yml
+3 −1 .gitignore
+0 −8 aliases.txt
+4 −0 cangen/CANField.py
+1 −0 cangen/CANMsg.py
+9 −2 cangen/README.md
+3 −1 cangen/Result.py
+71 −1 cangen/RustSynthFromJSON.py
+519 −577 cangen/can-messages/bms.json
+27 −0 cangen/can-messages/calypso_cmd.json
+44 −2 cangen/can-messages/charger.json
+221 −12 cangen/can-messages/dti.json
+158 −2 cangen/can-messages/mpu.json
+156 −0 cangen/can-messages/msb.json
+9 −0 cangen/can-messages/wheel.json
+2 −2 cangen/jsongen
+16 −0 ftdi_flash.cfg
+1 −0 general/.clang-format-ignore
+20 −29 general/include/LTC4015.h
+7 −5 general/include/ads131m04.h
+43 −36 general/include/lsm6dso.h
+49 −47 general/include/ltc68041.h
+7 −5 general/include/m24c32.h
+16 −14 general/include/max7314.h
+3 −4 general/include/mcp23008.h
+26 −21 general/include/pca9539.h
+12 −12 general/include/pi4ioe.h
+21 −19 general/include/sht30.h
+256 −183 general/include/vl6180x_api.h
+39 −31 general/include/vl6180x_cfg.h
+356 −296 general/include/vl6180x_def.h
+46 −30 general/include/vl6180x_i2c.h
+115 −94 general/include/vl6180x_platform.h
+17 −13 general/include/vl6180x_types.h
+49 −43 general/src/LTC4015.c
+78 −66 general/src/ads131m04.c
+56 −35 general/src/lsm6dso.c
+824 −736 general/src/ltc68041.c
+47 −41 general/src/m24c32.c
+136 −120 general/src/max7314.c
+12 −12 general/src/mcp23008.c
+18 −16 general/src/pca9539.c
+39 −39 general/src/pi4ioe.c
+48 −44 general/src/sht30.c
+2,459 −2,467 general/src/vl6180x_api.c
+163 −165 general/src/vl6180x_i2c.c
+0 −65 load_alias.py
+4 −7 middleware/include/c_utils.h
+17 −23 middleware/include/eepromdirectory.h
+16 −14 middleware/include/i2c_utility.h
+13 −13 middleware/include/pid.h
+13 −13 middleware/include/ringbuffer.h
+10 −11 middleware/include/timer.h
+13 −14 middleware/src/c_utils.c
+66 −75 middleware/src/eepromdirectory.c
+156 −138 middleware/src/i2c_utility.c
+46 −47 middleware/src/pid.c
+60 −60 middleware/src/ringbuffer.c
+16 −17 middleware/src/timer.c
+3 −0 ner_environment/build_system/__init__.py
+295 −0 ner_environment/build_system/build_system.py
+0 −0 ner_environment/build_system/clang_restage.py
+21 −10 ner_environment/build_system/miniterm.py
+42 −57 ner_environment/ner_setup.py
+2 −0 ner_environment/requirements.txt
+19 −0 ner_environment/setup.py
+7 −0 openocd.cfg
+6 −6 platforms/stm32f405/include/can.h
+18 −12 platforms/stm32f405/src/can.c
+3 −3 platforms/stm32g431/include/fdcan.h
+5 −6 platforms/stm32g431/src/fdcan.c
+0 −25 setup.py
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Ex. `cansend vcan0 702#01010101FFFFFFFF`
Now view calypso interpret the can message and broadcast it on `mqttui`


### Simulation Mode
- Same setup as above, then use the entry point `simulate` instead of `main`
- ```cargo run --bin simulate```
- ```cargo run --bin simulate -- -u localhost:1883```



### Generate Proto

Expand Down
5 changes: 5 additions & 0 deletions calypsogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
encode_data = open("./src/encode_data.rs", "w")
encode_master_mapping = open("./src/encode_master_mapping.rs", "w")

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

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


Expand Down Expand Up @@ -56,6 +58,9 @@
encode_master_mapping.write(result.encode_master_mapping)
encode_master_mapping.close()

simulate_data.write(result.simulate_data)
simulate_data.close()

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

69 changes: 69 additions & 0 deletions src/bin/simulate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{
thread::{self},
time::Duration,
};

use calypso::{
mqtt::MqttClient, serverdata, simulatable_message::SimulatedComponent, simulate_data::create_simulated_components
};
use clap::Parser;


/// Calypso command line arguments
#[derive(Parser, Debug)]
#[command(version)]
struct CalypsoArgs {
/// The host url of the siren, including port and excluding protocol prefix
#[arg(
short = 'u',
long,
env = "CALYPSO_SIREN_HOST_URL",
default_value = "localhost:1883"
)]
siren_host_url: String,
}


fn simulate_out(pub_path: &str) {
let mut client = MqttClient::new(pub_path, "calypso-simulator");
let _ = client.connect(); // todo: add error handling
let sleep_time = Duration::from_millis(10);

// todo: a way to turn individual components on and off
let mut simulated_components: Vec<SimulatedComponent> = create_simulated_components();

// loop through the simulated components, if they should update, update them and publish the data
loop {
for component in simulated_components.iter_mut() {
if component.should_update() {
component.update();
let data: calypso::data::DecodeData = component.get_data();
let mut payload = serverdata::ServerData::new();
payload.unit = data.unit.to_string();
payload.value = data.value.iter().map(|x| x.to_string()).collect();

client
.publish(
data.topic.to_string(),
protobuf::Message::write_to_bytes(&payload).unwrap_or_else(|e| {
format!("failed to serialize {}", e).as_bytes().to_vec()
}),
)
.expect("Could not publish!");
}
}
// sleep for a bit
thread::sleep(sleep_time);
}
}



/**
* Main Function
* Calls the `simulate_out` function with the siren host URL from the command line arguments.
*/
fn main() {
let cli = CalypsoArgs::parse();
simulate_out(&cli.siren_host_url);
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ pub mod encode_master_mapping;
pub mod format_data;
pub mod mqtt;
pub mod serverdata;
pub mod simulate_data;
pub mod simulatable_message;
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,4 @@ fn main() {

can_handle.join().expect("Decoder failed with ");
println!("Decoder ended");
}
}
Loading

0 comments on commit bcc1a76

Please sign in to comment.