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
main.cpp
00001 /******************************************************************************************* 00002 * 00003 * University of York Robot Lab Pi Swarm Robot Library 00004 * 00005 * User Command RF Test Program 00006 * 00007 * Based on the blank program for 0.6 API, this adds handling for user RF messages from the 00008 * table controller 00009 * 00010 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York 00011 * 00012 * Version 0.6 May 2014 00013 * 00014 * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2 00015 * 00016 ******************************************************************************************/ 00017 00018 #include "main.h" // Certain parameters can be set by changing the defines in piswarm.h 00019 00020 00021 PiSwarm piswarm; 00022 Serial pc (USBTX,USBRX); 00023 Timeout clear_screen_timeout; 00024 00025 //This is where the program code goes. In the simple demo, the outer LEDs are blinked. 00026 int main() { 00027 init(); 00028 int step = 0; 00029 int function = 0; 00030 //Each RF message will contain 1 byte: in this case the user ID. (Note it is note strictly necessary as the 00031 //user ID is already sent seperately and we are also sending the 4-bit function register, so for very simple 00032 //comms the message part can be omitted.) 00033 char message [2]; 00034 message[0] = piswarm.get_id() + 48; 00035 00036 while(1) { 00037 step ++; 00038 //Every 6 steps (1.5 seconds) send an RF message, incrementing the function counter 00039 if(step==6){ 00040 step=0; 00041 function ++; 00042 if(function == 16) function = 0; 00043 broadcast_user_rf_command(function,message,1); 00044 } 00045 switch (step%3){ 00046 case 0: piswarm.set_oled_colour(50,0,0); break; 00047 case 1: piswarm.set_oled_colour(0,50,0); break; 00048 case 2: piswarm.set_oled_colour(0,0,50); break; 00049 } 00050 switch (step%2){ 00051 case 0: piswarm.set_oleds(1,0,1,0,1,0,1,0,1,0); break; 00052 case 1: piswarm.set_oleds(0,1,0,1,0,1,0,1,0,1); break; 00053 } 00054 wait(0.25); 00055 } 00056 } 00057 00058 // This function is used to send the RF message: 00059 void broadcast_user_rf_command(int function, char * message, int length) 00060 { 00061 //This function augments the communications stack 00062 //It sends a 'user' RF command to all members (ie target_id = 0) 00063 //It sends a 'request', not a 'command', meaning it will still be handled if commands are disabled (RF_ALLOW_COMMANDS set to 0, recommended) 00064 //It takes three inputs: 00065 // * function (an integer from 0 to 15) 00066 // * message (a char array) 00067 // * length (length of message in bytes) 00068 send_rf_message(0,48+(function % 16),message,length); 00069 } 00070 00071 void clear_screen(){ 00072 piswarm.cls(); 00073 } 00074 00075 /*************************************************************************************************************************************** 00076 * 00077 * Beyond this point, empty code blocks for optional functions is given 00078 * 00079 * These may be left blank if not used, but should not be deleted 00080 * 00081 **************************************************************************************************************************************/ 00082 00083 // Communications 00084 00085 // If using the communication stack (USE_COMMUNICATION_STACK = 1), functionality for handling user RF responses should be added to the following functions 00086 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead 00087 00088 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){ 00089 piswarm.cls(); 00090 piswarm.locate(0,0); 00091 piswarm.printf("URF:%d",function); 00092 if(length > 0) { 00093 piswarm.locate(0,1); 00094 piswarm.printf("%s",data); 00095 } 00096 clear_screen_timeout.attach(&clear_screen,0.2); 00097 // A 'user' RF Command has been received: write the code here to process it 00098 // sender = ID of the sender, range 0 to 31 00099 // broadcast_message = 1 is message sent to all robots, 0 otherwise 00100 // request_response = 1 if a response is expected, 0 otherwise 00101 // id = Message ID, range 0 to 255 00102 // 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 00103 // function = The function identifier. Range 0 to 15 00104 // * data = Array containing extra data bytes 00105 // length = Length of extra data bytes held (range 0 to 57) 00106 00107 //Do something... 00108 } 00109 00110 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){ 00111 //pc.printf("Response:%s\n",data); 00112 // A 'user' RF Response has been received: write the code here to process it 00113 // sender = ID of the sender, range 0 to 31 00114 // broadcast_message = 1 is message sent to all robots, 0 otherwise 00115 // success = 1 if operation successful, 0 otherwise 00116 // id = Message ID, range 0 to 255 00117 // 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 00118 // function = The function identifier. Range 0 to 15 00119 // * data = Array containing extra data bytes 00120 // length = Length of extra data bytes held (range 0 to 57) 00121 00122 //Do something... 00123 } 00124 00125 void processRawRFData(char * rstring, char cCount){ 00126 // A raw RF packet has been received: write the code here to process it 00127 // rstring = The received packet 00128 // cCount = Packet length 00129 00130 //Do something... 00131 } 00132 00133 void switch_pressed() { 00134 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up} 00135 char switches = piswarm.get_switches(); 00136 00137 //Do something... 00138 }
Generated on Thu Jul 14 2022 22:53:12 by 1.7.2