1 | #include "r2ca.h"
|
---|
2 | #include "ESP8266.h"
|
---|
3 |
|
---|
4 | #include <SPI.h>
|
---|
5 | #include <Pixy.h>
|
---|
6 |
|
---|
7 | #include "../examples_gdef.h"
|
---|
8 |
|
---|
9 | #define WMODE_STATION
|
---|
10 |
|
---|
11 | extern void task1_setup();
|
---|
12 |
|
---|
13 | void setup()
|
---|
14 | {
|
---|
15 | int ret;
|
---|
16 |
|
---|
17 | Serial.println("Echo Server : Start!");
|
---|
18 | ret = WiFi.begin(Serial5, 115200);
|
---|
19 |
|
---|
20 | if(ret == 1) {
|
---|
21 | Serial.print("Cannot communicate with ESP8266.");
|
---|
22 | while(1);
|
---|
23 | } else if(ret == 2) {
|
---|
24 | Serial.println("FW Version mismatch.");
|
---|
25 | Serial.print("FW Version:");
|
---|
26 | Serial.println(WiFi.getVersion().c_str());
|
---|
27 | Serial.print("Supported FW Version:");
|
---|
28 | Serial.print(ESP8266_SUPPORT_VERSION);
|
---|
29 | while(1);
|
---|
30 | } else {
|
---|
31 | Serial.print("begin ok\r\n");
|
---|
32 | }
|
---|
33 |
|
---|
34 | Serial.print("FW Version:");
|
---|
35 | Serial.println(WiFi.getVersion().c_str());
|
---|
36 |
|
---|
37 | #ifdef WMODE_STATION
|
---|
38 | if (WiFi.setOprToStation()) {
|
---|
39 | Serial.print("to station ok\r\n");
|
---|
40 | } else {
|
---|
41 | Serial.print("to station err\r\n");
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (WiFi.joinAP(STA_SSID, STA_PASSWORD)) {
|
---|
45 | Serial.print("Join AP success\r\n");
|
---|
46 | Serial.print("IP: ");
|
---|
47 | Serial.println(WiFi.getLocalIP().c_str());
|
---|
48 | } else {
|
---|
49 | Serial.print("Join AP failure\r\n");
|
---|
50 | while(1);
|
---|
51 | }
|
---|
52 | #else /* !WMODE_STATION */
|
---|
53 | if (WiFi.setOprToSoftAP()) {
|
---|
54 | Serial.print("to softap ok\r\n");
|
---|
55 | } else {
|
---|
56 | Serial.print("to softap err\r\n");
|
---|
57 | }
|
---|
58 | if(WiFi.setSoftAPParam(AP_SSID, AP_PASSWORD, 7, 0)){
|
---|
59 | Serial.print("Set SoftAP success\r\n");
|
---|
60 | Serial.print("IP: ");
|
---|
61 | Serial.println(WiFi.getLocalIP().c_str());
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | Serial.print("Set SoftAP failure\r\n");
|
---|
65 | }
|
---|
66 | #endif /* WMODE_STATION */
|
---|
67 |
|
---|
68 | if (WiFi.enableMUX()) {
|
---|
69 | Serial.print("multiple ok\r\n");
|
---|
70 | } else {
|
---|
71 | Serial.print("multiple err\r\n");
|
---|
72 | while(1);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (WiFi.startTCPServer(80)) {
|
---|
76 | Serial.print("start tcp server ok\r\n");
|
---|
77 | } else {
|
---|
78 | Serial.print("start tcp server err\r\n");
|
---|
79 | while(1);
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (WiFi.setTCPServerTimeout(60)) {
|
---|
83 | Serial.print("set tcp server timout 60 seconds\r\n");
|
---|
84 | } else {
|
---|
85 | Serial.print("set tcp server timout err\r\n");
|
---|
86 | while(1);
|
---|
87 | }
|
---|
88 |
|
---|
89 | Serial.print("setup end\r\n");
|
---|
90 |
|
---|
91 | task1_setup();
|
---|
92 | }
|
---|
93 |
|
---|
94 | #define MUX_NULL 0xff
|
---|
95 |
|
---|
96 | uint8_t mux_id_ptn;
|
---|
97 |
|
---|
98 | uint8_t mux_id = MUX_NULL;
|
---|
99 | uint8_t task1_mux_id = MUX_NULL;
|
---|
100 |
|
---|
101 |
|
---|
102 | uint16_t pixy_blocks = 0;
|
---|
103 | char pixy_buf[16][128];
|
---|
104 |
|
---|
105 | void loop()
|
---|
106 | {
|
---|
107 | char buf[128];
|
---|
108 | uint8_t pre_mux_id_ptn;
|
---|
109 |
|
---|
110 | delay(1);
|
---|
111 | /* Check Connection Status */
|
---|
112 | pre_mux_id_ptn = mux_id_ptn;
|
---|
113 |
|
---|
114 | if(!WiFi.getMuxCStatus(&mux_id_ptn)) {
|
---|
115 | Serial.println("getMuxCStatus(&mux_id_ptn) : Error!");
|
---|
116 | }
|
---|
117 | else {
|
---|
118 | if (pre_mux_id_ptn != mux_id_ptn) {
|
---|
119 | Serial.print("Connection Status changed! : 0x");
|
---|
120 | Serial.println(mux_id_ptn, HEX);
|
---|
121 | if (mux_id_ptn & 0x01) {
|
---|
122 | mux_id = 0;
|
---|
123 | }
|
---|
124 | if (mux_id_ptn & 0x02) {
|
---|
125 | task1_mux_id = 1;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (mux_id == MUX_NULL) {
|
---|
131 | return;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (!WiFi.isConnected(mux_id)) {
|
---|
135 | Serial.print("Echo Server : Port is closed: ");
|
---|
136 | Serial.println(mux_id);
|
---|
137 | mux_id = MUX_NULL;
|
---|
138 | return;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if(pixy_blocks != 0) {
|
---|
142 | sprintf(buf, "Detected %d:\n", pixy_blocks);
|
---|
143 | WiFi.send(mux_id, (uint8_t*)buf, strlen(buf));
|
---|
144 | for (int j=0; j<pixy_blocks; j++){
|
---|
145 | WiFi.send(mux_id, (uint8_t*)pixy_buf[j], strlen(pixy_buf[j]));
|
---|
146 | }
|
---|
147 | pixy_blocks = 0;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | Pixy pixy;
|
---|
152 |
|
---|
153 | #define X_CENTER ((PIXY_MAX_X-PIXY_MIN_X)/2)
|
---|
154 | #define Y_CENTER ((PIXY_MAX_Y-PIXY_MIN_Y)/2)
|
---|
155 |
|
---|
156 | class ServoLoop
|
---|
157 | {
|
---|
158 | public:
|
---|
159 | ServoLoop(int32_t pgain, int32_t dgain);
|
---|
160 |
|
---|
161 | void update(int32_t error);
|
---|
162 |
|
---|
163 | int32_t m_pos;
|
---|
164 | int32_t m_prevError;
|
---|
165 | int32_t m_pgain;
|
---|
166 | int32_t m_dgain;
|
---|
167 | };
|
---|
168 |
|
---|
169 |
|
---|
170 | ServoLoop panLoop(300, 500);
|
---|
171 | ServoLoop tiltLoop(500, 700);
|
---|
172 |
|
---|
173 | ServoLoop::ServoLoop(int32_t pgain, int32_t dgain)
|
---|
174 | {
|
---|
175 | m_pos = PIXY_RCS_CENTER_POS;
|
---|
176 | m_pgain = pgain;
|
---|
177 | m_dgain = dgain;
|
---|
178 | m_prevError = 0x80000000L;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void ServoLoop::update(int32_t error)
|
---|
182 | {
|
---|
183 | long int vel;
|
---|
184 | char buf[32];
|
---|
185 | if (m_prevError!=0x80000000)
|
---|
186 | {
|
---|
187 | vel = (error*m_pgain + (error - m_prevError)*m_dgain)>>10;
|
---|
188 | //sprintf(buf, "%ld\n", vel);
|
---|
189 | //Serial.print(buf);
|
---|
190 | m_pos += vel;
|
---|
191 | if (m_pos>PIXY_RCS_MAX_POS)
|
---|
192 | m_pos = PIXY_RCS_MAX_POS;
|
---|
193 | else if (m_pos<PIXY_RCS_MIN_POS)
|
---|
194 | m_pos = PIXY_RCS_MIN_POS;
|
---|
195 | }
|
---|
196 | m_prevError = error;
|
---|
197 | }
|
---|
198 |
|
---|
199 | void task1_setup()
|
---|
200 | {
|
---|
201 | Serial.print("PIXY Starting...\n");
|
---|
202 | pixy.init();
|
---|
203 | pixy.setServos(PIXY_RCS_CENTER_POS, PIXY_RCS_CENTER_POS);
|
---|
204 | }
|
---|
205 |
|
---|
206 | void loop1()
|
---|
207 | {
|
---|
208 | static int i = 0;
|
---|
209 | int j;
|
---|
210 | uint16_t blocks;
|
---|
211 | char buf[128];
|
---|
212 | int32_t panError, tiltError;
|
---|
213 |
|
---|
214 | blocks = pixy.getBlocks();
|
---|
215 |
|
---|
216 | if (blocks) {
|
---|
217 | panError = X_CENTER-pixy.blocks[0].x;
|
---|
218 | tiltError = pixy.blocks[0].y-Y_CENTER;
|
---|
219 |
|
---|
220 | panLoop.update(panError);
|
---|
221 | tiltLoop.update(tiltError);
|
---|
222 |
|
---|
223 | pixy.setServos(panLoop.m_pos, tiltLoop.m_pos);
|
---|
224 | i++;
|
---|
225 |
|
---|
226 | // do this (print) every 50 frames because printing every
|
---|
227 | // frame would bog down the Arduino
|
---|
228 | if (i%50==0) {
|
---|
229 | sprintf(buf, "Detected %d:\n", blocks);
|
---|
230 | Serial.print(buf);
|
---|
231 | for (j=0; j<blocks; j++){
|
---|
232 | sprintf(buf, " block %d: ", j);
|
---|
233 | Serial.print(buf);
|
---|
234 | pixy.blocks[j].print();
|
---|
235 | }
|
---|
236 | if(pixy_blocks == 0) {
|
---|
237 | if (blocks > 16) {
|
---|
238 | blocks = 16;
|
---|
239 | }
|
---|
240 | pixy_blocks = blocks;
|
---|
241 | for (j=0; j<blocks; j++){
|
---|
242 | sprintf(pixy_buf[j], "sig: %d x: %d y: %d width: %d height: %d\n",
|
---|
243 | pixy.blocks[j].signature, pixy.blocks[j].x, pixy.blocks[j].y,
|
---|
244 | pixy.blocks[j].width, pixy.blocks[j].height);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | delay(10);
|
---|
251 | }
|
---|