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:
Sun Feb 02 21:18:16 2014 +0000
Revision:
1:37502eb3b70f
Parent:
0:46cd1498a39a
Child:
2:e806b595f9ce
added comms;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 1:37502eb3b70f 1 /* University of York Robot Lab Pi Swarm Robot Library
jah128 1:37502eb3b70f 2 *
jah128 1:37502eb3b70f 3 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
jah128 1:37502eb3b70f 4 *
jah128 1:37502eb3b70f 5 * Version 0.4 January 2014
jah128 1:37502eb3b70f 6 *
jah128 1:37502eb3b70f 7 * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.1
jah128 1:37502eb3b70f 8 */
jah128 0:46cd1498a39a 9
jah128 1:37502eb3b70f 10 #include "main.h" // Certain parameters can be set by changing the defines in main.h
jah128 0:46cd1498a39a 11
jah128 0:46cd1498a39a 12
jah128 1:37502eb3b70f 13 PiSwarm piswarm;
jah128 1:37502eb3b70f 14 Serial pc (USBTX,USBRX);
jah128 0:46cd1498a39a 15
jah128 1:37502eb3b70f 16 //This is where the program code goes. In the simple demo, the outer LEDs are blinked.
jah128 1:37502eb3b70f 17 int main() {
jah128 1:37502eb3b70f 18 init();
jah128 1:37502eb3b70f 19
jah128 1:37502eb3b70f 20 int step = 0;
jah128 0:46cd1498a39a 21
jah128 1:37502eb3b70f 22 while(1) {
jah128 1:37502eb3b70f 23 //pc.printf("%i\n",step);
jah128 1:37502eb3b70f 24 // Do something!
jah128 1:37502eb3b70f 25 step ++;
jah128 1:37502eb3b70f 26 if(step==6)step=0;
jah128 1:37502eb3b70f 27 switch (step%3){
jah128 1:37502eb3b70f 28 case 0: piswarm.set_oled_colour(200,0,0); break;
jah128 1:37502eb3b70f 29 case 1: piswarm.set_oled_colour(0,200,0); break;
jah128 1:37502eb3b70f 30 case 2: piswarm.set_oled_colour(0,0,200); break;
jah128 1:37502eb3b70f 31 }
jah128 1:37502eb3b70f 32 switch (step%2){
jah128 1:37502eb3b70f 33 case 0: piswarm.set_oleds(1,0,1,0,1,0,1,0,1,0); break;
jah128 1:37502eb3b70f 34 case 1: piswarm.set_oleds(0,1,0,1,0,1,0,1,0,1); break;
jah128 1:37502eb3b70f 35 }
jah128 1:37502eb3b70f 36 wait(0.25);
jah128 0:46cd1498a39a 37 }
jah128 0:46cd1498a39a 38 }
jah128 0:46cd1498a39a 39
jah128 1:37502eb3b70f 40 // Communications
jah128 1:37502eb3b70f 41
jah128 1:37502eb3b70f 42 // 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 43 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
jah128 1:37502eb3b70f 44
jah128 1:37502eb3b70f 45 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 46 // A 'user' RF Command has been received: write the code here to process it
jah128 1:37502eb3b70f 47 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 48 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 49 // request_response = 1 if a response is expected, 0 otherwise
jah128 1:37502eb3b70f 50 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 51 // 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 52 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 53 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 54 // length = Length of extra data bytes held (range 0 to 57)
jah128 1:37502eb3b70f 55
jah128 1:37502eb3b70f 56 }
jah128 1:37502eb3b70f 57
jah128 1:37502eb3b70f 58 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 59 // A 'user' RF Response has been received: write the code here to process it
jah128 1:37502eb3b70f 60 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 61 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 62 // success = 1 if operation successful, 0 otherwise
jah128 1:37502eb3b70f 63 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 64 // 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 65 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 66 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 67 // length = Length of extra data bytes held (range 0 to 57)
jah128 1:37502eb3b70f 68
jah128 1:37502eb3b70f 69 }
jah128 1:37502eb3b70f 70
jah128 1:37502eb3b70f 71 void processRawRFData(char * rstring, char cCount){
jah128 1:37502eb3b70f 72 // A raw RF packet has been received: write the code here to process it
jah128 1:37502eb3b70f 73 // rstring = The received packet
jah128 1:37502eb3b70f 74 // cCount = Packet length
jah128 0:46cd1498a39a 75 }
jah128 0:46cd1498a39a 76
jah128 1:37502eb3b70f 77 void switch_pressed() {
jah128 1:37502eb3b70f 78 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up}
jah128 1:37502eb3b70f 79 char switches = piswarm.get_switches();
jah128 1:37502eb3b70f 80
jah128 1:37502eb3b70f 81 //Do something...
jah128 0:46cd1498a39a 82 }