Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在iPhone/Xcode中计算没有GPS的速度_Ios_Objective C_Core Motion - Fatal编程技术网

Ios 如何在iPhone/Xcode中计算没有GPS的速度

Ios 如何在iPhone/Xcode中计算没有GPS的速度,ios,objective-c,core-motion,Ios,Objective C,Core Motion,我正在尝试在iPhone/Xcode中不使用GPS设备的情况下获得汽车速度,下面是我尝试过的代码,但它在汽车中不起作用,它只在行走和跑步时返回速度 我尝试根据CoreMotion加速度值计算速度,然后将速度值转换为mph #import "ViewController.h" #import "HighpassFilter.h" #import "GraphVC.h" @import UIKit; #include <AudioToolbox/AudioToolbox.h> #def

我正在尝试在iPhone/Xcode中不使用GPS设备的情况下获得汽车速度,下面是我尝试过的代码,但它在汽车中不起作用,它只在行走和跑步时返回速度

我尝试根据CoreMotion加速度值计算速度,然后将速度值转换为mph

#import "ViewController.h"
#import "HighpassFilter.h"
#import "GraphVC.h"
@import UIKit;
#include <AudioToolbox/AudioToolbox.h>

#define kUpdateFrequency    60.0
static double timeInterval = 1.0/kUpdateFrequency;
@interface ViewController () <GraphVCDelegate>
{
    double lastAx[4],lastAy[4],lastAz[4];
    int countX, countY, countZ, accCount;
    double lastVx, lastVy, lastVz, maxV;

    int type;

    HighpassFilter * filter;
    CMMotionManager * manager;
    GraphVC * graphVC;
}

@property (weak, nonatomic) IBOutlet UILabel *speedLabel;
@property (weak, nonatomic) IBOutlet UILabel *xaccLabel;
@property (weak, nonatomic) IBOutlet UILabel *yaccLabel;
@property (weak, nonatomic) IBOutlet UILabel *zaccLabel;
@property (weak, nonatomic) IBOutlet UILabel *maxvLabel;
@property (nonatomic, assign) BOOL paused;
@property (nonatomic, assign) BOOL soundPlayed;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    lastVx = 0, lastVy = 0, lastVz = 0;
    accCount = maxV = type = 0;
    _soundPlayed = false;

    for (int i = 0; i < 4; ++i){
        lastAx[i] = lastAy[i] = lastAz[i] = 0;
        }

        manager = [[CMMotionManager alloc] init];
        manager.accelerometerUpdateInterval = timeInterval;
    manager.gyroUpdateInterval = timeInterval;

    filter = [[HighpassFilter alloc] initWithSampleRate:kUpdateFrequency cutoffFrequency:5.0];

    [manager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                        withHandler:^(CMDeviceMotion *data, NSError *error) {
                                            [self outputAccelertion:data];
                                        }];
}

- (void)viewDidAppear:(BOOL)animated{
    graphVC = nil;
}

- (void)outputAccelertion:(CMDeviceMotion*)data
{
    CMAcceleration acc = [data userAcceleration];
    CMAcceleration gacc = [data gravity];
    acc.x += gacc.x, acc.y += gacc.y, acc.z += gacc.z;
    CMRotationMatrix rot = [data attitude].rotationMatrix;
    CMAcceleration accRef;

//first correct the direction
accRef.x = acc.x*rot.m11 + acc.y*rot.m12 + acc.z*rot.m13;
accRef.y = acc.x*rot.m21 + acc.y*rot.m22 + acc.z*rot.m23;
accRef.z = acc.x*rot.m31 + acc.y*rot.m32 + acc.z*rot.m33;

//filter the data
[filter addAcceleration:accRef];

if (!_paused && graphVC && type == 1){
    [graphVC.unfiltered addX:accRef.x y:accRef.y z:accRef.z];
    [graphVC.filtered addX:filter.x y:filter.y z:filter.z];
}

//add threshold
accRef.x = (fabs(filter.x) < 0.03) ? 0 : filter.x;
accRef.y = (fabs(filter.y) < 0.03) ? 0 : filter.y;
accRef.z = (fabs(filter.z) < 0.03) ? 0 : filter.z;

//we use simpson 3/8 integration method here
accCount = (accCount+1)%4;

lastAx[accCount] = accRef.x, lastAy[accCount] = accRef.y, lastAz[accCount] = accRef.z;

if (!_paused && graphVC && type == 0)
    [graphVC.unfiltered addX:lastVx+accRef.x*timeInterval y:lastVy+accRef.y*timeInterval z:lastVz+accRef.z*timeInterval];

if (accCount == 3){
    lastVx += (lastAx[0]+lastAx[1]*3+lastAx[2]*3+lastAx[3]) * 0.125 * timeInterval * 3;
    lastVy += (lastAy[0]+lastAy[1]*3+lastAy[2]*3+lastAy[3]) * 0.125 * timeInterval * 3;
    lastVz += (lastAz[0]+lastAz[1]*3+lastAz[2]*3+lastAz[3]) * 0.125 * timeInterval * 3;
}

//add a fake force
//(when acc is zero for a continuous time, we should assume that velocity is zero)
if (accRef.x == 0) countX++; else countX = 0;
if (accRef.y == 0) countY++; else countY = 0;
if (accRef.z == 0) countZ++; else countZ = 0;
if (countX == 10){
    countX = 0;
    lastVx = 0;
}
if (countY == 10){
    countY = 0;
    lastVy = 0;
}
if (countZ == 10){
    countZ = 0;
    lastVz = 0;
}

if (!_paused && graphVC && type == 0)
    [graphVC.filtered addX:lastVx y:lastVy z:lastVz];

//get total V
double vx = lastVx * 9.8, vy = lastVy * 9.8, vz = lastVz * 9.8;
double lastV = sqrt(vx * vx + vy * vy + vz * vz);

lastV = sqrt(lastV * 2.23694);

[_speedLabel setText:[NSString stringWithFormat:@"%.2f",lastV]];
[_xaccLabel setText:[NSString stringWithFormat:@"%.2f g",accRef.x]];
[_yaccLabel setText:[NSString stringWithFormat:@"%.2f g",accRef.y]];
[_zaccLabel setText:[NSString stringWithFormat:@"%.2f g",accRef.z]];

if (fabs(maxV) < fabs(lastV)){
    maxV = lastV;
    [_maxvLabel setText:[NSString stringWithFormat:@"%.2f mph", maxV]];

        if(maxV > 15 && _soundPlayed==false){

            AudioServicesPlaySystemSound(1104);
            _soundPlayed = true;
        }
    }
}

- (void)valueChanged:(int)n{
    type = n;
}

@end
#导入“ViewController.h”
#导入“HighpassFilter.h”
#导入“GraphVC.h”
@进口UIKit;
#包括
#定义kUpdateFrequency 60.0
静态双时间间隔=1.0/kUpdateFrequency;
@界面视图控制器()
{
双拉斯塔斯[4],拉斯塔斯[4],拉斯塔斯[4];
int countX、countY、countZ、accCount;
双lastVx、lastVy、lastVz、maxV;
int型;
高通滤波器*滤波器;
CMMotionManager*经理;
GraphVC*GraphVC;
}
@属性(弱、非原子)IBUILabel*speedLabel;
@性质(弱,非原子)IBUILabel*xaccLabel;
@性质(弱,非原子)IBUILabel*yaccLabel;
@性质(弱,非原子)IBUILabel*zaccLabel;
@属性(弱,非原子)IBUILabel*maxvLabel;
@属性(非原子,赋值)布尔暂停;
@属性(非原子,分配)布尔声音播放;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
lastVx=0,lastVy=0,lastVz=0;
accCount=maxV=type=0;
_声音播放=假;
对于(int i=0;i<4;++i){
lastAx[i]=lastAy[i]=lastAz[i]=0;
}
manager=[[CMMotionManager alloc]init];
manager.AccelerometrUpdateInterval=时间间隔;
manager.gyroUpdateInterval=时间间隔;
过滤器=[[HighpassFilter alloc]initWithSampleRate:kUpdateFrequency截止频率:5.0];
[manager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMDeviceMotion*数据,N错误*错误){
[自输出加速:数据];
}];
}
-(无效)视图显示:(BOOL)动画{
graphVC=nil;
}
-(void)OutputAcceleration:(CMDeviceMotion*)数据
{
cAcceleration acc=[data userAcceleration];
C加速度gacc=[数据重力];
附件x+=gacc.x,附件y+=gacc.y,附件z+=gacc.z;
CMRotationMatrix rot=[数据姿态]。rotationMatrix;
c加速度增大;
//首先纠正方向
加积x=acc.x*rot.m11+acc.y*rot.m12+acc.z*rot.m13;
增量y=acc.x*rot.m21+acc.y*rot.m22+acc.z*rot.m23;
加积z=acc.x*rot.m31+acc.y*rot.m32+acc.z*rot.m33;
//过滤数据
[过滤器添加加速度:增大];
如果(!\u暂停&&graphVC&&type==1){
[graphVC.unfilteredaddx:accRef.xy:accRef.yz:accRef.z];
[graphVC.filteredaddx:filter.xy:filter.yz:filter.z];
}
//添加阈值
增大系数x=(fabs(filter.x)<0.03)?0:filter.x;
加积y=(fabs(filter.y)<0.03)?0:filter.y;
加积系数z=(fabs(filter.z)<0.03)?0:filter.z;
//这里我们使用simpson 3/8积分法
会计科目=(会计科目+1)%4;
lastAx[accCount]=acfirf.x,lastAy[accCount]=acfirf.y,lastAz[accCount]=acfirf.z;
如果(!\u暂停&&graphVC&&type==0)
[graphVC.unfilted addX:lastVx+accRef.x*时间间隔y:lastVy+accRef.y*时间间隔z:lastVz+accRef.z*时间间隔];
如果(accCount==3){
lastVx+=(lastAx[0]+lastAx[1]*3+lastAx[2]*3+lastAx[3])*0.125*时间间隔*3;
lastVy+=(最后一天[0]+最后一天[1]*3+最后一天[2]*3+最后一天[3])*0.125*时间间隔*3;
lastVz+=(lastAz[0]+lastAz[1]*3+lastAz[2]*3+lastAz[3])*0.125*时间间隔*3;
}
//添加一个假的力量
//(当acc连续时间为零时,我们应假设速度为零)
如果(加积x==0)countX++;否则countX=0;
如果(accRef.y==0)country++;else country=0;
如果(accRef.z==0)countZ++;否则countZ=0;
如果(countX==10){
countX=0;
lastVx=0;
}
如果(县==10){
县=0;
lastVy=0;
}
如果(countZ==10){
countZ=0;
lastVz=0;
}
如果(!\u暂停&&graphVC&&type==0)
[graphVC.filteredaddx:lastVx y:lastVy z:lastVz];
//得到总V
双vx=lastVx*9.8,vy=lastVy*9.8,vz=lastVz*9.8;
双lastV=sqrt(vx*vx+vy*vy+vz*vz);
lastV=sqrt(lastV*2.23694);
[_SpeedLabelSetText:[NSString stringWithFormat:@“%.2f”,lastV]];
[_xacclabelsettext:[NSString stringWithFormat:@“%.2f g”,accRef.x]];
[[u yaccLabel setText:[NSString stringWithFormat:@“%.2f g”,accRef.y]];
[_zacclabelsettext:[NSString stringWithFormat:@“%.2f g”,accRef.z]];
if(晶圆厂(最大值)<晶圆厂(最小值)){
maxV=lastV;
[_maxvlabelsettext:[NSString stringWithFormat:@“%.2f mph”,maxV]];
如果(maxV>15&&u声音播放==false){
AudioServicesPlaySystemSound(1104);
_声音播放=正确;
}
}
}
-(无效)值已更改:(int)n{
类型=n;
}
@结束

如果它在行走/跑步时起作用,但在车内不起作用,那么它在车内做什么?仅仅说“它不工作”无助于诊断问题。它没用吗?它提供了错误的信息吗?它会产生误差吗?看起来你忽略了连续10个周期的恒定加速度,这是1/6秒,假设你在60Hz下采样。与人类不同的是,汽车更善于在短时间内不断加速=)你能像泰德·瑞格利(Ted Wrigley)所问的那样提供一个日志吗?如果它在行走/跑步时有效,但在车内不起作用,那么它在车内做什么?仅仅说“它不工作”无助于诊断问题。它没用吗?它提供了错误的信息吗?它会产生误差吗?看起来你忽略了连续10个周期的恒定加速度,这是1/6秒,假设你在60Hz下采样。与人类不同的是,汽车更擅长在短时间内不断加速(如泰德·瑞格利所问,你能提供一份日志吗?