![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Table controller for piswarm-office
Fork of PiSwarmTableController by
main.cpp@3:bc7a0f14b28a, 2014-06-10 (annotated)
- Committer:
- jah128
- Date:
- Tue Jun 10 11:08:58 2014 +0000
- Revision:
- 3:bc7a0f14b28a
- Parent:
- 2:c81f4ef63132
- Child:
- 4:3cbff30b7b7b
Pi Swarm Table Controller with IR Beacon\RF demo code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:d88fd55a27a6 | 1 | /* University of York Robot Lab |
jah128 | 0:d88fd55a27a6 | 2 | * |
jah128 | 1:e4a0d424ac8d | 3 | * Pi Swarm Table Controller Demo Code |
jah128 | 0:d88fd55a27a6 | 4 | * |
jah128 | 1:e4a0d424ac8d | 5 | * This file is intended for use exclusively with the Pi Swarm Table Controller (PCB 1.0) |
jah128 | 0:d88fd55a27a6 | 6 | * |
jah128 | 0:d88fd55a27a6 | 7 | * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York |
jah128 | 0:d88fd55a27a6 | 8 | * |
jah128 | 1:e4a0d424ac8d | 9 | * May 2014 |
jah128 | 0:d88fd55a27a6 | 10 | * |
jah128 | 0:d88fd55a27a6 | 11 | */ |
jah128 | 0:d88fd55a27a6 | 12 | |
jah128 | 0:d88fd55a27a6 | 13 | #include "mbed.h" |
jah128 | 2:c81f4ef63132 | 14 | #include "main.h" |
jah128 | 2:c81f4ef63132 | 15 | #include "communications.h" |
jah128 | 0:d88fd55a27a6 | 16 | #include "display.h" //Display driver for the Midas 16x2 I2C Display |
jah128 | 0:d88fd55a27a6 | 17 | |
jah128 | 2:c81f4ef63132 | 18 | |
jah128 | 1:e4a0d424ac8d | 19 | PwmOut ir_pwm_out(p21); //PWM Output for the IR LED driver |
jah128 | 1:e4a0d424ac8d | 20 | DigitalOut ir_led(LED1); |
jah128 | 2:c81f4ef63132 | 21 | DigitalOut tx_led(LED3); |
jah128 | 1:e4a0d424ac8d | 22 | DigitalOut pir_led(LED4); |
jah128 | 1:e4a0d424ac8d | 23 | AnalogIn input_1(p20); |
jah128 | 1:e4a0d424ac8d | 24 | AnalogIn input_2(p19); |
jah128 | 1:e4a0d424ac8d | 25 | DigitalIn input_3(p18); |
jah128 | 1:e4a0d424ac8d | 26 | DigitalIn input_4(p17); |
jah128 | 1:e4a0d424ac8d | 27 | DigitalIn input_5(p16); |
jah128 | 1:e4a0d424ac8d | 28 | DigitalIn input_6(p15); |
jah128 | 2:c81f4ef63132 | 29 | Serial pc (USBTX, USBRX); |
jah128 | 0:d88fd55a27a6 | 30 | |
jah128 | 0:d88fd55a27a6 | 31 | Display display; |
jah128 | 2:c81f4ef63132 | 32 | Alpha433 rf; |
jah128 | 3:bc7a0f14b28a | 33 | Timer ir_led_timer; //System timer is used for timer the on-off periods for the LEDs |
jah128 | 3:bc7a0f14b28a | 34 | Timer command_timer; //Timer for sending rf messages |
jah128 | 1:e4a0d424ac8d | 35 | Ticker polling_ticker; //Ticker for polling the input sensors |
jah128 | 0:d88fd55a27a6 | 36 | |
jah128 | 3:bc7a0f14b28a | 37 | int off_period = 975000; //Off-period for the IR LEDs in microseconds |
jah128 | 3:bc7a0f14b28a | 38 | int on_period = 25000; //On-period for the IR LEDs in microseconds |
jah128 | 2:c81f4ef63132 | 39 | char power = 1; //Output power for the IR LEDs : 0=25%, 1=50%, 2=75%, 3=100% (700mA) [NB The LEDs are rated 20mA and are in parallel runs of 20, so a maximum power of 50% is recommended for long term use] |
jah128 | 2:c81f4ef63132 | 40 | char use_ir_leds = 1; //Set to 0 to disable IR LEDs, 1 to enable |
jah128 | 2:c81f4ef63132 | 41 | |
jah128 | 2:c81f4ef63132 | 42 | char input_3_active = 0; |
jah128 | 2:c81f4ef63132 | 43 | char input_4_active = 0; |
jah128 | 2:c81f4ef63132 | 44 | char input_5_active = 0; |
jah128 | 2:c81f4ef63132 | 45 | char input_6_active = 0; |
jah128 | 2:c81f4ef63132 | 46 | |
jah128 | 2:c81f4ef63132 | 47 | int input_3_window = 0; |
jah128 | 2:c81f4ef63132 | 48 | int input_4_window = 0; |
jah128 | 2:c81f4ef63132 | 49 | int input_5_window = 0; |
jah128 | 2:c81f4ef63132 | 50 | int input_6_window = 0; |
jah128 | 2:c81f4ef63132 | 51 | |
jah128 | 2:c81f4ef63132 | 52 | float polling_rate = 0.1; // Period in seconds to poll sensors |
jah128 | 2:c81f4ef63132 | 53 | int polling_decay = 300; // Number of polls to decrement counters |
jah128 | 2:c81f4ef63132 | 54 | int polling_count = 0; |
jah128 | 0:d88fd55a27a6 | 55 | |
jah128 | 3:bc7a0f14b28a | 56 | void broadcast_user_rf_command(int function, char * message, int length) |
jah128 | 0:d88fd55a27a6 | 57 | { |
jah128 | 3:bc7a0f14b28a | 58 | //This function augments the communications stack |
jah128 | 3:bc7a0f14b28a | 59 | //It sends a 'user' RF command to all members (ie target_id = 0) |
jah128 | 3:bc7a0f14b28a | 60 | //It sends a 'request', not a 'command', meaning it will still be handled if commands are disabled (RF_ALLOW_COMMANDS set to 0, recommended) |
jah128 | 3:bc7a0f14b28a | 61 | //It takes three inputs: |
jah128 | 3:bc7a0f14b28a | 62 | // * function (an integer from 0 to 15) |
jah128 | 3:bc7a0f14b28a | 63 | // * message (a char array) |
jah128 | 3:bc7a0f14b28a | 64 | // * length (length of message in bytes) |
jah128 | 3:bc7a0f14b28a | 65 | send_rf_message(0,48+(function % 16),message,length); |
jah128 | 0:d88fd55a27a6 | 66 | } |
jah128 | 3:bc7a0f14b28a | 67 | |
jah128 | 0:d88fd55a27a6 | 68 | |
jah128 | 1:e4a0d424ac8d | 69 | int get_output_power(){ |
jah128 | 1:e4a0d424ac8d | 70 | switch(power){ |
jah128 | 1:e4a0d424ac8d | 71 | case 1: return 500; |
jah128 | 1:e4a0d424ac8d | 72 | case 2: return 750; |
jah128 | 1:e4a0d424ac8d | 73 | case 3: return 1000; |
jah128 | 1:e4a0d424ac8d | 74 | } |
jah128 | 1:e4a0d424ac8d | 75 | return 250; |
jah128 | 0:d88fd55a27a6 | 76 | } |
jah128 | 0:d88fd55a27a6 | 77 | |
jah128 | 1:e4a0d424ac8d | 78 | void polling(){ |
jah128 | 2:c81f4ef63132 | 79 | polling_count ++; |
jah128 | 2:c81f4ef63132 | 80 | char pir_active = 0; |
jah128 | 2:c81f4ef63132 | 81 | char state_changed = 0; |
jah128 | 2:c81f4ef63132 | 82 | if(input_3.read()==1){ |
jah128 | 2:c81f4ef63132 | 83 | pir_active = 1; |
jah128 | 2:c81f4ef63132 | 84 | if(input_3_active == 0){ |
jah128 | 2:c81f4ef63132 | 85 | state_changed = 1; |
jah128 | 2:c81f4ef63132 | 86 | input_3_active = 1; |
jah128 | 2:c81f4ef63132 | 87 | input_3_window ++; |
jah128 | 2:c81f4ef63132 | 88 | } |
jah128 | 2:c81f4ef63132 | 89 | }else { |
jah128 | 2:c81f4ef63132 | 90 | input_3_active = 0; |
jah128 | 2:c81f4ef63132 | 91 | if(polling_count == polling_decay){ |
jah128 | 2:c81f4ef63132 | 92 | input_3_window --; |
jah128 | 2:c81f4ef63132 | 93 | if(input_3_window < 0) input_3_window = 0; |
jah128 | 2:c81f4ef63132 | 94 | } |
jah128 | 2:c81f4ef63132 | 95 | } |
jah128 | 2:c81f4ef63132 | 96 | |
jah128 | 2:c81f4ef63132 | 97 | if(input_4.read()==1){ |
jah128 | 2:c81f4ef63132 | 98 | pir_active = 1; |
jah128 | 2:c81f4ef63132 | 99 | if(input_4_active == 0){ |
jah128 | 2:c81f4ef63132 | 100 | state_changed = 1; |
jah128 | 2:c81f4ef63132 | 101 | input_4_active = 1; |
jah128 | 2:c81f4ef63132 | 102 | input_4_window ++; |
jah128 | 2:c81f4ef63132 | 103 | } |
jah128 | 2:c81f4ef63132 | 104 | }else { |
jah128 | 2:c81f4ef63132 | 105 | input_4_active = 0; |
jah128 | 2:c81f4ef63132 | 106 | if(polling_count == polling_decay){ |
jah128 | 2:c81f4ef63132 | 107 | input_4_window --; |
jah128 | 2:c81f4ef63132 | 108 | if(input_4_window < 0) input_4_window = 0; |
jah128 | 2:c81f4ef63132 | 109 | } |
jah128 | 2:c81f4ef63132 | 110 | } |
jah128 | 2:c81f4ef63132 | 111 | |
jah128 | 2:c81f4ef63132 | 112 | if(input_5.read()==1){ |
jah128 | 2:c81f4ef63132 | 113 | pir_active = 1; |
jah128 | 2:c81f4ef63132 | 114 | if(input_5_active == 0){ |
jah128 | 2:c81f4ef63132 | 115 | state_changed = 1; |
jah128 | 2:c81f4ef63132 | 116 | input_5_active = 1; |
jah128 | 2:c81f4ef63132 | 117 | input_5_window ++; |
jah128 | 2:c81f4ef63132 | 118 | } |
jah128 | 2:c81f4ef63132 | 119 | }else { |
jah128 | 2:c81f4ef63132 | 120 | input_5_active = 0; |
jah128 | 2:c81f4ef63132 | 121 | if(polling_count == polling_decay){ |
jah128 | 2:c81f4ef63132 | 122 | input_5_window --; |
jah128 | 2:c81f4ef63132 | 123 | if(input_5_window < 0) input_5_window = 0; |
jah128 | 2:c81f4ef63132 | 124 | } |
jah128 | 2:c81f4ef63132 | 125 | } |
jah128 | 2:c81f4ef63132 | 126 | |
jah128 | 2:c81f4ef63132 | 127 | if(input_6.read()==1){ |
jah128 | 2:c81f4ef63132 | 128 | pir_active = 1; |
jah128 | 2:c81f4ef63132 | 129 | if(input_6_active == 0){ |
jah128 | 2:c81f4ef63132 | 130 | state_changed = 1; |
jah128 | 2:c81f4ef63132 | 131 | input_6_active = 1; |
jah128 | 2:c81f4ef63132 | 132 | input_6_window ++; |
jah128 | 2:c81f4ef63132 | 133 | } |
jah128 | 2:c81f4ef63132 | 134 | }else { |
jah128 | 2:c81f4ef63132 | 135 | input_6_active = 0; |
jah128 | 2:c81f4ef63132 | 136 | if(polling_count == polling_decay){ |
jah128 | 2:c81f4ef63132 | 137 | input_6_window --; |
jah128 | 2:c81f4ef63132 | 138 | if(input_6_window < 0) input_6_window = 0; |
jah128 | 2:c81f4ef63132 | 139 | } |
jah128 | 2:c81f4ef63132 | 140 | } |
jah128 | 2:c81f4ef63132 | 141 | |
jah128 | 2:c81f4ef63132 | 142 | pir_led=pir_active; |
jah128 | 2:c81f4ef63132 | 143 | if(polling_count == polling_decay) { |
jah128 | 2:c81f4ef63132 | 144 | polling_count = 0; |
jah128 | 2:c81f4ef63132 | 145 | state_changed = 1; |
jah128 | 2:c81f4ef63132 | 146 | } |
jah128 | 2:c81f4ef63132 | 147 | if(state_changed == 1){ |
jah128 | 2:c81f4ef63132 | 148 | display.clear_display(); |
jah128 | 2:c81f4ef63132 | 149 | display.set_position(0,0); |
jah128 | 2:c81f4ef63132 | 150 | char text [17]; |
jah128 | 2:c81f4ef63132 | 151 | snprintf(text,17,"3:%i 4:%i 5:%i 6:%i",input_3_window,input_4_window,input_5_window,input_6_window); |
jah128 | 2:c81f4ef63132 | 152 | display.write_string(text,16); |
jah128 | 2:c81f4ef63132 | 153 | } |
jah128 | 0:d88fd55a27a6 | 154 | } |
jah128 | 0:d88fd55a27a6 | 155 | |
jah128 | 0:d88fd55a27a6 | 156 | int main() |
jah128 | 0:d88fd55a27a6 | 157 | { |
jah128 | 0:d88fd55a27a6 | 158 | init(); |
jah128 | 1:e4a0d424ac8d | 159 | char phase = 0; |
jah128 | 3:bc7a0f14b28a | 160 | char motor_step = 0; |
jah128 | 3:bc7a0f14b28a | 161 | ir_led_timer.start(); |
jah128 | 3:bc7a0f14b28a | 162 | //command_timer.start(); |
jah128 | 1:e4a0d424ac8d | 163 | ir_pwm_out.period_us(1000); |
jah128 | 1:e4a0d424ac8d | 164 | ir_pwm_out.pulsewidth_us(0); |
jah128 | 1:e4a0d424ac8d | 165 | polling_ticker.attach(&polling,0.1); |
jah128 | 1:e4a0d424ac8d | 166 | |
jah128 | 0:d88fd55a27a6 | 167 | |
jah128 | 0:d88fd55a27a6 | 168 | while(1) { |
jah128 | 3:bc7a0f14b28a | 169 | if(command_timer.read_us() > 500000){ |
jah128 | 3:bc7a0f14b28a | 170 | command_timer.reset(); |
jah128 | 3:bc7a0f14b28a | 171 | motor_step ++; |
jah128 | 3:bc7a0f14b28a | 172 | if(motor_step > 4) motor_step = 0; |
jah128 | 3:bc7a0f14b28a | 173 | switch(motor_step){ |
jah128 | 3:bc7a0f14b28a | 174 | case 0: broadcast_user_rf_command(0,"Hello",5);break; |
jah128 | 3:bc7a0f14b28a | 175 | case 1: broadcast_user_rf_command(1,"there",5);break; |
jah128 | 3:bc7a0f14b28a | 176 | case 2: broadcast_user_rf_command(2,"you",3);break; |
jah128 | 3:bc7a0f14b28a | 177 | case 3: broadcast_user_rf_command(3,"munters",7);break; |
jah128 | 3:bc7a0f14b28a | 178 | } |
jah128 | 3:bc7a0f14b28a | 179 | } |
jah128 | 0:d88fd55a27a6 | 180 | if(phase==0){ |
jah128 | 3:bc7a0f14b28a | 181 | if(ir_led_timer.read_us() >= off_period){ |
jah128 | 3:bc7a0f14b28a | 182 | ir_led_timer.reset(); |
jah128 | 1:e4a0d424ac8d | 183 | int pw = get_output_power(); |
jah128 | 1:e4a0d424ac8d | 184 | if(use_ir_leds) ir_pwm_out.pulsewidth_us(pw); |
jah128 | 1:e4a0d424ac8d | 185 | ir_led=1; |
jah128 | 0:d88fd55a27a6 | 186 | phase = 1; |
jah128 | 0:d88fd55a27a6 | 187 | } |
jah128 | 0:d88fd55a27a6 | 188 | }else{ |
jah128 | 3:bc7a0f14b28a | 189 | if(ir_led_timer.read_us() >= on_period){ |
jah128 | 3:bc7a0f14b28a | 190 | ir_led_timer.reset(); |
jah128 | 1:e4a0d424ac8d | 191 | ir_pwm_out.pulsewidth_us(0); |
jah128 | 1:e4a0d424ac8d | 192 | ir_led=0; |
jah128 | 0:d88fd55a27a6 | 193 | phase = 0; |
jah128 | 0:d88fd55a27a6 | 194 | } |
jah128 | 0:d88fd55a27a6 | 195 | } |
jah128 | 0:d88fd55a27a6 | 196 | } |
jah128 | 2:c81f4ef63132 | 197 | } |
jah128 | 2:c81f4ef63132 | 198 | |
jah128 | 2:c81f4ef63132 | 199 | |
jah128 | 2:c81f4ef63132 | 200 | void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){ |
jah128 | 2:c81f4ef63132 | 201 | } |
jah128 | 2:c81f4ef63132 | 202 | |
jah128 | 2:c81f4ef63132 | 203 | void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){ |
jah128 | 2:c81f4ef63132 | 204 | } |
jah128 | 2:c81f4ef63132 | 205 | |
jah128 | 2:c81f4ef63132 | 206 | void processRawRFData(char * rstring, char cCount){ |
jah128 | 2:c81f4ef63132 | 207 | } |
jah128 | 2:c81f4ef63132 | 208 | |
jah128 | 2:c81f4ef63132 | 209 | |
jah128 | 2:c81f4ef63132 | 210 | void handleData(char * data, char length) |
jah128 | 2:c81f4ef63132 | 211 | { |
jah128 | 2:c81f4ef63132 | 212 | display.set_position(1,1); |
jah128 | 2:c81f4ef63132 | 213 | display.write_string(data,length); |
jah128 | 2:c81f4ef63132 | 214 | } |
jah128 | 3:bc7a0f14b28a | 215 | |
jah128 | 3:bc7a0f14b28a | 216 | |
jah128 | 3:bc7a0f14b28a | 217 | void init() |
jah128 | 3:bc7a0f14b28a | 218 | { |
jah128 | 3:bc7a0f14b28a | 219 | display.init_display(); |
jah128 | 3:bc7a0f14b28a | 220 | display.set_position(0,2); |
jah128 | 3:bc7a0f14b28a | 221 | display.write_string("YORK ROBOTICS",13); |
jah128 | 3:bc7a0f14b28a | 222 | display.set_position(1,3); |
jah128 | 3:bc7a0f14b28a | 223 | display.write_string("LABORATORY",10); |
jah128 | 3:bc7a0f14b28a | 224 | wait(0.45); |
jah128 | 3:bc7a0f14b28a | 225 | display.clear_display(); |
jah128 | 3:bc7a0f14b28a | 226 | display.set_position(0,1); |
jah128 | 3:bc7a0f14b28a | 227 | display.write_string("Pi Swarm Table",14); |
jah128 | 3:bc7a0f14b28a | 228 | display.set_position(1,3); |
jah128 | 3:bc7a0f14b28a | 229 | display.write_string("Controller",10); |
jah128 | 3:bc7a0f14b28a | 230 | wait(0.45); |
jah128 | 3:bc7a0f14b28a | 231 | rf.rf_init(); |
jah128 | 3:bc7a0f14b28a | 232 | rf.setFrequency(435000000); |
jah128 | 3:bc7a0f14b28a | 233 | rf.setDatarate(57600); |
jah128 | 3:bc7a0f14b28a | 234 | } |