Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.98 KB | None | 0 0
  1. // BeVolt Telemetry 10-June-19
  2.  
  3. #include "mbed.h"
  4. #include "EthernetInterface.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include "canIds.h"
  8. #include "TCPServer.h"
  9. #include "TCPSocket.h"
  10.  
  11. #include <sstream>
  12.  
  13. #define TRIT_DC_BASE     0x220
  14. #define TRIT_DC_DRIVE    0x01
  15.  
  16. CAN can(PD_0,PD_1); // for the F429ZI  (Rx, Tx)
  17. Serial          pc(USBTX, USBRX);
  18. DigitalOut      led(LED1);
  19. CANMessage      msg;
  20. Thread canThread;
  21. Thread serverThread;
  22. int counter = 0;
  23. int CanID = 0x0000002;
  24. TCPServer srv;
  25. TCPSocket clt_sock;
  26. SocketAddress clt_addr;
  27. EthernetInterface eth;
  28.  
  29. float volt_buf[31];
  30. float temp_buf[62];
  31. float current;
  32. float bps_trip;
  33. float all_clear;
  34. float bps_off;
  35. float soc;
  36. float wdog;
  37. float can_error;
  38. float command_msg;
  39.  
  40. #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
  41.  
  42. #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
  43.  
  44. #define HTTP_MESSAGE_BODY "" \
  45. "<html>" "\r\n" \
  46. "<head>" "\r\n" \
  47. "<meta http-equiv=\"refresh\" content=\"2\">" "\r\n" \
  48. "</head>" "\r\n"    \
  49. " <body style=\"display:flex;text-align:left\">" "\r\n" \
  50. " <div style=\"margin:auto\">" "\r\n" \
  51. " <h1 style=\"color: #cc5500\">BeVolt Telemetry</h1>" "\r\n" \
  52. //" <p>Data: </p>" "\r\n" \
  53. " </div>" "\r\n" \
  54. //" </body>" "\r\n" \
  55. "</html>"
  56.  
  57. #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
  58.                       HTTP_HEADER_FIELDS "\r\n" \
  59.                       "\r\n" \
  60.                       HTTP_MESSAGE_BODY "\r\n"
  61.                      
  62. char begdiv[] = "<div style=\"margin:auto;text-align:center\">";
  63. char enddiv[] = "</div>";
  64.  
  65. char title[] = "<h1> BeVolt Telemetry </h1>";
  66.  
  67. float bytes2Float(uint8_t* bytes_array) {
  68.     union {
  69.         float f;
  70.         uint8_t b[4];
  71.     } u;
  72.     u.b[3] = bytes_array[0];
  73.     u.b[2] = bytes_array[1];
  74.     u.b[1] = bytes_array[2];
  75.     u.b[0] = bytes_array[3];
  76.     return u.f;
  77. }
  78.  
  79.  
  80.  
  81. float receiveCan(void) {
  82.     uint32_t received;
  83.    
  84.     if (can.read(msg)) {
  85.         received = *((uint32_t*) msg.data);
  86.     } else {
  87.         pc.printf("Could not receive message\n");
  88.         return -1;
  89.     }
  90.     pc.printf("-----------------------\r\n");
  91.     //float bps_trip;
  92. //float all_clear;
  93. //float bps_off;
  94. //float soc;
  95. //float wdog;
  96. //float can_error;
  97. //float command_msg;
  98.     if (msg.id == BPS_TRIP) {
  99.         bps_trip = msg.data[0];
  100.         // write("BPS TRIP: %d", received);
  101.         pc.printf("BPS Trip is %d\n", received);
  102.     } else if (msg.id == BPS_ALL_CLEAR) {
  103.         all_clear = msg.data[0];
  104.         pc.printf("BPS All Clear is %d\n", received);
  105.     } else if (msg.id == BPS_OFF) {
  106.         bps_off = msg.data[0];
  107.         pc.printf("BPS Off is %d\n", received);    
  108.     } else if (msg.id == WDOG_TRIGGERED) {
  109.         wdog = msg.data[0];
  110.         pc.printf("WDOG Triggered is %d\n", received);    
  111.     } else if (msg.id == CAN_ERROR) {
  112.         can_error = msg.data[0];
  113.         pc.printf("CAN Error is %d\n", received);    
  114.     } else if (msg.id == VOLTAGE_DATA) {
  115.         // voltage
  116.         float voltage = bytes2Float(&msg.data[1]);
  117.         pc.printf("Voltage is %.3fV from array index %d\n", voltage, msg.data[0]);
  118.         volt_buf[msg.data[0]] = voltage;
  119.        
  120.         return voltage;
  121.     } else if (msg.id == TEMPERATURE_DATA){
  122.         // temperature
  123.         float temp = bytes2Float(&msg.data[1]);
  124.         pc.printf("Temp is %.3f from array index %d\n", temp, msg.data[0]);  
  125.         temp_buf[msg.data[0]] = temp;
  126.        
  127.         return temp;
  128.     } else if(msg.id == SOC_DATA){
  129.         // state of charge
  130.         soc = bytes2Float(msg.data);
  131.         pc.printf("SoC is %.3f%%\n", soc);
  132.         return received;
  133.     } else if(msg.id == CURRENT_DATA){
  134.         // current
  135.         current = bytes2Float(msg.data);
  136.         pc.printf("Current is %.3fA\n", current);
  137.         return current;
  138.     }
  139.     pc.printf("-----------------------\r\n");
  140.     // Thread::wait(100);
  141.     return -1;
  142. }
  143.  
  144. void received(){
  145.     while(1) {
  146.         //pc.printf("In can receive thread\n");
  147.         receiveCan();
  148.         //Thread::yield();
  149.         ThisThread::sleep_for(100); // 100 works
  150.     }
  151. }
  152.  
  153. void serverStuff(){
  154.     char temp_str[30];
  155.     while(1){
  156.         printf("In server thread\n");
  157.            
  158.         srv.accept(&clt_sock, &clt_addr);
  159.         clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
  160.        
  161.         // Voltage
  162.         std::string html_volt="<h2>Voltage</h2><br><p class=\"voltage\">";
  163.         /*
  164.         for(int i=0; i<31; i++){
  165.             //if(i%3==0) {html_volt+="\n";}
  166.             html_volt = html_volt + "Idx ";
  167.             sprintf(temp_str, "%d", i);
  168.             html_volt += temp_str;
  169.             html_volt += ": ";
  170.             sprintf(temp_str, "%f", volt_buf[i]);
  171.             html_volt += temp_str;
  172.             html_volt += " | ";
  173.         }
  174.         */
  175.        
  176.         for(int i=0; i<16; i++){
  177.             //if(i%3==0) {html_volt+="\n";}
  178.             html_volt = html_volt + "A";
  179.             sprintf(temp_str, "%d", i);
  180.             html_volt += temp_str;
  181.             html_volt += ": ";
  182.             sprintf(temp_str, "%f", volt_buf[i]);
  183.             html_volt += temp_str;
  184.             html_volt += " | ";
  185.         }
  186.         for(int i=16; i<31; i++){
  187.             //if(i%3==0) {html_volt+="\n";}
  188.             html_volt = html_volt + "B";
  189.             sprintf(temp_str, "%d", i-16);
  190.             html_volt += temp_str;
  191.             html_volt += ": ";
  192.             sprintf(temp_str, "%f", volt_buf[i]);
  193.             html_volt += temp_str;
  194.             html_volt += " | ";
  195.         }
  196.        
  197.         html_volt += "</p>";
  198.         // convert the string into character array
  199.         int n = html_volt.length();
  200.         char html_char[n+1];
  201.         strcpy(html_char, html_volt.c_str());
  202.         pc.printf("%s\n", html_char);
  203.         clt_sock.send(html_char, n+1);
  204.        
  205.         // Temp
  206.         std::string html_temp="<h2>Temperature</h2><br><p class=\"temperature\">";
  207.         /*
  208.         for(int i=0; i<62; i++) {
  209.             html_temp += "Idx ";
  210.             sprintf(temp_str, "%d", i);
  211.             html_temp += temp_str;
  212.             html_temp += ": ";
  213.             sprintf(temp_str, "%f", temp_buf[i]);
  214.             html_temp += temp_str;
  215.             html_temp += " | ";
  216.         }
  217.         */
  218.         char sub = 'a';
  219.         for(int i=0; i<31; i++) {
  220.             html_temp += "A";
  221.             sprintf(temp_str, "%d", i);
  222.             html_temp += temp_str;
  223.             html_temp += sub;
  224.             if(sub == 'a') {sub = 'b';} else {sub = 'a';}
  225.             html_temp += ": ";
  226.             sprintf(temp_str, "%f", temp_buf[i]);
  227.             html_temp += temp_str;
  228.             html_temp += " | ";
  229.         }
  230.         sub = 'a';
  231.         for(int i=31; i<62; i++) {
  232.             html_temp += "B";
  233.             sprintf(temp_str, "%d", i-31);
  234.             html_temp += temp_str;
  235.             html_temp += sub;
  236.             if(sub == 'a') {sub = 'b';} else {sub = 'a';}
  237.             html_temp += ": ";
  238.             sprintf(temp_str, "%f", temp_buf[i]);
  239.             html_temp += temp_str;
  240.             html_temp += " | ";
  241.         }
  242.        
  243.         html_temp += "</p>";
  244.         // convert the string into character array
  245.         int temp_char_len = html_temp.length();
  246.         char temp_char[temp_char_len+1];
  247.         strcpy(temp_char, html_temp.c_str());
  248.         pc.printf("%s\n", temp_char);
  249.         clt_sock.send(temp_char, temp_char_len+1);
  250.        
  251.         // current
  252.         std::string html_current = "<h2>Current</h2><br><p class=\"current\">";
  253.         sprintf(temp_str, "%f", current);
  254.         html_current += temp_str;
  255.         html_current += "\n</p>";
  256.         // convert str to char array
  257.         temp_char_len = html_current.length();
  258.         char current_char[html_current.length()+1];
  259.         strcpy(current_char, html_current.c_str());
  260.         pc.printf("%s\n", current_char);
  261.         clt_sock.send(current_char, temp_char_len+1);
  262.        
  263.         // soc
  264.         std::string html_soc = "<h2>State of Charge</h2><br><p class=\"soc\">";
  265.         sprintf(temp_str, "%f", soc);
  266.         html_soc += temp_str;
  267.         html_soc += "\n</p>";
  268.         // convert str to char array
  269.         temp_char_len = html_soc.length();
  270.         char soc_char[html_soc.length()+1];
  271.         strcpy(soc_char, html_soc.c_str());
  272.         pc.printf("%s\n", soc_char);
  273.         clt_sock.send(soc_char, temp_char_len+1);
  274.        
  275.         // flags
  276.         std::string flags = "<h2>Flags</h2><br><p class=\"flags\">";
  277.         sprintf(temp_str, "BPS_TRIP: %f\n", bps_trip);
  278.         flags += temp_str;
  279.         sprintf(temp_str, "ALL_CLEAR: %f\n", all_clear);
  280.         flags += temp_str;
  281.         sprintf(temp_str, "BPS_OFF: %f\n", bps_off);
  282.         flags += temp_str;
  283.         sprintf(temp_str, "WDOG: %f\n", wdog);
  284.         flags += temp_str;
  285.         flags += "</p>";
  286.         // convert str to char array
  287.         temp_char_len = flags.length();
  288.         char flag_char[flags.length()+1];
  289.         strcpy(flag_char, flags.c_str());
  290.         pc.printf("%s\n", flag_char);
  291.         clt_sock.send(flag_char, temp_char_len+1);
  292.                
  293.        
  294. //      float current;
  295. //float bps_trip;
  296. //float all_clear;
  297. //float bps_off;
  298. //float soc;
  299. //float wdog;
  300. //float can_error;
  301. //float command_msg;
  302.        
  303.         ThisThread::sleep_for(5000);
  304.     }
  305. }
  306.  
  307. void acceptFunction(){
  308.     while(1){
  309.         pc.printf("Inside web accept\n");
  310.         srv.accept(&clt_sock, &clt_addr);           // Might need this in another thread
  311.         printf("accept %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port());
  312.         clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
  313.         // Thread::wait(1000);
  314.         // Thread::yield();
  315.         ThisThread::sleep_for(1000);
  316.     }
  317. }
  318.  
  319.  
  320.  
  321. int main()
  322. {    
  323.  
  324.     char sbuffer[] = "<br>Pedal Position Voltage (uV):  ";
  325.     char motcur[10];
  326.     int alive = 0;  
  327.     char rdata[8];
  328.     int id1 = TRIT_DC_BASE + TRIT_DC_DRIVE;
  329.     int MotCur_int;
  330.  
  331.     CANMessage msg;
  332.     pc.baud(9600);          // set serial speed
  333.     can.frequency(125000);//(150000); // max 1Mbps, usually 150000
  334.    
  335.     union {
  336.     char rcvdata[4];
  337.     float rxdata;
  338.     } urxdata;
  339.    
  340.     printf("BeVolt Telemetry\r\n");
  341.    
  342.     // Set up ethernet connection
  343.     eth.connect();
  344.     printf("The target IP address is '%s'\r\n", eth.get_ip_address());
  345.    
  346.     // Open the server on ethernet stack
  347.     srv.open(&eth);
  348.     // Bind the HTTP port (TCP 80) to the server
  349.     srv.bind(eth.get_ip_address(), 80); // 23 ->80?
  350.     // Can handle 5 simultaneous connections
  351.     srv.listen(3);
  352.    
  353.     Thread canThread(received);
  354.     (&canThread)->set_priority(osPriorityHigh);
  355.     wait(1);
  356.     Thread serverThread(serverStuff);
  357.     (&serverThread)->set_priority(osPriorityHigh);
  358. //  wait(2);
  359. //  Thread acceptThread(acceptFunction);
  360. //  (&acceptThread)->set_priority(osPriorityLow);
  361.    
  362.     while(1) {
  363.         //wait(1); 
  364.         ThisThread::sleep_for(10000);
  365.     }
  366.    
  367.     // Thread initialization
  368.    
  369.  
  370.     //wait(2.0);
  371.    
  372.     //while(1) {
  373.         //receiveCan();
  374. //        Thread::wait(1000);
  375.         //alive=0;
  376. //
  377. //        while (alive == 0) {
  378. //          if(can1.read(msg) && msg.id == id1 ) {
  379. //              printf("  ID      = 0x%.3x\r\n", msg.id);
  380. //              printf("  Type    = %d\r\n", msg.type);
  381. //              printf("  Format  = %d\r\n", msg.format);
  382. //              printf("  Length  = %d\r\n", msg.len);
  383. //              printf("  Data    =");
  384. //              for(int i = 0; i < msg.len; i++) {
  385. //                      printf(" 0x%.2X", msg.data[i]);
  386. //                      rdata[i] = msg.data[i];
  387. //              }
  388. //              printf("\r\n");
  389. //              urxdata.rcvdata[3] = rdata[7];
  390. //              urxdata.rcvdata[2] = rdata[6];
  391. //              urxdata.rcvdata[1] = rdata[5];
  392. //              urxdata.rcvdata[0] = rdata[4];
  393. //              printf(" Motor Current (pedal voltage) = %f",urxdata.rxdata);
  394. //              printf("\r\n");
  395. //              MotCur_int = urxdata.rxdata*1000000;
  396. //              alive = 1;
  397. //          }
  398. //      }
  399.    
  400.     //sprintf(motcur,"%d",MotCur_int);
  401. //    printf(" Pedal Voltage (uV): ");  
  402. //    for(int i = 0; i < 10; i++) {
  403. //        printf("%c", motcur[i]);
  404. //    }
  405. //    printf("\r\n");
  406.     //***************
  407.    
  408.  
  409. //    clt_sock.close();
  410.     // **************
  411.     // clt_sock.send(sbuffer, strlen(sbuffer));
  412.     // clt_sock.send(motcur, 10);
  413.        
  414.         //uint8_t volt = (rand() % (40 - 27 + 1)) + 27;
  415. //      uint8_t curr = (rand() % (60 - 50 + 1)) + 50;
  416. //      uint8_t temp = (rand() % (73 - 0 + 1)) + 0;
  417. //
  418. //      char voltarr[4];   
  419. //      char currarr[4];
  420. //      char temparr[4];
  421. //
  422. //      sprintf(voltarr, "%d", volt);
  423. //      sprintf(currarr, "%d", curr);
  424. //      sprintf(temparr, "%d", temp);
  425. //     
  426. //     
  427. //      clt_sock.send(begdiv, strlen(begdiv));
  428. //      clt_sock.send(title, strlen(title));
  429. //     
  430. //      clt_sock.send(voltbuff, 14);
  431. //      clt_sock.send(voltarr, 4);
  432. //      clt_sock.send(tempbuff, 10);
  433. //      clt_sock.send(temparr, 4);
  434. //      clt_sock.send(currbuff, 13);
  435. //      clt_sock.send(currarr, 4);
  436. //      clt_sock.send(enddiv, strlen(enddiv));
  437.  
  438.        
  439.    
  440.     //}
  441. //    clt_sock.close();
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement