summaryrefslogtreecommitdiffstats
path: root/source/ct-Bot/include/command.h
blob: 7696635b463ab69fc70152325b97d8f728a319f2 (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
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
/*
 * c't-Sim - Robotersimulator fuer den 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 	command.h
 * @brief 	Kommando-Management
 * @author 	Benjamin Benz (bbe@heise.de)
 * @date 	20.12.05
*/

#ifndef __command_h_
#define __command_h_ 

#include "global.h"
#include "ct-Bot.h"

#define MAX_PAYLOAD 255  /*!< Max. Anzahl Bytes, die an ein Command angehaengt werden */

#ifdef PC
	#define low_read tcp_read 	/*!< Which function to use to read data */
	#define low_write tcp_send_cmd /*!< Which function to use to write data */
	#define low_write_data tcp_write /*!< Which function to use to write data */
#endif

#ifdef MCU
	#define low_read uart_read 	/*!< Which function to use to read data */
	#define low_write uart_send_cmd /*!< Which function to use to write data */
//	#define low_write uart_send_cmd /*!< Which function to use to write data */
	#define low_write_data uart_write /*!< Which function to use to write data */
#endif


/*!
 * Request Teil eines Kommandos
 */
typedef struct {
	uint8 command:8;	/*!< command */
	uint8 subcommand:7;	/*!< subcommand */
	uint8 direction:1;	/*!< 0 ist Anfrage, 1 ist Antwort */
#ifndef DOXYGEN
	} __attribute__ ((packed)) request_t; // Keine Luecken in der Struktur lassen
#else
	} request_t; // Keine Luecken in der Struktur lassen
#endif

/*!
 * Kommando
 */
typedef struct {
	uint8 startCode;	/*!< Markiert den Beginn eines Commands */
	request_t request; 			/*!< Command-ID */
	uint8  payload;		/*!< Bytes, die dem Kommando noch folgen*/
	int16 data_l;				/*!< Daten zum Kommando links*/
	int16 data_r;				/*!< Daten zum Kommando rechts*/
	int16 seq;					/*!< Paket-Sequenznummer*/
	uint8 CRC;			/*!< Markiert das Ende des Commands*/
#ifndef DOXYGEN
	} __attribute__ ((packed)) command_t;// Keine Luecken in der Struktur lassen
#else
	} command_t;
#endif

#define CMD_STARTCODE	'>'		/*!< Anfang eines Kommandos*/
#define CMD_STOPCODE	'<'		/*!< Ende eines Kommandos*/

//Sensoren
#define CMD_SENS_IR	'I'		/*!< Abstandssensoren*/
#define CMD_SENS_ENC	'E'		/*!< Radencoder*/
#define CMD_SENS_BORDER 'B'	/*!< Abgrundsensoren*/
#define CMD_SENS_LINE 	'L'		/*!< Liniensensoren*/
#define CMD_SENS_LDR 	'H'		/*!< Helligkeitssensoren */
#define CMD_SENS_TRANS	'T'		/*!< Ueberwachung Transportfach */
#define CMD_SENS_DOOR	'D'		/*!< Ueberwachung Klappe */
#define CMD_SENS_MOUSE	'm'		/*!< Maussensor */
#define CMD_SENS_ERROR 'e'		/*!< Motor- oder Batteriefehler */
#define CMD_SENS_RC5 	'R'		/*!< IR-Fernbedienung */

#define CMD_SENS_MOUSE_PICTURE	'P'		/*!< Bild vom Maussensor in data_l steht, welche Nummer da 1. Pixel hat*/


// Aktuatoren
#define CMD_AKT_MOT	'M'		/*!< Motorgeschwindigkeit */
#define CMD_AKT_DOOR	'd'		/*!< Steuerung Klappe */
#define CMD_AKT_SERVO	'S'		/*!< Steuerung Servo  */
#define CMD_AKT_LED	'l'		/*!< LEDs steuern */
#define CMD_AKT_LCD    'c'     /*!< LCD Anzeige */

#define CMD_DONE    'X'     	/*!< Markiert das Ende einer Uebertragung */


#define SUB_CMD_NORM	'N' 		/*!< Standard-Kommando */
#define SUB_CMD_LEFT	'L' 		/*!< Kommmando fuer links */
#define SUB_CMD_RIGHT	'R' 		/*!< Kommando fuer rechts */


// Subcommandos fuer LCD
#define SUB_LCD_CLEAR   'c'     /*!< Subkommando Clear Screen */
#define SUB_LCD_DATA    'D'     /*!< Subkommando Text ohne Cursor */
#define SUB_LCD_CURSOR  'C'     /*!< Subkommando Cursorkoordinaten */    

// Log-Ausgaben
#define CMD_LOG			'O'		/*!< Logausgaben */

//Kommandos fuer die Verbindung zum c't-Sim
#define CMD_WELCOME		'W'		/*!< Kommado zum anmelden an c't-Sim */
#define SUB_WELCOME_REAL	'R'		/*!< Subkommado zum anmelden eine realen Bots an c't-Sim */
#define SUB_WELCOME_SIM	'S'		/*!< Subkommado zum anmelden eines simulierten Bots an c't-Sim */

//Kommandos fuer die Remote-Calls
#define CMD_REMOTE_CALL		'r'		/*!< Kommado fuer Remote-Calls */
#define SUB_REMOTE_CALL_LIST	'L'		/*!< Anforderung an den Bot alle verfuegbaren Kommandos zu listen */
#define SUB_REMOTE_CALL_ENTRY	'E'		/*!< Hiermit leifert der Bot ein erfuegbares Kommandos an den PC */
#define SUB_REMOTE_CALL_ORDER	'O'		/*!< Hiermit gibt der PC einen Remote-call in Auftrag */
#define SUB_REMOTE_CALL_DONE	'D'		/*!< Hiermit signalisiert der MCU dem PC die beendigung des Auftrags. Ergebins steht in DataL 0=FAIL 1=SUCCESS */



#define DIR_REQUEST	0			/*!< Richtung fuer Anfragen */
#define DIR_ANSWER		1			/*!< Richtung fuer Antworten */

#ifdef PC	// Auf dem PC muss der Zugriff auf received_command Thread-sicher sein
	#include <pthread.h>
	extern pthread_mutex_t     command_mutex;	/*!< Zugriff auf das Kommando */
	#define	command_lock()		pthread_mutex_lock(&command_mutex) /*!< Zugriff auf das Kommando */
	#define	command_unlock()	pthread_mutex_unlock(&command_mutex) /*!< Zugriff auf das Kommando */
#endif


extern command_t received_command;		/*!< Puffer fuer Kommandos */

/*!
 * Liest ein Kommando ein, ist blockierend!
 * greift auf low_read() zurueck
 * @see low_read()
 */
int8 command_read(void);	

/*!
 * Uebertraegt ein Kommando und wartet nicht auf eine Antwort
 * @param command Kennung zum Command
 * @param subcommand Kennung des Subcommand
 * @param data_l Daten fuer den linken Kanal
 * @param data_r Daten fuer den rechten Kanal
 * @param payload Anzahl der Bytes, die diesem Kommando als Payload folgen
 */
void command_write(uint8 command, uint8 subcommand, int16* data_l,int16* data_r,uint8 payload);

/*!
 *  Gibt dem Simulator Daten mit Anhang und wartet nicht auf Antwort
 * @param command Kennung zum Command
 * @param subcommand Kennung des Subcommand
 * @param data_l Daten fuer den linken Kanal
 * @param data_r Daten fuer den rechten Kanal
 * @param data Datenanhang an das eigentliche Command
 */
void command_write_data(uint8 command, uint8 subcommand, int16* data_l, int16* data_r, const char* data);

/*!
 * Gibt dem Simulator Daten mit Anhang und wartet nicht auf Antwort
 * @param command Kennung zum Command
 * @param subcommand Kennung des Subcommand
 * @param data_l Daten fuer den linken Kanal
 * @param data_r Daten fuer den rechten Kanal
 * @param payload Anzahl der Bytes im Anhang
 * @param data Datenanhang an das eigentliche Command
 */
void command_write_rawdata(uint8 command, uint8 subcommand, int16* data_l, int16* data_r, uint8 payload, uint8* data);

/*!
 * Wertet das Kommando im Puffer aus
 * return 1, wenn Kommando schon bearbeitet wurde, 0 sonst
 */
int command_evaluate(void);

/*! 
 * Gibt ein Kommando auf dem Bildschirm aus
 */
void command_display(command_t * command);

#endif