Modified version of the UKESF lab source which can be carried out with no knowledge of C

Fork of PsiSwarm-Headstart by UKESF Headstart Summer School

Committer:
jah128
Date:
Fri Mar 11 13:51:24 2016 +0000
Revision:
2:c6986ee3c7c5
Parent:
1:060690a934a9
Child:
4:dc77a25f29de
Added Alex' IR functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d6269d17c8cf 1 /* University of York Robotics Laboratory PsiSwarm Library: PsiSwarm Core Source File
jah128 1:060690a934a9 2 *
jah128 0:d6269d17c8cf 3 * File: psiswarm.cpp
jah128 0:d6269d17c8cf 4 *
jah128 0:d6269d17c8cf 5 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 6 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 7 *
jah128 2:c6986ee3c7c5 8 * PsiSwarm Library Version: 0.41
jah128 0:d6269d17c8cf 9 *
jah128 2:c6986ee3c7c5 10 * March 2016
jah128 0:d6269d17c8cf 11 *
jah128 0:d6269d17c8cf 12 *
jah128 1:060690a934a9 13 */
jah128 0:d6269d17c8cf 14
jah128 0:d6269d17c8cf 15 #include "psiswarm.h"
jah128 0:d6269d17c8cf 16
jah128 0:d6269d17c8cf 17 //Setup MBED connections to PsiSwarm Robot
jah128 0:d6269d17c8cf 18 Serial pc(USBTX,USBRX);
jah128 0:d6269d17c8cf 19 I2C primary_i2c (p9, p10);
jah128 0:d6269d17c8cf 20 InterruptIn gpio_interrupt (p12);
jah128 0:d6269d17c8cf 21 Serial bt(p13, p14);
jah128 0:d6269d17c8cf 22 AnalogIn vin_current(p15);
jah128 0:d6269d17c8cf 23 AnalogIn vin_battery(p16);
jah128 0:d6269d17c8cf 24 AnalogIn vin_dc(p17);
jah128 0:d6269d17c8cf 25 PwmOut motor_left_f (p21);
jah128 0:d6269d17c8cf 26 PwmOut motor_left_r (p22);
jah128 0:d6269d17c8cf 27 PwmOut motor_right_f(p23);
jah128 0:d6269d17c8cf 28 PwmOut motor_right_r(p24);
jah128 0:d6269d17c8cf 29 PwmOut center_led_red(p25);
jah128 0:d6269d17c8cf 30 PwmOut center_led_green(p26);
jah128 0:d6269d17c8cf 31 Display display; //Connects to i2c(p28,p27), reset(p29), backlight(p30)
jah128 0:d6269d17c8cf 32 DigitalOut mbed_led1(LED1);
jah128 0:d6269d17c8cf 33 DigitalOut mbed_led2(LED2);
jah128 0:d6269d17c8cf 34 DigitalOut mbed_led3(LED3);
jah128 0:d6269d17c8cf 35 DigitalOut mbed_led4(LED4);
jah128 0:d6269d17c8cf 36
jah128 0:d6269d17c8cf 37
jah128 0:d6269d17c8cf 38 float center_led_brightness;
jah128 0:d6269d17c8cf 39 float backlight_brightness;
jah128 0:d6269d17c8cf 40
jah128 0:d6269d17c8cf 41 Ticker event_handler;
jah128 0:d6269d17c8cf 42 Timer uptime;
jah128 0:d6269d17c8cf 43 Timeout pause_usercode_timeout;
jah128 0:d6269d17c8cf 44 Ticker ultrasonic_ticker;
jah128 0:d6269d17c8cf 45 Timeout ultrasonic_timeout;
jah128 0:d6269d17c8cf 46 int timer_minute_count;
jah128 0:d6269d17c8cf 47 Ticker timer_ticker;
jah128 0:d6269d17c8cf 48
jah128 0:d6269d17c8cf 49 float firmware_version;
jah128 1:060690a934a9 50 float pcb_version;
jah128 1:060690a934a9 51 float serial_number;
jah128 1:060690a934a9 52
jah128 1:060690a934a9 53 char has_compass=0;
jah128 1:060690a934a9 54 char has_side_ir=1;
jah128 1:060690a934a9 55 char has_base_ir=1;
jah128 1:060690a934a9 56 char has_base_colour_sensor=0;
jah128 1:060690a934a9 57 char has_top_colour_sensor=0;
jah128 1:060690a934a9 58 char has_wheel_encoders=0;
jah128 1:060690a934a9 59 char has_audio_pic=0;
jah128 1:060690a934a9 60 char has_ultrasonic_sensor=0;
jah128 1:060690a934a9 61 char has_temperature_sensor=0;
jah128 1:060690a934a9 62 char has_recharging_circuit=0;
jah128 1:060690a934a9 63 char has_433_radio=0;
jah128 1:060690a934a9 64
jah128 0:d6269d17c8cf 65 char robot_id;
jah128 0:d6269d17c8cf 66 char previous_robot_id;
jah128 0:d6269d17c8cf 67
jah128 0:d6269d17c8cf 68 char wheel_encoder_byte;
jah128 0:d6269d17c8cf 69 char previous_wheel_encoder_byte;
jah128 0:d6269d17c8cf 70 signed int left_encoder;
jah128 0:d6269d17c8cf 71 signed int right_encoder;
jah128 0:d6269d17c8cf 72
jah128 0:d6269d17c8cf 73 char time_based_motor_action = 0;
jah128 0:d6269d17c8cf 74
jah128 0:d6269d17c8cf 75 char testing_voltage_regulators_flag = 1;
jah128 0:d6269d17c8cf 76 char power_good_motor_left = 2;
jah128 0:d6269d17c8cf 77 char power_good_motor_right = 2;
jah128 0:d6269d17c8cf 78 char power_good_infrared = 2;
jah128 0:d6269d17c8cf 79 char status_dc_in = 2;
jah128 0:d6269d17c8cf 80 char status_charging = 2;
jah128 0:d6269d17c8cf 81
jah128 0:d6269d17c8cf 82 char switch_byte;
jah128 0:d6269d17c8cf 83 char previous_switch_byte;
jah128 0:d6269d17c8cf 84
jah128 0:d6269d17c8cf 85 char debug_mode = DEBUG_MODE;
jah128 0:d6269d17c8cf 86 char debug_output = DEBUG_OUTPUT_STREAM;
jah128 1:060690a934a9 87
jah128 0:d6269d17c8cf 88 char firmware_bytes[21];
jah128 0:d6269d17c8cf 89
jah128 0:d6269d17c8cf 90 int base_colour_sensor_raw_values [4];
jah128 0:d6269d17c8cf 91 int top_colour_sensor_raw_values [4];
jah128 0:d6269d17c8cf 92
jah128 0:d6269d17c8cf 93 char waiting_for_ultrasonic = 0;
jah128 0:d6269d17c8cf 94 int ultrasonic_distance = 0;
jah128 0:d6269d17c8cf 95 char ultrasonic_distance_updated = 0;
jah128 0:d6269d17c8cf 96
jah128 0:d6269d17c8cf 97
jah128 0:d6269d17c8cf 98 float line_position = 0;
jah128 0:d6269d17c8cf 99 char line_found = 0;
jah128 0:d6269d17c8cf 100
jah128 0:d6269d17c8cf 101 unsigned short background_ir_values [8];
jah128 0:d6269d17c8cf 102 unsigned short illuminated_ir_values [8];
jah128 0:d6269d17c8cf 103 float reflected_ir_distances [8];
jah128 0:d6269d17c8cf 104 char ir_values_stored = 0;
jah128 0:d6269d17c8cf 105 unsigned short background_base_ir_values [5];
jah128 0:d6269d17c8cf 106 unsigned short illuminated_base_ir_values [5];
jah128 0:d6269d17c8cf 107 char base_ir_values_stored = 0;
jah128 0:d6269d17c8cf 108
jah128 0:d6269d17c8cf 109 float motor_left_speed;
jah128 0:d6269d17c8cf 110 float motor_right_speed;
jah128 0:d6269d17c8cf 111 char motor_left_brake;
jah128 0:d6269d17c8cf 112 char motor_right_brake;
jah128 0:d6269d17c8cf 113
jah128 0:d6269d17c8cf 114 char demo_on = 0;
jah128 0:d6269d17c8cf 115 char event = 0;
jah128 0:d6269d17c8cf 116 char change_id_event = 0;
jah128 0:d6269d17c8cf 117 char encoder_event = 0;
jah128 0:d6269d17c8cf 118 char switch_event = 0;
jah128 0:d6269d17c8cf 119 char user_code_running = 0;
jah128 0:d6269d17c8cf 120 char user_code_restore_mode = 0;
jah128 0:d6269d17c8cf 121 char system_warnings = 0;
jah128 0:d6269d17c8cf 122
jah128 0:d6269d17c8cf 123
jah128 0:d6269d17c8cf 124 vector<string> basic_filenames; //filenames are stored in a vector string
jah128 0:d6269d17c8cf 125 char psi_basic_file_count = 0;
jah128 0:d6269d17c8cf 126 char use_flash_basic = 0;
jah128 0:d6269d17c8cf 127 char file_transfer_mode = 0;
jah128 0:d6269d17c8cf 128
jah128 0:d6269d17c8cf 129 int ir_pulse_delay = DEFAULT_IR_PULSE_DELAY;
jah128 0:d6269d17c8cf 130 int base_ir_pulse_delay = DEFAULT_BASE_IR_PULSE_DELAY;
jah128 0:d6269d17c8cf 131
jah128 0:d6269d17c8cf 132
jah128 1:060690a934a9 133
jah128 1:060690a934a9 134
jah128 0:d6269d17c8cf 135 /**
jah128 0:d6269d17c8cf 136 * init()
jah128 0:d6269d17c8cf 137 *
jah128 0:d6269d17c8cf 138 * Main initialisation routine for the PsiSwarm robot
jah128 0:d6269d17c8cf 139 *
jah128 0:d6269d17c8cf 140 * Set up the GPIO expansion ICs, launch demo mode if button is held
jah128 0:d6269d17c8cf 141 */
jah128 0:d6269d17c8cf 142 void init()
jah128 0:d6269d17c8cf 143 {
jah128 0:d6269d17c8cf 144 firmware_version=0;
jah128 0:d6269d17c8cf 145 timer_minute_count = 0;
jah128 0:d6269d17c8cf 146 timer_ticker.attach(&IF_update_minutes, 300);
jah128 0:d6269d17c8cf 147 uptime.start();
jah128 0:d6269d17c8cf 148 primary_i2c.frequency(400000);
jah128 0:d6269d17c8cf 149 IF_setup_serial_interfaces();
jah128 0:d6269d17c8cf 150 debug("PsiSwarm Robot Library %1.2f\n\n",SOFTWARE_VERSION_CODE);
jah128 0:d6269d17c8cf 151 debug("- Setting up serial interface\n");
jah128 0:d6269d17c8cf 152 debug("- Reading firmware: ");
jah128 1:060690a934a9 153 if(read_firmware() == 1) {
jah128 1:060690a934a9 154 debug("Version %3.2f\n",firmware_version);
jah128 1:060690a934a9 155 IF_get_hardware_description();
jah128 1:060690a934a9 156 } else {
jah128 1:060690a934a9 157 debug("INVALID\n");
jah128 1:060690a934a9 158 debug("- WARNING: Check firmware to enable robot features");
jah128 1:060690a934a9 159 }
jah128 1:060690a934a9 160 if(ENABLE_BASIC == 1) {
jah128 1:060690a934a9 161 read_list_of_file_names();
jah128 1:060690a934a9 162 if(psi_basic_file_count == 0) {
jah128 1:060690a934a9 163 debug("- No PsiBasic files found\n");
jah128 1:060690a934a9 164 } else use_flash_basic = 1;
jah128 0:d6269d17c8cf 165 }
jah128 0:d6269d17c8cf 166 debug("- Setting up PIC microcontroller\n");
jah128 0:d6269d17c8cf 167 // IF_check_pic_firmware();
jah128 0:d6269d17c8cf 168 debug("- Setting up LED drivers\n");
jah128 1:060690a934a9 169 IF_init_leds();
jah128 0:d6269d17c8cf 170 if(IF_setup_led_expansion_ic() != 0) {
jah128 1:060690a934a9 171 debug("- WARNING: No I2C acknowledge for LED driver\n");
jah128 1:060690a934a9 172 system_warnings += 1;
jah128 0:d6269d17c8cf 173 }
jah128 0:d6269d17c8cf 174 debug("- Setting up motor drivers\n");
jah128 1:060690a934a9 175 IF_init_motors();
jah128 0:d6269d17c8cf 176 debug("- Setting up GPIO expansion\n");
jah128 0:d6269d17c8cf 177 reset_encoders();
jah128 0:d6269d17c8cf 178 IF_setup_gpio_expansion_ic();
jah128 1:060690a934a9 179 if(has_temperature_sensor) {
jah128 1:060690a934a9 180 debug("- Setting up temperature sensor\n");
jah128 1:060690a934a9 181 IF_setup_temperature_sensor();
jah128 1:060690a934a9 182 }
jah128 1:060690a934a9 183 if(has_base_colour_sensor) {
jah128 1:060690a934a9 184 debug("- Setting up base colour sensor\n");
jah128 1:060690a934a9 185 IF_check_base_colour_sensor();
jah128 1:060690a934a9 186 }
jah128 1:060690a934a9 187 if(has_ultrasonic_sensor) {
jah128 1:060690a934a9 188 debug("- Setting up ultrasonic sensor\n");
jah128 1:060690a934a9 189 //enable_ultrasonic_ticker();
jah128 1:060690a934a9 190 }
jah128 1:060690a934a9 191
jah128 0:d6269d17c8cf 192 debug("- Robot ID: %d\n",robot_id);
jah128 1:060690a934a9 193 char switchstate = IF_get_switch_state();
jah128 0:d6269d17c8cf 194 debug("- Switch State : %d\n",switchstate);
jah128 0:d6269d17c8cf 195 debug("- Battery Voltage: %1.3fV\n",get_battery_voltage());
jah128 0:d6269d17c8cf 196 debug("- DC Voltage : %1.3fV\n",get_dc_voltage());
jah128 0:d6269d17c8cf 197 debug("- Current Draw : %1.3fA\n",get_current());
jah128 1:060690a934a9 198 if(has_temperature_sensor){
jah128 0:d6269d17c8cf 199 debug("- Temperature : %1.3fC\n",get_temperature());
jah128 1:060690a934a9 200 }
jah128 0:d6269d17c8cf 201 char demo = 0;
jah128 0:d6269d17c8cf 202 if(ENABLE_DEMO == 1 && switchstate > 0) demo=1;
jah128 0:d6269d17c8cf 203 display.init_display(demo);
jah128 0:d6269d17c8cf 204 event_handler.attach_us(&IF_handle_events, 1000);
jah128 1:060690a934a9 205 if(demo > 0) {
jah128 0:d6269d17c8cf 206 debug("- Demo mode button is pressed\n");
jah128 0:d6269d17c8cf 207 wait(1.0);
jah128 0:d6269d17c8cf 208 demo = IF_get_switch_state();
jah128 0:d6269d17c8cf 209 if(demo > 0) demo_mode();
jah128 1:060690a934a9 210 display.init_display(0);
jah128 0:d6269d17c8cf 211 }
jah128 0:d6269d17c8cf 212 }
jah128 0:d6269d17c8cf 213
jah128 1:060690a934a9 214 void IF_update_minutes()
jah128 1:060690a934a9 215 {
jah128 0:d6269d17c8cf 216 uptime.reset();
jah128 0:d6269d17c8cf 217 timer_minute_count += 5;
jah128 0:d6269d17c8cf 218 }
jah128 0:d6269d17c8cf 219
jah128 0:d6269d17c8cf 220 void IF_handle_events()
jah128 0:d6269d17c8cf 221 {
jah128 0:d6269d17c8cf 222 // This is the main 'operating system' thread that handles events from robot stimuli
jah128 0:d6269d17c8cf 223 // By default it is run every 1ms and checks if there are events to handle
jah128 1:060690a934a9 224 if(event > 0) {
jah128 0:d6269d17c8cf 225 // There are some events to handle. We don't handle all events in every loop to keep the system responsive, instead they are priorised.
jah128 1:060690a934a9 226 if(encoder_event == 1) {
jah128 0:d6269d17c8cf 227 // The encoders have changed; update the encoder values
jah128 0:d6269d17c8cf 228 IF_update_encoders();
jah128 1:060690a934a9 229 encoder_event = 0;
jah128 1:060690a934a9 230 event--;
jah128 0:d6269d17c8cf 231 } else {
jah128 1:060690a934a9 232 if(switch_event == 1) {
jah128 1:060690a934a9 233 IF_update_switch();
jah128 1:060690a934a9 234 switch_event = 0;
jah128 1:060690a934a9 235 event--;
jah128 0:d6269d17c8cf 236 }
jah128 1:060690a934a9 237 if(change_id_event == 1) {
jah128 0:d6269d17c8cf 238 // The user ID for the robot has been changed
jah128 0:d6269d17c8cf 239 IF_update_user_id();
jah128 0:d6269d17c8cf 240 change_id_event = 0;
jah128 0:d6269d17c8cf 241 event--;
jah128 0:d6269d17c8cf 242 }
jah128 0:d6269d17c8cf 243 }
jah128 1:060690a934a9 244 }
jah128 0:d6269d17c8cf 245 }
jah128 0:d6269d17c8cf 246
jah128 0:d6269d17c8cf 247 void IF_update_encoders()
jah128 0:d6269d17c8cf 248 {
jah128 0:d6269d17c8cf 249 char rwep = previous_wheel_encoder_byte >> 2;
jah128 0:d6269d17c8cf 250 char rwe = wheel_encoder_byte >> 2;
jah128 0:d6269d17c8cf 251 char lwep = previous_wheel_encoder_byte % 4;
jah128 0:d6269d17c8cf 252 char lwe = wheel_encoder_byte % 4;
jah128 0:d6269d17c8cf 253 //pc.printf("L:%d P:%d R:%d P:%d \n",lwe,lwep,rwe,rwep);
jah128 0:d6269d17c8cf 254 if(lwe == 0 && lwep==1) left_encoder++;
jah128 0:d6269d17c8cf 255 if(lwe == 0 && lwep==2) left_encoder--;
jah128 0:d6269d17c8cf 256 if(rwe == 0 && rwep==1) right_encoder++;
jah128 0:d6269d17c8cf 257 if(rwe == 0 && rwep==2) right_encoder--;
jah128 0:d6269d17c8cf 258 if(left_encoder % 100 == 0) pc.printf("L:%d\n",left_encoder);
jah128 0:d6269d17c8cf 259 }
jah128 0:d6269d17c8cf 260
jah128 0:d6269d17c8cf 261 void IF_update_user_id()
jah128 0:d6269d17c8cf 262 {
jah128 0:d6269d17c8cf 263 }
jah128 0:d6269d17c8cf 264
jah128 0:d6269d17c8cf 265 void IF_update_switch()
jah128 0:d6269d17c8cf 266 {
jah128 0:d6269d17c8cf 267 // The user switch has changed state
jah128 0:d6269d17c8cf 268 // In this implementation we will only act on positive changes (rising edges)
jah128 0:d6269d17c8cf 269 // Subtracting new_state from (new_state & old_state) gives the positive changes
jah128 0:d6269d17c8cf 270 char positive_change = switch_byte - (switch_byte & previous_switch_byte);
jah128 0:d6269d17c8cf 271 if(demo_on) demo_handle_switch_event(positive_change);
jah128 0:d6269d17c8cf 272 else handle_switch_event(positive_change);
jah128 0:d6269d17c8cf 273 }
jah128 0:d6269d17c8cf 274
jah128 0:d6269d17c8cf 275 void reset_encoders()
jah128 0:d6269d17c8cf 276 {
jah128 0:d6269d17c8cf 277 left_encoder = 0;
jah128 1:060690a934a9 278 right_encoder = 0;
jah128 0:d6269d17c8cf 279 }
jah128 0:d6269d17c8cf 280
jah128 1:060690a934a9 281 void debug(const char* format, ...)
jah128 0:d6269d17c8cf 282 {
jah128 1:060690a934a9 283 char buffer[256];
jah128 1:060690a934a9 284 if (debug_mode) {
jah128 1:060690a934a9 285 va_list vl;
jah128 1:060690a934a9 286 va_start(vl, format);
jah128 1:060690a934a9 287 vsprintf(buffer,format,vl);
jah128 1:060690a934a9 288 if(debug_output & 2) bt.printf("%s", buffer);
jah128 1:060690a934a9 289 if(debug_output & 1) pc.printf("%s", buffer);
jah128 1:060690a934a9 290 if(debug_output & 4) display.debug_page(buffer,strlen(buffer));
jah128 1:060690a934a9 291 va_end(vl);
jah128 1:060690a934a9 292 }
jah128 0:d6269d17c8cf 293 }
jah128 0:d6269d17c8cf 294
jah128 0:d6269d17c8cf 295 float get_uptime(void)
jah128 0:d6269d17c8cf 296 {
jah128 1:060690a934a9 297 return uptime.read() + (timer_minute_count * 60);
jah128 0:d6269d17c8cf 298 }
jah128 0:d6269d17c8cf 299
jah128 1:060690a934a9 300 void pause_user_code(float period)
jah128 1:060690a934a9 301 {
jah128 0:d6269d17c8cf 302 user_code_restore_mode = user_code_running;
jah128 0:d6269d17c8cf 303 user_code_running = 0;
jah128 0:d6269d17c8cf 304 pause_usercode_timeout.attach(&IF_end_pause_user_code, period);
jah128 0:d6269d17c8cf 305 }
jah128 0:d6269d17c8cf 306
jah128 1:060690a934a9 307 void IF_end_pause_user_code()
jah128 1:060690a934a9 308 {
jah128 1:060690a934a9 309 user_code_running = user_code_restore_mode;
jah128 1:060690a934a9 310 }
jah128 1:060690a934a9 311
jah128 1:060690a934a9 312 void IF_get_hardware_description()
jah128 1:060690a934a9 313 {
jah128 1:060690a934a9 314 debug("- Robot serial number %1.2f\n",serial_number);
jah128 1:060690a934a9 315 debug("- PCB version %1.2f\n",pcb_version);
jah128 1:060690a934a9 316 debug("- Hardware: ");
jah128 1:060690a934a9 317 if(has_compass) debug("COMPASS, ");
jah128 1:060690a934a9 318 if(has_side_ir) debug("SIDE IR, ");
jah128 1:060690a934a9 319 if(has_base_ir) debug("BASE IR, ");
jah128 1:060690a934a9 320 if(has_base_colour_sensor) debug("BASE COLOUR, ");
jah128 1:060690a934a9 321 if(has_top_colour_sensor) debug("TOP COLOUR, ");
jah128 1:060690a934a9 322 if(has_wheel_encoders) debug("WHEEL ENC., ");
jah128 1:060690a934a9 323 if(has_audio_pic) debug("AUDIO, ");
jah128 1:060690a934a9 324 if(has_ultrasonic_sensor) debug("ULTRASONIC, ");
jah128 1:060690a934a9 325 if(has_temperature_sensor) debug("TEMPERATURE, ");
jah128 1:060690a934a9 326 if(has_recharging_circuit) debug("RECHARGING, ");
jah128 1:060690a934a9 327 if(has_433_radio) debug("433 RADIO.");
jah128 1:060690a934a9 328 debug("\n");
jah128 0:d6269d17c8cf 329 }