Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Pi_Swarm_Blank by
main.cpp@5:70a83245e681, 2014-06-11 (annotated)
- Committer:
- jah128
- Date:
- Wed Jun 11 14:14:50 2014 +0000
- Revision:
- 5:70a83245e681
- Parent:
- 4:823174be9a6b
- Child:
- 7:d03e54d9eb1c
Overwritten to previous published version
Who changed what in which revision?
| User | Revision | Line number | New 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 | 5:70a83245e681 | 59 | pc.printf("Command:%s\n",data); |
| jah128 | 1:37502eb3b70f | 60 | // A 'user' RF Command has been received: write the code here to process it |
| jah128 | 1:37502eb3b70f | 61 | // sender = ID of the sender, range 0 to 31 |
| jah128 | 1:37502eb3b70f | 62 | // broadcast_message = 1 is message sent to all robots, 0 otherwise |
| jah128 | 1:37502eb3b70f | 63 | // request_response = 1 if a response is expected, 0 otherwise |
| jah128 | 1:37502eb3b70f | 64 | // id = Message ID, range 0 to 255 |
| jah128 | 1:37502eb3b70f | 65 | // 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 | 66 | // function = The function identifier. Range 0 to 15 |
| jah128 | 1:37502eb3b70f | 67 | // * data = Array containing extra data bytes |
| jah128 | 1:37502eb3b70f | 68 | // length = Length of extra data bytes held (range 0 to 57) |
| jah128 | 4:823174be9a6b | 69 | |
| jah128 | 4:823174be9a6b | 70 | //Do something... |
| jah128 | 1:37502eb3b70f | 71 | } |
| jah128 | 1:37502eb3b70f | 72 | |
| jah128 | 1:37502eb3b70f | 73 | void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){ |
| jah128 | 5:70a83245e681 | 74 | pc.printf("Response:%s\n",data); |
| jah128 | 1:37502eb3b70f | 75 | // A 'user' RF Response has been received: write the code here to process it |
| jah128 | 1:37502eb3b70f | 76 | // sender = ID of the sender, range 0 to 31 |
| jah128 | 1:37502eb3b70f | 77 | // broadcast_message = 1 is message sent to all robots, 0 otherwise |
| jah128 | 1:37502eb3b70f | 78 | // success = 1 if operation successful, 0 otherwise |
| jah128 | 1:37502eb3b70f | 79 | // id = Message ID, range 0 to 255 |
| jah128 | 1:37502eb3b70f | 80 | // 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 | 81 | // function = The function identifier. Range 0 to 15 |
| jah128 | 1:37502eb3b70f | 82 | // * data = Array containing extra data bytes |
| jah128 | 1:37502eb3b70f | 83 | // length = Length of extra data bytes held (range 0 to 57) |
| jah128 | 4:823174be9a6b | 84 | |
| jah128 | 4:823174be9a6b | 85 | //Do something... |
| jah128 | 1:37502eb3b70f | 86 | } |
| jah128 | 1:37502eb3b70f | 87 | |
| jah128 | 1:37502eb3b70f | 88 | void processRawRFData(char * rstring, char cCount){ |
| jah128 | 1:37502eb3b70f | 89 | // A raw RF packet has been received: write the code here to process it |
| jah128 | 1:37502eb3b70f | 90 | // rstring = The received packet |
| jah128 | 1:37502eb3b70f | 91 | // cCount = Packet length |
| jah128 | 4:823174be9a6b | 92 | |
| jah128 | 4:823174be9a6b | 93 | //Do something... |
| jah128 | 0:46cd1498a39a | 94 | } |
| jah128 | 0:46cd1498a39a | 95 | |
| jah128 | 1:37502eb3b70f | 96 | void switch_pressed() { |
| jah128 | 1:37502eb3b70f | 97 | //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up} |
| jah128 | 1:37502eb3b70f | 98 | char switches = piswarm.get_switches(); |
| jah128 | 1:37502eb3b70f | 99 | |
| jah128 | 1:37502eb3b70f | 100 | //Do something... |
| jah128 | 0:46cd1498a39a | 101 | } |
