use phoxal::api;
use phoxal::prelude::*;
#[derive(serde::Deserialize, phoxal::Config)]
struct Config {}
#[derive(phoxal::Api)]
struct Api {
state: Latest<api::drive::State>,
target: CommandPublisher<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::client().drive().state()).await?,
target: ctx
.command_publisher(api::topic::client().drive().target())
.await?,
},
))
}
#[step(hz = 50)]
async fn step(&mut self, api: &mut Self::Api, _step: StepContext) -> Result<()> {
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.send(target)?;
Ok(())
}
#[shutdown]
async fn shutdown(&mut self, _api: &mut Self::Api) -> Result<()> {
Ok(())
}
}
fn main() -> phoxal::Result<()> {
phoxal::run::<AvoidObstacles>()
}