Example for sending and receiving simple broadcast commands using the RF interface on the Pi Swarm

Dependencies:   Pi_Swarm_Library_v06_alpha mbed

Fork of Pi_Swarm_Blank by piswarm

Committer:
jah128
Date:
Tue Mar 10 16:33:54 2015 +0000
Revision:
5:ca18d81a3212
Parent:
4:823174be9a6b
Added example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 2:e806b595f9ce 1 /*******************************************************************************************
jah128 2:e806b595f9ce 2 *
jah128 2:e806b595f9ce 3 * University of York Robot Lab Pi Swarm Robot Library
jah128 1:37502eb3b70f 4 *
jah128 5:ca18d81a3212 5 * User Command RF Test Program
jah128 4:823174be9a6b 6 *
jah128 5:ca18d81a3212 7 * Based on the blank program for 0.6 API, this adds handling for user RF messages from the
jah128 5:ca18d81a3212 8 * table controller
jah128 4:823174be9a6b 9 *
jah128 1:37502eb3b70f 10 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
jah128 1:37502eb3b70f 11 *
jah128 5:ca18d81a3212 12 * Version 0.6 May 2014
jah128 1:37502eb3b70f 13 *
jah128 2:e806b595f9ce 14 * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2
jah128 2:e806b595f9ce 15 *
jah128 2:e806b595f9ce 16 ******************************************************************************************/
jah128 0:46cd1498a39a 17
jah128 3:1aa1de26966a 18 #include "main.h" // Certain parameters can be set by changing the defines in piswarm.h
jah128 0:46cd1498a39a 19
jah128 0:46cd1498a39a 20
jah128 1:37502eb3b70f 21 PiSwarm piswarm;
jah128 1:37502eb3b70f 22 Serial pc (USBTX,USBRX);
jah128 5:ca18d81a3212 23 Timeout clear_screen_timeout;
jah128 0:46cd1498a39a 24
jah128 1:37502eb3b70f 25 //This is where the program code goes. In the simple demo, the outer LEDs are blinked.
jah128 1:37502eb3b70f 26 int main() {
jah128 1:37502eb3b70f 27 init();
jah128 1:37502eb3b70f 28 int step = 0;
jah128 5:ca18d81a3212 29 int function = 0;
jah128 5:ca18d81a3212 30 //Each RF message will contain 1 byte: in this case the user ID. (Note it is note strictly necessary as the
jah128 5:ca18d81a3212 31 //user ID is already sent seperately and we are also sending the 4-bit function register, so for very simple
jah128 5:ca18d81a3212 32 //comms the message part can be omitted.)
jah128 5:ca18d81a3212 33 char message [2];
jah128 5:ca18d81a3212 34 message[0] = piswarm.get_id() + 48;
jah128 0:46cd1498a39a 35
jah128 1:37502eb3b70f 36 while(1) {
jah128 1:37502eb3b70f 37 step ++;
jah128 5:ca18d81a3212 38 //Every 6 steps (1.5 seconds) send an RF message, incrementing the function counter
jah128 5:ca18d81a3212 39 if(step==6){
jah128 5:ca18d81a3212 40 step=0;
jah128 5:ca18d81a3212 41 function ++;
jah128 5:ca18d81a3212 42 if(function == 16) function = 0;
jah128 5:ca18d81a3212 43 broadcast_user_rf_command(function,message,1);
jah128 5:ca18d81a3212 44 }
jah128 1:37502eb3b70f 45 switch (step%3){
jah128 2:e806b595f9ce 46 case 0: piswarm.set_oled_colour(50,0,0); break;
jah128 2:e806b595f9ce 47 case 1: piswarm.set_oled_colour(0,50,0); break;
jah128 2:e806b595f9ce 48 case 2: piswarm.set_oled_colour(0,0,50); break;
jah128 1:37502eb3b70f 49 }
jah128 1:37502eb3b70f 50 switch (step%2){
jah128 1:37502eb3b70f 51 case 0: piswarm.set_oleds(1,0,1,0,1,0,1,0,1,0); break;
jah128 1:37502eb3b70f 52 case 1: piswarm.set_oleds(0,1,0,1,0,1,0,1,0,1); break;
jah128 1:37502eb3b70f 53 }
jah128 1:37502eb3b70f 54 wait(0.25);
jah128 0:46cd1498a39a 55 }
jah128 0:46cd1498a39a 56 }
jah128 0:46cd1498a39a 57
jah128 5:ca18d81a3212 58 // This function is used to send the RF message:
jah128 5:ca18d81a3212 59 void broadcast_user_rf_command(int function, char * message, int length)
jah128 5:ca18d81a3212 60 {
jah128 5:ca18d81a3212 61 //This function augments the communications stack
jah128 5:ca18d81a3212 62 //It sends a 'user' RF command to all members (ie target_id = 0)
jah128 5:ca18d81a3212 63 //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 5:ca18d81a3212 64 //It takes three inputs:
jah128 5:ca18d81a3212 65 // * function (an integer from 0 to 15)
jah128 5:ca18d81a3212 66 // * message (a char array)
jah128 5:ca18d81a3212 67 // * length (length of message in bytes)
jah128 5:ca18d81a3212 68 send_rf_message(0,48+(function % 16),message,length);
jah128 5:ca18d81a3212 69 }
jah128 5:ca18d81a3212 70
jah128 5:ca18d81a3212 71 void clear_screen(){
jah128 5:ca18d81a3212 72 piswarm.cls();
jah128 5:ca18d81a3212 73 }
jah128 5:ca18d81a3212 74
jah128 2:e806b595f9ce 75 /***************************************************************************************************************************************
jah128 2:e806b595f9ce 76 *
jah128 4:823174be9a6b 77 * Beyond this point, empty code blocks for optional functions is given
jah128 2:e806b595f9ce 78 *
jah128 4:823174be9a6b 79 * These may be left blank if not used, but should not be deleted
jah128 2:e806b595f9ce 80 *
jah128 2:e806b595f9ce 81 **************************************************************************************************************************************/
jah128 2:e806b595f9ce 82
jah128 1:37502eb3b70f 83 // Communications
jah128 1:37502eb3b70f 84
jah128 1:37502eb3b70f 85 // If using the communication stack (USE_COMMUNICATION_STACK = 1), functionality for handling user RF responses should be added to the following functions
jah128 1:37502eb3b70f 86 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
jah128 1:37502eb3b70f 87
jah128 1:37502eb3b70f 88 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){
jah128 5:ca18d81a3212 89 piswarm.cls();
jah128 5:ca18d81a3212 90 piswarm.locate(0,0);
jah128 5:ca18d81a3212 91 piswarm.printf("URF:%d",function);
jah128 5:ca18d81a3212 92 if(length > 0) {
jah128 5:ca18d81a3212 93 piswarm.locate(0,1);
jah128 5:ca18d81a3212 94 piswarm.printf("%s",data);
jah128 5:ca18d81a3212 95 }
jah128 5:ca18d81a3212 96 clear_screen_timeout.attach(&clear_screen,0.2);
jah128 1:37502eb3b70f 97 // A 'user' RF Command has been received: write the code here to process it
jah128 1:37502eb3b70f 98 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 99 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 100 // request_response = 1 if a response is expected, 0 otherwise
jah128 1:37502eb3b70f 101 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 102 // is_command = 1 is message is a command, 0 if it is a request. If RF_ALLOW_COMMANDS is not selected, only requests will be sent to this block
jah128 1:37502eb3b70f 103 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 104 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 105 // length = Length of extra data bytes held (range 0 to 57)
jah128 4:823174be9a6b 106
jah128 4:823174be9a6b 107 //Do something...
jah128 1:37502eb3b70f 108 }
jah128 1:37502eb3b70f 109
jah128 1:37502eb3b70f 110 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){
jah128 5:ca18d81a3212 111 //pc.printf("Response:%s\n",data);
jah128 1:37502eb3b70f 112 // A 'user' RF Response has been received: write the code here to process it
jah128 1:37502eb3b70f 113 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 114 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 115 // success = 1 if operation successful, 0 otherwise
jah128 1:37502eb3b70f 116 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 117 // is_command = 1 is message is a command, 0 if it is a request. If RF_ALLOW_COMMANDS is not selected, only requests will be sent to this block
jah128 1:37502eb3b70f 118 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 119 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 120 // length = Length of extra data bytes held (range 0 to 57)
jah128 4:823174be9a6b 121
jah128 4:823174be9a6b 122 //Do something...
jah128 1:37502eb3b70f 123 }
jah128 1:37502eb3b70f 124
jah128 1:37502eb3b70f 125 void processRawRFData(char * rstring, char cCount){
jah128 1:37502eb3b70f 126 // A raw RF packet has been received: write the code here to process it
jah128 1:37502eb3b70f 127 // rstring = The received packet
jah128 1:37502eb3b70f 128 // cCount = Packet length
jah128 4:823174be9a6b 129
jah128 4:823174be9a6b 130 //Do something...
jah128 0:46cd1498a39a 131 }
jah128 0:46cd1498a39a 132
jah128 1:37502eb3b70f 133 void switch_pressed() {
jah128 1:37502eb3b70f 134 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up}
jah128 1:37502eb3b70f 135 char switches = piswarm.get_switches();
jah128 1:37502eb3b70f 136
jah128 1:37502eb3b70f 137 //Do something...
jah128 0:46cd1498a39a 138 }