Allows the M3Pi to be used as a Sumo robot, using the sharp 100 distance sensors on the front. Run away strategy

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m3pi.h Source File

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_CALIBRATED_SENSOR_VALUES 0x87
00036 #define SEND_TRIMPOT 0xB0
00037 #define SEND_BATTERY_MILLIVOLTS 0xB1
00038 #define DO_PLAY 0xB3
00039 #define PI_CALIBRATE 0xB4
00040 #define DO_CLEAR 0xB7
00041 #define DO_PRINT 0xB8
00042 #define DO_LCD_GOTO_XY 0xB9
00043 #define LINE_SENSORS_RESET_CALIBRATION 0xB5
00044 #define SEND_LINE_POSITION 0xB6
00045 #define AUTO_CALIBRATE 0xBA
00046 #define SET_PID 0xBB
00047 #define STOP_PID 0xBC
00048 #define M1_FORWARD 0xC1
00049 #define M1_BACKWARD 0xC2
00050 #define M2_FORWARD 0xC5
00051 #define M2_BACKWARD 0xC6
00052 
00053 
00054 #define LINE_THRESHOLD 100
00055 
00056 
00057 /** m3pi control class
00058  *
00059  * Example:
00060  * @code
00061  * // Drive the m3pi forward, turn left, back, turn right, at half speed for half a second
00062 
00063    #include "mbed.h"
00064    #include "m3pi.h"
00065 
00066    m3pi pi;
00067 
00068    int main() {
00069 
00070      wait(0.5);
00071 
00072      pi.forward(0.5);
00073      wait (0.5);
00074      pi.left(0.5);
00075      wait (0.5);
00076      pi.backward(0.5);
00077      wait (0.5);
00078      pi.right(0.5);
00079      wait (0.5);
00080 
00081      pi.stop();
00082 
00083  }
00084  * @endcode
00085  */
00086 class m3pi :  public Stream {
00087 
00088     // Public functions
00089 public:
00090 
00091     /** Create the m3pi object connected to the default pins
00092      *
00093      */
00094     m3pi();
00095 
00096 
00097     
00098      
00099      /** Create the m3pi object connected to the spesific pins
00100      *
00101      * @param nrst GPIO pin used for reset. Default is p23
00102      * @param tx Serial transmit pin. Default is p9
00103      * @param rx Serial receive pin. Default is p10
00104      */
00105     m3pi(PinName nrst, PinName tx, PinName rx);
00106 
00107 
00108 
00109     /** Force a hardware reset of the 3pi
00110      */
00111     void reset (void);
00112 
00113     /** Directly control the speed and direction of the left motor
00114      *
00115      * @param speed A normalised number -1.0 - 1.0 represents the full range.
00116      */
00117     void left_motor (float speed);
00118 
00119     /** Directly control the speed and direction of the right motor
00120      *
00121      * @param speed A normalised number -1.0 - 1.0 represents the full range.
00122      */
00123     void right_motor (float speed);
00124 
00125     /** Drive both motors forward as the same speed
00126      *
00127      * @param speed A normalised number 0 - 1.0 represents the full range.
00128      */
00129     void forward (float speed);
00130 
00131     /** Drive both motors backward as the same speed
00132      *
00133      * @param speed A normalised number 0 - 1.0 represents the full range.
00134      */
00135     void backward (float speed);
00136 
00137     /** Drive left motor backwards and right motor forwards at the same speed to turn on the spot
00138      *
00139      * @param speed A normalised number 0 - 1.0 represents the full range.
00140      */
00141     void left (float speed);
00142 
00143     /** Drive left motor forward and right motor backwards at the same speed to turn on the spot
00144      * @param speed A normalised number 0 - 1.0 represents the full range.
00145      */
00146     void right (float speed);
00147 
00148     /** Stop both motors
00149      *
00150      */
00151     void stop (void);
00152 
00153     /** Read the voltage of the potentiometer on the 3pi
00154      * @returns voltage as a float
00155      *
00156      */
00157     float pot_voltage(void);
00158 
00159     /** Read the battery voltage on the 3pi
00160      * @returns battery voltage as a float
00161      */
00162     float battery(void);
00163 
00164     /** Read the position of the detected line
00165      * @returns position as A normalised number -1.0 - 1.0 represents the full range.
00166      *  -1.0 means line is on the left, or the line has been lost
00167      *   0.0 means the line is in the middle
00168      *   1.0 means the line is on the right
00169      */
00170     float line_position (void);
00171     
00172     /** Get the raw sensor values
00173      *
00174      * @param int* A pointer to a interger array
00175      */
00176     void get_raw_sensors(int* values);
00177     
00178     /** Get the calibrated sensor values
00179      *
00180      * @param int* A pointer to an float array
00181      */
00182     void get_calibrated_sensors(float* values);
00183     
00184     /** Records current levels as white, values used to detect a line
00185      *
00186      */
00187     void get_white_levels();
00188     
00189     /** Gets the line state
00190      *
00191      * @returns The line status: 0 means no line, -1 means line at back two sensors, 1 means line at front sensors
00192      */
00193     
00194     int is_line();
00195 
00196 
00197     /** Calibrate the sensors. This turns the robot left then right, looking for a line
00198      *
00199      */
00200     char sensor_auto_calibrate (void);
00201 
00202     /** Set calibration manually to the current settings.
00203      *
00204      */
00205     void calibrate(void);
00206 
00207     /** Clear the current calibration settings
00208      *
00209      */
00210     void reset_calibration (void);
00211 
00212     void PID_start(int max_speed, int a, int b, int c, int d);
00213 
00214     void PID_stop();
00215 
00216     /** Write to the 8 LEDs
00217      *
00218      * @param leds An 8 bit value to put on the LEDs
00219      */
00220     void leds(int val);
00221 
00222     /** Locate the cursor on the 8x2 LCD
00223      *
00224      * @param x The horizontal position, from 0 to 7
00225      * @param y The vertical position, from 0 to 1
00226      */
00227     void locate(int x, int y);
00228 
00229     /** Clear the LCD
00230      *
00231      */
00232     void cls(void);
00233 
00234     /** Send a character directly to the 3pi serial interface
00235      * @param c The character to send to the 3pi
00236      */
00237     int putc(int c);
00238 
00239     /** Receive a character directly to the 3pi serial interface
00240      * @returns c The character received from the 3pi
00241      */
00242     int getc();
00243 
00244     /** Send a string buffer to the 3pi serial interface
00245      * @param text A pointer to a char array
00246      * @param int The character to send to the 3pi
00247      */
00248     int print(char* text, int length);
00249     
00250     
00251     
00252     
00253 #ifdef MBED_RPC
00254     virtual const struct rpc_method *get_rpc_methods();
00255 #endif
00256 
00257 private :
00258 
00259     DigitalOut _nrst;
00260     Serial _ser;
00261     
00262     void motor (int motor, float speed);
00263     virtual int _putc(int c);
00264     virtual int _getc();
00265     
00266     int raw_white_levels[5];
00267 
00268 };
00269 
00270 #endif