Table controller for piswarm-office
Fork of PiSwarmTableController by
main.cpp
- Committer:
- jah128
- Date:
- 2014-05-22
- Revision:
- 2:c81f4ef63132
- Parent:
- 1:e4a0d424ac8d
- Child:
- 3:bc7a0f14b28a
File content as of revision 2:c81f4ef63132:
/* University of York Robot Lab * * Pi Swarm Table Controller Demo Code * * This file is intended for use exclusively with the Pi Swarm Table Controller (PCB 1.0) * * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York * * May 2014 * */ #include "mbed.h" #include "main.h" #include "communications.h" #include "display.h" //Display driver for the Midas 16x2 I2C Display PwmOut ir_pwm_out(p21); //PWM Output for the IR LED driver DigitalOut ir_led(LED1); DigitalOut tx_led(LED3); DigitalOut pir_led(LED4); AnalogIn input_1(p20); AnalogIn input_2(p19); DigitalIn input_3(p18); DigitalIn input_4(p17); DigitalIn input_5(p16); DigitalIn input_6(p15); Serial pc (USBTX, USBRX); Display display; Alpha433 rf; Timer system_timer; //System timer is used for timer the on-off periods for the LEDs Ticker polling_ticker; //Ticker for polling the input sensors int off_period = 950000; //Off-period for the IR LEDs in microseconds int on_period = 50000; //On-period for the IR LEDs in microseconds 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] char use_ir_leds = 1; //Set to 0 to disable IR LEDs, 1 to enable char input_3_active = 0; char input_4_active = 0; char input_5_active = 0; char input_6_active = 0; int input_3_window = 0; int input_4_window = 0; int input_5_window = 0; int input_6_window = 0; float polling_rate = 0.1; // Period in seconds to poll sensors int polling_decay = 300; // Number of polls to decrement counters int polling_count = 0; void init() { display.init_display(); display.set_position(0,2); display.write_string("YORK ROBOTICS",13); display.set_position(1,3); display.write_string("LABORATORY",10); wait(0.45); display.clear_display(); display.set_position(0,1); display.write_string("Pi Swarm Table",14); display.set_position(1,3); display.write_string("Controller",10); wait(0.45); } int get_output_power(){ switch(power){ case 1: return 500; case 2: return 750; case 3: return 1000; } return 250; } void polling(){ polling_count ++; char pir_active = 0; char state_changed = 0; if(input_3.read()==1){ pir_active = 1; if(input_3_active == 0){ state_changed = 1; input_3_active = 1; input_3_window ++; } }else { input_3_active = 0; if(polling_count == polling_decay){ input_3_window --; if(input_3_window < 0) input_3_window = 0; } } if(input_4.read()==1){ pir_active = 1; if(input_4_active == 0){ state_changed = 1; input_4_active = 1; input_4_window ++; } }else { input_4_active = 0; if(polling_count == polling_decay){ input_4_window --; if(input_4_window < 0) input_4_window = 0; } } if(input_5.read()==1){ pir_active = 1; if(input_5_active == 0){ state_changed = 1; input_5_active = 1; input_5_window ++; } }else { input_5_active = 0; if(polling_count == polling_decay){ input_5_window --; if(input_5_window < 0) input_5_window = 0; } } if(input_6.read()==1){ pir_active = 1; if(input_6_active == 0){ state_changed = 1; input_6_active = 1; input_6_window ++; } }else { input_6_active = 0; if(polling_count == polling_decay){ input_6_window --; if(input_6_window < 0) input_6_window = 0; } } pir_led=pir_active; if(polling_count == polling_decay) { polling_count = 0; state_changed = 1; } if(state_changed == 1){ display.clear_display(); display.set_position(0,0); char text [17]; snprintf(text,17,"3:%i 4:%i 5:%i 6:%i",input_3_window,input_4_window,input_5_window,input_6_window); display.write_string(text,16); } } int main() { init(); char phase = 0; system_timer.start(); ir_pwm_out.period_us(1000); ir_pwm_out.pulsewidth_us(0); polling_ticker.attach(&polling,0.1); while(1) { if(phase==0){ if(system_timer.read_us() >= off_period){ system_timer.reset(); int pw = get_output_power(); if(use_ir_leds) ir_pwm_out.pulsewidth_us(pw); ir_led=1; phase = 1; } }else{ if(system_timer.read_us() >= on_period){ system_timer.reset(); ir_pwm_out.pulsewidth_us(0); ir_led=0; phase = 0; } } } } void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){ } void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){ } void processRawRFData(char * rstring, char cCount){ } void handleData(char * data, char length) { display.set_position(1,1); display.write_string(data,length); }