Skip to content

Commit

Permalink
refactor plotter
Browse files Browse the repository at this point in the history
  • Loading branch information
strasdat committed Oct 7, 2023
1 parent 77e1fc2 commit 89520c0
Show file tree
Hide file tree
Showing 25 changed files with 1,258 additions and 1,231 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]

resolver = "2"
members = [
"rs/remote_plot",
"rs/plotting",
]
28 changes: 13 additions & 15 deletions cpp/farm_ng/core/plotting/remote_plot_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ int main(int argc, char* argv[]) {

pool->start(8);

plotting::Curve sin_graph;
sin_graph.path = "trig0/sin";
sin_graph.color = sophus::Color::neonRed();

plotting::Curve cos_graph;
cos_graph.path = "trig0/cos";
cos_graph.color = sophus::Color::neonGreen();
plotting::Vec3Curve trig_graph;
trig_graph.path = "trig/ {sin,cos,tan}";
trig_graph.color = {
sophus::Color::neonRed(),
sophus::Color::neonGreen(),
sophus::Color::neonBlue()};

plotting::Curve sawtooth;
sawtooth.path = "trig1/sawtooth";
Expand All @@ -58,21 +57,21 @@ int main(int argc, char* argv[]) {
while (true) {
double sin_x = std::sin(x);
double cos_x = std::cos(x);
double tan_x = std::clamp(std::tan(x), -1.0, 1.0);

double sawtooth_x = x - std::floor(x / (2.0 * M_PI)) * (2.0 * M_PI);

sin_graph.x_y_pairs.clear();
cos_graph.x_y_pairs.clear();
trig_graph.x_vec_pairs.clear();
sawtooth.x_y_pairs.clear();
timestamps.colored_rects.clear();

sin_graph.x_y_pairs.push_back({x, sin_x});
cos_graph.x_y_pairs.push_back({x, cos_x});
trig_graph.x_vec_pairs.push_back(Eigen::Vector4d(x, sin_x, cos_x, tan_x));

sawtooth.x_y_pairs.push_back({x, sawtooth_x});

messages.clear();

messages.push_back(sin_graph);
messages.push_back(cos_graph);
messages.push_back(trig_graph);
messages.push_back(sawtooth);

x_range0.range = {x - 2.0 * M_PI, x};
Expand All @@ -94,8 +93,7 @@ int main(int argc, char* argv[]) {
plotting->inMessages().send(messages);

x += 0.01;
sin_graph.reset.clear_x_smaller_than = x - 2.0 * M_PI;
cos_graph.reset.clear_x_smaller_than = x - 2.0 * M_PI;
trig_graph.reset.clear_x_smaller_than = x - 2.0 * M_PI;
sawtooth.reset.clear_x_smaller_than = x - 2.0 * M_PI;
timestamps.reset.clear_x_smaller_than = x - 2.0 * M_PI;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
Expand Down
10 changes: 9 additions & 1 deletion farm_ng_core.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
"variant": "cpp",
"__nullptr": "cpp"
},
"workbench.colorTheme": "GitHub Dark High Contrast"
"workbench.colorTheme": "GitHub Dark High Contrast",
"cSpell.words": [
"linalg",
"proto",
"protos"
],
"rust-analyzer.linkedProjects": [
"./rs/plotting/Cargo.toml"
]
}
}
File renamed without changes.
14 changes: 12 additions & 2 deletions rs/remote_plot/Cargo.toml → rs/plotting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "remote_plot"
name = "plotting"
version = "0.1.0"
edition = "2021"

Expand All @@ -9,7 +9,17 @@ clap = { version = "4", features = ["derive"] }
eframe = "0.22"
prost = "0.12"
tokio = { version = "1", features = ["full"] }
tonic = "0.10"
tonic = "0.10.2"
hollywood = "0.2.2"

async-trait = "0.1.51"

[build-dependencies]
tonic-build = "0.10"


[[bin]]
name = "plotting_service"

[[bin]]
name = "plotter_example"
File renamed without changes.
173 changes: 173 additions & 0 deletions rs/plotting/src/actors/grpc_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// use crate::{grpc::farm_ng::core::plotting::proto as grpc_protos, actors::plotter::PlotterMessage};

use std::sync::Arc;

use async_trait::async_trait;

use hollywood::compute::*;
use hollywood::core::actor::ActorNode;
use hollywood::core::actor::DormantActorNode;
use hollywood::core::inbound::ForwardMessage;
use hollywood::core::runner::Runner;
use hollywood::core::*;
use hollywood::macros::*;

use crate::graphs::packets::PlottingPackets;
use crate::grpc::farm_ng::core::plotting::proto::plotting_widget_server::PlottingWidget;
use crate::grpc::farm_ng::core::plotting::proto::plotting_widget_server::PlottingWidgetServer;
use crate::grpc::farm_ng::core::plotting::proto::Messages;
use crate::grpc::farm_ng::core::plotting::proto::PlottingReply;
use crate::grpc::proto::from_proto;

/// Outbound hub
#[actor_outputs]
pub struct GrpcSourceOutbound {
pub plotter_message: OutboundChannel<PlottingPackets>,
}

#[derive(Clone, Debug, Default)]
pub struct GrpcSourceProp {
pub port: u32,
}

impl Value for GrpcSourceProp {}

pub struct GrpcSourceActor {
/// unique identifier of the actor
pub actor_name: String,
}

impl
FromPropState<
GrpcSourceProp,
NullInbound,
NullState,
GrpcSourceOutbound,
NullMessage<GrpcSourceProp, NullState, GrpcSourceOutbound>,
GrpcSourceRunner,
> for GrpcSourceActor
{
fn name_hint(_prop: &GrpcSourceProp) -> String {
"Plotter".to_owned()
}
}

/// The custom runner for the plotter actor.
pub struct GrpcSourceRunner {}

impl
Runner<
GrpcSourceProp,
NullInbound,
NullState,
GrpcSourceOutbound,
NullMessage<GrpcSourceProp, NullState, GrpcSourceOutbound>,
> for GrpcSourceRunner
{
/// Create a new dormant actor.
fn new_dormant_actor(
name: String,
prop: GrpcSourceProp,
_state: NullState,
_receiver: tokio::sync::mpsc::Receiver<
NullMessage<GrpcSourceProp, NullState, GrpcSourceOutbound>,
>,
_forward: std::collections::HashMap<
String,
Box<
dyn ForwardMessage<
GrpcSourceProp,
NullState,
GrpcSourceOutbound,
NullMessage<GrpcSourceProp, NullState, GrpcSourceOutbound>,
> + Send
+ Sync,
>,
>,
outbound: GrpcSourceOutbound,
) -> Box<dyn DormantActorNode + Send + Sync> {
Box::new(DormantGrpcSourceActor {
name: name.clone(),
prop,
outbound,
})
}
}

/// The dormant plotter actor.
pub struct DormantGrpcSourceActor {
name: String,
prop: GrpcSourceProp,
outbound: GrpcSourceOutbound,
}

impl DormantActorNode for DormantGrpcSourceActor {
fn activate(mut self: Box<Self>) -> Box<dyn ActorNode + Send> {
self.outbound.activate();

Box::new(ActiveGrpcSourceNodeImpl {
name: self.name.clone(),
prop: self.prop,
outbound: Arc::new(self.outbound),
})
}
}

pub(crate) struct ActiveGrpcSourceNodeImpl {
pub(crate) name: String,
prop: GrpcSourceProp,
outbound: Arc<GrpcSourceOutbound>,
}

#[async_trait]
impl ActorNode for ActiveGrpcSourceNodeImpl {
fn name(&self) -> &String {
&self.name
}

fn reset(&mut self) {
// no-op
}

async fn run(&mut self, mut _kill: tokio::sync::broadcast::Receiver<()>) {
let address: String = format!("0.0.0.0:{}", self.prop.port);

let server = PlottingService::new(self.outbound.clone());
let server_future = tonic::transport::Server::builder()
.add_service(PlottingWidgetServer::new(server))
.serve(address.parse().unwrap());
match server_future.await {
Ok(_) =>{}
Err(e) =>{panic!("{}", e);}
}
}
}

struct PlottingService {
outbound: Arc<GrpcSourceOutbound>,
}

impl PlottingService {
/// Create a new service with the given sender
pub fn new(outbound: Arc<GrpcSourceOutbound>) -> Self {
PlottingService { outbound }
}
}

#[async_trait]
impl PlottingWidget for PlottingService {
async fn send(
&self,
request: tonic::Request<Messages>,
) -> std::result::Result<tonic::Response<PlottingReply>, tonic::Status> {
let proto_messages: Messages = request.into_inner();

let packets = from_proto(proto_messages);

println!("Got {} messages", packets.len());

self.outbound.as_ref().plotter_message.send(packets);

Ok(tonic::Response::new(PlottingReply {}))
}
}
2 changes: 2 additions & 0 deletions rs/plotting/src/actors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod grpc_source;
pub mod plotter;
Loading

0 comments on commit 89520c0

Please sign in to comment.