Added websocket control to Nerf Gun.

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Committer:
alexleversen
Date:
Mon Aug 18 16:55:44 2014 +0000
Revision:
1:282c51f938b3
Parent:
0:c9cc3d5f6e8b
Updated for general use. Does not work without editing line 27 to point to ws server.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexleversen 0:c9cc3d5f6e8b 1 #include "mbed.h"
alexleversen 0:c9cc3d5f6e8b 2 #include "EthernetInterface.h"
alexleversen 0:c9cc3d5f6e8b 3 #include "Websocket.h"
alexleversen 0:c9cc3d5f6e8b 4
alexleversen 0:c9cc3d5f6e8b 5 #define V_SERVO_CENTER 1800
alexleversen 0:c9cc3d5f6e8b 6 #define V_SERVO_MAX 2200
alexleversen 0:c9cc3d5f6e8b 7 #define V_SERVO_MIN 800
alexleversen 0:c9cc3d5f6e8b 8 #define H_SERVO_CENTER 1600
alexleversen 0:c9cc3d5f6e8b 9 #define H_SERVO_MAX 2200
alexleversen 0:c9cc3d5f6e8b 10 #define H_SERVO_MIN 800
alexleversen 0:c9cc3d5f6e8b 11
alexleversen 0:c9cc3d5f6e8b 12 DigitalOut myled1(LED1);
alexleversen 0:c9cc3d5f6e8b 13 DigitalOut myled2(LED2);
alexleversen 0:c9cc3d5f6e8b 14 DigitalOut myled3(LED3);
alexleversen 0:c9cc3d5f6e8b 15
alexleversen 0:c9cc3d5f6e8b 16 DigitalOut fire(PTB23);
alexleversen 0:c9cc3d5f6e8b 17 PwmOut v_servo(PTC2);
alexleversen 0:c9cc3d5f6e8b 18 PwmOut h_servo(PTA2);
alexleversen 0:c9cc3d5f6e8b 19
alexleversen 0:c9cc3d5f6e8b 20 int main()
alexleversen 0:c9cc3d5f6e8b 21 {
alexleversen 0:c9cc3d5f6e8b 22 EthernetInterface eth;
alexleversen 0:c9cc3d5f6e8b 23 eth.init(); //Use DHCP
alexleversen 0:c9cc3d5f6e8b 24 eth.connect();
alexleversen 0:c9cc3d5f6e8b 25 printf("IP Address is %s\n\r", eth.getIPAddress());
alexleversen 0:c9cc3d5f6e8b 26
alexleversen 1:282c51f938b3 27 Websocket ws(/*LINK TO YOUR WEBSOCKET SERVER GOES HERE*/);
alexleversen 0:c9cc3d5f6e8b 28 ws.connect();
alexleversen 0:c9cc3d5f6e8b 29 char recv[6]="fire";
alexleversen 0:c9cc3d5f6e8b 30
alexleversen 0:c9cc3d5f6e8b 31 v_servo.period_us(20000); // servo requires a 20ms period
alexleversen 0:c9cc3d5f6e8b 32 v_servo.pulsewidth_us(V_SERVO_CENTER);
alexleversen 0:c9cc3d5f6e8b 33 h_servo.period_us(20000); // servo requires a 20ms period
alexleversen 0:c9cc3d5f6e8b 34 h_servo.pulsewidth_us(H_SERVO_CENTER);
alexleversen 0:c9cc3d5f6e8b 35 fire = 0;
alexleversen 0:c9cc3d5f6e8b 36
alexleversen 0:c9cc3d5f6e8b 37 int v_pulse = V_SERVO_CENTER;
alexleversen 0:c9cc3d5f6e8b 38 int h_pulse = H_SERVO_CENTER;
alexleversen 0:c9cc3d5f6e8b 39
alexleversen 0:c9cc3d5f6e8b 40 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 41 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 42 int move_x=0;
alexleversen 0:c9cc3d5f6e8b 43 int move_y=0;
alexleversen 0:c9cc3d5f6e8b 44 wait(0.5);
alexleversen 0:c9cc3d5f6e8b 45
alexleversen 0:c9cc3d5f6e8b 46 while(1){
alexleversen 0:c9cc3d5f6e8b 47 if(ws.read(recv)){
alexleversen 0:c9cc3d5f6e8b 48 if(strcmp(recv,"fire")==0){
alexleversen 0:c9cc3d5f6e8b 49 fire=1;
alexleversen 0:c9cc3d5f6e8b 50 while(1){
alexleversen 0:c9cc3d5f6e8b 51 wait(0.02);
alexleversen 0:c9cc3d5f6e8b 52 ws.read(recv);
alexleversen 0:c9cc3d5f6e8b 53 if(strcmp(recv,"stop")==0){
alexleversen 0:c9cc3d5f6e8b 54 fire=0;
alexleversen 0:c9cc3d5f6e8b 55 break;
alexleversen 0:c9cc3d5f6e8b 56 }
alexleversen 0:c9cc3d5f6e8b 57 }
alexleversen 0:c9cc3d5f6e8b 58 }
alexleversen 0:c9cc3d5f6e8b 59 else if(strcmp(recv,"left")==0){
alexleversen 0:c9cc3d5f6e8b 60 while(1){
alexleversen 0:c9cc3d5f6e8b 61 move_x-=10;
alexleversen 0:c9cc3d5f6e8b 62 h_pulse = H_SERVO_CENTER + move_x;
alexleversen 0:c9cc3d5f6e8b 63
alexleversen 0:c9cc3d5f6e8b 64 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 65 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 66
alexleversen 0:c9cc3d5f6e8b 67 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 68 ws.read(recv);
alexleversen 0:c9cc3d5f6e8b 69 if(strcmp(recv,"stop")==0){
alexleversen 0:c9cc3d5f6e8b 70 break;
alexleversen 0:c9cc3d5f6e8b 71 }
alexleversen 0:c9cc3d5f6e8b 72 wait(0.02);
alexleversen 0:c9cc3d5f6e8b 73 }
alexleversen 0:c9cc3d5f6e8b 74 }
alexleversen 0:c9cc3d5f6e8b 75 else if(strcmp(recv,"right")==0){
alexleversen 0:c9cc3d5f6e8b 76 while(1){
alexleversen 0:c9cc3d5f6e8b 77 move_x+=10;
alexleversen 0:c9cc3d5f6e8b 78 h_pulse = H_SERVO_CENTER + move_x;
alexleversen 0:c9cc3d5f6e8b 79
alexleversen 0:c9cc3d5f6e8b 80 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 81 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 82
alexleversen 0:c9cc3d5f6e8b 83 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 84 ws.read(recv);
alexleversen 0:c9cc3d5f6e8b 85 if(strcmp(recv,"stop")==0){
alexleversen 0:c9cc3d5f6e8b 86 break;
alexleversen 0:c9cc3d5f6e8b 87 }
alexleversen 0:c9cc3d5f6e8b 88 wait(0.02);
alexleversen 0:c9cc3d5f6e8b 89 }
alexleversen 0:c9cc3d5f6e8b 90 }
alexleversen 0:c9cc3d5f6e8b 91 else if(strcmp(recv,"up")==0){
alexleversen 0:c9cc3d5f6e8b 92 while(1){
alexleversen 0:c9cc3d5f6e8b 93 move_y-=10;
alexleversen 0:c9cc3d5f6e8b 94 v_pulse = V_SERVO_CENTER + move_y;
alexleversen 0:c9cc3d5f6e8b 95
alexleversen 0:c9cc3d5f6e8b 96 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 97 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 98
alexleversen 0:c9cc3d5f6e8b 99 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 100 ws.read(recv);
alexleversen 0:c9cc3d5f6e8b 101 if(strcmp(recv,"stop")==0){
alexleversen 0:c9cc3d5f6e8b 102 break;
alexleversen 0:c9cc3d5f6e8b 103 }
alexleversen 0:c9cc3d5f6e8b 104 wait(0.02);
alexleversen 0:c9cc3d5f6e8b 105 }
alexleversen 0:c9cc3d5f6e8b 106 }
alexleversen 0:c9cc3d5f6e8b 107 else if(strcmp(recv,"down")==0){
alexleversen 0:c9cc3d5f6e8b 108 while(1){
alexleversen 0:c9cc3d5f6e8b 109 move_y+=10;
alexleversen 0:c9cc3d5f6e8b 110 v_pulse = V_SERVO_CENTER + move_y;
alexleversen 0:c9cc3d5f6e8b 111
alexleversen 0:c9cc3d5f6e8b 112 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 113 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 114
alexleversen 0:c9cc3d5f6e8b 115 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 116 ws.read(recv);
alexleversen 0:c9cc3d5f6e8b 117 if(strcmp(recv,"stop")==0){
alexleversen 0:c9cc3d5f6e8b 118 break;
alexleversen 0:c9cc3d5f6e8b 119 }
alexleversen 0:c9cc3d5f6e8b 120 wait(0.02);
alexleversen 0:c9cc3d5f6e8b 121 }
alexleversen 0:c9cc3d5f6e8b 122 }
alexleversen 0:c9cc3d5f6e8b 123 else if(strcmp(recv,"lpuls")==0){
alexleversen 0:c9cc3d5f6e8b 124 move_x-=25;
alexleversen 0:c9cc3d5f6e8b 125 h_pulse = H_SERVO_CENTER + move_x;
alexleversen 0:c9cc3d5f6e8b 126
alexleversen 0:c9cc3d5f6e8b 127 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 128 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 129
alexleversen 0:c9cc3d5f6e8b 130 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 131 }
alexleversen 0:c9cc3d5f6e8b 132 else if(strcmp(recv,"rpuls")==0){
alexleversen 0:c9cc3d5f6e8b 133 move_x+=25;
alexleversen 0:c9cc3d5f6e8b 134 h_pulse = H_SERVO_CENTER + move_x;
alexleversen 0:c9cc3d5f6e8b 135
alexleversen 0:c9cc3d5f6e8b 136 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 137 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 138
alexleversen 0:c9cc3d5f6e8b 139 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 140 }
alexleversen 0:c9cc3d5f6e8b 141 else if(strcmp(recv,"upuls")==0){
alexleversen 0:c9cc3d5f6e8b 142 move_y-=25;
alexleversen 0:c9cc3d5f6e8b 143 v_pulse = V_SERVO_CENTER + move_y;
alexleversen 0:c9cc3d5f6e8b 144
alexleversen 0:c9cc3d5f6e8b 145 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 146 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 147
alexleversen 0:c9cc3d5f6e8b 148 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 149 }
alexleversen 0:c9cc3d5f6e8b 150 else if(strcmp(recv,"dpuls")==0){
alexleversen 0:c9cc3d5f6e8b 151 move_y+=25;
alexleversen 0:c9cc3d5f6e8b 152 v_pulse = V_SERVO_CENTER + move_y;
alexleversen 0:c9cc3d5f6e8b 153
alexleversen 0:c9cc3d5f6e8b 154 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN;
alexleversen 0:c9cc3d5f6e8b 155 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX;
alexleversen 0:c9cc3d5f6e8b 156
alexleversen 0:c9cc3d5f6e8b 157 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms
alexleversen 0:c9cc3d5f6e8b 158 }
alexleversen 0:c9cc3d5f6e8b 159 }
alexleversen 0:c9cc3d5f6e8b 160 }
alexleversen 0:c9cc3d5f6e8b 161
alexleversen 0:c9cc3d5f6e8b 162 }