/
cm.h
307 lines (267 loc) · 7.85 KB
/
cm.h
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* This is the main library for the code written in the CreateSimulator.
* Written by Daryl Van Humbeck.
*
* Note that when we're compiling for embedded use, MODE_EMBEDDED is defined, while
* MODE_LOCAL is defined for when we're running on the local computer.
*/
// Always include the Open Interface library
#include "oi.h"
// Always include the standard C library
#include <stdlib.h>
// If we're compiling for the Command Module, include some AVR libraries
#ifdef MODE_EMBEDDED
#include <avr/interrupt.h>
#include <avr/io.h>
#endif
// If we're compiling for local execution, include some standard C libraries
#ifdef MODE_LOCAL
#include <stdio.h>
#endif
// Roomba IR codes:
// Remote codes
#define REMOTE_NO_BUTTON 255
#define REMOTE_LEFT 129
#define REMOTE_FORWARD 130
#define REMOTE_RIGHT 131
#define REMOTE_SPOT 132
#define REMOTE_MAX 133
#define REMOTE_SMALL 134
#define REMOTE_MEDIUM 135
#define REMOTE_LARGE 136
#define REMOTE_CLEAN 136
#define REMOTE_PAUSE 137
#define REMOTE_POWER 138
#define REMOTE_FORWARD_LEFT 139
#define REMOTE_FORWARD_RIGHT 140
#define REMOTE_STOP_DRIVING 141
// Scheduling remote codes:
#define REMOTE_SEND_ALL 142
#define REMOTE_SEEK_DOCK 143
// Home base codes:
#define HOME_BASE_RESERVED 240
#define HOME_BASE_RED 248
#define HOME_BASE_GREEN 244
#define HOME_BASE_FORCE_FIELD 242
#define HOME_BASE_RED_GREEN 252
#define HOME_BASE_RED_FORCE_FIELD 250
#define HOME_BASE_GREEN_FORCE_FIELD 246
#define HOME_BASE_RED_GREEN_FORCE_FIELD 254
// Demo codes:
#define DEMO_STOP 255
#define DEMO_COVER 0
#define DEMO_COVER_AND_DOCK 1
#define DEMO_SPOT 2
#define DEMO_MOUSE 3
#define DEMO_FIGURE8 4
#define DMEO_WIMP 5
#define DEMO_HOME 6
#define DEMO_TAG 7
#define DEMO_PACHELBEL 8
#define DEMO_BANJO 9
// Sensor codes
#define SEN_SET_0 0
#define SEN_SET_1 1
#define SEN_SET_2 2
#define SEN_SET_3 3
#define SEN_SET_4 4
#define SEN_SET_5 5
#define SEN_SET_6 6
#define SEN_BUMP_DROP 7
#define SEN_WALL 8
#define SEN_LEFT_CLIFF 9
#define SEN_FRONT_LEFT_CLIFF 10
#define SEN_FRONT_RIGHT_CLIFF 11
#define SEN_RIGHT_CLIFF 12
#define SEN_VIRTUAL_WALL 13
#define SEN_OVERCURRENT 14
#define SEN_UNUSED1 15
#define SEN_UNUSED2 16
#define SEN_IR_CODE 17
#define SEN_BUTTONS 18
#define SEN_DISTANCE 19
#define SEN_ANGLE 20
#define SEN_CHARGE_STATE 21
#define SEN_VOLTAGE 22
#define SEN_CURRENT 23
#define SEN_TEMPERATURE 24
#define SEN_CHARGE 25
#define SEN_CAPACITY 26
#define SEN_WALL_SIGNAL 27
#define SEN_LEFT_CLIFF_SIGNAL 28
#define SEN_FRONT_LEFT_CLIFF_SIGNAL 29
#define SEN_FRONT_RIGHT_CLIFF_SIGNAL 30
#define SEN_RIGHT_CLIFF_SIGNAL 31
#define SEN_DIGITAL_INPUTS 32
#define SEN_ANALOG_INPUTS 33
#define SEN_AVAILABLE_CHARGE_SOURCES 34
#define SEN_OI_MODE 35
#define SEN_SONG_NUMBER 36
#define SEN_SONG_PLAYING 37
#define SEN_NUMBER_STREAM_PACKETS 38
#define SEN_SPEED 39
#define SEN_RADIUS 40
#define SEN_RIGHT_WHEEL_SPEED 41
#define SEN_LEFT_WHEEL_SPEED 42
// Battery charge codes
#define BATTERY_NOT_CHARGING 0
#define BATTERY_RECONDITIONING_CHARGE 1
#define BATTERY_FULL_CHARGE 2
#define BATTERY_TRICKLE_CHARGE 3
#define BATTERY_WAITING 4
#define BATTERY_FAULT 5
// OI mode codes
#define OI_MODE_OFF 0
#define OI_MODE_PASSIVE 1
#define OI_MODE_SAFE 2
#define OI_MODE_FULL 3
// Generally-useful max and min values
#define MAX_UINT8 0xff
#define MIN_UINT8 0
#define MAX_INT8 0x7f
#define MIN_INT8 0x80
#define MAX_UINT16 0xffff
#define MIN_UINT16 0
#define MAX_INT16 0x7fff
#define MIN_INT16 0x8000
// Generally-useful macros
#define TO_UINT16(a,b) (((uint16_t)(a) << 8) | (b))
#define LED1Toggle (PORTD ^= LED1)
#define LED2Toggle (PORTD ^= LED2)
#define ON 1
#define OFF -1
#define UNCHANGED 0
/* ******************************* *
* CommandModule function headers: *
* ******************************* */
/**
* Initializes the CommandModule interface.
* Called automatically by the internal startup function.
*/
void cm_init(void);
/**
* Turns the Create/Roomba on.
*/
void cm_power_on(void);
/**
* Turns the Create/Roomba off.
*/
void cm_power_off(void);
/**
* Initializes the Create/Roomba's serial port.
* Called automatically by the internal startup function.
*/
void cm_start(void);
/**
* Grants full control of the robot (and all consequences) to your program.
*/
void cm_full_mode(void);
/**
* Conditionally grants full control of the robot to your program.
* If the robot enters a danger condition, the robot returns to passive mode.
*/
void cm_safe_mode(void);
/**
* Returns the robot to passive mode, where your program can query sensors,
* but not drive the robot.
* Note: This is the default mode.
*/
void cm_passive_mode(void);
/**
* Tells the robot to play the specified demo.
* See the DEMO_* constants for more information.
*/
void cm_play_demo(uint8_t demo);
/**
* Set the rate of data transfer between the robot and the Command Module.
* Not important for the most part.
* See the Baud* codes in oi.h for more information.
*/
void cm_baud_rate(uint8_t baud);
/**
* Tells the robot to run the "Cover" demo.
* Note: If the robot is a Roomba, it will clean the floor.
*/
void cm_demo_cover(void);
/**
* Tells the robot to run the "cover" demo and dock with its charging station
* when its battery gets low.
* Note: If the robot is a Roomba, it will clean the floor.
*/
void cm_demo_cover_and_dock(void);
/**
* Tells the robot to run the "spot clean" demo.
* Note: If the robot is a Roomba, it will clean the floor.
*/
void cm_demo_spot(void);
/**
* Tells the robot to start driving at the specified speed, with the specified
* driving radius to the left. Note that MAX_INT16 and MIN_INT16 have it
* drive as straight as possible, but it's still not entirely straight.
*/
void cm_drive(int16_t speed, int16_t radius);
/**
* Tells the robot to start driving by specifying the driving speed of each wheel.
*/
void cm_direct_drive(int16_t right_speed, int16_t left_speed);
/**
* Convenience function to tell the robot to stop driving.
*/
void cm_stop_driving();
/**
* Controls the LEDs on top of the robot.
* @param play Turns the Play light on if non-zero.
* @param advance Turns the Advance light on if non-zero.
* @param power_color Sets the shade of the Power light between green (at 0) and orange (at 255).
* @param power_intensity Sets the brightness of the Power light from off (at 0) to full brightness (at 255).
*/
void cm_set_leds(uint8_t play, uint8_t advance, uint8_t power_color, uint8_t power_intensity);
/**
* Sets the digital outputs on the Create's cargo bay connector.
* @param pin*: Turns the pin on if non-zero.
* Note 1: Not used in simulations.
* Note 2: Only available on Create robots. Does nothing on Roomba or Scooba robots.
*/
void cm_digital_outputs(uint8_t pin0, uint8_t pin1, uint8_t pin2);
/**
* Sends the provided byte out over IR.
*/
void cm_send_ir(uint8_t data);
/**
* Stores a song under the given number for later playback.
*/
void cm_store_song(uint8_t song_number, uint8_t song_length, const uint8_t song_notes[], const uint8_t song_lengths[]);
/**
* Plays the song stored under the given number.
*/
void cm_play_song(uint8_t song_number);
/* ************************* *
* Sensor-reading functions: *
* ************************* */
/**
* Returns 1 if the robot currently sees a wall on its right side.
* Note: There is only one wall sensor on all iRobot robots.
*/
uint8_t cm_read_wall();
/**
* Returns 1 if the robot sees a cliff on its far left cliff sensor.
*/
uint8_t cm_read_left_cliff();
/**
* Returns 1 if the robot sees a cliff on its front left cliff sensor.
*/
uint8_t cm_read_front_left_cliff();
/**
* Returns 1 if the robot sees a cliff on its front right cliff sensor.
*/
uint8_t cm_read_front_right_cliff();
/**
* Returns 1 if the robot sees a cliff on its far right cliff sensor.
*/
uint8_t cm_read_right_cliff();
/**
* Reads the byte last received from the IR sensor.
* @return The last message read by the IR sensor.
*/
uint8_t cm_read_ir();
// TODO: Set up the sensor query functions.