找到一个圆的任何电弧的长度 [英] find the length of any arc on a circle

查看:104
本文介绍了找到一个圆的任何电弧的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的(我反正)问题。我工作的OpenServo.org为V4,我试图确定行程和方向的弧的长度。

I have an interesting (to me anyway) problem. I am working on OpenServo.org for V4 and I am trying to determine the length of an arc of travel and its direction.

我有磁性连接codeR返回从0轴的位置,以4095

I have a magnetic encoder that returns the position of the shaft from 0 to 4095.

伺服有两个逻辑终点,称它们为MAX和MIN并将其设置在软件,并且可以在任何时间改变,并且该轴必须旋转(即行程)上的最大和最小位置之间的一个弧。例如,在图像的蓝色电弧是有效的,但红色不是之间的所有旅行和包括MIN和MAX

The servo has two logical end points, call them MAX and MIN which are set in software and can be changed at any time, and the shaft must rotate (i.e. travel) on one arc between the MAX and MIN positions. For example in the picture the blue arc is valid but the red is not for all travel between and including MIN and MAX.

我想只用整数运算,可以告诉我,可以在圆周上任何地方的任何两个点A和B之间的距离,由MIN和MAX,设有一作为当前地方围制定出一个简单的算法且B为目标位置时,或者B是当前位置和A是目标(其被表示为从B到A的负距离)。需要注意的身边,我允许旅行是已知的,它可以是红或蓝。

I am trying to work out a simple algorithm using only integer math that can tell me the distance between any two points A and B that can be anywhere on the circumference, bounded by MIN and MAX and with either A as the current place and B is the target position, or B is the current place and A is the target (which is denoted by a negative distance from B to A). Note the side I allowed to travel is known, it is either "red" or "blue".

问题是当4095/0存在于ARC,然后计算得到一个有趣一点。

The issue is when the 4095/0 exists in the ARC, then the calculations get a bit interesting.

推荐答案

下面是一个简单的算法:

Here's a simple algorithm:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int rotate_diff(int a, int b, bool * clockwise);

int main(void) {
    int degrees_rotated, a, b;
    bool clockwise;

    a = 4040;
    b = 1060;
    degrees_rotated = rotate_diff(a, b, &clockwise);
    printf("A = %d, B = %d, rotation = %d degrees, direction = %s\n",
            a, b, degrees_rotated,
            (clockwise ? "clockwise" : "counter-clockwise"));

    return EXIT_SUCCESS;
}

int rotate_diff(int a, int b, bool * clockwise) {
    static const int min = 2000;

    if ( a <= min ) {
        a += 4096;
    }
    if ( b <= min ) {
        b += 4096;
    }

    int degrees_rotated = b - a;
    if ( degrees_rotated > 0 ) {
        *clockwise = false;
    } else {
        degrees_rotated = -degrees_rotated;
        *clockwise = true;
    }

    return degrees_rotated * 360 / 4096;
}

请注意,这个给你走过的学位,而不是行驶距离,因为你不告诉我们什么轴的尺寸。为了让移动的距离,显然乘以走遍程度地受到360分。如果圆周您的积分0〜4095是某种已知的单位,那么就跳过上述算法转换为度,并相应地改变变量名。

Note that this gives you the degrees traveled, but not the distance traveled, since you don't tell us what dimensions of the shaft are. To get the distance traveled, obviously multiply the circumference by the degrees traveled divided by 360. If your points 0 through 4095 are some kind of known units, then just skip the conversion to degrees in the above algorithm, and change the variable names accordingly.

这篇关于找到一个圆的任何电弧的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆