Unfinished v0.7, added bearing detection

Fork of Pi_Swarm_Library_v06_alpha by piswarm

Committer:
jah128
Date:
Mon Jun 30 07:58:31 2014 +0000
Revision:
11:5ebcb52726cf
Parent:
10:5c111c07a2ed
Added prototype bearing detection to pi swarm library;

Who changed what in which revision?

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