Skip to content

Commit

Permalink
Restructured the caterpillar example to make it cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Jun 27, 2024
1 parent 118818a commit 8185c70
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 75 deletions.
18 changes: 9 additions & 9 deletions examples/cu_caterpillar/copperconfig.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
tasks: [
(
id: "src",
type: "CaterpillarSource",
type: "tasks::CaterpillarSource",
base_period_ns: 1000000000,
),
(
id: "ct-0",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -21,7 +21,7 @@
),
(
id: "ct-1",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -34,7 +34,7 @@
),
(
id: "ct-2",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -47,7 +47,7 @@
),
(
id: "ct-3",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -60,7 +60,7 @@
),
(
id: "ct-4",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -73,7 +73,7 @@
),
(
id: "ct-5",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -86,7 +86,7 @@
),
(
id: "ct-6",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand All @@ -99,7 +99,7 @@
),
(
id: "ct-7",
type: "CaterpillarTask",
type: "tasks::CaterpillarTask",
base_period_ns: 1000000000,
),
(
Expand Down
75 changes: 9 additions & 66 deletions examples/cu_caterpillar/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,27 @@
use copper::clock::{ClockProvider, OptionCuTime, RobotClock};
use copper::cutask::{CuMsg, CuSrcTask, CuTask, CuTaskLifecycle};
use copper::CuResult;
pub mod tasks;

use copper::clock::ClockProvider;
use copper::clock::CuDuration;
use copper::clock::CuTime;
use copper_derive::copper_runtime;
use copper_helpers::basic_logger_runtime_setup;
use copper_log_derive::debug;
use cu_rp_gpio::RPGpioMsg;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::thread::sleep;
use std::time::Duration;

#[copper_runtime(config = "copperconfig.ron")]
struct TheVeryHungryCaterpillar {}

#[derive(Serialize, Deserialize, Default)]
pub struct CaterpillarMsg(bool);

pub struct CaterpillarSource {
state: bool,
}

impl CuTaskLifecycle for CaterpillarSource {
fn new(_config: Option<&copper::config::NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Ok(Self { state: true })
}
}

impl CuSrcTask for CaterpillarSource {
type Output = RPGpioMsg;

fn process(&mut self, clock: &RobotClock, output: &mut CuMsg<Self::Output>) -> CuResult<()> {
// forward the state to the next task
self.state = !self.state;
output.payload = RPGpioMsg {
on: self.state,
creation: clock.now().into(),
actuation: OptionCuTime::none(),
};
Ok(())
}
}

pub struct CaterpillarTask {}

impl CuTaskLifecycle for CaterpillarTask {
fn new(_config: Option<&copper::config::NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Ok(Self {})
}
}

impl CuTask for CaterpillarTask {
type Input = RPGpioMsg;
type Output = RPGpioMsg;

fn process(
&mut self,
_clock: &RobotClock,
input: &CuMsg<Self::Input>,
output: &mut CuMsg<Self::Output>,
) -> CuResult<()> {
// forward the state to the next task
output.payload = input.payload;
Ok(())
}
}
struct CaterpillarApplication {}

fn main() {
let logger_runtime =
basic_logger_runtime_setup(&PathBuf::from("/tmp/caterpillar.copper"), true)
.expect("Failed to setup logger.");
debug!("Logger created.");
let clock = logger_runtime.get_clock();
debug!("Application created.");
debug!("Creating application... ");
let mut application =
TheVeryHungryCaterpillar::new(clock.clone()).expect("Failed to create runtime.");
CaterpillarApplication::new(clock.clone()).expect("Failed to create runtime.");
debug!("Running... starting clock: {}.", clock.now());
application.run(2).expect("Failed to run application.");
debug!("End of program: {}.", clock.now());
Expand Down
63 changes: 63 additions & 0 deletions examples/cu_caterpillar/src/tasks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use copper::clock::{OptionCuTime, RobotClock};
use copper::cutask::{CuMsg, CuSrcTask, CuTask, CuTaskLifecycle};
use copper::CuResult;
use cu_rp_gpio::RPGpioMsg;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default)]
pub struct CaterpillarMsg(bool);

pub struct CaterpillarSource {
state: bool,
}

impl CuTaskLifecycle for CaterpillarSource {
fn new(_config: Option<&copper::config::NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Ok(Self { state: true })
}
}

impl CuSrcTask for CaterpillarSource {
type Output = RPGpioMsg;

fn process(&mut self, clock: &RobotClock, output: &mut CuMsg<Self::Output>) -> CuResult<()> {
// forward the state to the next task
self.state = !self.state;
output.payload = RPGpioMsg {
on: self.state,
creation: clock.now().into(),
actuation: OptionCuTime::none(),
};
Ok(())
}
}

pub struct CaterpillarTask {}

impl CuTaskLifecycle for CaterpillarTask {
fn new(_config: Option<&copper::config::NodeInstanceConfig>) -> CuResult<Self>
where
Self: Sized,
{
Ok(Self {})
}
}

impl CuTask for CaterpillarTask {
type Input = RPGpioMsg;
type Output = RPGpioMsg;

fn process(
&mut self,
_clock: &RobotClock,
input: &CuMsg<Self::Input>,
output: &mut CuMsg<Self::Output>,
) -> CuResult<()> {
// forward the state to the next task
output.payload = input.payload;
Ok(())
}
}

0 comments on commit 8185c70

Please sign in to comment.