Psi Swarm robot library version 0.9
Fork of PsiSwarmV8_CPP by
psiswarm.cpp@5:3cdd1a37cdd7, 2016-10-15 (annotated)
- Committer:
- jah128
- Date:
- Sat Oct 15 13:29:37 2016 +0000
- Revision:
- 5:3cdd1a37cdd7
- Parent:
- 4:1c621cb8cf0d
- Child:
- 6:b340a527add9
Initial commit of API Version 7 (fork from version 5)
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:3cdd1a37cdd7 | 8 | * PsiSwarm Library Version: 0.7 |
jah128 | 0:d6269d17c8cf | 9 | * |
jah128 | 5:3cdd1a37cdd7 | 10 | * October 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 | 4:1c621cb8cf0d | 85 | |
jah128 | 4:1c621cb8cf0d | 86 | char use_motor_calibration = USE_MOTOR_CALIBRATION; |
jah128 | 4:1c621cb8cf0d | 87 | char motor_calibration_set; |
jah128 | 4:1c621cb8cf0d | 88 | float left_motor_calibration_value = 1.0; |
jah128 | 4:1c621cb8cf0d | 89 | float right_motor_calibration_value = 1.0; |
jah128 | 4:1c621cb8cf0d | 90 | |
jah128 | 0:d6269d17c8cf | 91 | char debug_mode = DEBUG_MODE; |
jah128 | 0:d6269d17c8cf | 92 | char debug_output = DEBUG_OUTPUT_STREAM; |
jah128 | 1:060690a934a9 | 93 | |
jah128 | 0:d6269d17c8cf | 94 | char firmware_bytes[21]; |
jah128 | 0:d6269d17c8cf | 95 | |
jah128 | 0:d6269d17c8cf | 96 | int base_colour_sensor_raw_values [4]; |
jah128 | 0:d6269d17c8cf | 97 | int top_colour_sensor_raw_values [4]; |
jah128 | 0:d6269d17c8cf | 98 | |
jah128 | 0:d6269d17c8cf | 99 | char waiting_for_ultrasonic = 0; |
jah128 | 0:d6269d17c8cf | 100 | int ultrasonic_distance = 0; |
jah128 | 0:d6269d17c8cf | 101 | char ultrasonic_distance_updated = 0; |
jah128 | 0:d6269d17c8cf | 102 | |
jah128 | 0:d6269d17c8cf | 103 | |
jah128 | 0:d6269d17c8cf | 104 | float line_position = 0; |
jah128 | 0:d6269d17c8cf | 105 | char line_found = 0; |
jah128 | 0:d6269d17c8cf | 106 | |
jah128 | 0:d6269d17c8cf | 107 | unsigned short background_ir_values [8]; |
jah128 | 0:d6269d17c8cf | 108 | unsigned short illuminated_ir_values [8]; |
jah128 | 0:d6269d17c8cf | 109 | float reflected_ir_distances [8]; |
jah128 | 0:d6269d17c8cf | 110 | char ir_values_stored = 0; |
jah128 | 0:d6269d17c8cf | 111 | unsigned short background_base_ir_values [5]; |
jah128 | 0:d6269d17c8cf | 112 | unsigned short illuminated_base_ir_values [5]; |
jah128 | 0:d6269d17c8cf | 113 | char base_ir_values_stored = 0; |
jah128 | 0:d6269d17c8cf | 114 | |
jah128 | 0:d6269d17c8cf | 115 | float motor_left_speed; |
jah128 | 0:d6269d17c8cf | 116 | float motor_right_speed; |
jah128 | 0:d6269d17c8cf | 117 | char motor_left_brake; |
jah128 | 0:d6269d17c8cf | 118 | char motor_right_brake; |
jah128 | 0:d6269d17c8cf | 119 | |
jah128 | 0:d6269d17c8cf | 120 | char demo_on = 0; |
jah128 | 0:d6269d17c8cf | 121 | char event = 0; |
jah128 | 0:d6269d17c8cf | 122 | char change_id_event = 0; |
jah128 | 0:d6269d17c8cf | 123 | char encoder_event = 0; |
jah128 | 0:d6269d17c8cf | 124 | char switch_event = 0; |
jah128 | 0:d6269d17c8cf | 125 | char user_code_running = 0; |
jah128 | 0:d6269d17c8cf | 126 | char user_code_restore_mode = 0; |
jah128 | 0:d6269d17c8cf | 127 | char system_warnings = 0; |
jah128 | 0:d6269d17c8cf | 128 | |
jah128 | 0:d6269d17c8cf | 129 | |
jah128 | 0:d6269d17c8cf | 130 | vector<string> basic_filenames; //filenames are stored in a vector string |
jah128 | 0:d6269d17c8cf | 131 | char psi_basic_file_count = 0; |
jah128 | 0:d6269d17c8cf | 132 | char use_flash_basic = 0; |
jah128 | 0:d6269d17c8cf | 133 | char file_transfer_mode = 0; |
jah128 | 0:d6269d17c8cf | 134 | |
jah128 | 0:d6269d17c8cf | 135 | int ir_pulse_delay = DEFAULT_IR_PULSE_DELAY; |
jah128 | 0:d6269d17c8cf | 136 | int base_ir_pulse_delay = DEFAULT_BASE_IR_PULSE_DELAY; |
jah128 | 0:d6269d17c8cf | 137 | |
jah128 | 0:d6269d17c8cf | 138 | |
jah128 | 1:060690a934a9 | 139 | |
jah128 | 1:060690a934a9 | 140 | |
jah128 | 0:d6269d17c8cf | 141 | /** |
jah128 | 0:d6269d17c8cf | 142 | * init() |
jah128 | 0:d6269d17c8cf | 143 | * |
jah128 | 0:d6269d17c8cf | 144 | * Main initialisation routine for the PsiSwarm robot |
jah128 | 0:d6269d17c8cf | 145 | * |
jah128 | 0:d6269d17c8cf | 146 | * Set up the GPIO expansion ICs, launch demo mode if button is held |
jah128 | 0:d6269d17c8cf | 147 | */ |
jah128 | 0:d6269d17c8cf | 148 | void init() |
jah128 | 0:d6269d17c8cf | 149 | { |
jah128 | 0:d6269d17c8cf | 150 | firmware_version=0; |
jah128 | 0:d6269d17c8cf | 151 | timer_minute_count = 0; |
jah128 | 0:d6269d17c8cf | 152 | timer_ticker.attach(&IF_update_minutes, 300); |
jah128 | 0:d6269d17c8cf | 153 | uptime.start(); |
jah128 | 0:d6269d17c8cf | 154 | primary_i2c.frequency(400000); |
jah128 | 0:d6269d17c8cf | 155 | IF_setup_serial_interfaces(); |
jah128 | 0:d6269d17c8cf | 156 | debug("PsiSwarm Robot Library %1.2f\n\n",SOFTWARE_VERSION_CODE); |
jah128 | 0:d6269d17c8cf | 157 | debug("- Setting up serial interface\n"); |
jah128 | 0:d6269d17c8cf | 158 | debug("- Reading firmware: "); |
jah128 | 1:060690a934a9 | 159 | if(read_firmware() == 1) { |
jah128 | 1:060690a934a9 | 160 | debug("Version %3.2f\n",firmware_version); |
jah128 | 1:060690a934a9 | 161 | IF_get_hardware_description(); |
jah128 | 4:1c621cb8cf0d | 162 | if(use_motor_calibration){ |
jah128 | 4:1c621cb8cf0d | 163 | if(!motor_calibration_set){ |
jah128 | 4:1c621cb8cf0d | 164 | if(firmware_version < 1.1){ |
jah128 | 4:1c621cb8cf0d | 165 | debug("- WARNING: This firmware is incompatible with motor calibration"); |
jah128 | 4:1c621cb8cf0d | 166 | debug("- WARNING: Please update the firmware to use this feature."); |
jah128 | 4:1c621cb8cf0d | 167 | use_motor_calibration = 0; |
jah128 | 4:1c621cb8cf0d | 168 | } |
jah128 | 4:1c621cb8cf0d | 169 | else { |
jah128 | 4:1c621cb8cf0d | 170 | debug("- WARNING: Motor calibration values have not been stored in firmware"); |
jah128 | 4:1c621cb8cf0d | 171 | debug("- WARNING: Run calibration routine to use this feature."); |
jah128 | 4:1c621cb8cf0d | 172 | use_motor_calibration = 0; |
jah128 | 4:1c621cb8cf0d | 173 | } |
jah128 | 4:1c621cb8cf0d | 174 | } |
jah128 | 4:1c621cb8cf0d | 175 | else { |
jah128 | 4:1c621cb8cf0d | 176 | debug("- Motor calibration in use [LEFT:%0.4f RIGHT:%0.4f]",left_motor_calibration_value,right_motor_calibration_value); |
jah128 | 4:1c621cb8cf0d | 177 | } |
jah128 | 4:1c621cb8cf0d | 178 | } |
jah128 | 1:060690a934a9 | 179 | } else { |
jah128 | 1:060690a934a9 | 180 | debug("INVALID\n"); |
jah128 | 1:060690a934a9 | 181 | debug("- WARNING: Check firmware to enable robot features"); |
jah128 | 1:060690a934a9 | 182 | } |
jah128 | 1:060690a934a9 | 183 | if(ENABLE_BASIC == 1) { |
jah128 | 1:060690a934a9 | 184 | read_list_of_file_names(); |
jah128 | 1:060690a934a9 | 185 | if(psi_basic_file_count == 0) { |
jah128 | 1:060690a934a9 | 186 | debug("- No PsiBasic files found\n"); |
jah128 | 1:060690a934a9 | 187 | } else use_flash_basic = 1; |
jah128 | 0:d6269d17c8cf | 188 | } |
jah128 | 0:d6269d17c8cf | 189 | debug("- Setting up PIC microcontroller\n"); |
jah128 | 0:d6269d17c8cf | 190 | // IF_check_pic_firmware(); |
jah128 | 0:d6269d17c8cf | 191 | debug("- Setting up LED drivers\n"); |
jah128 | 1:060690a934a9 | 192 | IF_init_leds(); |
jah128 | 0:d6269d17c8cf | 193 | if(IF_setup_led_expansion_ic() != 0) { |
jah128 | 1:060690a934a9 | 194 | debug("- WARNING: No I2C acknowledge for LED driver\n"); |
jah128 | 1:060690a934a9 | 195 | system_warnings += 1; |
jah128 | 0:d6269d17c8cf | 196 | } |
jah128 | 0:d6269d17c8cf | 197 | debug("- Setting up motor drivers\n"); |
jah128 | 1:060690a934a9 | 198 | IF_init_motors(); |
jah128 | 0:d6269d17c8cf | 199 | debug("- Setting up GPIO expansion\n"); |
jah128 | 0:d6269d17c8cf | 200 | reset_encoders(); |
jah128 | 0:d6269d17c8cf | 201 | IF_setup_gpio_expansion_ic(); |
jah128 | 1:060690a934a9 | 202 | if(has_temperature_sensor) { |
jah128 | 1:060690a934a9 | 203 | debug("- Setting up temperature sensor\n"); |
jah128 | 1:060690a934a9 | 204 | IF_setup_temperature_sensor(); |
jah128 | 1:060690a934a9 | 205 | } |
jah128 | 1:060690a934a9 | 206 | if(has_base_colour_sensor) { |
jah128 | 1:060690a934a9 | 207 | debug("- Setting up base colour sensor\n"); |
jah128 | 1:060690a934a9 | 208 | IF_check_base_colour_sensor(); |
jah128 | 1:060690a934a9 | 209 | } |
jah128 | 1:060690a934a9 | 210 | if(has_ultrasonic_sensor) { |
jah128 | 1:060690a934a9 | 211 | debug("- Setting up ultrasonic sensor\n"); |
jah128 | 1:060690a934a9 | 212 | //enable_ultrasonic_ticker(); |
jah128 | 1:060690a934a9 | 213 | } |
jah128 | 1:060690a934a9 | 214 | |
jah128 | 0:d6269d17c8cf | 215 | debug("- Robot ID: %d\n",robot_id); |
jah128 | 1:060690a934a9 | 216 | char switchstate = IF_get_switch_state(); |
jah128 | 0:d6269d17c8cf | 217 | debug("- Switch State : %d\n",switchstate); |
jah128 | 0:d6269d17c8cf | 218 | debug("- Battery Voltage: %1.3fV\n",get_battery_voltage()); |
jah128 | 0:d6269d17c8cf | 219 | debug("- DC Voltage : %1.3fV\n",get_dc_voltage()); |
jah128 | 0:d6269d17c8cf | 220 | debug("- Current Draw : %1.3fA\n",get_current()); |
jah128 | 1:060690a934a9 | 221 | if(has_temperature_sensor){ |
jah128 | 0:d6269d17c8cf | 222 | debug("- Temperature : %1.3fC\n",get_temperature()); |
jah128 | 1:060690a934a9 | 223 | } |
jah128 | 0:d6269d17c8cf | 224 | char demo = 0; |
jah128 | 0:d6269d17c8cf | 225 | if(ENABLE_DEMO == 1 && switchstate > 0) demo=1; |
jah128 | 0:d6269d17c8cf | 226 | display.init_display(demo); |
jah128 | 0:d6269d17c8cf | 227 | event_handler.attach_us(&IF_handle_events, 1000); |
jah128 | 1:060690a934a9 | 228 | if(demo > 0) { |
jah128 | 0:d6269d17c8cf | 229 | debug("- Demo mode button is pressed\n"); |
jah128 | 0:d6269d17c8cf | 230 | wait(1.0); |
jah128 | 0:d6269d17c8cf | 231 | demo = IF_get_switch_state(); |
jah128 | 0:d6269d17c8cf | 232 | if(demo > 0) demo_mode(); |
jah128 | 1:060690a934a9 | 233 | display.init_display(0); |
jah128 | 0:d6269d17c8cf | 234 | } |
jah128 | 0:d6269d17c8cf | 235 | } |
jah128 | 0:d6269d17c8cf | 236 | |
jah128 | 1:060690a934a9 | 237 | void IF_update_minutes() |
jah128 | 1:060690a934a9 | 238 | { |
jah128 | 0:d6269d17c8cf | 239 | uptime.reset(); |
jah128 | 0:d6269d17c8cf | 240 | timer_minute_count += 5; |
jah128 | 0:d6269d17c8cf | 241 | } |
jah128 | 0:d6269d17c8cf | 242 | |
jah128 | 0:d6269d17c8cf | 243 | void IF_handle_events() |
jah128 | 0:d6269d17c8cf | 244 | { |
jah128 | 0:d6269d17c8cf | 245 | // This is the main 'operating system' thread that handles events from robot stimuli |
jah128 | 0:d6269d17c8cf | 246 | // By default it is run every 1ms and checks if there are events to handle |
jah128 | 1:060690a934a9 | 247 | if(event > 0) { |
jah128 | 0:d6269d17c8cf | 248 | // 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 | 249 | if(encoder_event == 1) { |
jah128 | 0:d6269d17c8cf | 250 | // The encoders have changed; update the encoder values |
jah128 | 0:d6269d17c8cf | 251 | IF_update_encoders(); |
jah128 | 1:060690a934a9 | 252 | encoder_event = 0; |
jah128 | 1:060690a934a9 | 253 | event--; |
jah128 | 0:d6269d17c8cf | 254 | } else { |
jah128 | 1:060690a934a9 | 255 | if(switch_event == 1) { |
jah128 | 1:060690a934a9 | 256 | IF_update_switch(); |
jah128 | 1:060690a934a9 | 257 | switch_event = 0; |
jah128 | 1:060690a934a9 | 258 | event--; |
jah128 | 0:d6269d17c8cf | 259 | } |
jah128 | 1:060690a934a9 | 260 | if(change_id_event == 1) { |
jah128 | 0:d6269d17c8cf | 261 | // The user ID for the robot has been changed |
jah128 | 0:d6269d17c8cf | 262 | IF_update_user_id(); |
jah128 | 0:d6269d17c8cf | 263 | change_id_event = 0; |
jah128 | 0:d6269d17c8cf | 264 | event--; |
jah128 | 0:d6269d17c8cf | 265 | } |
jah128 | 0:d6269d17c8cf | 266 | } |
jah128 | 1:060690a934a9 | 267 | } |
jah128 | 0:d6269d17c8cf | 268 | } |
jah128 | 0:d6269d17c8cf | 269 | |
jah128 | 0:d6269d17c8cf | 270 | void IF_update_encoders() |
jah128 | 0:d6269d17c8cf | 271 | { |
jah128 | 0:d6269d17c8cf | 272 | char rwep = previous_wheel_encoder_byte >> 2; |
jah128 | 0:d6269d17c8cf | 273 | char rwe = wheel_encoder_byte >> 2; |
jah128 | 0:d6269d17c8cf | 274 | char lwep = previous_wheel_encoder_byte % 4; |
jah128 | 0:d6269d17c8cf | 275 | char lwe = wheel_encoder_byte % 4; |
jah128 | 0:d6269d17c8cf | 276 | //pc.printf("L:%d P:%d R:%d P:%d \n",lwe,lwep,rwe,rwep); |
jah128 | 0:d6269d17c8cf | 277 | if(lwe == 0 && lwep==1) left_encoder++; |
jah128 | 0:d6269d17c8cf | 278 | if(lwe == 0 && lwep==2) left_encoder--; |
jah128 | 0:d6269d17c8cf | 279 | if(rwe == 0 && rwep==1) right_encoder++; |
jah128 | 0:d6269d17c8cf | 280 | if(rwe == 0 && rwep==2) right_encoder--; |
jah128 | 0:d6269d17c8cf | 281 | if(left_encoder % 100 == 0) pc.printf("L:%d\n",left_encoder); |
jah128 | 0:d6269d17c8cf | 282 | } |
jah128 | 0:d6269d17c8cf | 283 | |
jah128 | 0:d6269d17c8cf | 284 | void IF_update_user_id() |
jah128 | 0:d6269d17c8cf | 285 | { |
jah128 | 0:d6269d17c8cf | 286 | } |
jah128 | 0:d6269d17c8cf | 287 | |
jah128 | 0:d6269d17c8cf | 288 | void IF_update_switch() |
jah128 | 0:d6269d17c8cf | 289 | { |
jah128 | 0:d6269d17c8cf | 290 | // The user switch has changed state |
jah128 | 0:d6269d17c8cf | 291 | // In this implementation we will only act on positive changes (rising edges) |
jah128 | 0:d6269d17c8cf | 292 | // Subtracting new_state from (new_state & old_state) gives the positive changes |
jah128 | 0:d6269d17c8cf | 293 | char positive_change = switch_byte - (switch_byte & previous_switch_byte); |
jah128 | 0:d6269d17c8cf | 294 | if(demo_on) demo_handle_switch_event(positive_change); |
jah128 | 0:d6269d17c8cf | 295 | else handle_switch_event(positive_change); |
jah128 | 0:d6269d17c8cf | 296 | } |
jah128 | 0:d6269d17c8cf | 297 | |
jah128 | 0:d6269d17c8cf | 298 | void reset_encoders() |
jah128 | 0:d6269d17c8cf | 299 | { |
jah128 | 0:d6269d17c8cf | 300 | left_encoder = 0; |
jah128 | 1:060690a934a9 | 301 | right_encoder = 0; |
jah128 | 0:d6269d17c8cf | 302 | } |
jah128 | 0:d6269d17c8cf | 303 | |
jah128 | 1:060690a934a9 | 304 | void debug(const char* format, ...) |
jah128 | 0:d6269d17c8cf | 305 | { |
jah128 | 1:060690a934a9 | 306 | char buffer[256]; |
jah128 | 1:060690a934a9 | 307 | if (debug_mode) { |
jah128 | 1:060690a934a9 | 308 | va_list vl; |
jah128 | 1:060690a934a9 | 309 | va_start(vl, format); |
jah128 | 1:060690a934a9 | 310 | vsprintf(buffer,format,vl); |
jah128 | 1:060690a934a9 | 311 | if(debug_output & 2) bt.printf("%s", buffer); |
jah128 | 1:060690a934a9 | 312 | if(debug_output & 1) pc.printf("%s", buffer); |
jah128 | 1:060690a934a9 | 313 | if(debug_output & 4) display.debug_page(buffer,strlen(buffer)); |
jah128 | 1:060690a934a9 | 314 | va_end(vl); |
jah128 | 1:060690a934a9 | 315 | } |
jah128 | 0:d6269d17c8cf | 316 | } |
jah128 | 0:d6269d17c8cf | 317 | |
jah128 | 0:d6269d17c8cf | 318 | float get_uptime(void) |
jah128 | 0:d6269d17c8cf | 319 | { |
jah128 | 1:060690a934a9 | 320 | return uptime.read() + (timer_minute_count * 60); |
jah128 | 0:d6269d17c8cf | 321 | } |
jah128 | 0:d6269d17c8cf | 322 | |
jah128 | 1:060690a934a9 | 323 | void pause_user_code(float period) |
jah128 | 1:060690a934a9 | 324 | { |
jah128 | 0:d6269d17c8cf | 325 | user_code_restore_mode = user_code_running; |
jah128 | 0:d6269d17c8cf | 326 | user_code_running = 0; |
jah128 | 0:d6269d17c8cf | 327 | pause_usercode_timeout.attach(&IF_end_pause_user_code, period); |
jah128 | 0:d6269d17c8cf | 328 | } |
jah128 | 0:d6269d17c8cf | 329 | |
jah128 | 1:060690a934a9 | 330 | void IF_end_pause_user_code() |
jah128 | 1:060690a934a9 | 331 | { |
jah128 | 1:060690a934a9 | 332 | user_code_running = user_code_restore_mode; |
jah128 | 1:060690a934a9 | 333 | } |
jah128 | 1:060690a934a9 | 334 | |
jah128 | 1:060690a934a9 | 335 | void IF_get_hardware_description() |
jah128 | 1:060690a934a9 | 336 | { |
jah128 | 1:060690a934a9 | 337 | debug("- Robot serial number %1.2f\n",serial_number); |
jah128 | 1:060690a934a9 | 338 | debug("- PCB version %1.2f\n",pcb_version); |
jah128 | 1:060690a934a9 | 339 | debug("- Hardware: "); |
jah128 | 1:060690a934a9 | 340 | if(has_compass) debug("COMPASS, "); |
jah128 | 1:060690a934a9 | 341 | if(has_side_ir) debug("SIDE IR, "); |
jah128 | 1:060690a934a9 | 342 | if(has_base_ir) debug("BASE IR, "); |
jah128 | 1:060690a934a9 | 343 | if(has_base_colour_sensor) debug("BASE COLOUR, "); |
jah128 | 1:060690a934a9 | 344 | if(has_top_colour_sensor) debug("TOP COLOUR, "); |
jah128 | 1:060690a934a9 | 345 | if(has_wheel_encoders) debug("WHEEL ENC., "); |
jah128 | 1:060690a934a9 | 346 | if(has_audio_pic) debug("AUDIO, "); |
jah128 | 1:060690a934a9 | 347 | if(has_ultrasonic_sensor) debug("ULTRASONIC, "); |
jah128 | 1:060690a934a9 | 348 | if(has_temperature_sensor) debug("TEMPERATURE, "); |
jah128 | 1:060690a934a9 | 349 | if(has_recharging_circuit) debug("RECHARGING, "); |
jah128 | 1:060690a934a9 | 350 | if(has_433_radio) debug("433 RADIO."); |
jah128 | 1:060690a934a9 | 351 | debug("\n"); |
jah128 | 0:d6269d17c8cf | 352 | } |