TORCS  1.3.9
The Open Racing Car Simulator
telemetry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : telemetry.cpp
4  created : Sat Feb 26 16:48:43 CET 2000
5  copyright : (C) 2000 by Eric Espie
6  email : torcs@free.fr
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #include <stdio.h>
21 #include <memory.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 
27 #include <tgf.h>
28 #include <telemetry.h>
29 
30 #include "tlm.h"
31 
32 typedef struct Channel
33 {
34  struct Channel *next;
35  const char *name; /* channel name */
36  tdble *val; /* monitored value */
38 } tChannel;
39 
40 typedef struct Tlm
41 {
42  FILE *file; /* associated file */
43  char *cmdfile;
44  int state;
48 } tTlm;
49 
50 static tTlm TlmData;
51 
52 
53 /*
54  * Function
55  * TlmInit
56  *
57  * Description
58  * Init telemetry internal structures.
59  *
60  * Parameters
61  * none
62  *
63  * Return
64  * none
65  */
66 void
68 {
69  TlmData.file = (FILE*)NULL;
70  TlmData.state = 0;
71  TlmData.ymin = ymin;
72  TlmData.ymax = ymax;
73  TlmData.chanList = (tChannel*)NULL;
74 }
75 
76 
77 /*
78  * Function
79  * TlmNewChannel
80  *
81  * Description
82  * Create a new channel
83  *
84  * Parameters
85  *
86  * Return
87  * channel ID or -1 if error
88  */
89 void
90 TlmNewChannel(const char *name, tdble *var, tdble min, tdble max)
91 {
92  tChannel *curChan;
93 
94  curChan = (tChannel *)calloc(sizeof(tChannel), 1);
95  if (TlmData.chanList == NULL) {
96  TlmData.chanList = curChan;
97  curChan->next = curChan;
98  } else {
99  curChan->next = TlmData.chanList->next;
100  TlmData.chanList->next = curChan;
101  TlmData.chanList = curChan;
102  }
103 
104  curChan->name = name;
105  curChan->val = var;
106  if ((min == 0) && (max == 0)) {
107  curChan->scale = 1.0;
108  } else {
109  curChan->scale = TlmData.ymax / max;
110  }
111 }
112 
113 void
114 TlmStartMonitoring(const char *filename)
115 {
116  const int BUFSIZE = 1024;
117  char buf[BUFSIZE];
118  FILE *fout;
119  FILE *fcmd;
120  tChannel *curChan;
121  int i;
122 
123  GfOut("Telemetry: start monitoring\n");
124 
125  snprintf(buf, BUFSIZE, "telemetry/%s.cmd", filename);
126  fcmd = fopen(buf, "w");
127  if (fcmd == NULL) {
128  return;
129  }
130  fprintf(fcmd, "#!/bin/sh\n");
131  fprintf(fcmd, "gnuplot -persist > telemetry/%s.png <<!!\n", filename);
132  fprintf(fcmd, "# set yrange [%f:%f]\n", TlmData.ymin, TlmData.ymax);
133  fprintf(fcmd, " set grid\n");
134  fprintf(fcmd, " set size 2.5,1.5\n");
135  fprintf(fcmd, " set terminal png color\n");
136  fprintf(fcmd, " set data style lines\n");
137  curChan = TlmData.chanList;
138  if (curChan != NULL) {
139  i = 2;
140  do {
141  curChan = curChan->next;
142  if (i == 2) {
143  fprintf(fcmd, "plot 'telemetry/%s.dat' using %d title '%s'", filename, i, curChan->name);
144  } else {
145  fprintf(fcmd, ", '' using %d title '%s'", i, curChan->name);
146  }
147  i++;
148  } while (curChan != TlmData.chanList);
149  fprintf(fcmd, "\n");
150  }
151  fprintf(fcmd, "!!\n");
152  fclose(fcmd);
153 
154  TlmData.cmdfile = strdup(buf);
155 
156  snprintf(buf, BUFSIZE, "telemetry/%s.dat", filename);
157  fout = TlmData.file = fopen(buf, "w");
158  if (fout == NULL) {
159  return;
160  }
161  curChan = TlmData.chanList;
162  fprintf(fout, "time");
163  if (curChan != NULL) {
164  do {
165  curChan = curChan->next;
166  fprintf(fout, " %s", curChan->name);
167  } while (curChan != TlmData.chanList);
168  fprintf(fout, "\n");
169  }
170 
171  TlmData.state = 1;
172 }
173 
174 void
175 TlmUpdate(double time)
176 {
177  FILE *fout;
178  tChannel *curChan;
179 
180  if (TlmData.state == 0) {
181  return;
182  }
183  fout = TlmData.file;
184  fprintf(fout, "%f ", time);
185 
186  curChan = TlmData.chanList;
187  if (curChan != NULL) {
188  do {
189  curChan = curChan->next;
190  fprintf(fout, "%f ", curChan->scale * (*curChan->val));
191  } while (curChan != TlmData.chanList);
192  }
193  fprintf(fout, "\n");
194 }
195 
196 
197 void
199 {
200  const int BUFSIZE = 256;
201  char buf[BUFSIZE];
202 
203  if (TlmData.state == 1) {
204  fclose(TlmData.file);
205  }
206  TlmData.file = (FILE*)NULL;
207  TlmData.state = 0;
208  GfOut("Telemetry: stop monitoring\n");
209 
210  snprintf(buf, BUFSIZE, "sh %s", TlmData.cmdfile);
211  system(buf);
212  free(TlmData.cmdfile);
213 }
214 
215 /*
216  * Function
217  * TlmShutdown
218  *
219  * Description
220  * release all the channels
221  *
222  * Parameters
223  * none
224  *
225  * Return
226  * none
227  */
228 void
230 {
231  if (TlmData.state == 1) {
233  }
234  /* GfRlstFree((tRingList **)&TlmData.chanList); */
235 }
static tdble ymax
Definition: track3.cpp:30
void TlmShutdown(void)
Definition: telemetry.cpp:229
tdble ymin
Definition: telemetry.cpp:45
Scalar max(Scalar x, Scalar y)
Definition: Basic.h:50
tChannel * chanList
Definition: telemetry.cpp:47
void TlmStopMonitoring(void)
Definition: telemetry.cpp:198
tdble ymax
Definition: telemetry.cpp:46
tdble scale
Definition: telemetry.cpp:37
int state
Definition: telemetry.cpp:44
void TlmInit(tdble ymin, tdble ymax)
Definition: telemetry.cpp:67
float tdble
Floating point type used in TORCS.
Definition: tgf.h:48
static tdble ymin
Definition: track3.cpp:30
#define GfOut
Definition: tgf.h:373
void TlmNewChannel(const char *name, tdble *var, tdble min, tdble max)
Definition: telemetry.cpp:90
The Gaming Framework API.
const char * name
Definition: telemetry.cpp:35
tdble * val
Definition: telemetry.cpp:36
void TlmStartMonitoring(const char *filename)
Definition: telemetry.cpp:114
void TlmUpdate(double time)
Definition: telemetry.cpp:175
char * cmdfile
Definition: telemetry.cpp:43
Scalar min(Scalar x, Scalar y)
Definition: Basic.h:49
FILE * file
Definition: telemetry.cpp:42
struct Channel * next
Definition: telemetry.cpp:34
static tTlm TlmData
Definition: telemetry.cpp:50
struct Tlm tTlm
struct Channel tChannel