za
Fork of m3pi by
m3pi.h@9:08cc708cee52, 2013-03-24 (annotated)
- Committer:
- s4stevo
- Date:
- Sun Mar 24 00:15:29 2013 +0000
- Revision:
- 9:08cc708cee52
- Parent:
- 8:4b7d6ea9b35b
- Child:
- 10:30aaa3c1910a
Rev A
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s4stevo | 9:08cc708cee52 | 1 | |
chris | 0:e6020bd04b45 | 2 | |
chris | 0:e6020bd04b45 | 3 | #ifndef M3PI_H |
chris | 0:e6020bd04b45 | 4 | #define M3PI_H |
chris | 0:e6020bd04b45 | 5 | |
s4stevo | 9:08cc708cee52 | 6 | |
chris | 0:e6020bd04b45 | 7 | #include "mbed.h" |
chris | 0:e6020bd04b45 | 8 | #include "platform.h" |
chris | 0:e6020bd04b45 | 9 | |
chris | 0:e6020bd04b45 | 10 | #ifdef MBED_RPC |
chris | 0:e6020bd04b45 | 11 | #include "rpc.h" |
chris | 0:e6020bd04b45 | 12 | #endif |
chris | 0:e6020bd04b45 | 13 | |
chris | 0:e6020bd04b45 | 14 | #define SEND_SIGNATURE 0x81 |
chris | 0:e6020bd04b45 | 15 | #define SEND_RAW_SENSOR_VALUES 0x86 |
chris | 0:e6020bd04b45 | 16 | #define SEND_TRIMPOT 0xB0 |
chris | 0:e6020bd04b45 | 17 | #define SEND_BATTERY_MILLIVOLTS 0xB1 |
chris | 0:e6020bd04b45 | 18 | #define DO_PLAY 0xB3 |
chris | 0:e6020bd04b45 | 19 | #define PI_CALIBRATE 0xB4 |
chris | 0:e6020bd04b45 | 20 | #define DO_CLEAR 0xB7 |
chris | 0:e6020bd04b45 | 21 | #define DO_PRINT 0xB8 |
chris | 0:e6020bd04b45 | 22 | #define DO_LCD_GOTO_XY 0xB9 |
chris | 0:e6020bd04b45 | 23 | #define LINE_SENSORS_RESET_CALIBRATION 0xB5 |
chris | 0:e6020bd04b45 | 24 | #define SEND_LINE_POSITION 0xB6 |
chris | 0:e6020bd04b45 | 25 | #define AUTO_CALIBRATE 0xBA |
chris | 0:e6020bd04b45 | 26 | #define SET_PID 0xBB |
chris | 0:e6020bd04b45 | 27 | #define STOP_PID 0xBC |
chris | 0:e6020bd04b45 | 28 | #define M1_FORWARD 0xC1 |
chris | 0:e6020bd04b45 | 29 | #define M1_BACKWARD 0xC2 |
chris | 0:e6020bd04b45 | 30 | #define M2_FORWARD 0xC5 |
chris | 0:e6020bd04b45 | 31 | #define M2_BACKWARD 0xC6 |
chris | 0:e6020bd04b45 | 32 | |
chris | 0:e6020bd04b45 | 33 | |
chris | 0:e6020bd04b45 | 34 | |
s4stevo | 9:08cc708cee52 | 35 | |
chris | 0:e6020bd04b45 | 36 | |
s4stevo | 9:08cc708cee52 | 37 | |
chris | 0:e6020bd04b45 | 38 | class m3pi : public Stream { |
chris | 0:e6020bd04b45 | 39 | |
chris | 0:e6020bd04b45 | 40 | // Public functions |
chris | 0:e6020bd04b45 | 41 | public: |
chris | 0:e6020bd04b45 | 42 | |
chris | 0:e6020bd04b45 | 43 | /** Create the m3pi object connected to the default pins |
chris | 0:e6020bd04b45 | 44 | * |
chris | 7:9b128cebb3c2 | 45 | * @param nrst GPIO pin used for reset. Default is p23 |
chris | 0:e6020bd04b45 | 46 | * @param tx Serial transmit pin. Default is p9 |
chris | 0:e6020bd04b45 | 47 | * @param rx Serial receive pin. Default is p10 |
chris | 0:e6020bd04b45 | 48 | */ |
chris | 7:9b128cebb3c2 | 49 | m3pi(); |
chris | 7:9b128cebb3c2 | 50 | |
chris | 7:9b128cebb3c2 | 51 | |
chris | 8:4b7d6ea9b35b | 52 | /** Create the m3pi object connected to specific pins |
chris | 7:9b128cebb3c2 | 53 | * |
chris | 7:9b128cebb3c2 | 54 | */ |
chris | 0:e6020bd04b45 | 55 | m3pi(PinName nrst, PinName tx, PinName rx); |
chris | 0:e6020bd04b45 | 56 | |
chris | 0:e6020bd04b45 | 57 | |
chris | 7:9b128cebb3c2 | 58 | |
chris | 0:e6020bd04b45 | 59 | /** Force a hardware reset of the 3pi |
chris | 0:e6020bd04b45 | 60 | */ |
chris | 0:e6020bd04b45 | 61 | void reset (void); |
chris | 0:e6020bd04b45 | 62 | |
chris | 0:e6020bd04b45 | 63 | /** Directly control the speed and direction of the left motor |
chris | 0:e6020bd04b45 | 64 | * |
chris | 0:e6020bd04b45 | 65 | * @param speed A normalised number -1.0 - 1.0 represents the full range. |
chris | 0:e6020bd04b45 | 66 | */ |
chris | 0:e6020bd04b45 | 67 | void left_motor (float speed); |
chris | 0:e6020bd04b45 | 68 | |
chris | 0:e6020bd04b45 | 69 | /** Directly control the speed and direction of the right motor |
chris | 0:e6020bd04b45 | 70 | * |
chris | 0:e6020bd04b45 | 71 | * @param speed A normalised number -1.0 - 1.0 represents the full range. |
chris | 0:e6020bd04b45 | 72 | */ |
chris | 0:e6020bd04b45 | 73 | void right_motor (float speed); |
chris | 0:e6020bd04b45 | 74 | |
chris | 0:e6020bd04b45 | 75 | /** Drive both motors forward as the same speed |
chris | 0:e6020bd04b45 | 76 | * |
chris | 0:e6020bd04b45 | 77 | * @param speed A normalised number 0 - 1.0 represents the full range. |
chris | 0:e6020bd04b45 | 78 | */ |
chris | 0:e6020bd04b45 | 79 | void forward (float speed); |
chris | 0:e6020bd04b45 | 80 | |
chris | 0:e6020bd04b45 | 81 | /** Drive both motors backward as the same speed |
chris | 0:e6020bd04b45 | 82 | * |
chris | 0:e6020bd04b45 | 83 | * @param speed A normalised number 0 - 1.0 represents the full range. |
chris | 0:e6020bd04b45 | 84 | */ |
chris | 0:e6020bd04b45 | 85 | void backward (float speed); |
chris | 0:e6020bd04b45 | 86 | |
chris | 0:e6020bd04b45 | 87 | /** Drive left motor backwards and right motor forwards at the same speed to turn on the spot |
chris | 0:e6020bd04b45 | 88 | * |
chris | 0:e6020bd04b45 | 89 | * @param speed A normalised number 0 - 1.0 represents the full range. |
chris | 0:e6020bd04b45 | 90 | */ |
chris | 0:e6020bd04b45 | 91 | void left (float speed); |
chris | 0:e6020bd04b45 | 92 | |
chris | 0:e6020bd04b45 | 93 | /** Drive left motor forward and right motor backwards at the same speed to turn on the spot |
chris | 0:e6020bd04b45 | 94 | * @param speed A normalised number 0 - 1.0 represents the full range. |
chris | 0:e6020bd04b45 | 95 | */ |
chris | 0:e6020bd04b45 | 96 | void right (float speed); |
chris | 0:e6020bd04b45 | 97 | |
chris | 0:e6020bd04b45 | 98 | /** Stop both motors |
chris | 0:e6020bd04b45 | 99 | * |
chris | 0:e6020bd04b45 | 100 | */ |
chris | 0:e6020bd04b45 | 101 | void stop (void); |
chris | 0:e6020bd04b45 | 102 | |
chris | 0:e6020bd04b45 | 103 | /** Read the voltage of the potentiometer on the 3pi |
chris | 0:e6020bd04b45 | 104 | * @returns voltage as a float |
chris | 0:e6020bd04b45 | 105 | * |
chris | 0:e6020bd04b45 | 106 | */ |
s4stevo | 9:08cc708cee52 | 107 | //float pot_voltage(void); |
chris | 0:e6020bd04b45 | 108 | |
chris | 0:e6020bd04b45 | 109 | /** Read the battery voltage on the 3pi |
chris | 0:e6020bd04b45 | 110 | * @returns battery voltage as a float |
chris | 0:e6020bd04b45 | 111 | */ |
chris | 0:e6020bd04b45 | 112 | float battery(void); |
chris | 0:e6020bd04b45 | 113 | |
chris | 0:e6020bd04b45 | 114 | /** Read the position of the detected line |
chris | 0:e6020bd04b45 | 115 | * @returns position as A normalised number -1.0 - 1.0 represents the full range. |
chris | 7:9b128cebb3c2 | 116 | * -1.0 means line is on the left, or the line has been lost |
chris | 7:9b128cebb3c2 | 117 | * 0.0 means the line is in the middle |
chris | 7:9b128cebb3c2 | 118 | * 1.0 means the line is on the right |
chris | 0:e6020bd04b45 | 119 | */ |
s4stevo | 9:08cc708cee52 | 120 | float position (void); |
chris | 0:e6020bd04b45 | 121 | |
chris | 0:e6020bd04b45 | 122 | |
chris | 7:9b128cebb3c2 | 123 | /** Calibrate the sensors. This turns the robot left then right, looking for a line |
chris | 0:e6020bd04b45 | 124 | * |
chris | 0:e6020bd04b45 | 125 | */ |
s4stevo | 9:08cc708cee52 | 126 | //char sensor_auto_calibrate (void); |
chris | 0:e6020bd04b45 | 127 | |
chris | 0:e6020bd04b45 | 128 | /** Set calibration manually to the current settings. |
chris | 0:e6020bd04b45 | 129 | * |
chris | 0:e6020bd04b45 | 130 | */ |
s4stevo | 9:08cc708cee52 | 131 | //void calibrate(void); |
chris | 0:e6020bd04b45 | 132 | |
chris | 0:e6020bd04b45 | 133 | /** Clear the current calibration settings |
chris | 0:e6020bd04b45 | 134 | * |
chris | 0:e6020bd04b45 | 135 | */ |
s4stevo | 9:08cc708cee52 | 136 | //void reset_calibration (void); |
chris | 0:e6020bd04b45 | 137 | |
chris | 1:816a80dcc1a3 | 138 | void PID_start(int max_speed, int a, int b, int c, int d); |
chris | 1:816a80dcc1a3 | 139 | |
chris | 1:816a80dcc1a3 | 140 | void PID_stop(); |
chris | 1:816a80dcc1a3 | 141 | |
chris | 5:09fb0636207b | 142 | /** Write to the 8 LEDs |
chris | 5:09fb0636207b | 143 | * |
chris | 5:09fb0636207b | 144 | * @param leds An 8 bit value to put on the LEDs |
chris | 5:09fb0636207b | 145 | */ |
chris | 5:09fb0636207b | 146 | void leds(int val); |
chris | 5:09fb0636207b | 147 | |
chris | 0:e6020bd04b45 | 148 | /** Locate the cursor on the 8x2 LCD |
chris | 0:e6020bd04b45 | 149 | * |
chris | 0:e6020bd04b45 | 150 | * @param x The horizontal position, from 0 to 7 |
chris | 0:e6020bd04b45 | 151 | * @param y The vertical position, from 0 to 1 |
chris | 0:e6020bd04b45 | 152 | */ |
chris | 0:e6020bd04b45 | 153 | void locate(int x, int y); |
chris | 0:e6020bd04b45 | 154 | |
s4stevo | 9:08cc708cee52 | 155 | |
s4stevo | 9:08cc708cee52 | 156 | |
chris | 0:e6020bd04b45 | 157 | /** Clear the LCD |
chris | 0:e6020bd04b45 | 158 | * |
chris | 0:e6020bd04b45 | 159 | */ |
chris | 0:e6020bd04b45 | 160 | void cls(void); |
chris | 0:e6020bd04b45 | 161 | |
chris | 0:e6020bd04b45 | 162 | /** Send a character directly to the 3pi serial interface |
chris | 0:e6020bd04b45 | 163 | * @param c The character to send to the 3pi |
chris | 0:e6020bd04b45 | 164 | */ |
chris | 0:e6020bd04b45 | 165 | int putc(int c); |
chris | 0:e6020bd04b45 | 166 | |
chris | 0:e6020bd04b45 | 167 | /** Receive a character directly to the 3pi serial interface |
chris | 0:e6020bd04b45 | 168 | * @returns c The character received from the 3pi |
chris | 0:e6020bd04b45 | 169 | */ |
chris | 0:e6020bd04b45 | 170 | int getc(); |
chris | 0:e6020bd04b45 | 171 | |
chris | 0:e6020bd04b45 | 172 | /** Send a string buffer to the 3pi serial interface |
chris | 0:e6020bd04b45 | 173 | * @param text A pointer to a char array |
chris | 0:e6020bd04b45 | 174 | * @param int The character to send to the 3pi |
chris | 0:e6020bd04b45 | 175 | */ |
chris | 0:e6020bd04b45 | 176 | int print(char* text, int length); |
chris | 0:e6020bd04b45 | 177 | |
chris | 0:e6020bd04b45 | 178 | #ifdef MBED_RPC |
chris | 0:e6020bd04b45 | 179 | virtual const struct rpc_method *get_rpc_methods(); |
chris | 0:e6020bd04b45 | 180 | #endif |
chris | 0:e6020bd04b45 | 181 | |
chris | 0:e6020bd04b45 | 182 | private : |
chris | 0:e6020bd04b45 | 183 | |
chris | 0:e6020bd04b45 | 184 | DigitalOut _nrst; |
chris | 0:e6020bd04b45 | 185 | Serial _ser; |
chris | 4:54c673c71fc0 | 186 | |
chris | 0:e6020bd04b45 | 187 | void motor (int motor, float speed); |
chris | 0:e6020bd04b45 | 188 | virtual int _putc(int c); |
chris | 0:e6020bd04b45 | 189 | virtual int _getc(); |
chris | 0:e6020bd04b45 | 190 | |
chris | 0:e6020bd04b45 | 191 | }; |
chris | 0:e6020bd04b45 | 192 | |
chris | 0:e6020bd04b45 | 193 | #endif |