C++中奇怪的功能输入

Strange input of function in C++

本文关键字:功能 输入 C++      更新时间:2023-10-16

(1L<<16u)在以下代码中做了什么?以及此pointer(&mode_absolute_pos_el.tracking)作为函数输入的作用是什么?

if (command_axis2.cif_mode_state == STATUS__DONE)
{
    delta_position = absolute_position - turret_pos.elevation_16bit_pm180deg;
    delta_position = MathRange_PlusMinusHalfRange32Bit(delta_position, 1L<<16u);
    mode_absolute_pos_el.speed_setpoint = MathTracking_Main(&mode_absolute_pos_el.tracking, delta_position, 0L);
}
这是完整函数

所要求的完整函数:

static int16_t TwinX_AbsPos_Calc_El(int16_t absolute_position)
{
    uint16_t position_reached = NO;
    int32_t delta_position = 0L;
    if (command_axis2.cif_mode_state == STATUS__DONE)
        {
        delta_position = absolute_position - turret_pos.elevation_16bit_pm180deg;
        delta_position = MathRange_PlusMinusHalfRange32Bit(delta_position, 1L<<16u);
        mode_absolute_pos_el.speed_setpoint = MathTracking_Main(&mode_absolute_pos_el.tracking, delta_position, 0L);
        /* verify that absolute position is reached with tolerance: +/- CUSTOMER_ABSOLUTE_POS_ERROR
        * for more than MODE_ABSOLUTE_POS_OK_DELAY_IN_MS:
        * bai: That has to be here because otherwise position_reached is always "Yes" because delta_position == 0L */
        if ((delta_position < TwinX_MODE_ABSOLUTE_POS_ERROR) && (delta_position > (-1 * TwinX_MODE_ABSOLUTE_POS_ERROR)))
        {
            position_reached = YES;
        }
        mode_absolute_pos_el.absolute_pos_reached = MathDebounce_Status(&mode_absolute_pos_el.debounce, position_reached);
    }
    else
    {
        mode_absolute_pos_el.speed_setpoint = 0;
        MathTracking_SetStartCondition(&mode_absolute_pos_el.tracking, turret_speed.elev_speed_max16bit);
    }
    return mode_absolute_pos_el.speed_setpoint;
}

下面你可以看到MATH_DEBOUNCE:

typedef struct
{
    bool_t debounced_status;       /* debounced status ZERO / ONE */
    uint32_t debounce_counter;     /* counter */
    uint32_t threshold_for_zero;   /* threshold to set debounced status to ZERO */
    uint32_t threshold_for_one;    /* threshold to set debounced status to ONE */
    uint32_t step_down_size;       /* step size to count down. used for                             underclock */
}MATH_DEBOUNCE_t;
void MathDebounce_Init(MATH_DEBOUNCE_t *const debounce_p,
                   bool_t initial_status,
                   uint16_t debounce_delay,
                   uint16_t underclock);
void MathDebounce_ResetStatus(MATH_DEBOUNCE_t *const debounce_p, bool_t     reset_status);
bool_t MathDebounce_Status(MATH_DEBOUNCE_t *const debounce_p, bool_t  status);

1 << 16 是 2^16。 <<是一个左二进制移位运算符。

让我们看看如何得到 2^3,想象一下你有 1,它以二进制形式看起来像这样0b0001。关于二进制系统的一点是,2 的幂在其二进制表示中只有一个位,所以2^3 = 8看起来像0b1000。这是获得 2 次方的非常快速的方法。

将指针传递给函数通常是从中获取多个输出的一种方法。就像当返回值类似于错误代码时,函数的实际输出是通过您传递给它的指针传递的。

但是很难说它到底做了什么,因为你没有提供函数的代码。