Linija pogona

Dependencies:   Motor jsmn mbed

Committer:
mario_meh
Date:
Wed Nov 22 08:56:31 2017 +0000
Revision:
0:02085be78662
Linija pogona

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mario_meh 0:02085be78662 1 #include "mbed.h"
mario_meh 0:02085be78662 2 #include "jsmn.h"
mario_meh 0:02085be78662 3 #include "Motor.h"
mario_meh 0:02085be78662 4 #include <string>
mario_meh 0:02085be78662 5 #include <cctype>
mario_meh 0:02085be78662 6
mario_meh 0:02085be78662 7 #define MAXTOKEN 64
mario_meh 0:02085be78662 8
mario_meh 0:02085be78662 9 const char *jsmn_type_str[] = {
mario_meh 0:02085be78662 10 "PRIMITIVE",
mario_meh 0:02085be78662 11 "OBJECT",
mario_meh 0:02085be78662 12 "ARRAY",
mario_meh 0:02085be78662 13 "STRING"
mario_meh 0:02085be78662 14 };
mario_meh 0:02085be78662 15
mario_meh 0:02085be78662 16 /*
mario_meh 0:02085be78662 17 typedef struct {
mario_meh 0:02085be78662 18 int kreirao;
mario_meh 0:02085be78662 19 char kod;
mario_meh 0:02085be78662 20 char mcu;
mario_meh 0:02085be78662 21 char model;
mario_meh 0:02085be78662 22 char extra;
mario_meh 0:02085be78662 23 }SER_DATA;*/
mario_meh 0:02085be78662 24
mario_meh 0:02085be78662 25 Serial pc(USBTX, USBRX);
mario_meh 0:02085be78662 26 //Motor sg90(p23, p6, p5); // pwm, fwd, rev
mario_meh 0:02085be78662 27 Serial esp(p28, p27); // tx, rx
mario_meh 0:02085be78662 28 DigitalOut reset(p26);
mario_meh 0:02085be78662 29 DigitalOut led1(LED1);
mario_meh 0:02085be78662 30 DigitalOut led4(LED4);
mario_meh 0:02085be78662 31 Timer t;
mario_meh 0:02085be78662 32
mario_meh 0:02085be78662 33 //==================
mario_meh 0:02085be78662 34 DigitalOut _a1(p6);
mario_meh 0:02085be78662 35 DigitalOut _a2(p5);
mario_meh 0:02085be78662 36 DigitalOut _en(p23);
mario_meh 0:02085be78662 37
mario_meh 0:02085be78662 38 int count,ended,timeout;
mario_meh 0:02085be78662 39 char buf[1024];
mario_meh 0:02085be78662 40 char kreirao[25];
mario_meh 0:02085be78662 41 char kod[25];
mario_meh 0:02085be78662 42 char mcu[25];
mario_meh 0:02085be78662 43 char model[25];
mario_meh 0:02085be78662 44 char extra[25];
mario_meh 0:02085be78662 45 int pos;
mario_meh 0:02085be78662 46
mario_meh 0:02085be78662 47 void getreply();
mario_meh 0:02085be78662 48 void ser_kreirao(char* inBuf, int start, int end);
mario_meh 0:02085be78662 49 void ser_kod(char* inBuf, int start, int end);
mario_meh 0:02085be78662 50 void ser_mcu(char* inBuf, int start, int end);
mario_meh 0:02085be78662 51 void ser_model(char* inBuf, int start, int end);
mario_meh 0:02085be78662 52 void ser_extra(char* inBuf, int start, int end);
mario_meh 0:02085be78662 53
mario_meh 0:02085be78662 54 int main() {
mario_meh 0:02085be78662 55
mario_meh 0:02085be78662 56 pc.printf("\n ESP 01 - jsmn \r\n");
mario_meh 0:02085be78662 57
mario_meh 0:02085be78662 58 // jsmn var
mario_meh 0:02085be78662 59 const char *js; // Pointer to json string
mario_meh 0:02085be78662 60 int r; // Number of token parsed
mario_meh 0:02085be78662 61 jsmn_parser p; // jsmn parser
mario_meh 0:02085be78662 62 jsmntok_t t[MAXTOKEN]; // Parsed token
mario_meh 0:02085be78662 63
mario_meh 0:02085be78662 64 //=================================
mario_meh 0:02085be78662 65 reset = 0;
mario_meh 0:02085be78662 66 pc.baud(9600);
mario_meh 0:02085be78662 67 wait(1);
mario_meh 0:02085be78662 68 reset = 1;
mario_meh 0:02085be78662 69 //timeout = 2;
mario_meh 0:02085be78662 70 getreply();
mario_meh 0:02085be78662 71
mario_meh 0:02085be78662 72 while(1) {
mario_meh 0:02085be78662 73 pc.printf("\r\n");
mario_meh 0:02085be78662 74 pc.printf("\n Počinjem preuzimati - wifi \r\n");
mario_meh 0:02085be78662 75 esp.baud(9600);
mario_meh 0:02085be78662 76 getreply();
mario_meh 0:02085be78662 77 timeout=1;
mario_meh 0:02085be78662 78
mario_meh 0:02085be78662 79 //==============================
mario_meh 0:02085be78662 80 jsmn_init(&p);
mario_meh 0:02085be78662 81 r = jsmn_parse(&p, buf, strlen(buf), t, MAXTOKEN);
mario_meh 0:02085be78662 82 pc.printf("Parsed %d tokens\n", r);
mario_meh 0:02085be78662 83 pc.printf(" TYPE START END SIZE PAR\n");
mario_meh 0:02085be78662 84 pc.printf(" ---------- ----- ---- ---- ---\n");
mario_meh 0:02085be78662 85 char ch;
mario_meh 0:02085be78662 86 jsmntok_t at; // A token for general use
mario_meh 0:02085be78662 87 //===============================
mario_meh 0:02085be78662 88 for (int i = 0; i < r; i++){
mario_meh 0:02085be78662 89 at = t[i];
mario_meh 0:02085be78662 90 pc.printf("Token %2d = %-10.10s (%4d - %4d, %3d, %2d) ",
mario_meh 0:02085be78662 91 i, jsmn_type_str[at.type],
mario_meh 0:02085be78662 92 at.start, at.end,
mario_meh 0:02085be78662 93 at.size, at.parent);
mario_meh 0:02085be78662 94
mario_meh 0:02085be78662 95 switch (i){
mario_meh 0:02085be78662 96 case 3:
mario_meh 0:02085be78662 97 ser_kreirao(buf, at.start, at.end);
mario_meh 0:02085be78662 98 pc.printf("\n");
mario_meh 0:02085be78662 99 break;
mario_meh 0:02085be78662 100
mario_meh 0:02085be78662 101 case 5:
mario_meh 0:02085be78662 102 ser_kod(buf, at.start, at.end);
mario_meh 0:02085be78662 103 pc.printf("\n");
mario_meh 0:02085be78662 104 break;
mario_meh 0:02085be78662 105
mario_meh 0:02085be78662 106 case 7:
mario_meh 0:02085be78662 107 ser_mcu(buf, at.start, at.end);
mario_meh 0:02085be78662 108 pc.printf("\n");
mario_meh 0:02085be78662 109 break;
mario_meh 0:02085be78662 110
mario_meh 0:02085be78662 111 case 9:
mario_meh 0:02085be78662 112 ser_model(buf, at.start, at.end);
mario_meh 0:02085be78662 113 pc.printf("\n");
mario_meh 0:02085be78662 114 break;
mario_meh 0:02085be78662 115
mario_meh 0:02085be78662 116 case 11:
mario_meh 0:02085be78662 117 ser_extra(buf, at.start, at.end);
mario_meh 0:02085be78662 118 pc.printf("\n");
mario_meh 0:02085be78662 119 break;
mario_meh 0:02085be78662 120
mario_meh 0:02085be78662 121 default:
mario_meh 0:02085be78662 122 pc.printf("\n");
mario_meh 0:02085be78662 123 break;
mario_meh 0:02085be78662 124 }
mario_meh 0:02085be78662 125 }
mario_meh 0:02085be78662 126
mario_meh 0:02085be78662 127 //====================================
mario_meh 0:02085be78662 128 pc.printf("\n");
mario_meh 0:02085be78662 129 pc.printf("Kreirao: %s\n", kreirao);
mario_meh 0:02085be78662 130 pc.printf("Kod: %s\n", kod);
mario_meh 0:02085be78662 131 pc.printf("Mcu: %s\n", mcu);
mario_meh 0:02085be78662 132 pc.printf("Model: %s\n", model);
mario_meh 0:02085be78662 133 pc.printf("Extra: %s\n", extra);
mario_meh 0:02085be78662 134 pc.printf("\n");
mario_meh 0:02085be78662 135 pc.printf("ESP Json - Django linija pogona\n");
mario_meh 0:02085be78662 136 pc.printf(buf);
mario_meh 0:02085be78662 137 pc.printf("\n");
mario_meh 0:02085be78662 138 //wait(2);
mario_meh 0:02085be78662 139 if(strcmp(extra, "lijevo") == 0){
mario_meh 0:02085be78662 140 pc.printf("Lijevo");
mario_meh 0:02085be78662 141 //=====================
mario_meh 0:02085be78662 142 //sg90.speed(0);
mario_meh 0:02085be78662 143 //for (float s= -1.0; s < 0 ; s += 0.01) {
mario_meh 0:02085be78662 144 // sg90.speed(s);
mario_meh 0:02085be78662 145 // wait(0.1);
mario_meh 0:02085be78662 146 //}
mario_meh 0:02085be78662 147 //=====================
mario_meh 0:02085be78662 148 _en = 1;
mario_meh 0:02085be78662 149 _a1 = 0;
mario_meh 0:02085be78662 150 _a2 = 1;
mario_meh 0:02085be78662 151 led4 = !led4;
mario_meh 0:02085be78662 152 memset(&extra[0], 0, 25);
mario_meh 0:02085be78662 153 }
mario_meh 0:02085be78662 154 if(strcmp(extra, "desno") == 0){
mario_meh 0:02085be78662 155 pc.printf("Desno");
mario_meh 0:02085be78662 156 //=====================
mario_meh 0:02085be78662 157 //sg90.speed(0);
mario_meh 0:02085be78662 158 //for (float s= 0; s < 1.0 ; s += 0.01) {
mario_meh 0:02085be78662 159 // sg90.speed(s);
mario_meh 0:02085be78662 160 // wait(0.1);
mario_meh 0:02085be78662 161 //}
mario_meh 0:02085be78662 162 //=====================
mario_meh 0:02085be78662 163 _en = 1;
mario_meh 0:02085be78662 164 _a1 = 1;
mario_meh 0:02085be78662 165 _a2 = 0;
mario_meh 0:02085be78662 166 led4 = !led4;
mario_meh 0:02085be78662 167 memset(&extra[0], 0, 25);
mario_meh 0:02085be78662 168 }
mario_meh 0:02085be78662 169 if(strcmp(extra, "brze") == 0){
mario_meh 0:02085be78662 170 pc.printf("Brze");
mario_meh 0:02085be78662 171 //=====================
mario_meh 0:02085be78662 172 //brzina = brzina + 0.1;
mario_meh 0:02085be78662 173 //sg90.speed(brzina);
mario_meh 0:02085be78662 174 led4 = !led4;
mario_meh 0:02085be78662 175 memset(&extra[0], 0, 25);
mario_meh 0:02085be78662 176 }
mario_meh 0:02085be78662 177 if(strcmp(extra, "sporije") == 0){
mario_meh 0:02085be78662 178 pc.printf("Sporije\n");
mario_meh 0:02085be78662 179 //=====================
mario_meh 0:02085be78662 180 //brzina = brzina - 0.1;
mario_meh 0:02085be78662 181 //sg90.speed(brzina);
mario_meh 0:02085be78662 182 led4 = !led4;
mario_meh 0:02085be78662 183 memset(&extra[0], 0, 25);
mario_meh 0:02085be78662 184 }
mario_meh 0:02085be78662 185 if(strcmp(extra, "stop") == 0){
mario_meh 0:02085be78662 186 pc.printf("Stop");
mario_meh 0:02085be78662 187 //=====================
mario_meh 0:02085be78662 188 //brzina = 0;
mario_meh 0:02085be78662 189 //sg90.speed(brzina);
mario_meh 0:02085be78662 190 _en = 0;
mario_meh 0:02085be78662 191 led4 = !led4;
mario_meh 0:02085be78662 192 memset(&extra[0], 0, 25);
mario_meh 0:02085be78662 193 }
mario_meh 0:02085be78662 194 if(strcmp(extra, "pauziraj") == 0){
mario_meh 0:02085be78662 195 pc.printf("Pauziraj");
mario_meh 0:02085be78662 196 //=====================
mario_meh 0:02085be78662 197 //brzina = 0;
mario_meh 0:02085be78662 198 //sg90.speed(brzina);
mario_meh 0:02085be78662 199 _en = 0;
mario_meh 0:02085be78662 200 led4 = !led4;
mario_meh 0:02085be78662 201 memset(&extra[0], 0, 25);
mario_meh 0:02085be78662 202 }
mario_meh 0:02085be78662 203 }
mario_meh 0:02085be78662 204 }
mario_meh 0:02085be78662 205
mario_meh 0:02085be78662 206 void getreply() {
mario_meh 0:02085be78662 207 memset(buf, '\0', sizeof(buf));
mario_meh 0:02085be78662 208 t.start();
mario_meh 0:02085be78662 209 ended=0;
mario_meh 0:02085be78662 210 count=0;
mario_meh 0:02085be78662 211
mario_meh 0:02085be78662 212 while(!ended) {
mario_meh 0:02085be78662 213 if(esp.readable()) {
mario_meh 0:02085be78662 214 led1 = !led1;
mario_meh 0:02085be78662 215 buf[count] = esp.getc();
mario_meh 0:02085be78662 216 count++;
mario_meh 0:02085be78662 217 }
mario_meh 0:02085be78662 218 if(t.read() > timeout) {
mario_meh 0:02085be78662 219 ended = 1;
mario_meh 0:02085be78662 220 t.stop();
mario_meh 0:02085be78662 221 t.reset();
mario_meh 0:02085be78662 222 }
mario_meh 0:02085be78662 223 }
mario_meh 0:02085be78662 224 }
mario_meh 0:02085be78662 225
mario_meh 0:02085be78662 226 void ser_kreirao(char* inBuf, int start, int end){
mario_meh 0:02085be78662 227 char* data = inBuf;
mario_meh 0:02085be78662 228 int j = 0;
mario_meh 0:02085be78662 229 for (int i = start; i < end; i++){
mario_meh 0:02085be78662 230 kreirao[j] = data[i];
mario_meh 0:02085be78662 231 j++;
mario_meh 0:02085be78662 232 }
mario_meh 0:02085be78662 233 }
mario_meh 0:02085be78662 234
mario_meh 0:02085be78662 235 void ser_kod(char* inBuf, int start, int end){
mario_meh 0:02085be78662 236 char* data = inBuf;
mario_meh 0:02085be78662 237 int j = 0;
mario_meh 0:02085be78662 238 for (int i = start; i < end; i++){
mario_meh 0:02085be78662 239 kod[j] = data[i];
mario_meh 0:02085be78662 240 j++;
mario_meh 0:02085be78662 241 }
mario_meh 0:02085be78662 242 }
mario_meh 0:02085be78662 243
mario_meh 0:02085be78662 244 void ser_mcu(char* inBuf, int start, int end){
mario_meh 0:02085be78662 245 char* data = inBuf;
mario_meh 0:02085be78662 246 int j = 0;
mario_meh 0:02085be78662 247 for (int i = start; i < end; i++){
mario_meh 0:02085be78662 248 mcu[j] = data[i];
mario_meh 0:02085be78662 249 j++;
mario_meh 0:02085be78662 250 }
mario_meh 0:02085be78662 251 }
mario_meh 0:02085be78662 252
mario_meh 0:02085be78662 253 void ser_model(char* inBuf, int start, int end){
mario_meh 0:02085be78662 254 char* data = inBuf;
mario_meh 0:02085be78662 255 int j = 0;
mario_meh 0:02085be78662 256 for (int i = start; i < end; i++){
mario_meh 0:02085be78662 257 model[j] = data[i];
mario_meh 0:02085be78662 258 j++;
mario_meh 0:02085be78662 259 }
mario_meh 0:02085be78662 260 }
mario_meh 0:02085be78662 261
mario_meh 0:02085be78662 262 void ser_extra(char* inBuf, int start, int end){
mario_meh 0:02085be78662 263 char* data = inBuf;
mario_meh 0:02085be78662 264 int j = 0;
mario_meh 0:02085be78662 265 for (int i = start; i < end; i++){
mario_meh 0:02085be78662 266 extra[j] = data[i];
mario_meh 0:02085be78662 267 j++;
mario_meh 0:02085be78662 268 }
mario_meh 0:02085be78662 269 }