Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BeVolt Telemetry 10-June-19
- #include "mbed.h"
- #include "EthernetInterface.h"
- #include <stdint.h>
- #include <string.h>
- #include "canIds.h"
- #include "TCPServer.h"
- #include "TCPSocket.h"
- #include <sstream>
- #define TRIT_DC_BASE 0x220
- #define TRIT_DC_DRIVE 0x01
- CAN can(PD_0,PD_1); // for the F429ZI (Rx, Tx)
- Serial pc(USBTX, USBRX);
- DigitalOut led(LED1);
- CANMessage msg;
- Thread canThread;
- Thread serverThread;
- int counter = 0;
- int CanID = 0x0000002;
- TCPServer srv;
- TCPSocket clt_sock;
- SocketAddress clt_addr;
- EthernetInterface eth;
- float volt_buf[31];
- float temp_buf[62];
- float current;
- float bps_trip;
- float all_clear;
- float bps_off;
- float soc;
- float wdog;
- float can_error;
- float command_msg;
- #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
- #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
- #define HTTP_MESSAGE_BODY "" \
- "<html>" "\r\n" \
- "<head>" "\r\n" \
- "<meta http-equiv=\"refresh\" content=\"2\">" "\r\n" \
- "</head>" "\r\n" \
- " <body style=\"display:flex;text-align:left\">" "\r\n" \
- " <div style=\"margin:auto\">" "\r\n" \
- " <h1 style=\"color: #cc5500\">BeVolt Telemetry</h1>" "\r\n" \
- //" <p>Data: </p>" "\r\n" \
- " </div>" "\r\n" \
- //" </body>" "\r\n" \
- "</html>"
- #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
- HTTP_HEADER_FIELDS "\r\n" \
- "\r\n" \
- HTTP_MESSAGE_BODY "\r\n"
- char begdiv[] = "<div style=\"margin:auto;text-align:center\">";
- char enddiv[] = "</div>";
- char title[] = "<h1> BeVolt Telemetry </h1>";
- float bytes2Float(uint8_t* bytes_array) {
- union {
- float f;
- uint8_t b[4];
- } u;
- u.b[3] = bytes_array[0];
- u.b[2] = bytes_array[1];
- u.b[1] = bytes_array[2];
- u.b[0] = bytes_array[3];
- return u.f;
- }
- float receiveCan(void) {
- uint32_t received;
- if (can.read(msg)) {
- received = *((uint32_t*) msg.data);
- } else {
- pc.printf("Could not receive message\n");
- return -1;
- }
- pc.printf("-----------------------\r\n");
- //float bps_trip;
- //float all_clear;
- //float bps_off;
- //float soc;
- //float wdog;
- //float can_error;
- //float command_msg;
- if (msg.id == BPS_TRIP) {
- bps_trip = msg.data[0];
- // write("BPS TRIP: %d", received);
- pc.printf("BPS Trip is %d\n", received);
- } else if (msg.id == BPS_ALL_CLEAR) {
- all_clear = msg.data[0];
- pc.printf("BPS All Clear is %d\n", received);
- } else if (msg.id == BPS_OFF) {
- bps_off = msg.data[0];
- pc.printf("BPS Off is %d\n", received);
- } else if (msg.id == WDOG_TRIGGERED) {
- wdog = msg.data[0];
- pc.printf("WDOG Triggered is %d\n", received);
- } else if (msg.id == CAN_ERROR) {
- can_error = msg.data[0];
- pc.printf("CAN Error is %d\n", received);
- } else if (msg.id == VOLTAGE_DATA) {
- // voltage
- float voltage = bytes2Float(&msg.data[1]);
- pc.printf("Voltage is %.3fV from array index %d\n", voltage, msg.data[0]);
- volt_buf[msg.data[0]] = voltage;
- return voltage;
- } else if (msg.id == TEMPERATURE_DATA){
- // temperature
- float temp = bytes2Float(&msg.data[1]);
- pc.printf("Temp is %.3f from array index %d\n", temp, msg.data[0]);
- temp_buf[msg.data[0]] = temp;
- return temp;
- } else if(msg.id == SOC_DATA){
- // state of charge
- soc = bytes2Float(msg.data);
- pc.printf("SoC is %.3f%%\n", soc);
- return received;
- } else if(msg.id == CURRENT_DATA){
- // current
- current = bytes2Float(msg.data);
- pc.printf("Current is %.3fA\n", current);
- return current;
- }
- pc.printf("-----------------------\r\n");
- // Thread::wait(100);
- return -1;
- }
- void received(){
- while(1) {
- //pc.printf("In can receive thread\n");
- receiveCan();
- //Thread::yield();
- ThisThread::sleep_for(100); // 100 works
- }
- }
- void serverStuff(){
- char temp_str[30];
- while(1){
- printf("In server thread\n");
- srv.accept(&clt_sock, &clt_addr);
- clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
- // Voltage
- std::string html_volt="<h2>Voltage</h2><br><p class=\"voltage\">";
- /*
- for(int i=0; i<31; i++){
- //if(i%3==0) {html_volt+="\n";}
- html_volt = html_volt + "Idx ";
- sprintf(temp_str, "%d", i);
- html_volt += temp_str;
- html_volt += ": ";
- sprintf(temp_str, "%f", volt_buf[i]);
- html_volt += temp_str;
- html_volt += " | ";
- }
- */
- for(int i=0; i<16; i++){
- //if(i%3==0) {html_volt+="\n";}
- html_volt = html_volt + "A";
- sprintf(temp_str, "%d", i);
- html_volt += temp_str;
- html_volt += ": ";
- sprintf(temp_str, "%f", volt_buf[i]);
- html_volt += temp_str;
- html_volt += " | ";
- }
- for(int i=16; i<31; i++){
- //if(i%3==0) {html_volt+="\n";}
- html_volt = html_volt + "B";
- sprintf(temp_str, "%d", i-16);
- html_volt += temp_str;
- html_volt += ": ";
- sprintf(temp_str, "%f", volt_buf[i]);
- html_volt += temp_str;
- html_volt += " | ";
- }
- html_volt += "</p>";
- // convert the string into character array
- int n = html_volt.length();
- char html_char[n+1];
- strcpy(html_char, html_volt.c_str());
- pc.printf("%s\n", html_char);
- clt_sock.send(html_char, n+1);
- // Temp
- std::string html_temp="<h2>Temperature</h2><br><p class=\"temperature\">";
- /*
- for(int i=0; i<62; i++) {
- html_temp += "Idx ";
- sprintf(temp_str, "%d", i);
- html_temp += temp_str;
- html_temp += ": ";
- sprintf(temp_str, "%f", temp_buf[i]);
- html_temp += temp_str;
- html_temp += " | ";
- }
- */
- char sub = 'a';
- for(int i=0; i<31; i++) {
- html_temp += "A";
- sprintf(temp_str, "%d", i);
- html_temp += temp_str;
- html_temp += sub;
- if(sub == 'a') {sub = 'b';} else {sub = 'a';}
- html_temp += ": ";
- sprintf(temp_str, "%f", temp_buf[i]);
- html_temp += temp_str;
- html_temp += " | ";
- }
- sub = 'a';
- for(int i=31; i<62; i++) {
- html_temp += "B";
- sprintf(temp_str, "%d", i-31);
- html_temp += temp_str;
- html_temp += sub;
- if(sub == 'a') {sub = 'b';} else {sub = 'a';}
- html_temp += ": ";
- sprintf(temp_str, "%f", temp_buf[i]);
- html_temp += temp_str;
- html_temp += " | ";
- }
- html_temp += "</p>";
- // convert the string into character array
- int temp_char_len = html_temp.length();
- char temp_char[temp_char_len+1];
- strcpy(temp_char, html_temp.c_str());
- pc.printf("%s\n", temp_char);
- clt_sock.send(temp_char, temp_char_len+1);
- // current
- std::string html_current = "<h2>Current</h2><br><p class=\"current\">";
- sprintf(temp_str, "%f", current);
- html_current += temp_str;
- html_current += "\n</p>";
- // convert str to char array
- temp_char_len = html_current.length();
- char current_char[html_current.length()+1];
- strcpy(current_char, html_current.c_str());
- pc.printf("%s\n", current_char);
- clt_sock.send(current_char, temp_char_len+1);
- // soc
- std::string html_soc = "<h2>State of Charge</h2><br><p class=\"soc\">";
- sprintf(temp_str, "%f", soc);
- html_soc += temp_str;
- html_soc += "\n</p>";
- // convert str to char array
- temp_char_len = html_soc.length();
- char soc_char[html_soc.length()+1];
- strcpy(soc_char, html_soc.c_str());
- pc.printf("%s\n", soc_char);
- clt_sock.send(soc_char, temp_char_len+1);
- // flags
- std::string flags = "<h2>Flags</h2><br><p class=\"flags\">";
- sprintf(temp_str, "BPS_TRIP: %f\n", bps_trip);
- flags += temp_str;
- sprintf(temp_str, "ALL_CLEAR: %f\n", all_clear);
- flags += temp_str;
- sprintf(temp_str, "BPS_OFF: %f\n", bps_off);
- flags += temp_str;
- sprintf(temp_str, "WDOG: %f\n", wdog);
- flags += temp_str;
- flags += "</p>";
- // convert str to char array
- temp_char_len = flags.length();
- char flag_char[flags.length()+1];
- strcpy(flag_char, flags.c_str());
- pc.printf("%s\n", flag_char);
- clt_sock.send(flag_char, temp_char_len+1);
- // float current;
- //float bps_trip;
- //float all_clear;
- //float bps_off;
- //float soc;
- //float wdog;
- //float can_error;
- //float command_msg;
- ThisThread::sleep_for(5000);
- }
- }
- void acceptFunction(){
- while(1){
- pc.printf("Inside web accept\n");
- srv.accept(&clt_sock, &clt_addr); // Might need this in another thread
- printf("accept %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port());
- clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
- // Thread::wait(1000);
- // Thread::yield();
- ThisThread::sleep_for(1000);
- }
- }
- int main()
- {
- char sbuffer[] = "<br>Pedal Position Voltage (uV): ";
- char motcur[10];
- int alive = 0;
- char rdata[8];
- int id1 = TRIT_DC_BASE + TRIT_DC_DRIVE;
- int MotCur_int;
- CANMessage msg;
- pc.baud(9600); // set serial speed
- can.frequency(125000);//(150000); // max 1Mbps, usually 150000
- union {
- char rcvdata[4];
- float rxdata;
- } urxdata;
- printf("BeVolt Telemetry\r\n");
- // Set up ethernet connection
- eth.connect();
- printf("The target IP address is '%s'\r\n", eth.get_ip_address());
- // Open the server on ethernet stack
- srv.open(ð);
- // Bind the HTTP port (TCP 80) to the server
- srv.bind(eth.get_ip_address(), 80); // 23 ->80?
- // Can handle 5 simultaneous connections
- srv.listen(3);
- Thread canThread(received);
- (&canThread)->set_priority(osPriorityHigh);
- wait(1);
- Thread serverThread(serverStuff);
- (&serverThread)->set_priority(osPriorityHigh);
- // wait(2);
- // Thread acceptThread(acceptFunction);
- // (&acceptThread)->set_priority(osPriorityLow);
- while(1) {
- //wait(1);
- ThisThread::sleep_for(10000);
- }
- // Thread initialization
- //wait(2.0);
- //while(1) {
- //receiveCan();
- // Thread::wait(1000);
- //alive=0;
- //
- // while (alive == 0) {
- // if(can1.read(msg) && msg.id == id1 ) {
- // printf(" ID = 0x%.3x\r\n", msg.id);
- // printf(" Type = %d\r\n", msg.type);
- // printf(" Format = %d\r\n", msg.format);
- // printf(" Length = %d\r\n", msg.len);
- // printf(" Data =");
- // for(int i = 0; i < msg.len; i++) {
- // printf(" 0x%.2X", msg.data[i]);
- // rdata[i] = msg.data[i];
- // }
- // printf("\r\n");
- // urxdata.rcvdata[3] = rdata[7];
- // urxdata.rcvdata[2] = rdata[6];
- // urxdata.rcvdata[1] = rdata[5];
- // urxdata.rcvdata[0] = rdata[4];
- // printf(" Motor Current (pedal voltage) = %f",urxdata.rxdata);
- // printf("\r\n");
- // MotCur_int = urxdata.rxdata*1000000;
- // alive = 1;
- // }
- // }
- //sprintf(motcur,"%d",MotCur_int);
- // printf(" Pedal Voltage (uV): ");
- // for(int i = 0; i < 10; i++) {
- // printf("%c", motcur[i]);
- // }
- // printf("\r\n");
- //***************
- // clt_sock.close();
- // **************
- // clt_sock.send(sbuffer, strlen(sbuffer));
- // clt_sock.send(motcur, 10);
- //uint8_t volt = (rand() % (40 - 27 + 1)) + 27;
- // uint8_t curr = (rand() % (60 - 50 + 1)) + 50;
- // uint8_t temp = (rand() % (73 - 0 + 1)) + 0;
- //
- // char voltarr[4];
- // char currarr[4];
- // char temparr[4];
- //
- // sprintf(voltarr, "%d", volt);
- // sprintf(currarr, "%d", curr);
- // sprintf(temparr, "%d", temp);
- //
- //
- // clt_sock.send(begdiv, strlen(begdiv));
- // clt_sock.send(title, strlen(title));
- //
- // clt_sock.send(voltbuff, 14);
- // clt_sock.send(voltarr, 4);
- // clt_sock.send(tempbuff, 10);
- // clt_sock.send(temparr, 4);
- // clt_sock.send(currbuff, 13);
- // clt_sock.send(currarr, 4);
- // clt_sock.send(enddiv, strlen(enddiv));
- //}
- // clt_sock.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement