(iOS) Accelerometer graph (convert g-force to +/- 128) granularity

I use this accelerometer graph from Apple and try to convert their G-force code to calculate +/- 128 .

The following figure shows that the x , y , z values in the labels do not correspond to the output on the graph: (Note that the addX:y:zvalues ​​are what is shown in the inscriptions above the graph)

enter image description here

ViewController

The x, y, z values are received from a bluetooth peripheral device and then converted using:

// 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];

    });
}

Graphview

- (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);
}

How can I calculate the code above to accurately show the correct accelerometer values ​​on a graph?

+4
1

aswer, , , , , , , ...

, . , xx/yy/zz . , 8192.

, , ...
[] 1/8192- . x -2 -0,0000, - ( , 1 ) ... , ( )

, - DrawGridlines. , ...

, , , + 127- , +127

:
1.) , ( . - , , ( _xGraph/_yGraph/_zGraph). 3 drawLayer??? 3 * 3 , 3 * 2 ...
2.) , Y, , ...
3.) CGContextMoveToPoint(); + CGContextAddLineToPoint(); , [] 2 * + 1 indecies...

+1

Source: https://habr.com/ru/post/1613715/


All Articles