summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/chrono.c
blob: b73b627cdab98277cb176d239b97e854f9eec209 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* chrono.c */
/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
 *
 * 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 "bot.h"
#include "main_timer.h"
#include "asserv.h"
#include "twi_master.h"

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

#include "chrono.h"

/**
 * Implementation notes.
 * This module compute the number of tic of the main loop it should count
 * before the match is over (chrono_init). Every tic of the main loop, it
 * decrements the counter (chrono_update). When the counter is zero, the
 * match is over (chrono_is_match_over, chrono_end_match).
 */

/**
 * Number of overflows of the timer/counter 0 to wait before the match is
 * over.
 * Basically, it is match_duration / timer_counter_0_overflow_duration.
 * Minus one is here for safety reason (because rounding is done at plus one).
 */
#define CHRONO_MATCH_OVERFLOW_COUNT (MATCH_DURATION_MS / MT_TC0_PERIOD - 1)

/**
 * Duration of a loop to emulate from the original behaviour, in ms.
 */
#define CHRONO_LOOP_DURATION_MS 4

/**
 * Time to wait before resetting slaves board, in ms.
 */
#define CHRONO_WAIT_BEFORE_RESET_MS 1000

/**
 * Number of time to overflow before the end of the match.
 */
static uint32_t chrono_ov_count_;

/**
 * Status of the chrono module.
 * Set to 0 if the module is disabled, otherwise set to a non 0 value.
 */
static uint8_t chrono_enabled_ = 0;


void
chrono_init (void)
{
    /* Enable chrono. */
    chrono_enable ();
    /* Set the overflow counter to the maximum of overflow before the end of
     * the match. */
    chrono_ov_count_ = CHRONO_MATCH_OVERFLOW_COUNT;
}

void
chrono_update (void)
{
    /* Decrement overflow counter if it is possible. */
    if (chrono_enabled_ && chrono_ov_count_)
	chrono_ov_count_--;
}

uint8_t
chrono_is_match_over (void)
{
    if (!chrono_enabled_ || chrono_ov_count_)
	return 0;
    else
	return 1;
}

void
chrono_enable (void)
{
    chrono_enabled_ = 1;
}

void
chrono_disable (void)
{
    chrono_enabled_ = 0;
}

uint8_t
chrono_enabled (void)
{
    return chrono_enabled_;
}

uint32_t
chrono_remaining_time (void)
{
    return chrono_ov_count_ * MT_TC0_PERIOD;
}

void
chrono_end_match (uint8_t block)
{
    /* Make sure previous command has been acknowledged. If not, retransmit
     * until acknowledged */
    while (!twi_master_sync ())
	utils_delay_ms (CHRONO_LOOP_DURATION_MS);

    /* Make the bot stop moving */
    asserv_stop_motor ();

    /* Wait until complete */
    while (!twi_master_sync ())
	utils_delay_ms (CHRONO_LOOP_DURATION_MS);

    /* Wait CHRONO_WAIT_BEFORE_RESET ms before reseting */
    utils_delay_ms (CHRONO_WAIT_BEFORE_RESET_MS);
    /* Reset the asserv board */
    asserv_reset ();
    /* Block indefinitely */
    if (block)
	while (42)
#ifdef HOST
	  {
	    mex_node_wait ();
	  }
#else
	    ;
#endif
}