2023-02-10 14:00:14 +01:00
|
|
|
use crate::action::ActionRequest;
|
2023-04-26 17:17:35 +02:00
|
|
|
use crate::can_ids::{DeviceId, MessageId, MessageModel};
|
2023-02-10 14:00:14 +01:00
|
|
|
use crate::power_handler::{DeviceState, PowerSwitcher};
|
|
|
|
use crate::requests::{Request, RequestWithToken};
|
2023-04-26 17:17:35 +02:00
|
|
|
use eurosim_obsw::{RequestTargetId};
|
2023-03-09 09:54:21 +01:00
|
|
|
use log::debug;
|
2023-02-15 09:56:14 +01:00
|
|
|
use satrs_core::power::{PowerSwitchInfo, PowerSwitcherCommandSender, SwitchId};
|
2023-04-26 17:17:35 +02:00
|
|
|
use satrs_core::pus::verification::{VerificationReporterWithSender};
|
2023-03-09 09:54:21 +01:00
|
|
|
use satrs_core::pus::MpscPusInStoreSendError;
|
2023-02-09 15:38:32 +01:00
|
|
|
use satrs_core::spacepackets::time::cds::TimeProvider;
|
|
|
|
use satrs_core::spacepackets::time::TimeWriter;
|
2023-02-10 14:00:14 +01:00
|
|
|
use std::sync::mpsc;
|
2023-02-15 09:56:14 +01:00
|
|
|
use std::sync::mpsc::{Receiver, Sender};
|
2023-03-09 09:54:21 +01:00
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2023-02-09 15:38:32 +01:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
pub enum CameraMode {
|
|
|
|
Idle,
|
|
|
|
PictureRequest,
|
|
|
|
Verification,
|
|
|
|
Start,
|
|
|
|
End,
|
2023-03-09 09:54:21 +01:00
|
|
|
Broken,
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CameraHandler {
|
|
|
|
power_switcher: PowerSwitcher,
|
|
|
|
camera_device_id: DeviceId,
|
|
|
|
camera_switch_id: SwitchId,
|
|
|
|
device_state: DeviceState,
|
2023-04-26 17:17:35 +02:00
|
|
|
can_tx: Sender<MessageModel>,
|
|
|
|
can_rx: Receiver<MessageModel>,
|
2023-02-09 15:38:32 +01:00
|
|
|
mode: CameraMode,
|
|
|
|
mode_rx: Receiver<CameraMode>,
|
2023-02-10 14:00:14 +01:00
|
|
|
action_rx: Receiver<RequestWithToken>,
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CameraHandler {
|
2023-02-10 14:00:14 +01:00
|
|
|
pub fn new(
|
|
|
|
power_switcher: PowerSwitcher,
|
|
|
|
camera_device_id: DeviceId,
|
2023-04-26 17:17:35 +02:00
|
|
|
can_tx: Sender<MessageModel>,
|
|
|
|
can_rx: Receiver<MessageModel>,
|
2023-02-10 14:00:14 +01:00
|
|
|
mode_rx: Receiver<CameraMode>,
|
|
|
|
action_rx: Receiver<RequestWithToken>,
|
|
|
|
) -> CameraHandler {
|
2023-02-09 15:38:32 +01:00
|
|
|
let camera_switch_id = camera_device_id as u16;
|
2023-02-10 14:00:14 +01:00
|
|
|
CameraHandler {
|
|
|
|
power_switcher,
|
|
|
|
camera_device_id,
|
|
|
|
camera_switch_id,
|
|
|
|
device_state: DeviceState::Off,
|
|
|
|
can_tx,
|
|
|
|
can_rx,
|
|
|
|
mode: CameraMode::Idle,
|
|
|
|
mode_rx,
|
|
|
|
action_rx,
|
|
|
|
}
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_mode(&mut self, mode: CameraMode) {
|
2023-02-10 14:00:14 +01:00
|
|
|
if self.mode == CameraMode::Idle {
|
2023-02-09 15:38:32 +01:00
|
|
|
if mode == CameraMode::PictureRequest {
|
2023-02-10 14:00:14 +01:00
|
|
|
self.mode = CameraMode::PictureRequest;
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mode(&self) -> CameraMode {
|
|
|
|
self.mode
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:00:14 +01:00
|
|
|
pub fn handle_mode_requests(&mut self) {
|
|
|
|
match self.mode_rx.try_recv() {
|
|
|
|
Ok(mode) => {
|
|
|
|
self.set_mode(mode);
|
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*pub fn handle_action_requests(&mut self) {
|
|
|
|
match self.action_rx.try_recv() {
|
|
|
|
Ok(request_with_token) => {
|
|
|
|
match request_with_token.0 {
|
|
|
|
Request::ActionRequest(action_id) => {
|
|
|
|
match action_id {
|
|
|
|
ActionRequest::ImageRequest(target_id) => {
|
|
|
|
assert_eq!(target_id, RequestTargetId::PldSubsystem);
|
|
|
|
camera_handler.set_mode(CameraMode::PictureRequest);
|
|
|
|
|
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
|
|
|
// send start verification and get token
|
|
|
|
let start_token = reporter_pld
|
|
|
|
.start_success(request_with_token.1, Some(&time_stamp_buf))
|
|
|
|
.expect("Error sending start success.");
|
|
|
|
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("{:?}", camera_handler.get_mode());
|
2023-02-10 14:00:14 +01:00
|
|
|
while camera_handler.get_mode() != CameraMode::Idle {
|
|
|
|
camera_handler.periodic_op();
|
|
|
|
}
|
|
|
|
|
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
|
|
|
// send end verification with token
|
|
|
|
reporter_pld
|
|
|
|
.completion_success(
|
|
|
|
start_token,
|
|
|
|
Some(&time_stamp_buf),
|
|
|
|
)
|
|
|
|
.expect("Error sending start success.");
|
|
|
|
}
|
|
|
|
ActionRequest::OrientationRequest(_) => {}
|
|
|
|
ActionRequest::PointingRequest(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn take_picture(&mut self) {}
|
|
|
|
|
2023-02-09 15:38:32 +01:00
|
|
|
pub fn periodic_op(&mut self) {
|
|
|
|
// Camera Device Handler State Machine
|
|
|
|
|
|
|
|
match self.mode {
|
2023-03-09 09:54:21 +01:00
|
|
|
CameraMode::Broken => {}
|
2023-02-09 15:38:32 +01:00
|
|
|
CameraMode::Idle => {}
|
|
|
|
CameraMode::PictureRequest => {
|
|
|
|
if self.device_state == DeviceState::Off {
|
2023-02-10 14:00:14 +01:00
|
|
|
self.power_switcher
|
|
|
|
.send_switch_on_cmd(self.camera_switch_id)
|
|
|
|
.expect("sending switch cmd failed");
|
2023-02-09 15:38:32 +01:00
|
|
|
self.device_state = DeviceState::SwitchingPower;
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("switching power");
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
if self.device_state == DeviceState::SwitchingPower {
|
2023-03-09 09:54:21 +01:00
|
|
|
let allowed_cycles = 10;
|
|
|
|
for i in 0..allowed_cycles {
|
|
|
|
if self
|
|
|
|
.power_switcher
|
|
|
|
.get_is_switch_on(self.camera_switch_id)
|
|
|
|
.expect("reading switch state failed")
|
|
|
|
{
|
|
|
|
self.device_state = DeviceState::On;
|
|
|
|
debug!("device on");
|
|
|
|
}
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
2023-03-09 09:54:21 +01:00
|
|
|
self.mode = CameraMode::Broken;
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
if self.device_state == DeviceState::On {
|
2023-03-09 09:54:21 +01:00
|
|
|
self.can_tx
|
2023-04-26 17:17:35 +02:00
|
|
|
.send(MessageModel::new(MessageId::CameraImageRequest, &[1]).unwrap())
|
2023-03-09 09:54:21 +01:00
|
|
|
.unwrap();
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("sent camera request");
|
2023-02-09 15:38:32 +01:00
|
|
|
self.mode = CameraMode::Verification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CameraMode::Verification => {
|
|
|
|
if self.device_state == DeviceState::On {
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("waiting for image request confirmation");
|
2023-02-09 15:38:32 +01:00
|
|
|
if let Ok(msg) = self.can_rx.recv() {
|
2023-04-26 17:17:35 +02:00
|
|
|
if msg.message_id() == MessageId::CameraImageRequestConfirmation {
|
2023-02-09 15:38:32 +01:00
|
|
|
self.mode = CameraMode::Start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CameraMode::Start => {
|
|
|
|
if self.device_state == DeviceState::On {
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("waiting for image start confirmation");
|
2023-02-09 15:38:32 +01:00
|
|
|
if let Ok(msg) = self.can_rx.recv() {
|
2023-04-26 17:17:35 +02:00
|
|
|
if msg.message_id() == MessageId::CameraImageExecutionStart {
|
2023-02-09 15:38:32 +01:00
|
|
|
self.mode = CameraMode::End;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CameraMode::End => {
|
|
|
|
if self.device_state == DeviceState::On {
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("waiting for image end confirmation");
|
2023-02-09 15:38:32 +01:00
|
|
|
if let Ok(msg) = self.can_rx.recv() {
|
2023-04-26 17:17:35 +02:00
|
|
|
if msg.message_id() == MessageId::CameraImageExectutionEnd {
|
2023-02-10 14:00:14 +01:00
|
|
|
self.power_switcher
|
|
|
|
.send_switch_off_cmd(self.camera_switch_id)
|
|
|
|
.expect("sending switch command failed");
|
2023-02-09 15:38:32 +01:00
|
|
|
self.device_state = DeviceState::SwitchingPower;
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("switching power");
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.device_state == DeviceState::SwitchingPower {
|
2023-02-10 14:00:14 +01:00
|
|
|
if !self
|
|
|
|
.power_switcher
|
|
|
|
.get_is_switch_on(self.camera_switch_id)
|
|
|
|
.expect("reading switch state failed")
|
|
|
|
{
|
2023-02-09 15:38:32 +01:00
|
|
|
self.device_state = DeviceState::Off;
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("device off");
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.device_state == DeviceState::Off {
|
|
|
|
self.mode = CameraMode::Idle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:00:14 +01:00
|
|
|
pub fn core_pld_task(
|
|
|
|
power_switcher: PowerSwitcher,
|
|
|
|
pld_thread_rx: Receiver<RequestWithToken>,
|
2023-04-26 17:17:35 +02:00
|
|
|
pld_can_rx: Receiver<MessageModel>,
|
|
|
|
pld_can_tx: Sender<MessageModel>,
|
2023-03-09 09:54:21 +01:00
|
|
|
reporter_pld: &mut VerificationReporterWithSender<MpscPusInStoreSendError>,
|
2023-02-10 14:00:14 +01:00
|
|
|
) {
|
2023-02-15 09:56:14 +01:00
|
|
|
let (_camera_mode_tx, camera_mode_rx) = mpsc::channel();
|
|
|
|
let (_action_tx, action_rx) = mpsc::channel();
|
2023-02-09 15:38:32 +01:00
|
|
|
|
2023-02-10 14:00:14 +01:00
|
|
|
let mut camera_handler = CameraHandler::new(
|
|
|
|
power_switcher,
|
|
|
|
DeviceId::Camera,
|
2023-02-15 09:56:14 +01:00
|
|
|
pld_can_tx,
|
2023-02-10 14:00:14 +01:00
|
|
|
pld_can_rx,
|
|
|
|
camera_mode_rx,
|
|
|
|
action_rx,
|
|
|
|
);
|
2023-02-09 15:38:32 +01:00
|
|
|
|
|
|
|
let mut time_stamp_buf: [u8; 7] = [0; 7];
|
2023-04-26 17:17:35 +02:00
|
|
|
loop {
|
2023-02-09 15:38:32 +01:00
|
|
|
match pld_thread_rx.try_recv() {
|
|
|
|
Ok(request_with_token) => {
|
|
|
|
match request_with_token.0 {
|
|
|
|
Request::ActionRequest(action_id) => {
|
|
|
|
match action_id {
|
|
|
|
ActionRequest::ImageRequest(target_id) => {
|
|
|
|
assert_eq!(target_id, RequestTargetId::PldSubsystem);
|
|
|
|
camera_handler.set_mode(CameraMode::PictureRequest);
|
|
|
|
|
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
|
|
|
// send start verification and get token
|
|
|
|
let start_token = reporter_pld
|
2023-03-09 09:54:21 +01:00
|
|
|
.start_success(
|
|
|
|
request_with_token.1.unwrap(),
|
|
|
|
Some(&time_stamp_buf),
|
|
|
|
)
|
2023-02-09 15:38:32 +01:00
|
|
|
.expect("Error sending start success.");
|
|
|
|
|
2023-02-15 14:57:43 +01:00
|
|
|
debug!("{:?}", camera_handler.get_mode());
|
2023-02-09 15:38:32 +01:00
|
|
|
while camera_handler.get_mode() != CameraMode::Idle {
|
|
|
|
camera_handler.periodic_op();
|
2023-03-17 11:09:17 +01:00
|
|
|
debug!("{:?}", camera_handler.get_mode());
|
|
|
|
sleep(Duration::from_millis(1000));
|
2023-02-09 15:38:32 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 14:00:14 +01:00
|
|
|
let cds_stamp = TimeProvider::from_now_with_u16_days().unwrap();
|
|
|
|
cds_stamp.write_to_bytes(&mut time_stamp_buf).unwrap();
|
2023-02-09 15:38:32 +01:00
|
|
|
// send end verification with token
|
|
|
|
reporter_pld
|
2023-02-10 14:00:14 +01:00
|
|
|
.completion_success(start_token, Some(&time_stamp_buf))
|
2023-02-09 15:38:32 +01:00
|
|
|
.expect("Error sending start success.");
|
|
|
|
}
|
|
|
|
ActionRequest::OrientationRequest(_) => {}
|
|
|
|
ActionRequest::PointingRequest(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 14:00:14 +01:00
|
|
|
}
|