V0.5 library for the Pi Swarm robot
piswarm.h@5:09d535cb9e9d, 2014-02-02 (annotated)
- Committer:
- jah128
- Date:
- Sun Feb 02 22:43:22 2014 +0000
- Revision:
- 5:09d535cb9e9d
- Parent:
- 4:52b3e4c5a425
- Child:
- 6:69a48b1ec3af
Add comments to piswarm.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 4:52b3e4c5a425 | 1 | /******************************************************************************************* |
jah128 | 4:52b3e4c5a425 | 2 | * |
jah128 | 4:52b3e4c5a425 | 3 | * University of York Robot Lab Pi Swarm Robot Library |
jah128 | 0:9ffe8ebd1c40 | 4 | * |
jah128 | 0:9ffe8ebd1c40 | 5 | * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York |
jah128 | 0:9ffe8ebd1c40 | 6 | * |
jah128 | 4:52b3e4c5a425 | 7 | * Version 0.5 February 2014 |
jah128 | 0:9ffe8ebd1c40 | 8 | * |
jah128 | 0:9ffe8ebd1c40 | 9 | * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2 |
jah128 | 0:9ffe8ebd1c40 | 10 | * |
jah128 | 4:52b3e4c5a425 | 11 | ******************************************************************************************/ |
jah128 | 0:9ffe8ebd1c40 | 12 | |
jah128 | 0:9ffe8ebd1c40 | 13 | #ifndef PISWARM_H |
jah128 | 0:9ffe8ebd1c40 | 14 | #define PISWARM_H |
jah128 | 0:9ffe8ebd1c40 | 15 | |
jah128 | 0:9ffe8ebd1c40 | 16 | #include "mbed.h" |
jah128 | 0:9ffe8ebd1c40 | 17 | #include "platform.h" |
jah128 | 0:9ffe8ebd1c40 | 18 | #include "alpha433.h" |
jah128 | 0:9ffe8ebd1c40 | 19 | #include "main.h" |
jah128 | 0:9ffe8ebd1c40 | 20 | |
jah128 | 0:9ffe8ebd1c40 | 21 | #ifdef MBED_RPC |
jah128 | 0:9ffe8ebd1c40 | 22 | #include "rpc.h" |
jah128 | 0:9ffe8ebd1c40 | 23 | #endif |
jah128 | 0:9ffe8ebd1c40 | 24 | |
jah128 | 5:09d535cb9e9d | 25 | // The following defines are user parameters and may be edited |
jah128 | 5:09d535cb9e9d | 26 | // SWARM_SIZE - size of swarm (+1). Max 32. |
jah128 | 5:09d535cb9e9d | 27 | // USE_COMMUNICATION_STACK - 1 to enable, 0 to bypass |
jah128 | 5:09d535cb9e9d | 28 | // CALIBRATE_COLOURS - 1 to increase blue intensity, 0 to not |
jah128 | 5:09d535cb9e9d | 29 | // CENTER_LED_BRIGHTNESS - Range 0 to 100 |
jah128 | 5:09d535cb9e9d | 30 | // OUTER_LED_BRIGHTNESS - Range 0 to 100 |
jah128 | 5:09d535cb9e9d | 31 | // PC_BAUD - PC Serial Baudrate. Max 115200 |
jah128 | 5:09d535cb9e9d | 32 | // RF_ALLOW_COMMANDS - 1 to allow remote RF commands, 0 to block |
jah128 | 5:09d535cb9e9d | 33 | // RF_USE_LEDS - 1 to use MBED LEDs to display RF status |
jah128 | 5:09d535cb9e9d | 34 | // RF_USE_TDMA - 1 to use time-delayed responses to broadcast messages |
jah128 | 5:09d535cb9e9d | 35 | // RF_TDMA_TIME_PERIOD_US 15625 - Period of delay to use (per ID) in us. |
jah128 | 5:09d535cb9e9d | 36 | // PISWARM_DEBUG - 1 to send general Pi Swarm debug information over USB serial interface |
jah128 | 5:09d535cb9e9d | 37 | // RF_DEBUG - 1 to send communication stack debug information over USB serial interface |
jah128 | 5:09d535cb9e9d | 38 | // RF_VERBOSE - 1 to send verbose RF debug information over USB serial interface |
jah128 | 5:09d535cb9e9d | 39 | // START_RADIO_ON_BOOT - 1 to enable RF transceiver and comms stack on boot |
jah128 | 5:09d535cb9e9d | 40 | // RF_FREQUENCY - Frequency of RF chip. See datasheet for details (default 435000000) |
jah128 | 5:09d535cb9e9d | 41 | // RF_DATARATE - Baud rate of the RF chip. Max 115200, default 57600. |
jah128 | 5:09d535cb9e9d | 42 | |
jah128 | 5:09d535cb9e9d | 43 | #define SWARM_SIZE 32 |
jah128 | 5:09d535cb9e9d | 44 | #define USE_COMMUNICATION_STACK 1 |
jah128 | 5:09d535cb9e9d | 45 | #define CALIBRATE_COLOURS 1 |
jah128 | 5:09d535cb9e9d | 46 | #define CENTER_LED_BRIGHTNESS 30 |
jah128 | 5:09d535cb9e9d | 47 | #define OUTER_LED_BRIGHTNESS 50 |
jah128 | 5:09d535cb9e9d | 48 | #define PC_BAUD 115200 |
jah128 | 5:09d535cb9e9d | 49 | #define RF_ALLOW_COMMANDS 1 |
jah128 | 5:09d535cb9e9d | 50 | #define RF_USE_LEDS 1 |
jah128 | 5:09d535cb9e9d | 51 | #define RF_USE_TDMA 1 |
jah128 | 5:09d535cb9e9d | 52 | #define RF_TDMA_TIME_PERIOD_US 15625 |
jah128 | 5:09d535cb9e9d | 53 | #define PISWARM_DEBUG 1 |
jah128 | 5:09d535cb9e9d | 54 | #define RF_DEBUG 1 |
jah128 | 5:09d535cb9e9d | 55 | #define RF_VERBOSE 1 |
jah128 | 5:09d535cb9e9d | 56 | #define START_RADIO_ON_BOOT 1 |
jah128 | 5:09d535cb9e9d | 57 | #define RF_FREQUENCY 435000000 |
jah128 | 5:09d535cb9e9d | 58 | #define RF_DATARATE 57600 |
jah128 | 5:09d535cb9e9d | 59 | |
jah128 | 5:09d535cb9e9d | 60 | // The remaining defines give the addresses of hardware and 3-Pi instructions etc. and should not be changed |
jah128 | 0:9ffe8ebd1c40 | 61 | #define SEND_SIGNATURE 0x81 |
jah128 | 0:9ffe8ebd1c40 | 62 | #define SEND_RAW_SENSOR_VALUES 0x86 |
jah128 | 0:9ffe8ebd1c40 | 63 | #define SEND_TRIMPOT 0xB0 |
jah128 | 0:9ffe8ebd1c40 | 64 | #define SEND_BATTERY_MILLIVOLTS 0xB1 |
jah128 | 0:9ffe8ebd1c40 | 65 | #define DO_PLAY 0xB3 |
jah128 | 0:9ffe8ebd1c40 | 66 | #define PI_CALIBRATE 0xB4 |
jah128 | 0:9ffe8ebd1c40 | 67 | #define DO_CLEAR 0xB7 |
jah128 | 0:9ffe8ebd1c40 | 68 | #define DO_PRINT 0xB8 |
jah128 | 0:9ffe8ebd1c40 | 69 | #define DO_LCD_GOTO_XY 0xB9 |
jah128 | 0:9ffe8ebd1c40 | 70 | #define LINE_SENSORS_RESET_CALIBRATION 0xB5 |
jah128 | 0:9ffe8ebd1c40 | 71 | #define SEND_LINE_POSITION 0xB6 |
jah128 | 0:9ffe8ebd1c40 | 72 | #define AUTO_CALIBRATE 0xBA |
jah128 | 0:9ffe8ebd1c40 | 73 | #define SET_PID 0xBB |
jah128 | 0:9ffe8ebd1c40 | 74 | #define STOP_PID 0xBC |
jah128 | 0:9ffe8ebd1c40 | 75 | #define M1_FORWARD 0xC1 |
jah128 | 0:9ffe8ebd1c40 | 76 | #define M1_BACKWARD 0xC2 |
jah128 | 0:9ffe8ebd1c40 | 77 | #define M2_FORWARD 0xC5 |
jah128 | 0:9ffe8ebd1c40 | 78 | #define M2_BACKWARD 0xC6 |
jah128 | 0:9ffe8ebd1c40 | 79 | #define EXPANSION_IC_ADDRESS 0x40 |
jah128 | 0:9ffe8ebd1c40 | 80 | #define MAGNETOMETER_ADDRESS 0x1C |
jah128 | 0:9ffe8ebd1c40 | 81 | #define ADC_ADDRESS 0x46 |
jah128 | 0:9ffe8ebd1c40 | 82 | #define EEPROM_ADDRESS 0XA0 |
jah128 | 0:9ffe8ebd1c40 | 83 | |
jah128 | 0:9ffe8ebd1c40 | 84 | class PiSwarm : public Stream { |
jah128 | 0:9ffe8ebd1c40 | 85 | |
jah128 | 0:9ffe8ebd1c40 | 86 | // Public functions |
jah128 | 0:9ffe8ebd1c40 | 87 | public: |
jah128 | 0:9ffe8ebd1c40 | 88 | |
jah128 | 1:b067a08ff54e | 89 | // Create the Pi Swarm object connected to the default pins [see end of file] |
jah128 | 0:9ffe8ebd1c40 | 90 | PiSwarm(); |
jah128 | 4:52b3e4c5a425 | 91 | |
jah128 | 4:52b3e4c5a425 | 92 | |
jah128 | 4:52b3e4c5a425 | 93 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 94 | // LED Functions |
jah128 | 4:52b3e4c5a425 | 95 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 0:9ffe8ebd1c40 | 96 | |
jah128 | 0:9ffe8ebd1c40 | 97 | // Returns the RGB Colour of the Outer LED as an int (r<<16 g<<8 b) |
jah128 | 0:9ffe8ebd1c40 | 98 | int get_oled_colour(void); |
jah128 | 0:9ffe8ebd1c40 | 99 | |
jah128 | 1:b067a08ff54e | 100 | // Returns the enable state of an single outer led |
jah128 | 1:b067a08ff54e | 101 | char get_oled_state(char oled); |
jah128 | 1:b067a08ff54e | 102 | |
jah128 | 4:52b3e4c5a425 | 103 | // Set colour of outer LEDs |
jah128 | 4:52b3e4c5a425 | 104 | void set_oled_colour ( char red, char green, char blue ); |
jah128 | 0:9ffe8ebd1c40 | 105 | |
jah128 | 0:9ffe8ebd1c40 | 106 | // Toggle the state of a single outer LED without affected the others |
jah128 | 0:9ffe8ebd1c40 | 107 | void set_oled(char oled, char value); |
jah128 | 4:52b3e4c5a425 | 108 | |
jah128 | 0:9ffe8ebd1c40 | 109 | // Toggle the states of all 10 outer LEDs |
jah128 | 0:9ffe8ebd1c40 | 110 | void set_oleds(char oled_0, char oled_1, char oled_2, char oled_3, char oled_4, char oled_5, char oled_6, char oled_7, char oled_8, char oled_9); |
jah128 | 0:9ffe8ebd1c40 | 111 | |
jah128 | 4:52b3e4c5a425 | 112 | // Disable all the LEDs and the dedicated power supply |
jah128 | 4:52b3e4c5a425 | 113 | void turn_off_all_oleds(void); |
jah128 | 4:52b3e4c5a425 | 114 | |
jah128 | 4:52b3e4c5a425 | 115 | // Set brightness of outer LEDs (works by increasing total period for PWM) |
jah128 | 4:52b3e4c5a425 | 116 | void set_oled_brightness ( char brightness ); |
jah128 | 4:52b3e4c5a425 | 117 | |
jah128 | 4:52b3e4c5a425 | 118 | // Sends the message to the GPIO expander to update which OLEDs are active |
jah128 | 0:9ffe8ebd1c40 | 119 | void activate_oleds(void); |
jah128 | 0:9ffe8ebd1c40 | 120 | |
jah128 | 4:52b3e4c5a425 | 121 | // Returns the RGB Colour of the Center LED as an int (r<<16 g<<8 b) |
jah128 | 4:52b3e4c5a425 | 122 | int get_cled_colour(void); |
jah128 | 4:52b3e4c5a425 | 123 | |
jah128 | 4:52b3e4c5a425 | 124 | // Returns the enable state of the center LED |
jah128 | 4:52b3e4c5a425 | 125 | char get_cled_state(void); |
jah128 | 0:9ffe8ebd1c40 | 126 | |
jah128 | 0:9ffe8ebd1c40 | 127 | // Set colour of central LED |
jah128 | 0:9ffe8ebd1c40 | 128 | void set_cled_colour ( char red, char green, char blue ); |
jah128 | 4:52b3e4c5a425 | 129 | |
jah128 | 4:52b3e4c5a425 | 130 | // Enable or disable the center LED |
jah128 | 4:52b3e4c5a425 | 131 | void enable_cled ( char enable ); |
jah128 | 0:9ffe8ebd1c40 | 132 | |
jah128 | 0:9ffe8ebd1c40 | 133 | // Set brightness of central LED (works by increasing total period for PWM) |
jah128 | 0:9ffe8ebd1c40 | 134 | void set_cled_brightness ( char brightness ); |
jah128 | 0:9ffe8ebd1c40 | 135 | |
jah128 | 4:52b3e4c5a425 | 136 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 137 | // IR Functions |
jah128 | 4:52b3e4c5a425 | 138 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 139 | |
jah128 | 4:52b3e4c5a425 | 140 | // Returns an approximate distance of any reflected obstacle in front of the given IR sensor (in millimeters, range 0 - 100) |
jah128 | 0:9ffe8ebd1c40 | 141 | float read_reflected_ir_distance ( char index ); |
jah128 | 0:9ffe8ebd1c40 | 142 | |
jah128 | 3:4c0f2f3de33e | 143 | // Returns the illuminated raw ADC value for a given IR sensor (500us pulse) |
jah128 | 3:4c0f2f3de33e | 144 | unsigned short read_illuminated_raw_ir_value ( char index ); |
jah128 | 3:4c0f2f3de33e | 145 | |
jah128 | 0:9ffe8ebd1c40 | 146 | // Returns the raw ADC value for a given IR sensor |
jah128 | 0:9ffe8ebd1c40 | 147 | unsigned short read_adc_value( char index ); |
jah128 | 0:9ffe8ebd1c40 | 148 | |
jah128 | 4:52b3e4c5a425 | 149 | // Turns on or off the LDO outputs based on the current enable variables (enable_ir_ldo and enable_led_ldo) |
jah128 | 4:52b3e4c5a425 | 150 | void enable_ldo_outputs ( void ); |
jah128 | 4:52b3e4c5a425 | 151 | |
jah128 | 4:52b3e4c5a425 | 152 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 153 | // Sensor Functions |
jah128 | 4:52b3e4c5a425 | 154 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 155 | |
jah128 | 4:52b3e4c5a425 | 156 | // Read the gyroscope yaw value. Return value is degrees per second clockwise. |
jah128 | 4:52b3e4c5a425 | 157 | float read_gyro ( void ); |
jah128 | 4:52b3e4c5a425 | 158 | |
jah128 | 4:52b3e4c5a425 | 159 | // Read the X value from the accelerometer. Return value is mps2. |
jah128 | 4:52b3e4c5a425 | 160 | float read_accelerometer_x ( void ); |
jah128 | 4:52b3e4c5a425 | 161 | |
jah128 | 4:52b3e4c5a425 | 162 | // Read the Y value from the accelerometer. Return value is mps2. |
jah128 | 4:52b3e4c5a425 | 163 | float read_accelerometer_y ( void ); |
jah128 | 4:52b3e4c5a425 | 164 | |
jah128 | 4:52b3e4c5a425 | 165 | // Read the Z value from the accelerometer. Return value is mps2. Generally this will have g against it. |
jah128 | 4:52b3e4c5a425 | 166 | float read_accelerometer_z ( void ); |
jah128 | 4:52b3e4c5a425 | 167 | |
jah128 | 0:9ffe8ebd1c40 | 168 | // Read the I2C magnetometer and store values locally: use before get_magnetometer_x etc... |
jah128 | 0:9ffe8ebd1c40 | 169 | char read_magnetometer ( void ); |
jah128 | 0:9ffe8ebd1c40 | 170 | |
jah128 | 0:9ffe8ebd1c40 | 171 | // Return the magnetometer x value stored on last call of read_magnetometer() |
jah128 | 0:9ffe8ebd1c40 | 172 | signed short get_magnetometer_x ( void ); |
jah128 | 0:9ffe8ebd1c40 | 173 | |
jah128 | 0:9ffe8ebd1c40 | 174 | // Return the magnetometer y value stored on last call of read_magnetometer() |
jah128 | 0:9ffe8ebd1c40 | 175 | signed short get_magnetometer_y ( void ); |
jah128 | 0:9ffe8ebd1c40 | 176 | |
jah128 | 0:9ffe8ebd1c40 | 177 | // Return the magnetometer z value stored on last call of read_magnetometer() |
jah128 | 0:9ffe8ebd1c40 | 178 | signed short get_magnetometer_z ( void ); |
jah128 | 0:9ffe8ebd1c40 | 179 | |
jah128 | 4:52b3e4c5a425 | 180 | // Read temperature from the temperature IC; return is temperature in degrees Celsius |
jah128 | 4:52b3e4c5a425 | 181 | float read_temperature ( void ); |
jah128 | 4:52b3e4c5a425 | 182 | |
jah128 | 0:9ffe8ebd1c40 | 183 | // Read the value from the light sensor. This will return a value ranging from 0 to a peak of around 100 in direct, bright light. |
jah128 | 0:9ffe8ebd1c40 | 184 | float read_light_sensor ( void ); |
jah128 | 0:9ffe8ebd1c40 | 185 | |
jah128 | 4:52b3e4c5a425 | 186 | // Read the raw line sensors into a 5-int array (range 0-2000) |
jah128 | 4:52b3e4c5a425 | 187 | void read_raw_sensors ( int * raw_ls_array ); |
jah128 | 4:52b3e4c5a425 | 188 | |
jah128 | 4:52b3e4c5a425 | 189 | // Returns the uptime of the system in seconds |
jah128 | 4:52b3e4c5a425 | 190 | float get_uptime (void); |
jah128 | 4:52b3e4c5a425 | 191 | |
jah128 | 4:52b3e4c5a425 | 192 | // Returns the battery voltage |
jah128 | 4:52b3e4c5a425 | 193 | float battery(void); |
jah128 | 4:52b3e4c5a425 | 194 | |
jah128 | 4:52b3e4c5a425 | 195 | // Read the voltage of the user potentiometer on the 3-pi |
jah128 | 4:52b3e4c5a425 | 196 | float pot_voltage(void); |
jah128 | 4:52b3e4c5a425 | 197 | |
jah128 | 0:9ffe8ebd1c40 | 198 | // Read the robot ID set using the DIP switches (these are read during setup and not updated). |
jah128 | 0:9ffe8ebd1c40 | 199 | char get_id ( void ); |
jah128 | 0:9ffe8ebd1c40 | 200 | |
jah128 | 0:9ffe8ebd1c40 | 201 | // Read the switches value from the last call of the ISR |
jah128 | 0:9ffe8ebd1c40 | 202 | char get_switches ( void ); |
jah128 | 0:9ffe8ebd1c40 | 203 | |
jah128 | 4:52b3e4c5a425 | 204 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 205 | // Motor and Actuator Functions |
jah128 | 4:52b3e4c5a425 | 206 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 207 | |
jah128 | 4:52b3e4c5a425 | 208 | // Returns the last speed value set for the left motor |
jah128 | 4:52b3e4c5a425 | 209 | float get_left_motor (void); |
jah128 | 4:52b3e4c5a425 | 210 | |
jah128 | 4:52b3e4c5a425 | 211 | // Returns the last speed value set for the right motor |
jah128 | 4:52b3e4c5a425 | 212 | float get_right_motor (void); |
jah128 | 4:52b3e4c5a425 | 213 | |
jah128 | 4:52b3e4c5a425 | 214 | // Directly control the speed and direction of the left motor (range -1.0 to 1.0) |
jah128 | 4:52b3e4c5a425 | 215 | void left_motor (float speed); |
jah128 | 4:52b3e4c5a425 | 216 | |
jah128 | 4:52b3e4c5a425 | 217 | // Directly control the speed and direction of the right motor (range -1.0 to 1.0) |
jah128 | 4:52b3e4c5a425 | 218 | void right_motor (float speed); |
jah128 | 4:52b3e4c5a425 | 219 | |
jah128 | 4:52b3e4c5a425 | 220 | // Drive both motors forward as the same speed (range -1.0 to 1.0) |
jah128 | 4:52b3e4c5a425 | 221 | void forward (float speed); |
jah128 | 4:52b3e4c5a425 | 222 | |
jah128 | 4:52b3e4c5a425 | 223 | // Drive both motors backward as the same speed (range -1.0 to 1.0) |
jah128 | 4:52b3e4c5a425 | 224 | void backward (float speed); |
jah128 | 4:52b3e4c5a425 | 225 | |
jah128 | 4:52b3e4c5a425 | 226 | // Drive left motor backwards and right motor forwards at the same speed to turn on the spot (range -1.0 to 1.0) |
jah128 | 4:52b3e4c5a425 | 227 | void left (float speed); |
jah128 | 4:52b3e4c5a425 | 228 | |
jah128 | 4:52b3e4c5a425 | 229 | // Drive left motor forward and right motor backwards at the same speed to turn on the spot (range -1.0 to 1.0) |
jah128 | 4:52b3e4c5a425 | 230 | void right (float speed); |
jah128 | 4:52b3e4c5a425 | 231 | |
jah128 | 4:52b3e4c5a425 | 232 | // Stop both motors |
jah128 | 4:52b3e4c5a425 | 233 | void stop (void); |
jah128 | 0:9ffe8ebd1c40 | 234 | |
jah128 | 4:52b3e4c5a425 | 235 | // Play a tune on the 3-pi buzzer (see notes at end of file for more details) |
jah128 | 4:52b3e4c5a425 | 236 | void play_tune (char * tune, int length); |
jah128 | 4:52b3e4c5a425 | 237 | |
jah128 | 4:52b3e4c5a425 | 238 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 239 | // EEPROM Functions |
jah128 | 4:52b3e4c5a425 | 240 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 241 | |
jah128 | 0:9ffe8ebd1c40 | 242 | // Write single byte to EEPROM |
jah128 | 0:9ffe8ebd1c40 | 243 | void write_eeprom_byte ( int address, char data ); |
jah128 | 0:9ffe8ebd1c40 | 244 | |
jah128 | 0:9ffe8ebd1c40 | 245 | // Write (length) bytes to EEPROM |
jah128 | 0:9ffe8ebd1c40 | 246 | void write_eeprom_block ( int address, char length, char * data); |
jah128 | 0:9ffe8ebd1c40 | 247 | |
jah128 | 0:9ffe8ebd1c40 | 248 | // Read single byte from EEPROM |
jah128 | 0:9ffe8ebd1c40 | 249 | char read_eeprom_byte ( int address ); |
jah128 | 0:9ffe8ebd1c40 | 250 | |
jah128 | 0:9ffe8ebd1c40 | 251 | // Read next byte from EEPROM at current address |
jah128 | 0:9ffe8ebd1c40 | 252 | char read_next_eeprom_byte ( void ); |
jah128 | 0:9ffe8ebd1c40 | 253 | |
jah128 | 0:9ffe8ebd1c40 | 254 | // Read (length) from EEPROM |
jah128 | 0:9ffe8ebd1c40 | 255 | char read_eeprom_block ( int address, char length ); |
jah128 | 4:52b3e4c5a425 | 256 | |
jah128 | 4:52b3e4c5a425 | 257 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 258 | // Display Functions |
jah128 | 4:52b3e4c5a425 | 259 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 260 | |
jah128 | 4:52b3e4c5a425 | 261 | // Locate the cursor on the 8x2 LCD [to position x (0-7), y (0-1)] |
jah128 | 4:52b3e4c5a425 | 262 | void locate(int x, int y); |
jah128 | 0:9ffe8ebd1c40 | 263 | |
jah128 | 4:52b3e4c5a425 | 264 | // Clear the LCD |
jah128 | 4:52b3e4c5a425 | 265 | void cls(void); |
jah128 | 4:52b3e4c5a425 | 266 | |
jah128 | 4:52b3e4c5a425 | 267 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 268 | // Setup and Internal Functions |
jah128 | 4:52b3e4c5a425 | 269 | // Generally these functions should not be called by user code |
jah128 | 4:52b3e4c5a425 | 270 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 271 | |
jah128 | 4:52b3e4c5a425 | 272 | // Send a message of given length over the RF interface |
jah128 | 4:52b3e4c5a425 | 273 | void send_rf_message(char* message, char length); |
jah128 | 4:52b3e4c5a425 | 274 | |
jah128 | 4:52b3e4c5a425 | 275 | // The default setup routine: called when the Pi Swarm is created |
jah128 | 4:52b3e4c5a425 | 276 | void setup ( void ); |
jah128 | 4:52b3e4c5a425 | 277 | |
jah128 | 4:52b3e4c5a425 | 278 | // Sets up the IO expansion IC using I2C commands; returns a zero if successful |
jah128 | 4:52b3e4c5a425 | 279 | int setup_expansion_ic ( void ); |
jah128 | 4:52b3e4c5a425 | 280 | |
jah128 | 4:52b3e4c5a425 | 281 | // Called after a delay from the IO expansion interrupt; delay helps debounce switches |
jah128 | 4:52b3e4c5a425 | 282 | void interrupt_timeout_event ( void ); |
jah128 | 0:9ffe8ebd1c40 | 283 | |
jah128 | 4:52b3e4c5a425 | 284 | // The interrupt handler for the IO expansion IC; calls the timeout event to debounce switches |
jah128 | 4:52b3e4c5a425 | 285 | void expansion_interrupt_handler ( void ); |
jah128 | 4:52b3e4c5a425 | 286 | |
jah128 | 4:52b3e4c5a425 | 287 | // Turns off interrupt LED after a delay to indicate switch has been pressed |
jah128 | 4:52b3e4c5a425 | 288 | void led_timeout_event ( void ); |
jah128 | 4:52b3e4c5a425 | 289 | |
jah128 | 4:52b3e4c5a425 | 290 | // Routine to calibrate gyro; by default run in setup, robot should be stationary to avoid errors (Returns 1 if successful, 0 if failed) |
jah128 | 4:52b3e4c5a425 | 291 | char calibrate_gyro ( void ); |
jah128 | 4:52b3e4c5a425 | 292 | |
jah128 | 4:52b3e4c5a425 | 293 | // Routine to calibrate accelerometer, by default run after successful gyro calibration, robot should be flat and stationary to avoid errors (Returns 1 if successful, 0 if failed) |
jah128 | 4:52b3e4c5a425 | 294 | char calibrate_accelerometer ( void ); |
jah128 | 0:9ffe8ebd1c40 | 295 | |
jah128 | 4:52b3e4c5a425 | 296 | // Routine to calibrate magnetometer (Returns 1 if successful, 0 if failed) |
jah128 | 4:52b3e4c5a425 | 297 | char calibrate_magnetometer ( void ); |
jah128 | 4:52b3e4c5a425 | 298 | |
jah128 | 4:52b3e4c5a425 | 299 | // Send instructions to the I2C ADC (for IR measurement) |
jah128 | 4:52b3e4c5a425 | 300 | char setup_adc( void ); |
jah128 | 4:52b3e4c5a425 | 301 | |
jah128 | 4:52b3e4c5a425 | 302 | // Routine to initialise radio |
jah128 | 4:52b3e4c5a425 | 303 | void setup_radio( void ); |
jah128 | 4:52b3e4c5a425 | 304 | |
jah128 | 4:52b3e4c5a425 | 305 | // Blinks the calibration LED during calibration (turns off if calibration successful) |
jah128 | 4:52b3e4c5a425 | 306 | void calibrate_ticker_routine ( void ); |
jah128 | 0:9ffe8ebd1c40 | 307 | |
jah128 | 4:52b3e4c5a425 | 308 | // Used to debug/test the OLED array |
jah128 | 4:52b3e4c5a425 | 309 | void test_oled(void); |
jah128 | 4:52b3e4c5a425 | 310 | |
jah128 | 4:52b3e4c5a425 | 311 | // Starts the system timer |
jah128 | 4:52b3e4c5a425 | 312 | void start_system_timer(void); |
jah128 | 4:52b3e4c5a425 | 313 | |
jah128 | 4:52b3e4c5a425 | 314 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 315 | // Native m3Pi Functions |
jah128 | 4:52b3e4c5a425 | 316 | // These should preserve functionality of (most) m3pi code but may be fully functional on Pi Swarm |
jah128 | 4:52b3e4c5a425 | 317 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
jah128 | 4:52b3e4c5a425 | 318 | |
jah128 | 1:b067a08ff54e | 319 | // Force a hardware reset of the 3-pi |
jah128 | 0:9ffe8ebd1c40 | 320 | void reset (void); |
jah128 | 0:9ffe8ebd1c40 | 321 | |
jah128 | 4:52b3e4c5a425 | 322 | // Read the position of the detected line [using line-following IR] |
jah128 | 0:9ffe8ebd1c40 | 323 | float line_position (void); |
jah128 | 0:9ffe8ebd1c40 | 324 | |
jah128 | 4:52b3e4c5a425 | 325 | // Calibrate the sensors. This turns the robot left then right, looking for a line [using line-following IR] |
jah128 | 0:9ffe8ebd1c40 | 326 | char sensor_auto_calibrate (void); |
jah128 | 0:9ffe8ebd1c40 | 327 | |
jah128 | 4:52b3e4c5a425 | 328 | // Set calibration manually to the current settings. |
jah128 | 0:9ffe8ebd1c40 | 329 | void calibrate(void); |
jah128 | 0:9ffe8ebd1c40 | 330 | |
jah128 | 4:52b3e4c5a425 | 331 | // Clear the current calibration settings |
jah128 | 0:9ffe8ebd1c40 | 332 | void reset_calibration (void); |
jah128 | 0:9ffe8ebd1c40 | 333 | |
jah128 | 4:52b3e4c5a425 | 334 | // Start the PID contoller |
jah128 | 0:9ffe8ebd1c40 | 335 | void PID_start(int max_speed, int a, int b, int c, int d); |
jah128 | 0:9ffe8ebd1c40 | 336 | |
jah128 | 4:52b3e4c5a425 | 337 | // Stop the PID controller |
jah128 | 0:9ffe8ebd1c40 | 338 | void PID_stop(); |
jah128 | 0:9ffe8ebd1c40 | 339 | |
jah128 | 4:52b3e4c5a425 | 340 | // Send a character directly to the 3pi serial interface |
jah128 | 0:9ffe8ebd1c40 | 341 | int putc(int c); |
jah128 | 0:9ffe8ebd1c40 | 342 | |
jah128 | 4:52b3e4c5a425 | 343 | // Receive a character directly to the 3pi serial interface |
jah128 | 0:9ffe8ebd1c40 | 344 | int getc(); |
jah128 | 0:9ffe8ebd1c40 | 345 | |
jah128 | 4:52b3e4c5a425 | 346 | // Send a string buffer to the 3pi serial interface |
jah128 | 0:9ffe8ebd1c40 | 347 | int print(char* text, int length); |
jah128 | 0:9ffe8ebd1c40 | 348 | |
jah128 | 4:52b3e4c5a425 | 349 | #ifdef MBED_RPC |
jah128 | 4:52b3e4c5a425 | 350 | virtual const struct rpc_method *get_rpc_methods(); |
jah128 | 4:52b3e4c5a425 | 351 | #endif |
jah128 | 0:9ffe8ebd1c40 | 352 | |
jah128 | 4:52b3e4c5a425 | 353 | private : |
jah128 | 0:9ffe8ebd1c40 | 354 | |
jah128 | 0:9ffe8ebd1c40 | 355 | Serial _ser; |
jah128 | 0:9ffe8ebd1c40 | 356 | PwmOut _oled_r, _oled_g, _oled_b, _cled_r, _cled_g, _cled_b; |
jah128 | 0:9ffe8ebd1c40 | 357 | AnalogIn _gyro, _accel_x, _accel_y, _accel_z, _temperature, _light; |
jah128 | 4:52b3e4c5a425 | 358 | I2C _i2c; |
jah128 | 0:9ffe8ebd1c40 | 359 | DigitalOut _irpulse_1, _irpulse_2; |
jah128 | 0:9ffe8ebd1c40 | 360 | Alpha433 _rf; |
jah128 | 0:9ffe8ebd1c40 | 361 | Timer _system_timer; |
jah128 | 0:9ffe8ebd1c40 | 362 | |
jah128 | 0:9ffe8ebd1c40 | 363 | void motor (int motor, float speed); |
jah128 | 0:9ffe8ebd1c40 | 364 | virtual int _putc(int c); |
jah128 | 0:9ffe8ebd1c40 | 365 | virtual int _getc(); |
jah128 | 0:9ffe8ebd1c40 | 366 | |
jah128 | 0:9ffe8ebd1c40 | 367 | }; |
jah128 | 0:9ffe8ebd1c40 | 368 | |
jah128 | 0:9ffe8ebd1c40 | 369 | void display_system_time(void); |
jah128 | 0:9ffe8ebd1c40 | 370 | void init(void); |
jah128 | 0:9ffe8ebd1c40 | 371 | extern Serial pc; |
jah128 | 0:9ffe8ebd1c40 | 372 | extern PiSwarm piswarm; |
jah128 | 0:9ffe8ebd1c40 | 373 | |
jah128 | 0:9ffe8ebd1c40 | 374 | #endif |
jah128 | 0:9ffe8ebd1c40 | 375 | |
jah128 | 1:b067a08ff54e | 376 | /******************************************************************************** |
jah128 | 1:b067a08ff54e | 377 | * MBED Pin connection layout * |
jah128 | 1:b067a08ff54e | 378 | * * |
jah128 | 1:b067a08ff54e | 379 | * * Pin 05 : Alpha Transceiver SDI * |
jah128 | 1:b067a08ff54e | 380 | * * Pin 06 : Alpha Transceiver SDO * |
jah128 | 1:b067a08ff54e | 381 | * * Pin 07 : Alpha Transceiver CLK * |
jah128 | 1:b067a08ff54e | 382 | * * Pin 08 : Alpha Transceiver FSS * |
jah128 | 1:b067a08ff54e | 383 | * * Pin 09 : Serial (3pi) transmit * |
jah128 | 1:b067a08ff54e | 384 | * * Pin 10 : Serial (3pi) receive * |
jah128 | 1:b067a08ff54e | 385 | * * Pin 11 : Alpha Transceiver NIRQ * |
jah128 | 1:b067a08ff54e | 386 | * * Pin 12 : IR Pulse Output for IR1,2,7,8 * |
jah128 | 1:b067a08ff54e | 387 | * * Pin 13 : IR Pulse Output for IR3,4,5,6 * |
jah128 | 1:b067a08ff54e | 388 | * * Pin 14 : Echo Pin for Sonar * |
jah128 | 1:b067a08ff54e | 389 | * * Pin 15 : Light Sensor Analogue Input * |
jah128 | 1:b067a08ff54e | 390 | * * Pin 16 : Gyrometer Analogue Input * |
jah128 | 1:b067a08ff54e | 391 | * * Pin 17 : Accelerometer Z Analogue Input * |
jah128 | 1:b067a08ff54e | 392 | * * Pin 18 : Accelerometer Y Analogue Input * |
jah128 | 1:b067a08ff54e | 393 | * * Pin 19 : Accelerometer X Analogue Input * |
jah128 | 1:b067a08ff54e | 394 | * * Pin 20 : Temperature Sensor Analogue Input * |
jah128 | 1:b067a08ff54e | 395 | * * Pin 21 : Center LED Blue PWM Output * |
jah128 | 1:b067a08ff54e | 396 | * * Pin 22 : Center LED Green PWM Output * |
jah128 | 1:b067a08ff54e | 397 | * * Pin 23 : Center LED Red PWM Output * |
jah128 | 1:b067a08ff54e | 398 | * * Pin 24 : Outer LED Blue PWM Output * |
jah128 | 1:b067a08ff54e | 399 | * * Pin 25 : Outer LED Green PWM Output * |
jah128 | 1:b067a08ff54e | 400 | * * Pin 26 : Outer LED Red PWM Output * |
jah128 | 1:b067a08ff54e | 401 | * * Pin 27 : I2C SCL * |
jah128 | 1:b067a08ff54e | 402 | * * Pin 28 : I2C SDA * |
jah128 | 1:b067a08ff54e | 403 | * * Pin 29 : GPIO Expander Interrupt * |
jah128 | 1:b067a08ff54e | 404 | * * Pin 30 : Trigger Pin for Sonar * |
jah128 | 1:b067a08ff54e | 405 | * * Pin 31 : USB D+ Connector * |
jah128 | 1:b067a08ff54e | 406 | * * Pin 32 : USB D- Connector * |
jah128 | 1:b067a08ff54e | 407 | * * |
jah128 | 1:b067a08ff54e | 408 | *******************************************************************************/ |
jah128 | 0:9ffe8ebd1c40 | 409 | |
jah128 | 0:9ffe8ebd1c40 | 410 | /******************************************************************************** |
jah128 | 1:b067a08ff54e | 411 | * Using the 3-Pi Buzzer * |
jah128 | 1:b067a08ff54e | 412 | * * |
jah128 | 1:b067a08ff54e | 413 | * play_tune (char * tune, int length) * |
jah128 | 1:b067a08ff54e | 414 | * * |
jah128 | 1:b067a08ff54e | 415 | * tune - Pointer to a character sequence of the tune * |
jah128 | 1:b067a08ff54e | 416 | * length - Length of characters in sequence * |
jah128 | 1:b067a08ff54e | 417 | * * |
jah128 | 1:b067a08ff54e | 418 | * The character sequence recognises the following characters: * |
jah128 | 1:b067a08ff54e | 419 | * * |
jah128 | 1:b067a08ff54e | 420 | * A – G : used to specify the notes that will be played * |
jah128 | 1:b067a08ff54e | 421 | * * |
jah128 | 1:b067a08ff54e | 422 | * R : used to specify a rest (no sound for the duration of the note) * |
jah128 | 1:b067a08ff54e | 423 | * * |
jah128 | 1:b067a08ff54e | 424 | * < or > : plays the next note one octave lower or higher * |
jah128 | 1:b067a08ff54e | 425 | * * |
jah128 | 1:b067a08ff54e | 426 | * + or # : raises the previous notes pitch by one semitone * |
jah128 | 1:b067a08ff54e | 427 | * * |
jah128 | 1:b067a08ff54e | 428 | * - : lowers the previous note pitch by one semitone * |
jah128 | 1:b067a08ff54e | 429 | * * |
jah128 | 1:b067a08ff54e | 430 | * . : extend length of previous by 50% (each additional dot adds half as * |
jah128 | 1:b067a08ff54e | 431 | * much as the previous dot, so that A.. is 1.75 times the length of A * |
jah128 | 1:b067a08ff54e | 432 | * * |
jah128 | 1:b067a08ff54e | 433 | * O followed by a number : sets the octave (default : O4) * |
jah128 | 1:b067a08ff54e | 434 | * * |
jah128 | 1:b067a08ff54e | 435 | * T followed by a number : sets the tempo in beats/min (default : T120) * |
jah128 | 1:b067a08ff54e | 436 | * * |
jah128 | 1:b067a08ff54e | 437 | * L followed by a number : sets the default note duration specified by the* |
jah128 | 1:b067a08ff54e | 438 | * number - 4 for quarter notes, 8 for eighth, 16 * |
jah128 | 1:b067a08ff54e | 439 | * for sixteenth notes, etc. (default : L4) * |
jah128 | 1:b067a08ff54e | 440 | * * |
jah128 | 1:b067a08ff54e | 441 | * V followed by a number : sets the volume (range 0 - 15, default : V15) * |
jah128 | 1:b067a08ff54e | 442 | * * |
jah128 | 1:b067a08ff54e | 443 | * MS : sets all subsequent notes to play staccato (each note is played for* |
jah128 | 1:b067a08ff54e | 444 | * 1/2 of its allotted time followed by an equal period of silence) * |
jah128 | 1:b067a08ff54e | 445 | * * |
jah128 | 1:b067a08ff54e | 446 | * ML : sets all subsequent notes to play legato (each note is played for * |
jah128 | 1:b067a08ff54e | 447 | * full length. This is the default setting. * |
jah128 | 1:b067a08ff54e | 448 | * * |
jah128 | 1:b067a08ff54e | 449 | * ! : resets the octave, tempo, duration, volume, and staccato setting to * |
jah128 | 1:b067a08ff54e | 450 | * their default values. These settings persist from one play_tune() to* |
jah128 | 1:b067a08ff54e | 451 | * the next, which allows you to more conveniently break up your music * |
jah128 | 1:b067a08ff54e | 452 | * into reusable sections. * |
jah128 | 1:b067a08ff54e | 453 | * * |
jah128 | 1:b067a08ff54e | 454 | * 1 - 2000 : when immediately following a note, a number determines the * |
jah128 | 1:b067a08ff54e | 455 | * duration of the note. For example, C16 specifies C played as * |
jah128 | 1:b067a08ff54e | 456 | * a sixteenth note (1/16th the length of a whole note). * |
jah128 | 1:b067a08ff54e | 457 | * * |
jah128 | 1:b067a08ff54e | 458 | * * |
jah128 | 1:b067a08ff54e | 459 | * Some examples: * |
jah128 | 1:b067a08ff54e | 460 | * * |
jah128 | 1:b067a08ff54e | 461 | * Blue Danube Waltz (played badly!) * |
jah128 | 1:b067a08ff54e | 462 | * play_tune("MSCEGGR>F>FR>D>DRCCEGGR>G>GR>E>ER<B<BDAAR>A>AR>F>F",51); * |
jah128 | 1:b067a08ff54e | 463 | * * |
jah128 | 1:b067a08ff54e | 464 | *******************************************************************************/ |
jah128 | 1:b067a08ff54e | 465 | |
jah128 | 1:b067a08ff54e | 466 | /******************************************************************************** |
jah128 | 0:9ffe8ebd1c40 | 467 | * COPYRIGHT NOTICE * |
jah128 | 0:9ffe8ebd1c40 | 468 | * * |
jah128 | 4:52b3e4c5a425 | 469 | * * |
jah128 | 4:52b3e4c5a425 | 470 | * Parts of code based on the original m3pi library, Copyright (c) 2010 cstyles * |
jah128 | 4:52b3e4c5a425 | 471 | * * |
jah128 | 0:9ffe8ebd1c40 | 472 | * Permission is hereby granted, free of charge, to any person obtaining a copy * |
jah128 | 0:9ffe8ebd1c40 | 473 | * of this software and associated documentation files (the "Software"), to deal* |
jah128 | 0:9ffe8ebd1c40 | 474 | * in the Software without restriction, including without limitation the rights * |
jah128 | 0:9ffe8ebd1c40 | 475 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * |
jah128 | 0:9ffe8ebd1c40 | 476 | * copies of the Software, and to permit persons to whom the Software is * |
jah128 | 0:9ffe8ebd1c40 | 477 | * furnished to do so, subject to the following conditions: * |
jah128 | 0:9ffe8ebd1c40 | 478 | * * |
jah128 | 0:9ffe8ebd1c40 | 479 | * The above copyright notice and this permission notice shall be included in * |
jah128 | 0:9ffe8ebd1c40 | 480 | * all copies or substantial portions of the Software. * |
jah128 | 0:9ffe8ebd1c40 | 481 | * * |
jah128 | 0:9ffe8ebd1c40 | 482 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * |
jah128 | 0:9ffe8ebd1c40 | 483 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * |
jah128 | 0:9ffe8ebd1c40 | 484 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * |
jah128 | 0:9ffe8ebd1c40 | 485 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * |
jah128 | 0:9ffe8ebd1c40 | 486 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* |
jah128 | 0:9ffe8ebd1c40 | 487 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * |
jah128 | 0:9ffe8ebd1c40 | 488 | * THE SOFTWARE. * |
jah128 | 0:9ffe8ebd1c40 | 489 | * * |
jah128 | 0:9ffe8ebd1c40 | 490 | *******************************************************************************/ |