Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
m3pi.h
00001 /* mbed m3pi Library 00002 * Copyright (c) 2007-2010 cstyles 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 #ifndef M3PI_H 00024 #define M3PI_H 00025 00026 #include "mbed.h" 00027 #include "platform.h" 00028 00029 #ifdef MBED_RPC 00030 #include "rpc.h" 00031 #endif 00032 00033 #define SEND_SIGNATURE 0x81 00034 #define SEND_RAW_SENSOR_VALUES 0x86 00035 #define SEND_TRIMPOT 0xB0 00036 #define SEND_BATTERY_MILLIVOLTS 0xB1 00037 #define DO_PLAY 0xB3 00038 #define PI_CALIBRATE 0xB4 00039 #define DO_CLEAR 0xB7 00040 #define DO_PRINT 0xB8 00041 #define DO_LCD_GOTO_XY 0xB9 00042 #define LINE_SENSORS_RESET_CALIBRATION 0xB5 00043 #define SEND_LINE_POSITION 0xB6 00044 #define AUTO_CALIBRATE 0xBA 00045 #define SET_PID 0xBB 00046 #define STOP_PID 0xBC 00047 #define M1_FORWARD 0xC1 00048 #define M1_BACKWARD 0xC2 00049 #define M2_FORWARD 0xC5 00050 #define M2_BACKWARD 0xC6 00051 00052 00053 00054 /** m3pi control class 00055 * 00056 * Example: 00057 * @code 00058 * // Drive the m3pi forward, turn left, back, turn right, at half speed for half a second 00059 00060 #include "mbed.h" 00061 #include "m3pi.h" 00062 00063 m3pi pi; 00064 00065 int main() { 00066 00067 wait(0.5); 00068 00069 pi.forward(0.5); 00070 wait (0.5); 00071 pi.left(0.5); 00072 wait (0.5); 00073 pi.backward(0.5); 00074 wait (0.5); 00075 pi.right(0.5); 00076 wait (0.5); 00077 00078 pi.stop(); 00079 00080 } 00081 * @endcode 00082 */ 00083 class m3pi : public Stream { 00084 00085 // Public functions 00086 public: 00087 00088 /** Create the m3pi object connected to the default pins 00089 * 00090 * @param nrst GPIO pin used for reset. Default is p23 00091 * @param tx Serial transmit pin. Default is p9 00092 * @param rx Serial receive pin. Default is p10 00093 */ 00094 m3pi(); 00095 00096 00097 /** Create the m3pi object connected to specific pins 00098 * 00099 */ 00100 m3pi(PinName nrst, PinName tx, PinName rx); 00101 00102 00103 00104 /** Force a hardware reset of the 3pi 00105 */ 00106 void reset (void); 00107 00108 /** Directly control the speed and direction of the left motor 00109 * 00110 * @param speed A normalised number -1.0 - 1.0 represents the full range. 00111 */ 00112 void left_motor (float speed); 00113 00114 /** Directly control the speed and direction of the right motor 00115 * 00116 * @param speed A normalised number -1.0 - 1.0 represents the full range. 00117 */ 00118 void right_motor (float speed); 00119 00120 /** Drive both motors forward as the same speed 00121 * 00122 * @param speed A normalised number 0 - 1.0 represents the full range. 00123 */ 00124 void forward (float speed); 00125 00126 /** Drive both motors backward as the same speed 00127 * 00128 * @param speed A normalised number 0 - 1.0 represents the full range. 00129 */ 00130 void backward (float speed); 00131 00132 /** Drive left motor backwards and right motor forwards at the same speed to turn on the spot 00133 * 00134 * @param speed A normalised number 0 - 1.0 represents the full range. 00135 */ 00136 void left (float speed); 00137 00138 /** Drive left motor forward and right motor backwards at the same speed to turn on the spot 00139 * @param speed A normalised number 0 - 1.0 represents the full range. 00140 */ 00141 void right (float speed); 00142 00143 /** Stop both motors 00144 * 00145 */ 00146 void stop (void); 00147 00148 /** Read the voltage of the potentiometer on the 3pi 00149 * @returns voltage as a float 00150 * 00151 */ 00152 float pot_voltage(void); 00153 00154 /** Read the battery voltage on the 3pi 00155 * @returns battery voltage as a float 00156 */ 00157 float battery(void); 00158 00159 /** Read the position of the detected line 00160 * @returns position as A normalised number -1.0 - 1.0 represents the full range. 00161 * -1.0 means line is on the left, or the line has been lost 00162 * 0.0 means the line is in the middle 00163 * 1.0 means the line is on the right 00164 */ 00165 float line_position (void); 00166 00167 00168 /** Read line sensors 00169 * @returns sensor values as an array of floats 00170 */ 00171 void sensor_reading(int*); 00172 00173 00174 /** Calibrate the sensors. This turns the robot left then right, looking for a line 00175 * 00176 */ 00177 char sensor_auto_calibrate (void); 00178 00179 /** Set calibration manually to the current settings. 00180 * 00181 */ 00182 void calibrate(void); 00183 00184 /** Clear the current calibration settings 00185 * 00186 */ 00187 void reset_calibration (void); 00188 00189 void PID_start(int max_speed, int a, int b, int c, int d); 00190 00191 void PID_stop(); 00192 00193 /** Write to the 8 LEDs 00194 * 00195 * @param leds An 8 bit value to put on the LEDs 00196 */ 00197 void leds(int val); 00198 00199 /** Locate the cursor on the 8x2 LCD 00200 * 00201 * @param x The horizontal position, from 0 to 7 00202 * @param y The vertical position, from 0 to 1 00203 */ 00204 void locate(int x, int y); 00205 00206 /** Clear the LCD 00207 * 00208 */ 00209 void cls(void); 00210 00211 /** Send a character directly to the 3pi serial interface 00212 * @param c The character to send to the 3pi 00213 */ 00214 int putc(int c); 00215 00216 /** Receive a character directly to the 3pi serial interface 00217 * @returns c The character received from the 3pi 00218 */ 00219 int getc(); 00220 00221 /** Send a string buffer to the 3pi serial interface 00222 * @param text A pointer to a char array 00223 * @param int The character to send to the 3pi 00224 */ 00225 int print(char* text, int length); 00226 00227 #ifdef MBED_RPC 00228 virtual const struct rpc_method *get_rpc_methods(); 00229 #endif 00230 00231 private : 00232 00233 DigitalOut _nrst; 00234 Serial _ser; 00235 00236 void motor (int motor, float speed); 00237 virtual int _putc(int c); 00238 virtual int _getc(); 00239 00240 }; 00241 00242 #endif
Generated on Fri Jul 15 2022 20:45:56 by
1.7.2