1 #include "match-timing.h"
5 #define COMPETITION_TIME_DEFAULT 90
6 #define TIME_TO_DEPOSITE_DEFAULT 60
8 #define COMPETITION_TIME_DEFAULT 90
9 #define TIME_TO_DEPOSITE_DEFAULT 60
12 /* competition time in seconds */
13 #define COMPETITION_TIME COMPETITION_TIME_DEFAULT
14 #define TIME_TO_DEPOSITE TIME_TO_DEPOSITE_DEFAULT
15 /* competition time in seconds */
19 /** *********************************************************************
20 * Competition timer. Stop robot when the timer exceeds.
21 ********************************************************************** */
23 static struct timespec start;
25 void *timing_thread(void *arg)
27 sem_wait(&robot.start);
28 clock_gettime(CLOCK_MONOTONIC, &start);
32 t.tv_sec = start.tv_sec+sec; \
33 t.tv_nsec = start.tv_nsec; \
34 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL); \
38 // // microswitch (backside opponent detector), ignore it while at starting point
39 // robot.use_back_switch = true;
40 // printf("Back switch not ignored\n");
42 WAIT(TIME_TO_DEPOSITE);
43 robot.short_time_to_end = true;
45 WAIT(COMPETITION_TIME);
46 printf("%d seconds timer exceeded! exiting!\n", COMPETITION_TIME);
52 /* Subtract the `struct timespec' values X and Y,
53 storing the result in RESULT (result = x - y).
54 Return 1 if the difference is negative, otherwise 0. */
57 timespec_subtract (struct timespec *result,
61 /* Perform the carry for the later subtraction by updating Y. */
62 if (x->tv_nsec < y->tv_nsec) {
63 int num_sec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
64 y->tv_nsec -= 1000000000 * num_sec;
67 if (x->tv_nsec - y->tv_nsec > 1000000000) {
68 int num_sec = (x->tv_nsec - y->tv_nsec) / 1000000000;
69 y->tv_nsec += 1000000000 * num_sec;
73 /* Compute the time remaining to wait.
74 `tv_nsec' is certainly positive. */
75 result->tv_sec = x->tv_sec - y->tv_sec;
76 result->tv_nsec = x->tv_nsec - y->tv_nsec;
78 /* Return 1 if result is negative. */
79 return x->tv_sec < y->tv_sec;
83 float robot_current_time()
85 struct timespec now, diff;
86 clock_gettime(CLOCK_MONOTONIC, &now);
87 timespec_subtract(&diff, &now, &start);
88 return diff.tv_sec + diff.tv_nsec/1000000000.0;