summaryrefslogtreecommitdiffhomepage
path: root/digital/ai/src/utils/chrono.c
blob: 8560c3e0b046e3b6436b76dbb4b7812aa3050045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* chrono.c */
/* ai - Robot Artificial Intelligence. {{{
 *
 * Copyright (C) 2008 Dufour Jérémy
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"

#include "timer.h"
#include "asserv.h"
#include "twi_master.h"

#include "modules/utils/utils.h"
#include "modules/host/mex.h"

#include "chrono.h"

/** Number of timer tick to wait before the match is over. */
#define CHRONO_MATCH_TICK_COUNT \
    (CHRONO_MATCH_DURATION_MS / TIMER_PERIOD_MS)

/** Time to wait before freeing asserv motors, in ms. */
#define CHRONO_WAIT_BEFORE_FREE_MS 1000

/** Number of timer tick left before the match ends. */
static uint32_t chrono_tick_left_;

/** Last timer tick value. */
static uint16_t chrono_last_tick_;

/** Is chrono started? */
static uint8_t chrono_started_;

void
chrono_start (void)
{
    chrono_started_ = 1;
    chrono_tick_left_ = CHRONO_MATCH_TICK_COUNT;
    chrono_last_tick_ = timer_get_tick ();
}

void
chrono_update (void)
{
    if (chrono_started_)
      {
	uint16_t new_tick = timer_get_tick ();
	uint16_t diff = new_tick - chrono_last_tick_;
	chrono_last_tick_ = new_tick;
	if (diff > chrono_tick_left_)
	    chrono_tick_left_ = 0;
	else
	    chrono_tick_left_ -= diff;
      }
}

uint8_t
chrono_is_match_over (void)
{
    if (!chrono_started_ || chrono_tick_left_)
	return 0;
    else
	return 1;
}

uint32_t
chrono_remaining_time (void)
{
    if (!chrono_started_)
	return CHRONO_MATCH_DURATION_MS;
    else
	return chrono_tick_left_ * TIMER_PERIOD_MS;
}

void
chrono_end_match (uint8_t block)
{
    /* Make the bot stop moving */
    asserv_stop_motor ();
    /* Wait until complete */
    while (!twi_master_sync ())
	timer_wait ();
    /* Wait, then release motors. */
    utils_delay_ms (CHRONO_WAIT_BEFORE_FREE_MS);
    asserv_free_motor ();
    /* Wait until complete */
    while (!twi_master_sync ())
	timer_wait ();
    /* Block indefinitely */
    while (block)
	utils_delay_ms (1000);
}