Handheld controller (RF) for Pi Swarm system
Fork of Pi_Swarm_Handheld_Controller by
main.cpp@0:d63a63feb104, 2014-06-10 (annotated)
- Committer:
- jah128
- Date:
- Tue Jun 10 11:05:23 2014 +0000
- Revision:
- 0:d63a63feb104
Handheld controller for Pi Swarm (old code)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:d63a63feb104 | 1 | /******************************************************************************************* |
jah128 | 0:d63a63feb104 | 2 | * |
jah128 | 0:d63a63feb104 | 3 | * University of York Robot Lab Pi Swarm Handheld Controller Software |
jah128 | 0:d63a63feb104 | 4 | * |
jah128 | 0:d63a63feb104 | 5 | * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York |
jah128 | 0:d63a63feb104 | 6 | * |
jah128 | 0:d63a63feb104 | 7 | * Version 0.5 February 2014 |
jah128 | 0:d63a63feb104 | 8 | * |
jah128 | 0:d63a63feb104 | 9 | * Designed for use with the Pi Swarm Board (enhanced MBED sensor board) v1.2 |
jah128 | 0:d63a63feb104 | 10 | * |
jah128 | 0:d63a63feb104 | 11 | ******************************************************************************************/ |
jah128 | 0:d63a63feb104 | 12 | |
jah128 | 0:d63a63feb104 | 13 | #include "mbed.h" |
jah128 | 0:d63a63feb104 | 14 | #include "main.h" |
jah128 | 0:d63a63feb104 | 15 | #include "communications.h" |
jah128 | 0:d63a63feb104 | 16 | #include "display.h" |
jah128 | 0:d63a63feb104 | 17 | |
jah128 | 0:d63a63feb104 | 18 | Serial pc (USBTX, USBRX); |
jah128 | 0:d63a63feb104 | 19 | DigitalOut myled(LED1); |
jah128 | 0:d63a63feb104 | 20 | Display display; |
jah128 | 0:d63a63feb104 | 21 | Alpha433 rf; |
jah128 | 0:d63a63feb104 | 22 | Ticker switch_ticker; |
jah128 | 0:d63a63feb104 | 23 | DigitalIn switch_up(p21); |
jah128 | 0:d63a63feb104 | 24 | DigitalIn switch_upright(p22); |
jah128 | 0:d63a63feb104 | 25 | DigitalIn switch_right(p23); |
jah128 | 0:d63a63feb104 | 26 | DigitalIn switch_downright(p24); |
jah128 | 0:d63a63feb104 | 27 | DigitalIn switch_down(p25); |
jah128 | 0:d63a63feb104 | 28 | DigitalIn switch_downleft(p26); |
jah128 | 0:d63a63feb104 | 29 | DigitalIn switch_left(p27); |
jah128 | 0:d63a63feb104 | 30 | DigitalIn switch_upleft(p29); |
jah128 | 0:d63a63feb104 | 31 | DigitalIn switch_center(p28); |
jah128 | 0:d63a63feb104 | 32 | DigitalOut tx_led(p18); |
jah128 | 0:d63a63feb104 | 33 | DigitalOut rx_led(p17); |
jah128 | 0:d63a63feb104 | 34 | char target_id = 0; |
jah128 | 0:d63a63feb104 | 35 | int switch_state = 0; |
jah128 | 0:d63a63feb104 | 36 | int speed_mode = 0; |
jah128 | 0:d63a63feb104 | 37 | float speed = 0.1; |
jah128 | 0:d63a63feb104 | 38 | |
jah128 | 0:d63a63feb104 | 39 | void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){ |
jah128 | 0:d63a63feb104 | 40 | // A 'user' RF Command has been received: write the code here to process it |
jah128 | 0:d63a63feb104 | 41 | // sender = ID of the sender, range 0 to 31 |
jah128 | 0:d63a63feb104 | 42 | // broadcast_message = 1 is message sent to all robots, 0 otherwise |
jah128 | 0:d63a63feb104 | 43 | // request_response = 1 if a response is expected, 0 otherwise |
jah128 | 0:d63a63feb104 | 44 | // id = Message ID, range 0 to 255 |
jah128 | 0:d63a63feb104 | 45 | // 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 | 0:d63a63feb104 | 46 | // function = The function identifier. Range 0 to 15 |
jah128 | 0:d63a63feb104 | 47 | // * data = Array containing extra data bytes |
jah128 | 0:d63a63feb104 | 48 | // length = Length of extra data bytes held (range 0 to 57) |
jah128 | 0:d63a63feb104 | 49 | |
jah128 | 0:d63a63feb104 | 50 | } |
jah128 | 0:d63a63feb104 | 51 | |
jah128 | 0:d63a63feb104 | 52 | void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){ |
jah128 | 0:d63a63feb104 | 53 | // A 'user' RF Response has been received: write the code here to process it |
jah128 | 0:d63a63feb104 | 54 | // sender = ID of the sender, range 0 to 31 |
jah128 | 0:d63a63feb104 | 55 | // broadcast_message = 1 is message sent to all robots, 0 otherwise |
jah128 | 0:d63a63feb104 | 56 | // success = 1 if operation successful, 0 otherwise |
jah128 | 0:d63a63feb104 | 57 | // id = Message ID, range 0 to 255 |
jah128 | 0:d63a63feb104 | 58 | // 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 | 0:d63a63feb104 | 59 | // function = The function identifier. Range 0 to 15 |
jah128 | 0:d63a63feb104 | 60 | // * data = Array containing extra data bytes |
jah128 | 0:d63a63feb104 | 61 | // length = Length of extra data bytes held (range 0 to 57) |
jah128 | 0:d63a63feb104 | 62 | |
jah128 | 0:d63a63feb104 | 63 | } |
jah128 | 0:d63a63feb104 | 64 | |
jah128 | 0:d63a63feb104 | 65 | |
jah128 | 0:d63a63feb104 | 66 | void processRawRFData(char * rstring, char cCount){ |
jah128 | 0:d63a63feb104 | 67 | // A raw RF packet has been received: write the code here to process it |
jah128 | 0:d63a63feb104 | 68 | // rstring = The received packet |
jah128 | 0:d63a63feb104 | 69 | // cCount = Packet length |
jah128 | 0:d63a63feb104 | 70 | } |
jah128 | 0:d63a63feb104 | 71 | |
jah128 | 0:d63a63feb104 | 72 | void check_switch() |
jah128 | 0:d63a63feb104 | 73 | { |
jah128 | 0:d63a63feb104 | 74 | int new_state = 0; |
jah128 | 0:d63a63feb104 | 75 | if(!switch_up) new_state = 1; |
jah128 | 0:d63a63feb104 | 76 | if(!switch_upright) new_state = 2; |
jah128 | 0:d63a63feb104 | 77 | if(!switch_right) new_state = 3; |
jah128 | 0:d63a63feb104 | 78 | if(!switch_downright) new_state=4; |
jah128 | 0:d63a63feb104 | 79 | if(!switch_down) new_state=5; |
jah128 | 0:d63a63feb104 | 80 | if(!switch_downleft) new_state=6; |
jah128 | 0:d63a63feb104 | 81 | if(!switch_left) new_state=7; |
jah128 | 0:d63a63feb104 | 82 | if(!switch_upleft) new_state=8; |
jah128 | 0:d63a63feb104 | 83 | if(!switch_center) new_state=9; |
jah128 | 0:d63a63feb104 | 84 | if(new_state!=switch_state) { |
jah128 | 0:d63a63feb104 | 85 | switch_state=new_state; |
jah128 | 0:d63a63feb104 | 86 | display.set_position(0,8); |
jah128 | 0:d63a63feb104 | 87 | switch(switch_state) { |
jah128 | 0:d63a63feb104 | 88 | case 0: |
jah128 | 0:d63a63feb104 | 89 | display.printf(" "); |
jah128 | 0:d63a63feb104 | 90 | break; |
jah128 | 0:d63a63feb104 | 91 | case 1: |
jah128 | 0:d63a63feb104 | 92 | display.printf("UP "); |
jah128 | 0:d63a63feb104 | 93 | break; |
jah128 | 0:d63a63feb104 | 94 | case 2: |
jah128 | 0:d63a63feb104 | 95 | display.printf("UP-RIGHT "); |
jah128 | 0:d63a63feb104 | 96 | break; |
jah128 | 0:d63a63feb104 | 97 | case 3: |
jah128 | 0:d63a63feb104 | 98 | display.printf("RIGHT "); |
jah128 | 0:d63a63feb104 | 99 | break; |
jah128 | 0:d63a63feb104 | 100 | case 4: |
jah128 | 0:d63a63feb104 | 101 | display.printf("DOWN-RIGHT"); |
jah128 | 0:d63a63feb104 | 102 | break; |
jah128 | 0:d63a63feb104 | 103 | case 5: |
jah128 | 0:d63a63feb104 | 104 | display.printf("DOWN "); |
jah128 | 0:d63a63feb104 | 105 | break; |
jah128 | 0:d63a63feb104 | 106 | case 6: |
jah128 | 0:d63a63feb104 | 107 | display.printf("DOWN-LEFT "); |
jah128 | 0:d63a63feb104 | 108 | break; |
jah128 | 0:d63a63feb104 | 109 | case 7: |
jah128 | 0:d63a63feb104 | 110 | display.printf("LEFT "); |
jah128 | 0:d63a63feb104 | 111 | break; |
jah128 | 0:d63a63feb104 | 112 | case 8: |
jah128 | 0:d63a63feb104 | 113 | display.printf("UP-LEFT "); |
jah128 | 0:d63a63feb104 | 114 | break; |
jah128 | 0:d63a63feb104 | 115 | case 9: |
jah128 | 0:d63a63feb104 | 116 | display.printf("CENTER "); |
jah128 | 0:d63a63feb104 | 117 | speed_mode ++; |
jah128 | 0:d63a63feb104 | 118 | if (speed_mode == 4) speed_mode = 0; |
jah128 | 0:d63a63feb104 | 119 | switch(speed_mode){ |
jah128 | 0:d63a63feb104 | 120 | case 0: speed = 0.1;break; |
jah128 | 0:d63a63feb104 | 121 | case 1: speed = 0.2;break; |
jah128 | 0:d63a63feb104 | 122 | case 2: speed = 0.5;break; |
jah128 | 0:d63a63feb104 | 123 | case 3: speed = 1.0;break; |
jah128 | 0:d63a63feb104 | 124 | } |
jah128 | 0:d63a63feb104 | 125 | display.set_position(1,0); |
jah128 | 0:d63a63feb104 | 126 | display.printf("Speed: %.1f",speed); |
jah128 | 0:d63a63feb104 | 127 | break; |
jah128 | 0:d63a63feb104 | 128 | } |
jah128 | 0:d63a63feb104 | 129 | transmit_message(); |
jah128 | 0:d63a63feb104 | 130 | } |
jah128 | 0:d63a63feb104 | 131 | } |
jah128 | 0:d63a63feb104 | 132 | |
jah128 | 0:d63a63feb104 | 133 | void transmit_message() |
jah128 | 0:d63a63feb104 | 134 | { |
jah128 | 0:d63a63feb104 | 135 | tx_led = 1; |
jah128 | 0:d63a63feb104 | 136 | switch(switch_state){ |
jah128 | 0:d63a63feb104 | 137 | case 0: send_rf_command_stop(target_id,1);break; |
jah128 | 0:d63a63feb104 | 138 | case 1: send_rf_command_forward(target_id,1,speed);break; |
jah128 | 0:d63a63feb104 | 139 | case 5: send_rf_command_backward(target_id,1,speed);break; |
jah128 | 0:d63a63feb104 | 140 | case 7: send_rf_command_left(target_id,1,speed);break; |
jah128 | 0:d63a63feb104 | 141 | case 3: send_rf_command_right(target_id,1,speed);break; |
jah128 | 0:d63a63feb104 | 142 | case 9: send_rf_message(target_id,0x32,"Hello",5);break; |
jah128 | 0:d63a63feb104 | 143 | } |
jah128 | 0:d63a63feb104 | 144 | wait(0.1); |
jah128 | 0:d63a63feb104 | 145 | tx_led=0; |
jah128 | 0:d63a63feb104 | 146 | } |
jah128 | 0:d63a63feb104 | 147 | |
jah128 | 0:d63a63feb104 | 148 | int main() |
jah128 | 0:d63a63feb104 | 149 | { |
jah128 | 0:d63a63feb104 | 150 | pc.baud(PC_BAUD); |
jah128 | 0:d63a63feb104 | 151 | display.init_display(); |
jah128 | 0:d63a63feb104 | 152 | display.printf("YORK ROBOTICS LAB PI-SWARM CONTROLLER Software 0.5"); |
jah128 | 0:d63a63feb104 | 153 | wait(2.0); |
jah128 | 0:d63a63feb104 | 154 | display.clear_display(); |
jah128 | 0:d63a63feb104 | 155 | display.printf("Command:"); |
jah128 | 0:d63a63feb104 | 156 | rf.rf_init(); |
jah128 | 0:d63a63feb104 | 157 | rf.setFrequency(435000000); |
jah128 | 0:d63a63feb104 | 158 | rf.setDatarate(57600); |
jah128 | 0:d63a63feb104 | 159 | setup_switches(); |
jah128 | 0:d63a63feb104 | 160 | switch_ticker.attach( &check_switch , 0.05 ); |
jah128 | 0:d63a63feb104 | 161 | while(1) { |
jah128 | 0:d63a63feb104 | 162 | myled = 1; |
jah128 | 0:d63a63feb104 | 163 | // rx_led=1; |
jah128 | 0:d63a63feb104 | 164 | // tx_led=0; |
jah128 | 0:d63a63feb104 | 165 | wait(0.5); |
jah128 | 0:d63a63feb104 | 166 | // rx_led=0; |
jah128 | 0:d63a63feb104 | 167 | // tx_led=1; |
jah128 | 0:d63a63feb104 | 168 | myled = 0; |
jah128 | 0:d63a63feb104 | 169 | wait(0.5); |
jah128 | 0:d63a63feb104 | 170 | } |
jah128 | 0:d63a63feb104 | 171 | |
jah128 | 0:d63a63feb104 | 172 | } |
jah128 | 0:d63a63feb104 | 173 | |
jah128 | 0:d63a63feb104 | 174 | void setup_switches() |
jah128 | 0:d63a63feb104 | 175 | { |
jah128 | 0:d63a63feb104 | 176 | switch_center.mode(PullUp); |
jah128 | 0:d63a63feb104 | 177 | switch_up.mode(PullUp); |
jah128 | 0:d63a63feb104 | 178 | switch_upleft.mode(PullUp); |
jah128 | 0:d63a63feb104 | 179 | switch_left.mode(PullUp); |
jah128 | 0:d63a63feb104 | 180 | switch_downleft.mode(PullUp); |
jah128 | 0:d63a63feb104 | 181 | switch_down.mode(PullUp); |
jah128 | 0:d63a63feb104 | 182 | switch_downright.mode(PullUp); |
jah128 | 0:d63a63feb104 | 183 | switch_right.mode(PullUp); |
jah128 | 0:d63a63feb104 | 184 | switch_upright.mode(PullUp); |
jah128 | 0:d63a63feb104 | 185 | } |
jah128 | 0:d63a63feb104 | 186 | |
jah128 | 0:d63a63feb104 | 187 | |
jah128 | 0:d63a63feb104 | 188 | void handleData(char * data, char length) |
jah128 | 0:d63a63feb104 | 189 | { |
jah128 | 0:d63a63feb104 | 190 | display.set_position(1,1); |
jah128 | 0:d63a63feb104 | 191 | display.write_string(data,length); |
jah128 | 0:d63a63feb104 | 192 | } |