我正在尝试在C中生成实时图形,其中我的y轴数据是实时/实时测量的,以调试我的机器人。有谁知道如何创建“实时”情节?我尝试gnuplot
如下,但图表只显示1点。我对任何其他生成实时图表的方法持开放态度。目前,我的raspberry pi已设置为wifi服务器,因此我无法将所有x
和y
轴转储到云端或在线服务以绘制图形。
我也试过the suggestion here。虽然我已经正确设置了所有库并成功编译,但没有显示任何内容。
我的数据始终为x
和y
- 轴的2浮点值。 while(1)
是否会导致任何问题,因为我无法知道x
轴的最大值?
谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#define DT 0.005 //5ms
int mymillis();
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1);
void plot_graph(FILE* p, float x, float y);
int main() {
int startInt = mymillis();
struct timeval tvBegin, tvEnd, tvDiff;
gettimeofday(&tvBegin, NULL);
int i = 1;
float x = 0.0;
float y = 0.0;
FILE *f = fopen("file.dat", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
FILE *p = popen("gnuplot", "w");
fprintf(p, "plot - with lines\n");
while (1)
{
startInt = mymillis();
//Do somethings over here e.g read sensor and filtering
y = generateX();//Function to generate y-axis data
x = (float i) * DT;//x-axis
plot_graph(p, x, y);
//Each loop should be at least 5ms.
while (mymillis() - startInt < (DT * 1000))
{
usleep(100);
}
i++;
}
return 0;
}
int mymillis()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
}
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
result->tv_sec = diff / 1000000;
result->tv_usec = diff % 1000000;
return (diff<0);
}
void plot_graph(FILE *p, float x, float y) {
fprintf(p,"plot '-'\n");
fprintf(p, "%f %f\n", x, y); // plot `a` points
fprintf(p,"e\n"); // finally, e
fflush(p); // flush the pipe to update the plot
}