获取具有特定距离的键值对数组中的第二个最大数字

时间:2018-05-08 23:53:19

标签: c++ c arduino pseudocode

  • 我正在研究一个在颈部上映射高度点的项目。

  • 使用Arduino和霍尔传感器,我在不同的位置绘制阻力点。

  • 我在HashMap中映射我的点(点[stepper_motor_location] [抵抗])
  • 我需要找到中间点(Cricothyriod韧带)
  • 我知道如何通过从所有位置获得最大阻力来获得上部点(甲状软骨)。
  • 但我不知道如何获得最低点(Cricoid Cartilage),这是第二高峰,但距离第一个最高峰有相当大的距离。

所以,问题是:

你如何获得第2个最高阻力点[位置] [阻力],与最高阻力位置相差至少2-10?

C,C ++,Arduino或伪代码,我真诚地不介意。

这是我的代码:

/* ###### VARIABLE ######## */
 /*  */
found bool = false; /* while our point is not found, FOUND IS FALSE */
resistance int []; /* resistance vector */
location int []; /* Motor location vector */
current_location int = 0; /* 0 which means the top part of the motor container */
current_resistance int; /* the resistance which the Hall Sensor registers at that moment */
points HashMap; /* HashMap with location as key and resistance at its location as value */
min_distance int = 50; /* value for the minimum distante between the highest point and the second highest point (Variable to be modified as we test more) */

/* ######## FUNCTIONS ######### */
 /*  */
move_check()
{
    location.push(current_location);
    current_resistance = GET_CURRENT_RESISTANCE;
    resistance.push(current_resistance);
    MOVE_MOTOR++;
    location++;
}

map_points()
{
    for(;location <= MAX_DISTANCE;) /* WE SET THE MAX_DISTANCE TO WHAT WE MEASURED BEFORE AS MAX DISTANCE */
    {
        move_check();
        RED_LED = 1;
        RED_LED = 0; /* TO SEE THAT IS MAPPING POINTS */
        }
    }
    CreateHashMap (points, location, resistance, MAX_DISTANCE); /* (VARIABLE_NAME , int, int, NUMBER_OF_ITEMS) */
}

get_top() /* gets the thryoid Cartilage location */
{
    maximum int = 0;
    max_location int = 0;
    for(int i = 0; i<= location.length(); i++)
    {
        if(maximum > max(points[i], maximum))
        {
            maximum = max(points[i], maximum);
        }
        else
        {
            maximum = max(points[i], maximum);
            max_location = i;
        }
    }
    return max_location;
}

get_bottom() /* gets the cricoid cartilage location*/
{
    bottom_point int;
    ??????????????????????????????
    return bottom_point;
}

goto_middle_point()
{
    RED_LED = 1;
    while(location != floor((get_top() + get_bottom()) / 2))
    {
        location--;
    }
    RED_LED = 0;
    BEEP_SPEAKER_LONG;
}

wait_button_press()
{
    resistance_now int = GET_CURRENT_RESISTANCE;
    while(resistance_now != GET_CURRENT_RESISTANCE)
    {
        RED_LED = 1;
        RED_LED = 0;
    }
    SLEEP(1000);
    found = true;
}

done(bool found)
{
    if(found)
    {
        while(location)/*  if location is 0 == false (top location) */
        {
            location--;
            MOVE_MOTOR--;
        }
    }
    BEEP_SPEAKER;
    BEEP_SPEAKER; /* beep speaker twice for done */
    RED_LED = 0; /* stops working */
    GREEN_LED = 1; /* signal for ready */
}

int main()
{
    map_points();
    goto_middle_point();
    wait_button_press();
    done();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我不知道您正在使用哪种Arduino HashMap实现,但如果它this one,那么这是我能提出的最佳API。它肯定不是最优的,更好的数据结构会更快。

假设我正确理解了问题,我会做一个线性搜索。你知道如何获得最高分。您知道第二高点的位置必须低于最高点。 (我在这里添加,你可能需要减去你的坐标系是否不同)。假设位置的最小差异必须是min_dist(某些值为2-10)。

HashMap<int, int, N> points = ...;
int key_low = upper_position + min_dist;
int peak_location = -1;
for(int i=0; i < points.size(); ++i)
{ 
    int location = points.keyAt(i);
    if (location < key_low) continue;
    int resistance = points[location];
    if (peak_location < 0 || resistance > points[peak_location])
    {
        peak_location = location; 
    }
}