use phoxal::api;
use phoxal::prelude::*;
#[derive(serde::Deserialize, phoxal::Config)]
struct Config {}
#[derive(phoxal::Api)]
struct Api {
state: Latest<api::drive::State>,
target: Publisher<api::drive::Target>,
}
#[phoxal::service(id = "avoid-obstacles")]
struct AvoidObstacles;
#[phoxal::behavior]
impl AvoidObstacles {
#[setup]
async fn setup(ctx: &mut SetupContext<Self>) -> Result<(Self, Self::Api)> {
Ok((
Self,
Self::Api {
state: ctx.latest(api::topic::new().drive().state()).await?,
target: ctx.publisher(api::topic::new().drive().target()).await?,
},
))
}
#[step(hz = 50)]
async fn step(&mut self, api: &mut Self::Api, step: StepContext) -> Result<()> {
let now = step.time();
let target = match api.state.latest() {
Some(_) => api::drive::Target {
linear_x_mps: 0.2,
angular_z_radps: 0.0,
curvature_limit_radpm: None,
},
None => api::drive::Target {
linear_x_mps: 0.0,
angular_z_radps: 0.0,
curvature_limit_radpm: None,
},
};
api.target.publish_at(now, target).await?;
Ok(())
}
#[shutdown]
async fn shutdown(&mut self, _api: &mut Self::Api) -> Result<()> {
Ok(())
}
}
fn main() -> phoxal::Result<()> {
phoxal::run::<AvoidObstacles>()
}