Table controller for piswarm-office
Fork of PiSwarmTableController by
main.cpp@1:e4a0d424ac8d, 2014-05-21 (annotated)
- Committer:
- jah128
- Date:
- Wed May 21 12:47:29 2014 +0000
- Revision:
- 1:e4a0d424ac8d
- Parent:
- 0:d88fd55a27a6
- Child:
- 2:c81f4ef63132
Pi Swarm Table Controller Draft (no RF)
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 | 0:d88fd55a27a6 | 14 | #include "display.h" //Display driver for the Midas 16x2 I2C Display |
jah128 | 0:d88fd55a27a6 | 15 | |
jah128 | 1:e4a0d424ac8d | 16 | PwmOut ir_pwm_out(p21); //PWM Output for the IR LED driver |
jah128 | 1:e4a0d424ac8d | 17 | DigitalOut ir_led(LED1); |
jah128 | 1:e4a0d424ac8d | 18 | DigitalOut pir_led(LED4); |
jah128 | 1:e4a0d424ac8d | 19 | AnalogIn input_1(p20); |
jah128 | 1:e4a0d424ac8d | 20 | AnalogIn input_2(p19); |
jah128 | 1:e4a0d424ac8d | 21 | DigitalIn input_3(p18); |
jah128 | 1:e4a0d424ac8d | 22 | DigitalIn input_4(p17); |
jah128 | 1:e4a0d424ac8d | 23 | DigitalIn input_5(p16); |
jah128 | 1:e4a0d424ac8d | 24 | DigitalIn input_6(p15); |
jah128 | 0:d88fd55a27a6 | 25 | |
jah128 | 0:d88fd55a27a6 | 26 | Display display; |
jah128 | 0:d88fd55a27a6 | 27 | Timer system_timer; //System timer is used for timer the on-off periods for the LEDs |
jah128 | 1:e4a0d424ac8d | 28 | Ticker polling_ticker; //Ticker for polling the input sensors |
jah128 | 0:d88fd55a27a6 | 29 | |
jah128 | 1:e4a0d424ac8d | 30 | int off_period = 950000; //Off-period for the IR LEDs in microseconds |
jah128 | 1:e4a0d424ac8d | 31 | int on_period = 50000; //On-period for the IR LEDs in microseconds |
jah128 | 1:e4a0d424ac8d | 32 | char power = 0; //Output power for the IR LEDs : 0=25%, 1=50%, 2=75%, 3=100% (700mA) |
jah128 | 1:e4a0d424ac8d | 33 | char use_ir_leds = 0; //Set to 0 to disable IR LEDs, 1 to enable |
jah128 | 0:d88fd55a27a6 | 34 | |
jah128 | 0:d88fd55a27a6 | 35 | void init() |
jah128 | 0:d88fd55a27a6 | 36 | { |
jah128 | 0:d88fd55a27a6 | 37 | display.init_display(); |
jah128 | 0:d88fd55a27a6 | 38 | display.set_position(0,2); |
jah128 | 0:d88fd55a27a6 | 39 | display.write_string("YORK ROBOTICS",13); |
jah128 | 0:d88fd55a27a6 | 40 | display.set_position(1,3); |
jah128 | 0:d88fd55a27a6 | 41 | display.write_string("LABORATORY",10); |
jah128 | 0:d88fd55a27a6 | 42 | wait(0.45); |
jah128 | 0:d88fd55a27a6 | 43 | display.clear_display(); |
jah128 | 1:e4a0d424ac8d | 44 | display.set_position(0,1); |
jah128 | 1:e4a0d424ac8d | 45 | display.write_string("Pi Swarm Table",14); |
jah128 | 0:d88fd55a27a6 | 46 | display.set_position(1,3); |
jah128 | 1:e4a0d424ac8d | 47 | display.write_string("Controller",10); |
jah128 | 0:d88fd55a27a6 | 48 | wait(0.45); |
jah128 | 0:d88fd55a27a6 | 49 | } |
jah128 | 0:d88fd55a27a6 | 50 | |
jah128 | 1:e4a0d424ac8d | 51 | int get_output_power(){ |
jah128 | 1:e4a0d424ac8d | 52 | switch(power){ |
jah128 | 1:e4a0d424ac8d | 53 | case 1: return 500; |
jah128 | 1:e4a0d424ac8d | 54 | case 2: return 750; |
jah128 | 1:e4a0d424ac8d | 55 | case 3: return 1000; |
jah128 | 1:e4a0d424ac8d | 56 | } |
jah128 | 1:e4a0d424ac8d | 57 | return 250; |
jah128 | 0:d88fd55a27a6 | 58 | } |
jah128 | 0:d88fd55a27a6 | 59 | |
jah128 | 1:e4a0d424ac8d | 60 | void polling(){ |
jah128 | 1:e4a0d424ac8d | 61 | pir_led=input_5.read(); |
jah128 | 0:d88fd55a27a6 | 62 | } |
jah128 | 0:d88fd55a27a6 | 63 | |
jah128 | 0:d88fd55a27a6 | 64 | int main() |
jah128 | 0:d88fd55a27a6 | 65 | { |
jah128 | 0:d88fd55a27a6 | 66 | init(); |
jah128 | 1:e4a0d424ac8d | 67 | char phase = 0; |
jah128 | 0:d88fd55a27a6 | 68 | system_timer.start(); |
jah128 | 1:e4a0d424ac8d | 69 | ir_pwm_out.period_us(1000); |
jah128 | 1:e4a0d424ac8d | 70 | ir_pwm_out.pulsewidth_us(0); |
jah128 | 1:e4a0d424ac8d | 71 | polling_ticker.attach(&polling,0.1); |
jah128 | 1:e4a0d424ac8d | 72 | |
jah128 | 1:e4a0d424ac8d | 73 | //Setup interrupts for |
jah128 | 0:d88fd55a27a6 | 74 | |
jah128 | 0:d88fd55a27a6 | 75 | while(1) { |
jah128 | 0:d88fd55a27a6 | 76 | if(phase==0){ |
jah128 | 1:e4a0d424ac8d | 77 | if(system_timer.read_us() >= off_period){ |
jah128 | 0:d88fd55a27a6 | 78 | system_timer.reset(); |
jah128 | 1:e4a0d424ac8d | 79 | int pw = get_output_power(); |
jah128 | 1:e4a0d424ac8d | 80 | if(use_ir_leds) ir_pwm_out.pulsewidth_us(pw); |
jah128 | 1:e4a0d424ac8d | 81 | ir_led=1; |
jah128 | 0:d88fd55a27a6 | 82 | phase = 1; |
jah128 | 0:d88fd55a27a6 | 83 | } |
jah128 | 0:d88fd55a27a6 | 84 | }else{ |
jah128 | 1:e4a0d424ac8d | 85 | if(system_timer.read_us() >= on_period){ |
jah128 | 0:d88fd55a27a6 | 86 | system_timer.reset(); |
jah128 | 1:e4a0d424ac8d | 87 | ir_pwm_out.pulsewidth_us(0); |
jah128 | 1:e4a0d424ac8d | 88 | ir_led=0; |
jah128 | 0:d88fd55a27a6 | 89 | phase = 0; |
jah128 | 0:d88fd55a27a6 | 90 | } |
jah128 | 0:d88fd55a27a6 | 91 | } |
jah128 | 0:d88fd55a27a6 | 92 | } |
jah128 | 0:d88fd55a27a6 | 93 | } |