use phoxal::api;
use phoxal::prelude::*;
struct Api {
state: StateView<api::drive::State>,
target: SetpointPublisher<api::drive::Target>,
}
#[phoxal::service(id = "avoid-obstacles", api = Api)]
struct AvoidObstacles;
impl Participant for AvoidObstacles {
async fn setup(
&self,
ctx: &mut SetupContext<Self>,
_config: Self::Config,
) -> Result<(Self::State, Self::Api)> {
Ok((
(),
Api {
state: ctx
.state_view(api::topics().drive().state().client())
.await?,
target: ctx.setpoint_publisher(api::topics().drive().target().client())?,
},
))
}
#[phoxal::step(hz = 50)]
fn step(&self, api: &Self::Api, _step: StepContext, _state: &mut Self::State) -> Result<()> {
let target = match api.state.latest() {
Some(_) => api::drive::Target::try_new(0.2, 0.0)?,
None => api::drive::Target::stopped(),
};
api.target.send(target)?;
Ok(())
}
}
fn main() -> phoxal::Result<()> {
phoxal::run::<AvoidObstacles>()
}