/*
该文件cmd用于对各种命令的中转处理，以防止在remote遥控器任务中过于冗杂堆叠
*/

/* Includes ----------------------------------------------------------------- */
#include "cmd.h"
#include "string.h"
#include "device/device.h"

/* Private function  -------------------------------------------------------- */
/*Export function --------------------------------------------------------------*/

void CMD_Init(Chassis_CMD_t *cmd, Assemble_CMD_t *g_cmd)
{

    cmd->cmd_power_on_safe = 0;
    cmd->mode = STOP;
    g_cmd->mode = Assemble_MODE_RELAX;
}

static void CMD_remote(const CMD_RC_t *rc, Chassis_CMD_t *c_cmd, Assemble_CMD_t *a_cmd)
{

    if (rc->rc_type == RC_DR16)
    {

        c_cmd->Vx = rc->DJ.ch_r_x;
        c_cmd->Vy = rc->DJ.ch_r_y;
        c_cmd->Vw = rc->DJ.ch_l_x;
        // c_   cmd->throttle = rc->DJ.ch_l_x;
       
    }
    if (rc->rc_type == RC_ET16s)
    {

        c_cmd->Vx = rc->ET16s.ch_l_y;
        c_cmd->Vy = rc->ET16s.ch_l_x;
        c_cmd->Vw = rc->ET16s.ch_r_x;

        c_cmd->x_l = rc->ET16s.ch_l_x;
        c_cmd->y_l = rc->ET16s.ch_l_y;

  
    }
        if (rc->rc_type == RC_AT9S)
    {

        c_cmd->Vx = rc->LD.ch_r_y;
        c_cmd->Vy = rc->LD.ch_r_x;
        c_cmd->Vw = rc->LD.ch_l_x;

        c_cmd->x_l = rc->LD.ch_r_x;
        c_cmd->y_l = rc->LD.ch_r_y;

  
    }

}
/**
 * @brief rc失控时机器人恢复放松模式
 *
 * @param cmd 主结构体
 */
static void CMD_RcLostLogic(Chassis_CMD_t *cmd,Assemble_CMD_t *a_cmd)
{
    /* 机器人底盘运行模式恢复至放松模式 */
    cmd->mode = STOP;
    
}

void CMD_ParseRc( CMD_RC_t *rc,Chassis_CMD_t *c_cmd, Assemble_CMD_t *a_cmd)
{

    switch (rc->rc_type)
    {
    case RC_DR16:
        /*c当rc丢控时，恢复机器人至默认状态 */
        if ((rc->DJ.sw_l == CMD_SW_ERR) || (rc->DJ.sw_r == CMD_SW_ERR))
        {
            CMD_RcLostLogic(c_cmd,a_cmd);
            c_cmd->cmd_power_on_safe = 0;
        }
        else
        {
            CMD_remote(rc, c_cmd, a_cmd);
        }
        break;
    case RC_AT9S:
        if (rc->LD.key_C == 0)
        {
            CMD_RcLostLogic(c_cmd,a_cmd);
        }
        else
        {
            CMD_remote(rc, c_cmd, a_cmd);
        }
        break;
        
    case RC_ET16s:
        if (rc->ET16s.key_D == 0)
        {
            CMD_RcLostLogic(c_cmd,a_cmd);
        }
        else
        {
            CMD_remote(rc, c_cmd, a_cmd);
        }
        break;

    case Control_loss:
        CMD_RcLostLogic(c_cmd,a_cmd);
        break;
    }
}
int8_t CMD_CtrlSet(Chassis_CMD_t *c_cmd, const CMD_RC_t *rc, Assemble_CMD_t *a_cmd)
{

    // 大疆
    if (rc->rc_type == RC_DR16)
    {


    }
    // 乐迪
    else if(rc->rc_type == RC_AT9S)
    {
        switch(rc->LD.key_C)
        {
            case CMD_SW_UP:
            c_cmd->mode = RC;
             break;
            break;
            case CMD_SW_MID:
            c_cmd->mode = RC;
             break;
            break;
            case CMD_SW_DOWN:
            c_cmd->mode = STOP;
             break;
            break;
            default:
            break;
        }

    }
    // 天地飞
    else if (rc->rc_type == RC_ET16s)
    {

        switch (rc->ET16s.key_A)
        {
            case CMD_SW_UP:
            a_cmd->setpoint.motor_4310_setpoint[0] = 1.3f;
            a_cmd->setpoint.motor_4310_setpoint[1] = 3.8f;
            a_cmd->setpoint.motor_4310_setpoint[2] = 5.3f;
            break;
            case CMD_SW_MID:
            a_cmd->setpoint.motor_4310_setpoint[0] = 1.3f;
            a_cmd->setpoint.motor_4310_setpoint[1] = 4.5f;
            a_cmd->setpoint.motor_4310_setpoint[2] = 0.5f;
            break;
            case CMD_SW_DOWN:
            a_cmd->setpoint.motor_4310_setpoint[0] = 0.0f;
            a_cmd->setpoint.motor_4310_setpoint[1] = 0.0f;
            a_cmd->setpoint.motor_4310_setpoint[2] = 0.0f;
            break;
            default:
            break;
        }

        switch(rc->ET16s.key_E)
        {
            case CMD_SW_UP:
            c_cmd->mode = STOP;
             break;
            break;
            case CMD_SW_MID:
            c_cmd->mode = RC;
             break;
            break;
            case CMD_SW_DOWN:
            c_cmd->mode = RC;
             break;
            break;
            default:
            break;
        }

    }
    
  return 0;
}