use crate::{network::NetWork, types::*};
use robot_behavior::RobotResult;
use serde::{Deserialize, Serialize};
#[derive(Default, Clone)]
pub struct RobotImpl<const N: usize> {
network: NetWork,
}
macro_rules! cmd_fn {
($fn_name:ident, $command:expr; $arg_name:ident: $arg_type:ty; $ret_type:ty) => {
pub fn $fn_name(&mut self, arg: $arg_type) -> RobotResult<$ret_type> {
let response: Response<$command, $ret_type> =
self.network
.send_and_recv(Request::<$command, $arg_type>::from(arg))?;
Ok(response.state)
}
};
($fn_name:ident, $command:expr;; $ret_type:ty) => {
pub fn $fn_name(&mut self) -> RobotResult<$ret_type> {
let response: Response<$command, $ret_type> =
self.network
.send_and_recv(Request::<$command, ()>::from(()))?;
Ok(response.state)
}
};
}
impl<const N: usize> RobotImpl<N> {
pub fn new(ip: &str) -> Self {
RobotImpl { network: NetWork::new(ip) }
}
}
impl<const N: usize> RobotImpl<N>
where
[f64; N]: Serialize + for<'a> Deserialize<'a>,
{
cmd_fn!(_power_on, {Command::PowerOn};; PowerOnState);
cmd_fn!(_power_off, {Command::PowerOff};; PowerOffState);
cmd_fn!(_enable, {Command::EnableRobot};; EnableRobotState);
cmd_fn!(_joint_move, {Command::JointMove}; data: JointMoveData::<N>; JointMoveState);
cmd_fn!(_end_move, {Command::EndMove};data: EndMoveData::<N>; EndMoveState);
cmd_fn!(_shutdown, {Command::Shutdown};; ShutdownState);
cmd_fn!(_quit, {Command::Quit};; QuitState);
cmd_fn!(_get_robot_state, {Command::GetRobotState};; GetRobotStateState);
cmd_fn!(_disable, {Command::DisableRobot};; DisableRobotState);
cmd_fn!(_torque_control_enable, {Command::TorqueControlEnable};; TorqueControlEnableState);
cmd_fn!(_torque_feedforward, {Command::TorqueFeedforward}; data: TorqueFeedforwardData; TorqueFeedforwardState);
cmd_fn!(_servo_move, {Command::ServoMove}; data: ServoMoveData; ServoMoveState);
cmd_fn!(_servo_j, {Command::ServoJ}; data: ServoJData::<N>; ServoJState);
cmd_fn!(_servo_p, {Command::ServoP}; data: ServoPData; ServoPState);
cmd_fn!(_get_data, {Command::GetData};; GetDataState);
cmd_fn!(_rapid_rate, {Command::RapidRate}; data: RapidRateData; RapidRateState);
cmd_fn!(_load_program, {Command::LoadProgram}; data: LoadProgramData; LoadProgramState);
cmd_fn!(_get_loaded_program, {Command::GetLoadedProgram};; GetLoadedProgramState);
cmd_fn!(_play_program, {Command::PlayProgram};; PlayProgramState);
cmd_fn!(_pause_program, {Command::PauseProgram};; PauseProgramState);
cmd_fn!(_resume_program, {Command::ResumeProgram};; ResumeProgramState);
cmd_fn!(_stop_program, {Command::StopProgram};; StopProgramState);
cmd_fn!(_get_program_state, {Command::GetProgramState};; GetProgramStateState);
cmd_fn!(_set_digital_output, {Command::SetDigitalOutput}; data: SetDigitalOutputData; SetDigitalOutputState);
cmd_fn!(_get_digital_input_status, {Command::GetDigitalInputStatus};; GetDigitalInputStatusState);
cmd_fn!(_set_analog_output, {Command::SetAnalogOutput}; data: SetAnalogOutputData; SetAnalogOutputState);
cmd_fn!(_set_tool_offsets, {Command::SetToolOffsets}; data: SetToolOffsetsData; SetToolOffsetsState);
cmd_fn!(_set_tool_id, {Command::SetToolId}; data: SetToolIdData; SetToolIdState);
cmd_fn!(_set_user_offsets, {Command::SetUserOffsets}; data: SetUserOffsetsData; SetUserOffsetsState);
cmd_fn!(_set_user_id, {Command::SetUserId}; data: SetUserIdData; SetUserIdState);
cmd_fn!(_get_extio_status, {Command::GetExtioStatus};; GetExtioStatusState);
cmd_fn!(_get_funcdi_status, {Command::GetFuncdiStatus};; GetFuncdiStatusState);
cmd_fn!(_drag_status, {Command::DragStatus};; DragStatusState);
cmd_fn!(_query_user_defined_variable, {Command::QueryUserDefinedVariable};; QueryUserDefinedVariableState);
cmd_fn!(_modify_user_defined_variable, {Command::ModifyUserDefinedVariable}; data: ModifyUserDefinedVariableData; ModifyUserDefinedVariableState);
cmd_fn!(_protective_stop_status, {Command::ProtectiveStopStatus};; ProtectiveStopStatusState);
cmd_fn!(_jog, {Command::Jog}; data: JogData; JogState);
cmd_fn!(_move_l, {Command::MoveL}; data: MoveLData; MoveLState);
cmd_fn!(_wait_complete, {Command::WaitComplete};; WaitCompleteState);
cmd_fn!(_set_payload, {Command::SetPayload}; data: SetPayloadData; SetPayloadState);
cmd_fn!(_get_payload, {Command::GetPayload};; GetPayloadState);
cmd_fn!(_set_clsn_sensitivity, {Command::SetClsnSensitivity}; data: SetClsnSensitivityData; SetClsnSensitivityState);
cmd_fn!(_get_clsn_sensitivity, {Command::GetClsnSensitivity};; GetClsnSensitivityState);
cmd_fn!(_kine_forward, {Command::KineForward}; data: KineForwardData; KineForwardState);
cmd_fn!(_kine_inverse, {Command::KineInverse}; data: KineInverseData; KineInverseState);
cmd_fn!(_clear_error, {Command::ClearError};; ClearErrorState);
cmd_fn!(_get_joint_pos, {Command::GetJointPos};; GetJointPosState);
cmd_fn!(_get_tcp_pos, {Command::GetTcpPos};; GetTcpPosState);
cmd_fn!(_set_tio_vout_param, {Command::SetTioVoutParam}; data: SetTioVoutParamData; SetTioVoutParamState);
cmd_fn!(_get_tio_vout_param, {Command::GetTioVoutParam};; GetTioVoutParamState);
}