Fix main loop for more runs, add timestamp interpolation.

This commit is contained in:
Stefan Bühler 2008-07-12 10:40:15 +02:00
parent 7983343bc4
commit b285897838
6 changed files with 93 additions and 63 deletions

View File

@ -96,17 +96,19 @@ void trial_reset_run(trial *t) {
control_parser_reset(t);
}
void trial_wait_for_start(trial *t) {
while (0 == t->telemetry.length) {
trial_wait_for_input(t);
int trial_wait_for_start(trial *t) {
while (!t->finished && !t->alive) {
if (-1 == trial_wait_for_input(t)) return -1;
}
return 0;
}
static char buffer[16*1024];
void trial_check_input(trial *t) {
int trial_check_input(trial *t) {
ssize_t len;
struct pollfd p;
p.fd = t->socket;
p.events = POLLIN;
switch (poll(&p, 1, 0)) {
@ -114,34 +116,44 @@ void trial_check_input(trial *t) {
switch (errno) {
case EAGAIN:
case EINTR:
return;
return 0;
}
fprintf(stderr, "poll error: %s\n", strerror(errno));
exit(127);
break;
return -1;
case 0:
return;
return 0;
}
len = read(t->socket, buffer, sizeof(buffer));
if (len < 0) {
fprintf(stderr, "read error: %s\n", strerror(errno));
exit(127);
return -1;
} else if (len == 0) {
t->finished = 1;
if (t->alive) {
fprintf(stderr, "unexpected connection close\n");
return -1;
}
return 0;
}
g_string_append_len(t->parse_ctx->buffer, buffer, len);
control_parse(t);
return (RUN_ERROR != control_parse(t)) ? 0 : -1;
}
void trial_wait_for_input(trial *t) {
int trial_wait_for_input(trial *t) {
ssize_t len = read(t->socket, buffer, sizeof(buffer));
if (len < 0) {
fprintf(stderr, "read error: %s\n", strerror(errno));
exit(127);
return -1;
} else if (len == 0) {
fprintf(stderr, "connection closed\n");
exit(125);
if (t->alive) {
fprintf(stderr, "unexpected connection close\n");
return -1;
}
t->finished = 1;
return 0;
}
g_string_append_len(t->parse_ctx->buffer, buffer, len);
control_parse(t);
return (RUN_ERROR != control_parse(t)) ? 0 : -1;
}
void trial_free(trial *t) {

View File

@ -4,11 +4,14 @@
#include "config.h"
#include <unistd.h>
#include <sys/time.h>
#include <glib.h>
#define INLINE static inline
#define UNUSED(x) ((void)(x))
typedef enum { RUN_ERROR, RUN_DONE, RUN_GO_ON, RUN_FINISHED } run_t;
typedef enum { ACCEL, ROLL, BREAK } accel_t;
typedef enum { TURN_HARD_LEFT, TURN_LEFT, TURN_STRAIGHT, TURN_RIGHT, TURN_HARD_RIGHT } turn_t;
@ -66,7 +69,8 @@ struct trial {
timestamp last_ts;
GQueue telemetry;
vehicle vehicle; /* our view */
int alive;
int alive, finished;
struct timeval started;
/* internal */
int socket;
@ -76,9 +80,9 @@ struct trial {
/* trial */
trial *trial_new(const char *hostname, const char *port);
void trial_reset_run(trial *t);
void trial_wait_for_start(trial *t);
void trial_check_input(trial *t);
void trial_wait_for_input(trial *t);
int trial_wait_for_start(trial *t);
int trial_check_input(trial *t);
int trial_wait_for_input(trial *t);
void trial_free(trial *t);
/* debugging stuff */
@ -99,6 +103,8 @@ INLINE void vehicle_straight(trial *t);
INLINE void vehicle_right(trial *t);
INLINE void vehicle_hard_right(trial *t);
INLINE timestamp getcurts(trial *t);
/* needed by parser */
telemetry* telemetry_new();
@ -183,4 +189,10 @@ INLINE void vehicle_hard_right(trial *t) {
t->vehicle.turn = TURN_HARD_RIGHT;
}
INLINE timestamp getcurts(trial *t) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec - t->started.tv_sec) * 1000 + (tv.tv_usec - t->started.tv_sec) / 1000;
}
#endif

View File

@ -27,7 +27,7 @@ static timestamp extract_ts(context *ctx, char *fpc) {
return atoi(ctx->tmp->str);
}
#line 122 "control_parser.rl"
#line 123 "control_parser.rl"
@ -240,7 +240,7 @@ static const int control_parser_error = 0;
static const int control_parser_en_main = 1;
#line 125 "control_parser.rl"
#line 126 "control_parser.rl"
static int control_parser_has_error(context *ctx) {
return ctx->cs == control_parser_error;
@ -257,7 +257,7 @@ void control_parser_new(trial *t) {
{
( ctx->cs) = control_parser_start;
}
#line 137 "control_parser.rl"
#line 138 "control_parser.rl"
ctx->buffer = g_string_sized_new(0);
ctx->tmp = g_string_sized_new(0);
ctx->mark = -1;
@ -271,7 +271,7 @@ void control_parser_reset(trial *t) {
{
( ctx->cs) = control_parser_start;
}
#line 146 "control_parser.rl"
#line 147 "control_parser.rl"
g_string_truncate(ctx->tmp, 0);
ctx->mark = -1;
ctx->pos = 0;
@ -389,12 +389,13 @@ _match:
t->vehicle.turn = ctx->tm->vehicle.turn;
}
t->last_ts = ctx->tm->ts;
gettimeofday(&t->started, NULL);
ctx->tm = NULL;
t->alive = 1;
}
break;
case 4:
#line 47 "control_parser.rl"
#line 48 "control_parser.rl"
{
ctx->tm->ts = ctx->ts;
ctx->tm->vehicle.x = ctx->x;
@ -409,115 +410,115 @@ _match:
}
break;
case 5:
#line 64 "control_parser.rl"
#line 65 "control_parser.rl"
{ t->map.dx = extract_double(ctx, p); }
break;
case 6:
#line 65 "control_parser.rl"
#line 66 "control_parser.rl"
{ t->map.dy = extract_double(ctx, p); }
break;
case 7:
#line 66 "control_parser.rl"
#line 67 "control_parser.rl"
{ t->map.min_sensor = extract_double(ctx, p); }
break;
case 8:
#line 67 "control_parser.rl"
#line 68 "control_parser.rl"
{ t->map.max_sensor = extract_double(ctx, p); }
break;
case 9:
#line 68 "control_parser.rl"
#line 69 "control_parser.rl"
{ t->map.max_speed = extract_double(ctx, p); }
break;
case 10:
#line 69 "control_parser.rl"
#line 70 "control_parser.rl"
{ t->map.max_turn = extract_double(ctx, p); }
break;
case 11:
#line 70 "control_parser.rl"
#line 71 "control_parser.rl"
{ t->map.max_hard_turn = extract_double(ctx, p); }
break;
case 12:
#line 74 "control_parser.rl"
#line 75 "control_parser.rl"
{ ctx->x = extract_double(ctx, p); }
break;
case 13:
#line 75 "control_parser.rl"
#line 76 "control_parser.rl"
{ ctx->y = extract_double(ctx, p); }
break;
case 14:
#line 76 "control_parser.rl"
#line 77 "control_parser.rl"
{ ctx->r = extract_double(ctx, p); }
break;
case 15:
#line 77 "control_parser.rl"
#line 78 "control_parser.rl"
{ ctx->dir = extract_double(ctx, p); }
break;
case 16:
#line 78 "control_parser.rl"
#line 79 "control_parser.rl"
{ ctx->speed = extract_double(ctx, p); }
break;
case 17:
#line 80 "control_parser.rl"
#line 81 "control_parser.rl"
{ ctx->tm->vehicle.accel = ACCEL; }
break;
case 18:
#line 81 "control_parser.rl"
#line 82 "control_parser.rl"
{ ctx->tm->vehicle.accel = ROLL; }
break;
case 19:
#line 82 "control_parser.rl"
#line 83 "control_parser.rl"
{ ctx->tm->vehicle.accel = BREAK; }
break;
case 20:
#line 84 "control_parser.rl"
#line 85 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_LEFT; }
break;
case 21:
#line 85 "control_parser.rl"
#line 86 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_LEFT; }
break;
case 22:
#line 86 "control_parser.rl"
#line 87 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_STRAIGHT; }
break;
case 23:
#line 87 "control_parser.rl"
#line 88 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_RIGHT; }
break;
case 24:
#line 88 "control_parser.rl"
#line 89 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_RIGHT; }
break;
case 25:
#line 90 "control_parser.rl"
#line 91 "control_parser.rl"
{ ctx->ts = extract_ts(ctx, p); }
break;
case 26:
#line 91 "control_parser.rl"
#line 92 "control_parser.rl"
{ printf("Score %u\n", extract_ts(ctx, p)); }
break;
case 27:
#line 93 "control_parser.rl"
#line 94 "control_parser.rl"
{
object o = { BOLDER, ctx->x, ctx->y, ctx->r, 0, 0 };
g_array_append_val(ctx->tm->objects, o);
}
break;
case 28:
#line 97 "control_parser.rl"
#line 98 "control_parser.rl"
{
object o = { CRATER, ctx->x, ctx->y, ctx->r, 0, 0 };
g_array_append_val(ctx->tm->objects, o);
}
break;
case 29:
#line 102 "control_parser.rl"
#line 103 "control_parser.rl"
{
object o = { MARTIAN, ctx->x, ctx->y, ctx->r, ctx->dir, ctx->speed };
g_array_append_val(ctx->tm->objects, o);
}
break;
#line 521 "control_parser.c"
#line 522 "control_parser.c"
}
}
@ -529,7 +530,7 @@ _again:
_test_eof: {}
_out: {}
}
#line 169 "control_parser.rl"
#line 170 "control_parser.rl"
ctx->pos = p - ctx->buffer->str;
if (ctx->mark == -1) {

View File

@ -4,8 +4,6 @@
struct control_parser_ctx;
typedef struct control_parser_ctx control_parser_ctx;
typedef enum { RUN_ERROR, RUN_DONE, RUN_GO_ON } run_t;
#include "control.h"
struct control_parser_ctx {

View File

@ -41,6 +41,7 @@ static timestamp extract_ts(context *ctx, char *fpc) {
t->vehicle.turn = ctx->tm->vehicle.turn;
}
t->last_ts = ctx->tm->ts;
gettimeofday(&t->started, NULL);
ctx->tm = NULL;
t->alive = 1;
}

View File

@ -4,9 +4,23 @@
#include <stdio.h>
void trial_loop(trial *t) {
path *p;
do {
if (-1 == trial_wait_for_start(t)) return;
if (t->finished) break;
/*create trivial path towards the origin*/
p = path_new(&t->map, &t->vehicle);
while (t->alive) {
path_execute(t,p);
if (-1 == trial_check_input(t)) return;
}
path_free(p);
} while (!t->finished);
}
int main(int argc, char **argv) {
trial *t;
path *p;
if (argc <= 2) {
fprintf(stderr, "Syntax: %s hostname port\n", argv[0]);
@ -17,16 +31,8 @@ int main(int argc, char **argv) {
return 2;
}
/*create trivial path towards the origin*/
trial_loop(t);
trial_wait_for_start(t);
p = path_new(&t->map,&t->vehicle);
while(t->alive){
path_execute(t,p);
trial_wait_for_input(t);
}
path_free(p);
trial_free(t);
return 0;