Added websocket control to Nerf Gun.
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "Websocket.h" 00004 00005 #define V_SERVO_CENTER 1800 00006 #define V_SERVO_MAX 2200 00007 #define V_SERVO_MIN 800 00008 #define H_SERVO_CENTER 1600 00009 #define H_SERVO_MAX 2200 00010 #define H_SERVO_MIN 800 00011 00012 DigitalOut myled1(LED1); 00013 DigitalOut myled2(LED2); 00014 DigitalOut myled3(LED3); 00015 00016 DigitalOut fire(PTB23); 00017 PwmOut v_servo(PTC2); 00018 PwmOut h_servo(PTA2); 00019 00020 int main() 00021 { 00022 EthernetInterface eth; 00023 eth.init(); //Use DHCP 00024 eth.connect(); 00025 printf("IP Address is %s\n\r", eth.getIPAddress()); 00026 00027 Websocket ws(/*LINK TO YOUR WEBSOCKET SERVER GOES HERE*/); 00028 ws.connect(); 00029 char recv[6]="fire"; 00030 00031 v_servo.period_us(20000); // servo requires a 20ms period 00032 v_servo.pulsewidth_us(V_SERVO_CENTER); 00033 h_servo.period_us(20000); // servo requires a 20ms period 00034 h_servo.pulsewidth_us(H_SERVO_CENTER); 00035 fire = 0; 00036 00037 int v_pulse = V_SERVO_CENTER; 00038 int h_pulse = H_SERVO_CENTER; 00039 00040 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00041 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00042 int move_x=0; 00043 int move_y=0; 00044 wait(0.5); 00045 00046 while(1){ 00047 if(ws.read(recv)){ 00048 if(strcmp(recv,"fire")==0){ 00049 fire=1; 00050 while(1){ 00051 wait(0.02); 00052 ws.read(recv); 00053 if(strcmp(recv,"stop")==0){ 00054 fire=0; 00055 break; 00056 } 00057 } 00058 } 00059 else if(strcmp(recv,"left")==0){ 00060 while(1){ 00061 move_x-=10; 00062 h_pulse = H_SERVO_CENTER + move_x; 00063 00064 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN; 00065 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX; 00066 00067 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00068 ws.read(recv); 00069 if(strcmp(recv,"stop")==0){ 00070 break; 00071 } 00072 wait(0.02); 00073 } 00074 } 00075 else if(strcmp(recv,"right")==0){ 00076 while(1){ 00077 move_x+=10; 00078 h_pulse = H_SERVO_CENTER + move_x; 00079 00080 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN; 00081 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX; 00082 00083 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00084 ws.read(recv); 00085 if(strcmp(recv,"stop")==0){ 00086 break; 00087 } 00088 wait(0.02); 00089 } 00090 } 00091 else if(strcmp(recv,"up")==0){ 00092 while(1){ 00093 move_y-=10; 00094 v_pulse = V_SERVO_CENTER + move_y; 00095 00096 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN; 00097 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX; 00098 00099 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00100 ws.read(recv); 00101 if(strcmp(recv,"stop")==0){ 00102 break; 00103 } 00104 wait(0.02); 00105 } 00106 } 00107 else if(strcmp(recv,"down")==0){ 00108 while(1){ 00109 move_y+=10; 00110 v_pulse = V_SERVO_CENTER + move_y; 00111 00112 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN; 00113 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX; 00114 00115 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00116 ws.read(recv); 00117 if(strcmp(recv,"stop")==0){ 00118 break; 00119 } 00120 wait(0.02); 00121 } 00122 } 00123 else if(strcmp(recv,"lpuls")==0){ 00124 move_x-=25; 00125 h_pulse = H_SERVO_CENTER + move_x; 00126 00127 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN; 00128 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX; 00129 00130 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00131 } 00132 else if(strcmp(recv,"rpuls")==0){ 00133 move_x+=25; 00134 h_pulse = H_SERVO_CENTER + move_x; 00135 00136 if (h_pulse <= H_SERVO_MIN) h_pulse = H_SERVO_MIN; 00137 if (h_pulse >= H_SERVO_MAX) h_pulse = H_SERVO_MAX; 00138 00139 h_servo.pulsewidth_us(h_pulse); // servo position determined by a pulsewidth between 1-2ms 00140 } 00141 else if(strcmp(recv,"upuls")==0){ 00142 move_y-=25; 00143 v_pulse = V_SERVO_CENTER + move_y; 00144 00145 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN; 00146 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX; 00147 00148 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00149 } 00150 else if(strcmp(recv,"dpuls")==0){ 00151 move_y+=25; 00152 v_pulse = V_SERVO_CENTER + move_y; 00153 00154 if (v_pulse <= V_SERVO_MIN) v_pulse = V_SERVO_MIN; 00155 if (v_pulse >= V_SERVO_MAX) v_pulse = V_SERVO_MAX; 00156 00157 v_servo.pulsewidth_us(v_pulse); // servo position determined by a pulsewidth between 1-2ms 00158 } 00159 } 00160 } 00161 00162 }
Generated on Tue Jul 12 2022 23:12:50 by 1.7.2