use embassy_sync::{
blocking_mutex::raw::CriticalSectionRawMutex,
watch::{Receiver, Sender, Watch},
};
#[cfg(feature = "autopilot")]
use radio_controllers::RcMode;
use radio_controllers::{Radio, Rates, RatesConfig, RcModes, RxChannel, RxRadio};
use static_cell::StaticCell;
use crate::{
config::{
ConfigItem, ConfigPublisher, ConfigSubscriber, FastConfigPublisher, config_publisher, config_subscriber,
fast_config_publisher,
},
flight::{RcAdjustments, RxMessage},
};
static RX_CTX: StaticCell<RxContext> = StaticCell::new();
const RX_WATCH_COUNT: usize = 3;
static RX_WATCH: Watch<CriticalSectionRawMutex, RxMessage, RX_WATCH_COUNT> = Watch::new();
type RxMessageSender = Sender<'static, CriticalSectionRawMutex, RxMessage, RX_WATCH_COUNT>;
fn rx_message_sender() -> RxMessageSender {
RX_WATCH.sender()
}
pub type RxMessageReceiver = Receiver<'static, CriticalSectionRawMutex, RxMessage, RX_WATCH_COUNT>;
#[allow(clippy::expect_used)]
pub fn rx_message_receiver() -> RxMessageReceiver {
RX_WATCH.receiver().expect("rx_receiver failed")
}
#[cfg(feature = "autopilot")]
use super::autopilot::{AutopilotReceiver, autopilot_receiver};
pub struct RxContext {
pub radio: Radio,
pub rx_message_sender: RxMessageSender,
pub config_subscriber: ConfigSubscriber,
pub config_publisher: ConfigPublisher,
pub fast_config_publisher: FastConfigPublisher,
pub rc_modes: RcModes,
pub rates: Rates,
pub rc_adjustments: RcAdjustments,
#[cfg(feature = "autopilot")]
pub autopilot_receiver: AutopilotReceiver,
}
impl RxContext {
#[rustfmt::skip]
pub fn new(radio: Radio, rates_config: RatesConfig) -> Self {
Self {
radio,
rx_message_sender: rx_message_sender(),
config_subscriber: config_subscriber(),
config_publisher: config_publisher(),
fast_config_publisher: fast_config_publisher(),
rates: Rates::new(rates_config),
rc_modes: RcModes::with_mac_arm(),
rc_adjustments: RcAdjustments::new(),
#[cfg(feature = "autopilot")] autopilot_receiver: autopilot_receiver(),
}
}
}
pub fn init(radio: Radio, rates: RatesConfig) -> &'static mut RxContext {
RX_CTX.init(RxContext::new(radio, rates))
}
#[embassy_executor::task]
pub async fn run(ctx: &'static mut RxContext) {
let mut loop_count: u32 = 0;
let mut ticker = embassy_time::Ticker::every(embassy_time::Duration::from_millis(20));
log::info!(" RX: task started");
loop {
ticker.next().await;
let mut rx_frame = ctx.radio.rx_frame();
rx_frame.channels[RxChannel::AUX1] = match loop_count {
0..100 | 200..400 => RxChannel::MID_HIGH,
_ => RxChannel::LOW,
};
let failsafe = 0;
if let Some(wait_result) = ctx.config_subscriber.try_next_message()
&& let embassy_sync::pubsub::WaitResult::Message(ConfigItem::Rates(rates_config)) = wait_result
{
ctx.rates.set(rates_config);
}
ctx.rc_modes.update_activated_modes(&rx_frame);
ctx.rc_adjustments.process_adjustments(&ctx.config_publisher, &ctx.fast_config_publisher).await;
#[allow(unused_mut)]
let mut rx_message = RxMessage::new_from(&rx_frame, &ctx.rates, &ctx.rc_modes, loop_count, failsafe);
#[cfg(feature = "autopilot")]
if let Some(autopilot_message) = ctx.autopilot_receiver.try_changed() {
if ctx.rc_modes.is_mode_active(RcMode::ALTITUDE_HOLD) {
rx_message.rc_controls.throttle_stick = autopilot_message.rc_controls.throttle_stick;
} else if ctx.rc_modes.is_mode_active(RcMode::POSITION_HOLD)
|| ctx.rc_modes.is_mode_active(RcMode::GPS_RESCUE)
|| ctx.rc_modes.is_mode_active(RcMode::AUTOPILOT)
{
rx_message.rc_controls = autopilot_message.rc_controls;
}
}
ctx.rx_message_sender.send(rx_message);
if loop_count.is_multiple_of(10) {
log::info!(" RX: loop {loop_count}");
}
loop_count = loop_count.wrapping_add(1);
}
}