-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3pi.h
224 lines (183 loc) · 6.05 KB
/
m3pi.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
/* mbed m3pi Library
* Copyright (c) 2007-2010 cstyles
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef M3PI_H
#define M3PI_H
#include "lpc_types.h"
#define SEND_SIGNATURE 0x81
#define SEND_RAW_SENSOR_VALUES 0x86
#define SEND_CALIBRATED_SENSOR_VALUES 0x87
#define SEND_TRIMPOT 0xB0
#define SEND_BATTERY_MILLIVOLTS 0xB1
#define DO_PLAY 0xB3
#define PI_CALIBRATE 0xB4
#define DO_CLEAR 0xB7
#define DO_PRINT 0xB8
#define DO_LCD_GOTO_XY 0xB9
#define LINE_SENSORS_RESET_CALIBRATION 0xB5
#define SEND_LINE_POSITION 0xB6
#define AUTO_CALIBRATE 0xBA
#define SET_PID 0xBB
#define STOP_PID 0xBC
#define M1_FORWARD 0xC1
#define M1_BACKWARD 0xC2
#define M2_FORWARD 0xC5
#define M2_BACKWARD 0xC6
/** m3pi control class
*
* Example:
* @code
* // Drive the m3pi forward, turn left, back, turn right, at half speed for half a second
#include "mbed.h"
#include "m3pi.h"
m3pi pi;
int main() {
_ser.putc(DO_PRINT);
_ser.putc(0x1);
_ser.putc(c);
wait(0.5);
pi.forward(0.5);
wait (0.5);
pi.left(0.5);
wait (0.5);
pi.backward(0.5);
wait (0.5);
pi.right(0.5);
wait (0.5);
pi.stop();
}
* @endcode
*/
/** Force a hardware reset of the 3pi
*/
// IMPLEMENT WHEN NEEDED
//void reset (void);
/** Directly control the speed and direction of the left motor
*
* @param speed A normalised number -1.0 - 1.0 represents the full range.
*/
void left_motor (int8_t speed);
/** Directly control the speed and direction of the right motor
*
* @param speed A normalised number -1.0 - 1.0 represents the full range.
*/
void right_motor (int8_t speed);
/** Drive both motors forward as the same speed
*
* @param speed A normalised number 0 - 1.0 represents the full range.
*/
void forward (int8_t speed);
/** Drive both motors forward as the same speed for a specified distance
*
* @param speed A value from 0-127.
*/
void forwardSD (uint8_t speed);
/** Drive both motors backward as the same speed
*
* @param speed A normalised number 0 - 1.0 represents the full range.
*/
void backward (int8_t speed);
/** Drive left motor backwards and right motor forwards at the same speed to turn on the spot
*
* @param speed A normalised number 0 - 1.0 represents the full range.
*/
void left (int8_t speed);
/** Drive left motor forward and right motor backwards at the same speed to turn on the spot
* @param speed A normalised number 0 - 1.0 represents the full range.
*/
void right (int8_t speed);
/** Stop both motors
*
*/
void stop (void);
/** Start single motor
*
*/
void motor (int motor, int8_t speed);
/** Read the voltage of the potentiometer on the 3pi
* @returns voltage as a float
*
*/
float pot_voltage(void);
/** Read the battery voltage on the 3pi
* @returns battery voltage as a float
*/
float battery(void);
/** Read the position of the detected line
* @returns position as A normalised number -1.0 - 1.0 represents the full range.
* -1.0 means line is on the left, or the line has been lost
* 0.0 means the line is in the middle
* 1.0 means the line is on the right
*/
uint16_t line_position ();
/** Read the position of the detected line
* @param Array to be populated with sensor values. Assumes 5 sensors.
* @returns position as A normalised number 0 - 4000 represents the full range.
* 0 means line is on the left, or the line has been lost
* 2000 means the line is in the middle
* 4000 means the line is on the right
*/
uint16_t sensors_line_position(uint16_t* sensors);
/** Calibrate the sensors. This turns the robot left then right, looking for a line
*
*/
char sensor_auto_calibrate (void);
/** Set calibration manually to the current settings.
*
*/
void calibrate(void);
/** Clear the current calibration settings
*
*/
void reset_calibration (void);
void PID_start(int max_speed, int a, int b, int c, int d);
void PID_stop();
/** Write to the 8 LEDs
*
* @param leds An 8 bit value to put on the LEDs
*/
// IMPLEMENT WHEN NEEDED
//void leds(int val);
void play_sound(uint8_t length, unsigned char *play);
/** Locate the cursor on the 8x2 LCD
*
* @param x The horizontal position, from 0 to 7
* @param y The vertical position, from 0 to 1
*/
void locate(int x, int y);
/** Clear the LCD
*
*/
void cls(void);
/** Send a character directly to the 3pi serial interface
* @param c The character to send to the 3pi
*/
int _putc(int c);
/** Receive a character directly to the 3pi serial interface
* @returns c The character received from the 3pi
*/
int _getc();
/** Send a string buffer to the 3pi serial interface
* @param text A pointer to a char array
* @param int The character to send to the 3pi
*/
int print(char* text, int length);
#endif