Table controller for piswarm-office

Dependencies:   mbed

Fork of PiSwarmTableController by piswarm

Revision:
3:bc7a0f14b28a
Parent:
2:c81f4ef63132
Child:
4:3cbff30b7b7b
--- a/main.cpp	Thu May 22 14:25:10 2014 +0000
+++ b/main.cpp	Tue Jun 10 11:08:58 2014 +0000
@@ -30,11 +30,12 @@
 
 Display display;
 Alpha433 rf;
-Timer system_timer;         //System timer is used for timer the on-off periods for the LEDs
+Timer ir_led_timer;         //System timer is used for timer the on-off periods for the LEDs
+Timer command_timer;        //Timer for sending rf messages
 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
+int off_period = 975000;    //Off-period for the IR LEDs in microseconds
+int on_period = 25000;      //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
 
@@ -52,21 +53,18 @@
 int polling_decay = 300;    // Number of polls to decrement counters
 int polling_count = 0;
 
-void init()
+void broadcast_user_rf_command(int function, char * message, int length)
 {
-    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);
+    //This function augments the communications stack 
+    //It sends a 'user' RF command to all members (ie target_id = 0)
+    //It sends a 'request', not a 'command', meaning it will still be handled if commands are disabled (RF_ALLOW_COMMANDS set to 0, recommended)     
+    //It takes three inputs: 
+    // * function (an integer from 0 to 15) 
+    // * message  (a char array)
+    // * length   (length of message in bytes) 
+    send_rf_message(0,48+(function % 16),message,length);
 }
+        
 
 int get_output_power(){
     switch(power){
@@ -159,24 +157,37 @@
 {
     init();
     char phase = 0;
-    system_timer.start();
+    char motor_step = 0;
+    ir_led_timer.start();
+    //command_timer.start();
     ir_pwm_out.period_us(1000);
     ir_pwm_out.pulsewidth_us(0);
     polling_ticker.attach(&polling,0.1);
     
 
     while(1) {
+        if(command_timer.read_us() > 500000){
+            command_timer.reset();
+            motor_step ++;
+            if(motor_step > 4) motor_step = 0;
+            switch(motor_step){            
+                case 0: broadcast_user_rf_command(0,"Hello",5);break;
+                case 1: broadcast_user_rf_command(1,"there",5);break;   
+                case 2: broadcast_user_rf_command(2,"you",3);break;
+                case 3: broadcast_user_rf_command(3,"munters",7);break;   
+            }   
+        }
         if(phase==0){
-          if(system_timer.read_us() >= off_period){
-            system_timer.reset();
+          if(ir_led_timer.read_us() >= off_period){
+            ir_led_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();
+          if(ir_led_timer.read_us() >= on_period){
+            ir_led_timer.reset();
             ir_pwm_out.pulsewidth_us(0);
             ir_led=0;
             phase = 0;
@@ -201,3 +212,23 @@
     display.set_position(1,1);
     display.write_string(data,length);
 }
+
+
+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);
+    rf.rf_init();
+    rf.setFrequency(435000000);
+    rf.setDatarate(57600);
+}