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:z
values are what is shown in the inscriptions above the graph)
ViewController
The x, y, z values are received from a bluetooth peripheral device and then converted using:
- (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];
});
}
- (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 (index > 0)
{
--index;
xhistory[index] = x;
yhistory[index] = y;
zhistory[index] = z;
[layer setNeedsDisplay];
}
return index == 0;
}
- (void)drawLayer:(CALayer*)l inContext:(CGContextRef)context
{
CGContextSetFillColorWithColor(context, kUIColorLightGray(1.f).CGColor);
CGContextFillRect(context, layer.bounds);
DrawGridlines(context, 0.0, 32.0);
CGPoint lines[64];
int i;
float _granularity = 16.f;
NSInteger _granualCount = 32;
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);
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);
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?