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