ft. button press reset
Dependencies: mbed
Fork of BeaconDemo_RobotCodeNew by
main.cpp@8:00558287a4ef, 2015-10-22 (annotated)
- Committer:
- jah128
- Date:
- Thu Oct 22 15:36:16 2015 +0000
- Revision:
- 8:00558287a4ef
- Parent:
- 7:ef9ab01b9e26
- Child:
- 9:085e090e1ec1
Added time_based_turn_degrees
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 6:ff3c66f7372b | 1 | /*********************************************************************** |
jah128 | 0:8a5497a2e366 | 2 | ** ██████╗ ███████╗██╗███████╗██╗ ██╗ █████╗ ██████╗ ███╗ ███╗ ** |
jah128 | 0:8a5497a2e366 | 3 | ** ██╔══██╗██╔════╝██║██╔════╝██║ ██║██╔══██╗██╔══██╗████╗ ████║ ** |
jah128 | 0:8a5497a2e366 | 4 | ** ██████╔╝███████╗██║███████╗██║ █╗ ██║███████║██████╔╝██╔████╔██║ ** |
jah128 | 0:8a5497a2e366 | 5 | ** ██╔═══╝ ╚════██║██║╚════██║██║███╗██║██╔══██║██╔══██╗██║╚██╔╝██║ ** |
jah128 | 0:8a5497a2e366 | 6 | ** ██║ ███████║██║███████║╚███╔███╔╝██║ ██║██║ ██║██║ ╚═╝ ██║ ** |
jah128 | 0:8a5497a2e366 | 7 | ** ╚═╝ ╚══════╝╚═╝╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ** |
jah128 | 0:8a5497a2e366 | 8 | ************************************************************************ |
jah128 | 0:8a5497a2e366 | 9 | **(C) Dr James Hilder - York Robotics Laboratory - University of York ** |
jah128 | 0:8a5497a2e366 | 10 | ***********************************************************************/ |
jah128 | 0:8a5497a2e366 | 11 | |
jah128 | 6:ff3c66f7372b | 12 | /// PsiSwarm Beautiful Meme Project Source Code |
jah128 | 6:ff3c66f7372b | 13 | /// Version 0.1 |
jah128 | 6:ff3c66f7372b | 14 | /// James Hilder, Alan Millard, Homero Elizondo, Jon Timmis |
jah128 | 6:ff3c66f7372b | 15 | /// University of York |
jah128 | 0:8a5497a2e366 | 16 | |
jah128 | 1:f6356cf1cefc | 17 | /// Include psiswarm.h - this includes all the other necessary core files |
jah128 | 0:8a5497a2e366 | 18 | #include "psiswarm.h" |
jah128 | 0:8a5497a2e366 | 19 | |
jah128 | 6:ff3c66f7372b | 20 | // IMPORTANT!!! |
jah128 | 6:ff3c66f7372b | 21 | // Do not call the IR functions at all as they will interfere with the correct operation of this program |
jah128 | 6:ff3c66f7372b | 22 | // Instead, use the values held in the variables below; they are updated every 500ms |
jah128 | 6:ff3c66f7372b | 23 | |
jah128 | 6:ff3c66f7372b | 24 | char beacon_found = 0; // This will be a 1 when a beacon was detected during the previous 500ms window |
jah128 | 6:ff3c66f7372b | 25 | int beacon_heading = 0; // This is the heading from the last time a beacon was detected |
jah128 | 6:ff3c66f7372b | 26 | char robots_found[8]; // These will be a 1 when the respective robot [excluding self] was detected during the previous 500ms window |
jah128 | 7:ef9ab01b9e26 | 27 | int robots_heading[8]; // These are the headings from the last time the respective robots were detected |
jah128 | 6:ff3c66f7372b | 28 | unsigned short robots_distance[8]; // This is the maximum sensor value from the last time the respective robot was detected |
jah128 | 6:ff3c66f7372b | 29 | unsigned short reflected_sensor_data[8]; // The reflected IR values when this robots emitters are on |
jah128 | 6:ff3c66f7372b | 30 | unsigned short background_sensor_data[8];// The raw IR values when no robot (or beacon) should have its IR on |
jah128 | 6:ff3c66f7372b | 31 | |
jah128 | 6:ff3c66f7372b | 32 | char * program_name = "B-Meme"; |
jah128 | 4:25039ea5eb09 | 33 | char * author_name = "YRL"; |
jah128 | 1:f6356cf1cefc | 34 | char * version_name = "1.0"; |
jah128 | 0:8a5497a2e366 | 35 | |
jah128 | 6:ff3c66f7372b | 36 | char user_code_debug = 1; // Set to 1 to show terminal messages from "out" function [specific to this code] |
jah128 | 0:8a5497a2e366 | 37 | |
jah128 | 0:8a5497a2e366 | 38 | |
jah128 | 1:f6356cf1cefc | 39 | ///Place user code here that should be run after initialisation but before the main loop |
jah128 | 6:ff3c66f7372b | 40 | void user_code_setup() |
jah128 | 6:ff3c66f7372b | 41 | { |
jah128 | 8:00558287a4ef | 42 | for(int i=0;i<10;i++){ |
jah128 | 8:00558287a4ef | 43 | time_based_turn_degrees(1, 90); |
jah128 | 8:00558287a4ef | 44 | wait(1); |
jah128 | 8:00558287a4ef | 45 | } |
jah128 | 8:00558287a4ef | 46 | |
jah128 | 6:ff3c66f7372b | 47 | out("------------------------------------------------------\n"); |
jah128 | 6:ff3c66f7372b | 48 | out("Beautiful Meme Project Demo Code \n"); |
jah128 | 6:ff3c66f7372b | 49 | out("------------------------------------------------------\n"); |
jah128 | 6:ff3c66f7372b | 50 | locate_beacon(); |
jah128 | 6:ff3c66f7372b | 51 | while(beacon_found == 0) { |
jah128 | 6:ff3c66f7372b | 52 | locate_beacon(); |
jah128 | 6:ff3c66f7372b | 53 | } |
jah128 | 0:8a5497a2e366 | 54 | |
jah128 | 6:ff3c66f7372b | 55 | start_infrared_timers(); |
jah128 | 0:8a5497a2e366 | 56 | } |
jah128 | 0:8a5497a2e366 | 57 | |
jah128 | 1:f6356cf1cefc | 58 | ///This function is the loop where user code should be placed |
jah128 | 6:ff3c66f7372b | 59 | void user_code_loop() |
jah128 | 6:ff3c66f7372b | 60 | { |
jah128 | 4:25039ea5eb09 | 61 | |
jah128 | 1:f6356cf1cefc | 62 | ///Do not place code within a loop, but consider this function to be a loop that is always run |
jah128 | 1:f6356cf1cefc | 63 | ///unless the user code is externally paused (such as by debug or recharging system) |
jah128 | 6:ff3c66f7372b | 64 | |
jah128 | 6:ff3c66f7372b | 65 | //pc.printf("User loop code block\n"); |
jah128 | 6:ff3c66f7372b | 66 | |
jah128 | 6:ff3c66f7372b | 67 | |
jah128 | 6:ff3c66f7372b | 68 | wait(0.5); |
jah128 | 0:8a5497a2e366 | 69 | } |
jah128 | 0:8a5497a2e366 | 70 | |
jah128 | 0:8a5497a2e366 | 71 | |
jah128 | 1:f6356cf1cefc | 72 | /// Code goes here to handle what should happen when the user switch is pressed |
jah128 | 0:8a5497a2e366 | 73 | void handle_switch_event(char switch_state) |
jah128 | 0:8a5497a2e366 | 74 | { |
jah128 | 6:ff3c66f7372b | 75 | /// Switch_state = 1 if up is pressed, 2 if down is pressed, 4 if left is pressed, 8 if right is pressed and 16 if the center button is pressed |
jah128 | 1:f6356cf1cefc | 76 | /// NB For maximum compatability it is recommended to minimise reliance on center button press |
jah128 | 6:ff3c66f7372b | 77 | |
jah128 | 6:ff3c66f7372b | 78 | //pc.printf("User switch code block: %d\n",switch_state); |
jah128 | 0:8a5497a2e366 | 79 | } |
jah128 | 0:8a5497a2e366 | 80 | |
jah128 | 1:f6356cf1cefc | 81 | /// The main routine: it is recommended to leave this function alone and add user code to the above functions |
jah128 | 6:ff3c66f7372b | 82 | int main() |
jah128 | 6:ff3c66f7372b | 83 | { |
jah128 | 1:f6356cf1cefc | 84 | ///init() in psiswarm.cpp sets up the robot |
jah128 | 0:8a5497a2e366 | 85 | init(); |
jah128 | 0:8a5497a2e366 | 86 | user_code_setup(); |
jah128 | 0:8a5497a2e366 | 87 | user_code_running = 1; |
jah128 | 0:8a5497a2e366 | 88 | while(1) { |
jah128 | 6:ff3c66f7372b | 89 | if(user_code_running)user_code_loop(); |
jah128 | 6:ff3c66f7372b | 90 | if(demo_on) demo_mode(); |
jah128 | 6:ff3c66f7372b | 91 | //wait_us(5); |
jah128 | 6:ff3c66f7372b | 92 | } |
jah128 | 6:ff3c66f7372b | 93 | } |
jah128 | 6:ff3c66f7372b | 94 | |
jah128 | 6:ff3c66f7372b | 95 | /// Verbose output |
jah128 | 6:ff3c66f7372b | 96 | void out(const char* format, ...) |
jah128 | 6:ff3c66f7372b | 97 | { |
jah128 | 6:ff3c66f7372b | 98 | char buffer[256]; |
jah128 | 6:ff3c66f7372b | 99 | if (debug_mode) { |
jah128 | 6:ff3c66f7372b | 100 | va_list vl; |
jah128 | 6:ff3c66f7372b | 101 | va_start(vl, format); |
jah128 | 6:ff3c66f7372b | 102 | vsprintf(buffer,format,vl); |
jah128 | 6:ff3c66f7372b | 103 | if(user_code_debug == 1) pc.printf("%s", buffer); |
jah128 | 6:ff3c66f7372b | 104 | va_end(vl); |
jah128 | 0:8a5497a2e366 | 105 | } |
jah128 | 0:8a5497a2e366 | 106 | } |