ios - (iOS) 加速度计图(将重力转换为 +/- 128)粒度

标签 ios objective-c graph accelerometer

我正在使用这个 Accelerometer graph来自 Apple 并尝试转换他们的 G-force 代码以计算 +/- 128

下图显示标签中的 xyz 值与图表上的输出不匹配:(请注意,addX:y:z 值显示在图表上方的标签中)

enter image description here

View Controller

x、y、z 值是从蓝牙外设接收的,然后使用以下方法转换:

// Updates LABELS
- (void)didReceiveRawAcceleromaterDataWithX:(NSInteger)x Y:(NSInteger)y Z:(NSInteger)z
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _labelAccel.text = [NSString stringWithFormat:@"x:%li  y:%li  z:%li", (long)x, (long)y, (long)z];
    });
}

// Updates GRAPHS
- (void)didReceiveAcceleromaterDataWithX:(NSInteger)x Y:(NSInteger)y Z:(NSInteger)z
{
    dispatch_async(dispatch_get_main_queue(), ^{

        float xx = ((float)x) / 8192;
        float yy = ((float)y) / 8192;
        float zz = ((float)z) / 8192;

        [_xGraph addX:xx y:0 z:0];
        [_yGraph addX:0 y:yy z:0];
        [_zGraph addX:0 y:0 z:zz];

    });
}

图形 View

- (BOOL)addX:(UIAccelerationValue)x y:(UIAccelerationValue)y z:(UIAccelerationValue)z
{
    // If this segment is not full, then we add a new acceleration value to the history.
    if (index > 0)
    {
        // First decrement, both to get to a zero-based index and to flag one fewer position left
        --index;
        xhistory[index] = x;
        yhistory[index] = y;
        zhistory[index] = z;
        // And inform Core Animation to redraw the layer.
        [layer setNeedsDisplay];
    }
    // And return if we are now full or not (really just avoids needing to call isFull after adding a value).
    return index == 0;
}

- (void)drawLayer:(CALayer*)l inContext:(CGContextRef)context
{
    // Fill in the background
    CGContextSetFillColorWithColor(context, kUIColorLightGray(1.f).CGColor);
    CGContextFillRect(context, layer.bounds);

    // Draw the grid lines
    DrawGridlines(context, 0.0, 32.0);

    // Draw the graph
    CGPoint lines[64];
    int i;

    float _granularity = 16.f; // 16
    NSInteger _granualCount = 32; // 32

    // X
    for (i = 0; i < _granualCount; ++i)
    {
        lines[i*2].x = i;
        lines[i*2+1].x = i + 1;

        lines[i*2].y = xhistory[i] * _granularity;
        lines[i*2+1].y = xhistory[i+1] * _granularity;
    }
    CGContextSetStrokeColorWithColor(context, _xColor.CGColor);
    CGContextStrokeLineSegments(context, lines, 64);

    // Y
    for (i = 0; i < _granualCount; ++i)
    {
        lines[i*2].y = yhistory[i] * _granularity;
        lines[i*2+1].y = yhistory[i+1] * _granularity;
    }
    CGContextSetStrokeColorWithColor(context, _yColor.CGColor);
    CGContextStrokeLineSegments(context, lines, 64);

    // Z
    for (i = 0; i < _granualCount; ++i)
    {
        lines[i*2].y = zhistory[i] * _granularity;
        lines[i*2+1].y = zhistory[i+1] * _granularity;
    }
    CGContextSetStrokeColorWithColor(context, _zColor.CGColor);
    CGContextStrokeLineSegments(context, lines, 64);
}

如何计算上述代码以在图表上精确显示正确的加速度计值?

最佳答案

我将此作为回答而不是评论发布,因为我没有足够的声誉,但我写的内容可能足以让您朝着正确的方向前进,甚至可以算作答案...

您的问题仍然没有包含真正重要的内容。我假设 xx/yy/zz 的计算没有问题。虽然我不知道 8192 应该是什么意思。

我想问题出在将值映射到像素坐标的部分...
lines[] 包含您的值,范围是标签中值的 1/8192。所以你的 -2 的 x 值应该在 -0.0000something 的像素位置,所以略微(远小于 1 像素)在 View 上方......因为你看到这条线更远,所以必须有一些翻译(未显示在您的代码中)

第二个重要但未显示的部分是 DrawGridlines。可能有一种不同的方法将值映射到像素坐标...

使用调试器检查绘制 +127 线时得到的像素坐标以及在历史数组中插入 +127 的值时得到的像素坐标

以及阅读代码时的一些改进想法:
1.) 将图形放在它自己的类中,绘制一个图形(并且只有一个历史。不知何故,你似乎已经部分拥有了(否则我无法弄清楚你的_xGraph/_yGraph/_zGraph)但另一方面你画了所有一个 drawLayer 中有 3 个值???目前您似乎有 3*3 个历史缓冲区,其中 3*2 个缓冲区填充了零...
2.) 使用一个计算 Y 的地方,同时用于绘制网格和绘制线条...
3.) 使用 CGContextMoveToPoint(); + CGContextAddLineToPoint();而不是用这些丑陋的 2*i+1 索引复制到 lines[]...

关于ios - (iOS) 加速度计图(将重力转换为 +/- 128)粒度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423104/

相关文章:

iphone - UIScrollView 在模拟器中过来 UIImage

ios - iPad Mini 上的 UIImage 渲染不清晰

Java:为类的所有实例设置 boolean 值

algorithm - 计算数组元素持久化算法

graph - 默认情况下使 Graphite UI 数据累积

ios - 为什么我的 iOS 应用程序在将模型对象保存到 ManagedObjectContext 时崩溃

ios - NSFetchedResultController - 对 2 个属性的自定义排序

ios - 如何使用自定义单元格重新排列 UITableView?

ios - 为 uinavigationcontroller 堆栈上的每个 View Controller 创建关闭按钮

ios - 如何在 Objective-C 中按下第一个按钮时显示第二个按钮