#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define GRAVITY 9.8
double mag(double x, double y, double z);
double metersFallen(double fallTime);
double velocity (double v, double t1, double t2, double magAcc);
double position (double x, double v, double t1, double t2);
int main()
{
printf("Ok, I'm now receiving data.\n");
int running = TRUE;
double secFall = 0;;
double distance = 0;
int counter = 0;
int programStartTime = 0;
double currentTime = 0;
int time = 0;
int start = 0, end = 0;
double x, y, z, v, p;
double seconds;
scanf("%d,%lf,%lf,%lf", &time, &x, &y, &z);
printf("I'm Waiting");
programStartTime = time;
seconds = time;
while(running){
scanf("%d,%lf,%lf,%lf", &time, &x, &y, &z);
if((time - programStartTime) >= 1000){
printf(".");
programStartTime = time;
}
if(mag(x, y, z) < .75 ){
start = time;
printf("Help me! I'm falling");
while(end == 0){
if((time - programStartTime) >= 1000){
printf("!");
programStartTime = time;
}
scanf("%d,%lf,%lf,%lf", &time, &x, &y, &z);
v = velocity(v, time, seconds, mag(x,y,z));
p = position(x, v, time, seconds);
seconds = time;
if(mag(x, y, z) > .95 ){
end = time;
running = FALSE;
}
fflush(stdout);
}
}
fflush(stdout);
}
secFall = ((end - start)/1000.0);
distance = metersFallen(secFall);
//percent = ;
printf("\nTime falling was %6.3lf seconds\n", secFall);
printf("Distance fallen was %6.3lf meters\n", distance);
printf("Compensating for air resistance, the fall was %lf meters.\n", p);
//printf("This is %lf less than computed before\n", percent);
printf("%lf\n", mag(x,y,z));
return 0;
}
double metersFallen(double fallTime)
{
return(.5*GRAVITY*fallTime*fallTime);
}
double mag(double x, double y, double z)
{
return sqrt(x*x+y*y+z*z);
}
double velocity (double v, double t1, double t2, double magAcc)
{
int numVelocity = v + GRAVITY*(1-magAcc)*(t1 - t2)/1000;
return numVelocity;
}
double position (double x, double v, double t1, double t2)
{
double p = x + v*(t1 - t2)/1000;
return p;
}
使用此示例数据运行 http://www.filedropper.com/lab6sampledata20131
我需要位置功能才能工作,但是当Velocity返回零时我不能。我们正在使用带有加速度计的设备来运行它。仅供参考我是一名相当擅长编程的学生,请耐心等待。
答案 0 :(得分:2)
你的速度函数几乎肯定会返回0,因为你已将numVelocity
定义为int
。
在C中,当对整数进行计算时,答案将始终向下舍入到最接近的整数,并且当您的时间步长为5时,您的第二项将接近于0.将此添加到某些内容接近0将向下舍入为0.
据我所知,你还没有初步化v
并且你正在将v
(unitialised)传递到你的力度函数来计算v
,这将给你未定义结果(很可能是0 ......)。
此外,在以双精度进行计算时,应编写小数点为0的常量。 1000.0
。这被程序解释为double,而不是1000
,这是一个int。
同样,time是一个整数变量,你已经要求它是你函数的双精度变量。您可以通过传递(double)time
而不是time
来强制执行此操作。
尝试在代码中更改这些内容(通过将其设置为初始速度来初始化v)并查看更改内容:)