-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbotcontrol.c
227 lines (194 loc) · 5.89 KB
/
botcontrol.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* c't-Bot
*
* 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.
*
*/
/**
* \file botcontrol.c
* \brief High-level Steuerungsroutinen, z.B. Funktionen fuer die Bot-Hauptschleife
* \author Timo Sandmann (mail@timosandmann.de)
* \date 18.05.2011
*/
#include "ct-Bot.h"
#include "botcontrol.h"
#include "bot-2-sim.h"
#include "sensor-low.h"
#include "sensor.h"
#include "command.h"
#include "trace.h"
#include "timer.h"
#include "gui.h"
#include "display.h"
#include "os_thread.h"
#include "log.h"
#include "motor.h"
#include "map.h"
#include "init.h"
#include "ena.h"
#include "bot-2-atmega.h"
#include "bot-2-linux.h"
//#define DEBUG_TIMES /**< Gibt Debug-Infos zu Timing / Auslastung aus */
#if defined PC && defined DEBUG_TIMES
struct timeval init_start, init_stop; /**< Zeit von Beginn und Ende des Verhaltensdurchlaufs */
#endif // PC && DEBUG_TIMES
/**
* Fuehrt die Verarbeitung in der Hauptschlaufe vor dem
* Verhaltenscode durch. Dazu gehoert beispielsweise, die Sensoren
* abzufragen und auf Pakete des Simulators zu reagieren.
*/
void pre_behaviour(void) {
#ifdef ARM_LINUX_BOARD
/* Daten vom ATmega empfangen */
bot_2_atmega_listen();
#endif // ARM_LINUX_BOARD
#ifdef BOT_2_SIM_AVAILABLE
/* Daten vom Sim empfangen */
bot_2_sim_listen();
#ifdef ARM_LINUX_BOARD
set_bot_2_atmega();
#endif // ARM_LINUX_BOARD
#endif // BOT_2_SIM_AVAILABLE
/* Sensordaten aktualisieren / auswerten */
bot_sens();
#if defined PC && defined DEBUG_TIMES
/* Zum Debuggen der Zeiten */
GETTIMEOFDAY(&init_start, NULL);
int32_t t1 = (init_start.tv_sec - init_stop.tv_sec) * 1000000 + init_start.tv_usec - init_stop.tv_usec;
LOG_DEBUG("Done-Token (%d) in nach %d usec", received_command.data_l, t1);
#endif // PC && DEBUG_TIMES
#ifdef RC5_AVAILABLE
rc5_control(); // Abfrage der IR-Fernbedienung
#endif // RC5_AVAILABLE
#ifdef BOT_2_RPI_AVAILABLE
bot_2_linux_inform();
#endif // BOT_2_RPI_AVAILABLE
}
/**
* Fuehrt die Verarbeitung in der Hauptschleife nach dem
* Verhaltenscode durch. Dazu gehoert beispielsweise, die
* Bildschirmanzeige zu steuern und den Simulator ueber den aktuellen
* Zustand zu informieren.
*/
void post_behaviour(void) {
static uint16_t comm_ticks = 0;
static uint8_t uart_gui = 0;
#if defined MCU && defined DEBUG_TIMES
static uint32_t log_ticks = 0;
#endif
#ifdef BOT_2_RPI_AVAILABLE
bot_2_linux_listen();
#endif // BOT_2_RPI_AVAILABLE
#ifdef CREATE_TRACEFILE_AVAILABLE
trace_add_sensors();
#endif // CREATE_TRACEFILE_AVAILABLE
/* jeweils alle 100 ms kommunizieren Bot, User und Sim */
if (timer_ms_passed_16(&comm_ticks, 50)
#ifdef RC5_AVAILABLE
|| RC5_Code != 0
#endif
) {
if (uart_gui == 0
#ifdef RC5_AVAILABLE
|| RC5_Code != 0
#endif
) {
#ifdef DISPLAY_AVAILABLE
/* GUI-Behandlung starten */
gui_display(display_screen);
#endif // DISPLAY_AVAILABLE
uart_gui = 1; // bot-2-sim ist erst beim naechsten Mal dran
} else {
#ifdef BOT_2_SIM_AVAILABLE
/* Den Sim ueber Sensoren und Aktuatoren informieren */
bot_2_sim_inform(); // NOP auf PC
#endif // BOT_2_SIM_AVAILABLE
uart_gui = 0; // naechstes Mal wieder mit GUI anfangen
}
}
#if defined MCU && defined DEBUG_TIMES
if (timer_ms_passed_32(&log_ticks, 1000UL)) {
uint8_t cpu, uart_in, uart_out;
os_get_utilizations(&cpu, &uart_in, &uart_out);
LOG_INFO("CPU=%3u%% UART_IN=%3u%% UART_OUT=%3u%%", cpu, uart_in, uart_out);
}
#endif // MCU && DEBUG_TIMES
#ifdef PC
#ifdef CREATE_TRACEFILE_AVAILABLE
trace_add_actuators();
#endif // CREATE_TRACEFILE_AVAILABLE
/* Sim oder ATmega ueber naechsten Schleifendurchlauf / Bot-Zyklus informieren */
#ifdef ARM_LINUX_BOARD
set_bot_2_atmega();
#endif // ARM_LINUX_BOARD
command_write(CMD_DONE, SUB_CMD_NORM, simultime, 0, 0); // flusht auch den Sendepuffer
#ifdef DEBUG_TIMES
/* Zum Debuggen der Zeiten */
GETTIMEOFDAY(&init_stop, NULL);
int32_t t2 = (init_stop.tv_sec - init_start.tv_sec) * 1000000 + init_stop.tv_usec - init_start.tv_usec;
LOG_DEBUG("Done-Token (%d) out after %d usec", simultime, t2);
#endif // DEBUG_TIMES
#endif // PC
#if defined OS_AVAILABLE
/* Rest der Zeitscheibe (OS_TIME_SLICE ms) schlafen legen */
os_thread_yield();
#endif // OS_AVAILABLE
}
extern uint8_t gui_keypad_table[][5];
extern uint8_t pwmSlow[];
extern uint16_t goto_pos_err[];
extern uint16_t turn_err[];
extern char abl_eeprom_data[];
#ifdef BOOTLOADER_AVAILABLE
void bootloader_main(void);
#endif // BOOTLOADER_AVAILABLE
/**
* Faehrt den Bot sauber herunter
*/
void ctbot_shutdown(void) {
LOG_INFO("Shutting c't-Bot down...");
motor_set(BOT_SPEED_STOP, BOT_SPEED_STOP);
#ifdef MAP_AVAILABLE
map_flush_cache();
#endif
#ifdef LOG_MMC_AVAILABLE
log_flush();
#endif
#ifdef SDFAT_AVAILABLE
sdfat_c_sync_vol();
#endif
#ifdef DISPLAY_AVAILABLE
display_clear();
display_cursor(1, 1);
display_puts("SYSTEM HALTED.");
#endif // DISPLAY_AVAILABLE
#ifdef ENA_AVAILABLE
ENA_off(0xff);
#endif
ctbot_shutdown_low();
uint8_t tmp;
tmp = * (volatile uint8_t*) gui_keypad_table;
tmp = * (volatile uint8_t*) pwmSlow;
tmp = * (volatile uint8_t*) sensDistDataL;
tmp = * (volatile uint8_t*) sensDistDataR;
tmp = * (volatile uint8_t*) &bot_address;
tmp = * (volatile uint8_t*) goto_pos_err;
tmp = * (volatile uint8_t*) turn_err;
tmp = * (volatile uint8_t*) abl_eeprom_data;
(void) tmp;
#ifdef BOOTLOADER_AVAILABLE
bootloader_main();
#endif // BOOTLOADER_AVAILABLE
}