Ward Elder / m3pi_V6_Ward_copy
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m3pi.h Source File

m3pi.h

00001 #ifndef M3PI_H
00002 #define M3PI_H
00003 
00004 #include "mbed.h"
00005 
00006 /** m3pi Class
00007 @brief Library to control m3pi robot from Polulu running the serial slave code
00008 @brief Revision 1.0
00009 @author Craig A. Evans <C.A.Evans@leeds.ac.uk>
00010 @date   April 2017
00011 
00012 */
00013 class m3pi
00014 {
00015 
00016 public:
00017 
00018     /** m3pi constructor
00019     */
00020     m3pi();
00021     /** m3pi destructor
00022     */
00023     ~m3pi();
00024 
00025 
00026     /** Initialisation function
00027     * @details Should be called at the start of the program to interrupt demo
00028     * @details code and start 3pi in serial slave mode
00029     */
00030     void init();
00031     
00032     /** Causes the robot to read the IR sensors
00033     * @details is an alterative to get_*_values methods if you don't need the values themselves
00034     */
00035     void scan();
00036 
00037     /** Get signature of slave firmware
00038     * @param signature - array of size 7 to store signature
00039     */
00040     void get_signature(char *signature);
00041 
00042     /** Read raw sensor values from IR sensors (0 - 2000)
00043     * @param values - array of size 5 to store values
00044     */
00045     void get_raw_values(unsigned int *values);
00046 
00047     /** Read calibrated sensor values from IR sensors (0 - 1000)
00048     * @param values - array of size 5 to store values
00049     */
00050     void get_calibrated_values(unsigned int *values);
00051 
00052     /** Read user potentiometer values (0.0 - 1.0)
00053     * @returns float in range  0.0 to 1.0
00054     */
00055     float get_trimpot_value();
00056 
00057     /** Read battery voltage
00058     * @returns battery voltage in volts
00059     */
00060     float get_battery_voltage();
00061 
00062     /** Play music
00063     * @param notes - const array containing Pololu notes
00064     * @param length - number of notes in array
00065     */
00066     void play_music(const char notes[],int length);
00067 
00068     /** Calibrate
00069     * @details manual calibration of sensors. Should be called repeatedly.
00070     */
00071     void calibrate();
00072 
00073     /** Reset previous calibration values
00074     */
00075     void reset_calibration();
00076 
00077     /** Returns estimate of normalised line position
00078     * @returns float in the range -1.0 to 1.0
00079     * @details -1.0 for line under sensor 0, -0.5 for line under sensor 1
00080     * @details 0.0 for line under sensor 2, 0.5 for line under sensor 3 etc.
00081     */
00082     float get_line_position();
00083     
00084     /** Returns an estimate of the normalised line position
00085     * @returns float in the range -1.0 to 1.0
00086     * @details uses the values of sensor stored in the class - must call scan() first!
00087     * @details -1.0 for line under sensor 0, -0.5 for line under sensor 1
00088     * @details 0.0 for line under sensor 2, 0.5 for line under sensor 3 etc.
00089     */
00090     float read_line(); 
00091 
00092     /** Clear LCD
00093     */
00094     void lcd_clear();
00095 
00096     /** Print text on LCD
00097     * @param text[] - string of max size 8
00098     * @param - length of string
00099     */
00100     void lcd_print(char text[],int length);
00101 
00102     /** Move cursor on LCD
00103     * @param x - x position
00104     * @param y - line number
00105     */
00106     void lcd_goto_xy(int x, int y);
00107 
00108     /** Call automatic calibration function
00109     */
00110     void auto_calibrate();
00111 
00112     /** Move left motor
00113     * @param speed - value in range -1.0 (full-speed backward) to 1.0 (full-speed forward)
00114     */
00115     void left_motor(float speed);
00116 
00117     /** Move right motor
00118     * @param speed - value in range -1.0 (full-speed backward) to 1.0 (full-speed forward)
00119     */
00120     void right_motor(float speed);
00121 
00122     /** Move both motors
00123     * @param left speed - value in range -1.0 (full-speed backward) to 1.0 (full-speed forward)
00124     * @param right speed - value in range -1.0 (full-speed backward) to 1.0 (full-speed forward)
00125     */
00126     void motors(float left_speed,float right_speed);
00127 
00128     /** Stop both motors
00129     */
00130     void stop();
00131 
00132     /** Move buggy forward
00133     * @param speed - value in range 0.0 to 1.0 (full-speed forward)
00134     */
00135     void forward(float speed);
00136 
00137     /** Reverse buggy
00138     * @param speed - value in range 0.0 to 1.0 (full-speed forward)
00139     */
00140     void reverse(float speed);
00141 
00142     /** Spin right
00143     * @param speed - value in range 0.0 to 1.0
00144     */
00145     void spin_right(float speed);
00146 
00147     /** Spin left
00148     * @param speed - value in range 0.0 to 1.0
00149     */
00150     void spin_left(float speed);
00151 
00152     /** Display battery voltage on LCD
00153     * @param x - x position on LCD to display
00154     * @param y - line on LCD to display
00155     */
00156     void display_battery_voltage(int x,int y);
00157 
00158     /** Display slave firmware signature on LCD
00159     * @param x - x position on LCD to display
00160     * @param y - line on LCD to display
00161     */
00162     void display_signature(int x,int y);
00163     
00164     /** Display line position and bar graph of sensor readings on LCD
00165     * @details must call scan() and read_line() first
00166     */
00167     void display_data();
00168     
00169     /** Display sensor values on LCD
00170     * @param values - array of calibrated sensor values
00171     * @param y - line on LCD to display
00172     */
00173     void display_sensor_values(unsigned int values[],int y);
00174     
00175     /** Gets value of sensor array expressed as decimal number
00176     * @details Each sensor is a bit (PC4 is bit 0, PC0 is bit 5)
00177     * @returns value - 5-bit value of sensor reading
00178     */
00179     unsigned int get_sensor_array_value(unsigned int values[]);
00180     
00181     /** Calculates an estimate of normalised line position from the sensor readings
00182     * @returns float in the range -1.0 to 1.0
00183     * @details -1.0 for line under sensor 0, -0.5 for line under sensor 1
00184     * @details 0.0 for line under sensor 2, 0.5 for line under sensor 3 etc.
00185     */
00186     float calc_line_position(unsigned int values[]);
00187 
00188 
00189 private:
00190     BufferedSerial* _serial;
00191     DigitalOut* _reset;
00192     
00193     float _last_line_position;
00194     
00195     char _bar_graph[7];
00196     unsigned int _values[5];
00197 
00198     void reset();
00199 
00200 };
00201 
00202 
00203 
00204 #endif