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 Feb 18 17:18:43 2014 +0000
Revision:
4:823174be9a6b
Parent:
3:1aa1de26966a
Child:
5:ca18d81a3212
Blank program for API 0.6 (unfinished)

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 4:823174be9a6b 5 * "Blank" Program
jah128 4:823174be9a6b 6 *
jah128 4:823174be9a6b 7 * Use this file as the template to produce custom controllers
jah128 4:823174be9a6b 8 *
jah128 1:37502eb3b70f 9 * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York
jah128 1:37502eb3b70f 10 *
jah128 4:823174be9a6b 11 * Version 0.6 February 2014
jah128 1:37502eb3b70f 12 *
jah128 2:e806b595f9ce 13 * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2
jah128 2:e806b595f9ce 14 *
jah128 2:e806b595f9ce 15 ******************************************************************************************/
jah128 0:46cd1498a39a 16
jah128 3:1aa1de26966a 17 #include "main.h" // Certain parameters can be set by changing the defines in piswarm.h
jah128 0:46cd1498a39a 18
jah128 0:46cd1498a39a 19
jah128 1:37502eb3b70f 20 PiSwarm piswarm;
jah128 1:37502eb3b70f 21 Serial pc (USBTX,USBRX);
jah128 0:46cd1498a39a 22
jah128 1:37502eb3b70f 23 //This is where the program code goes. In the simple demo, the outer LEDs are blinked.
jah128 1:37502eb3b70f 24 int main() {
jah128 1:37502eb3b70f 25 init();
jah128 1:37502eb3b70f 26
jah128 1:37502eb3b70f 27 int step = 0;
jah128 0:46cd1498a39a 28
jah128 1:37502eb3b70f 29 while(1) {
jah128 1:37502eb3b70f 30 step ++;
jah128 1:37502eb3b70f 31 if(step==6)step=0;
jah128 1:37502eb3b70f 32 switch (step%3){
jah128 2:e806b595f9ce 33 case 0: piswarm.set_oled_colour(50,0,0); break;
jah128 2:e806b595f9ce 34 case 1: piswarm.set_oled_colour(0,50,0); break;
jah128 2:e806b595f9ce 35 case 2: piswarm.set_oled_colour(0,0,50); break;
jah128 1:37502eb3b70f 36 }
jah128 1:37502eb3b70f 37 switch (step%2){
jah128 1:37502eb3b70f 38 case 0: piswarm.set_oleds(1,0,1,0,1,0,1,0,1,0); break;
jah128 1:37502eb3b70f 39 case 1: piswarm.set_oleds(0,1,0,1,0,1,0,1,0,1); break;
jah128 1:37502eb3b70f 40 }
jah128 1:37502eb3b70f 41 wait(0.25);
jah128 0:46cd1498a39a 42 }
jah128 0:46cd1498a39a 43 }
jah128 0:46cd1498a39a 44
jah128 2:e806b595f9ce 45 /***************************************************************************************************************************************
jah128 2:e806b595f9ce 46 *
jah128 4:823174be9a6b 47 * Beyond this point, empty code blocks for optional functions is given
jah128 2:e806b595f9ce 48 *
jah128 4:823174be9a6b 49 * These may be left blank if not used, but should not be deleted
jah128 2:e806b595f9ce 50 *
jah128 2:e806b595f9ce 51 **************************************************************************************************************************************/
jah128 2:e806b595f9ce 52
jah128 1:37502eb3b70f 53 // Communications
jah128 1:37502eb3b70f 54
jah128 1:37502eb3b70f 55 // 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 56 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
jah128 1:37502eb3b70f 57
jah128 1:37502eb3b70f 58 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 59 // A 'user' RF Command 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 // request_response = 1 if a response is expected, 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 4:823174be9a6b 68
jah128 4:823174be9a6b 69 //Do something...
jah128 1:37502eb3b70f 70 }
jah128 1:37502eb3b70f 71
jah128 1:37502eb3b70f 72 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 73 // A 'user' RF Response has been received: write the code here to process it
jah128 1:37502eb3b70f 74 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 75 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 76 // success = 1 if operation successful, 0 otherwise
jah128 1:37502eb3b70f 77 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 78 // 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 79 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 80 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 81 // length = Length of extra data bytes held (range 0 to 57)
jah128 4:823174be9a6b 82
jah128 4:823174be9a6b 83 //Do something...
jah128 1:37502eb3b70f 84 }
jah128 1:37502eb3b70f 85
jah128 1:37502eb3b70f 86 void processRawRFData(char * rstring, char cCount){
jah128 1:37502eb3b70f 87 // A raw RF packet has been received: write the code here to process it
jah128 1:37502eb3b70f 88 // rstring = The received packet
jah128 1:37502eb3b70f 89 // cCount = Packet length
jah128 4:823174be9a6b 90
jah128 4:823174be9a6b 91 //Do something...
jah128 0:46cd1498a39a 92 }
jah128 0:46cd1498a39a 93
jah128 1:37502eb3b70f 94 void switch_pressed() {
jah128 1:37502eb3b70f 95 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up}
jah128 1:37502eb3b70f 96 char switches = piswarm.get_switches();
jah128 1:37502eb3b70f 97
jah128 1:37502eb3b70f 98 //Do something...
jah128 0:46cd1498a39a 99 }