use phoxal::api;
use phoxal::prelude::*;
struct Api {
state: StateView<api::endpoint::drive::StateEndpoint>,
target: SetpointPublisher<api::endpoint::drive::TargetEndpoint>,
}
#[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::topic::client().drive().state()).await?,
target: ctx.setpoint_publisher(api::topic::client().drive().target())?,
},
))
}
#[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>()
}