Thomas Glatzel / m3pi_ng

Dependents:   newlib PID Robot WarehouseBot1 ... more

Fork of m3pi_ng by Nikolas Goldin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m3pi_ng.h Source File

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