2021-03-25 18:25:31 +00:00
import processing.serial.*;
2021-03-21 14:31:05 +00:00
2021-03-21 16:30:07 +00:00
int vis_textsize=12; //copy from Visualization class
2021-03-25 18:59:12 +00:00
//String logfile_name="LOG00008_rumfahren_neu.TXT";
String logfile_name="LOG00180_random.TXT";
2021-03-25 20:05:13 +00:00
boolean useSerial=true; //false=read from csv log, true=read from serial port
String serial_port="COM3";
2021-03-25 18:25:31 +00:00
Serial serial;
String serialString=""; //last read string
int serial_endchar=10; //10=ASCII Linefeed
2021-03-21 16:30:07 +00:00
Visualization vis_cmd_FrontL;
Visualization vis_cmd_FrontR;
Visualization vis_cmd_RearL;
Visualization vis_cmd_RearR;
Visualization vis_current_FrontL;
Visualization vis_current_FrontR;
Visualization vis_current_RearL;
Visualization vis_current_RearR;
Visualization vis_speed_FrontL;
Visualization vis_speed_FrontR;
Visualization vis_speed_RearL;
Visualization vis_speed_RearR;
2021-03-21 14:31:05 +00:00
2021-03-21 17:21:05 +00:00
Visualization vis_throttle;
Visualization vis_brake;
Visualization vis_currentAll;
//vis_c means calculated value, not raw value from log
Visualization vis_c_speed_mean;
2021-03-21 17:58:51 +00:00
Visualization vis_graph_currentAll;
Visualization vis_graph_speed_mean;
2021-03-21 14:31:05 +00:00
2021-03-21 15:12:51 +00:00
long lastTimeData=0; //last time data received
2021-03-21 14:31:05 +00:00
2021-03-21 15:12:51 +00:00
Table logdata;
int nextID=0; //next row number to be displayed
long nextTime=0; //time of nextID row
2021-03-26 16:02:23 +00:00
int dataErrorCount=0;
2021-03-21 15:12:51 +00:00
//Data from log
2021-03-21 16:30:07 +00:00
int cmd_FrontL;
int cmd_FrontR;
int cmd_RearL;
int cmd_RearR;
float current_FrontL;
float current_FrontR;
float current_RearL;
float current_RearR;
int speed_FrontL;
int speed_FrontR;
int speed_RearL;
int speed_RearR;
float temp_Front;
float temp_Rear;
float vbat_Front;
float vbat_Rear;
float currentAll;
int throttle;
int brake;
2021-03-21 15:12:51 +00:00
2021-03-21 17:58:51 +00:00
color bg=color(0);
2021-03-21 14:31:05 +00:00
void setup() {
2021-03-21 15:12:51 +00:00
size(1920, 1080);
2021-03-21 14:31:05 +00:00
frameRate(100);
2021-03-25 18:25:31 +00:00
if (useSerial) {
printArray(Serial.list());
// Open the port you are using at the rate you want:
2021-03-25 20:05:13 +00:00
serial = new Serial(this, serial_port, 115200);
2021-03-25 18:25:31 +00:00
serial.clear();
// Throw out the first reading, in case we started reading
// in the middle of a string from the sender.
2021-03-25 20:05:13 +00:00
println("readUntil");
2021-03-25 18:25:31 +00:00
serialString = serial.readStringUntil(serial_endchar);
2021-03-25 20:05:13 +00:00
println("read:"+serialString);
2021-03-25 18:25:31 +00:00
serialString = null;
}else{
2021-03-25 18:59:12 +00:00
logdata = loadTable(logfile_name, "header, csv");
2021-03-25 18:25:31 +00:00
println("loaded "+logdata.getRowCount()+" lines. Times: "+logdata.getRow(0).getFloat("time")+"s to "+logdata.getRow(logdata.getRowCount()-1).getFloat("time")+"s");
}
2021-03-21 16:30:07 +00:00
PVector pos_vis_cmd = new PVector(100,150);
PVector size_vis_cmd = new PVector(10,100);
PVector dist_vis_cmd = new PVector(80,150);
2021-03-21 17:21:05 +00:00
colorMode(RGB, 255, 255, 255);
2021-03-21 16:30:07 +00:00
//cmd
color c_cmd=color(255,50,0);
vis_cmd_FrontL = new BarV_cmd((int)pos_vis_cmd.x,(int)pos_vis_cmd.y,(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1000,1000);
vis_cmd_FrontR = new BarV_cmd((int)(pos_vis_cmd.x+dist_vis_cmd.x),(int)pos_vis_cmd.y,(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1000,1000);
2021-03-21 17:58:51 +00:00
vis_cmd_FrontR.setTitle("cmd");
2021-03-21 16:30:07 +00:00
vis_cmd_RearL = new BarV_cmd((int)pos_vis_cmd.x,(int)(pos_vis_cmd.y+dist_vis_cmd.y),(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1000,1000);
vis_cmd_RearR = new BarV_cmd((int)(pos_vis_cmd.x+dist_vis_cmd.x),(int)(pos_vis_cmd.y+dist_vis_cmd.y),(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1000,1000);
vis_cmd_FrontL.setcmain(c_cmd);
vis_cmd_FrontR.setcmain(c_cmd);
vis_cmd_RearL.setcmain(c_cmd);
vis_cmd_RearR.setcmain(c_cmd);
// Speed
color c_speed=color(50,50,255);
2021-03-21 17:58:51 +00:00
vis_speed_FrontL = new BarV_cmd((int)(pos_vis_cmd.x-size_vis_cmd.x*2),(int)pos_vis_cmd.y+vis_textsize,(int)size_vis_cmd.x,(int)size_vis_cmd.y,-100,600);
2021-03-21 16:30:07 +00:00
vis_speed_FrontR = new BarV_cmd((int)(pos_vis_cmd.x+dist_vis_cmd.x+size_vis_cmd.x*2),(int)pos_vis_cmd.y+vis_textsize,(int)size_vis_cmd.x,(int)size_vis_cmd.y,-100,600);
vis_speed_FrontR.setTitle("speed");
vis_speed_RearL = new BarV_cmd((int)(pos_vis_cmd.x-size_vis_cmd.x*2),(int)(pos_vis_cmd.y+vis_textsize+dist_vis_cmd.y),(int)size_vis_cmd.x,(int)size_vis_cmd.y,-100,600);
2021-03-21 17:58:51 +00:00
vis_speed_RearL.setValueUnit("rpm");
2021-03-21 16:30:07 +00:00
vis_speed_RearR = new BarV_cmd((int)(pos_vis_cmd.x+dist_vis_cmd.x+size_vis_cmd.x*2),(int)(pos_vis_cmd.y+vis_textsize+dist_vis_cmd.y),(int)size_vis_cmd.x,(int)size_vis_cmd.y,-100,600);
vis_speed_FrontL.setcmain(c_speed);
vis_speed_FrontR.setcmain(c_speed);
vis_speed_RearL.setcmain(c_speed);
vis_speed_RearR.setcmain(c_speed);
// Current
color c_current=color(255,200,50);
vis_current_FrontL = new BarV_cmd((int)(pos_vis_cmd.x-size_vis_cmd.x*2*2),(int)pos_vis_cmd.y+vis_textsize*2,(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1,10);
2021-03-21 17:21:05 +00:00
2021-03-21 16:30:07 +00:00
vis_current_FrontR = new BarV_cmd((int)(pos_vis_cmd.x+dist_vis_cmd.x+size_vis_cmd.x*2*2),(int)pos_vis_cmd.y+vis_textsize*2,(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1,10);
vis_current_FrontR.setTitle("current");
vis_current_RearL = new BarV_cmd((int)(pos_vis_cmd.x-size_vis_cmd.x*2*2),(int)(pos_vis_cmd.y+vis_textsize*2+dist_vis_cmd.y),(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1,10);
2021-03-21 17:58:51 +00:00
vis_current_RearL.setValueUnit("A");
2021-03-21 16:30:07 +00:00
vis_current_RearR = new BarV_cmd((int)(pos_vis_cmd.x+dist_vis_cmd.x+size_vis_cmd.x*2*2),(int)(pos_vis_cmd.y+vis_textsize*2+dist_vis_cmd.y),(int)size_vis_cmd.x,(int)size_vis_cmd.y,-1,10);
vis_current_FrontL.setcmain(c_current);
vis_current_FrontR.setcmain(c_current);
vis_current_RearL.setcmain(c_current);
vis_current_RearR.setcmain(c_current);
2021-03-21 14:31:05 +00:00
2021-03-21 17:21:05 +00:00
//Inputs
PVector pos_vis_inputs = new PVector(width/2,height-50); //will be center for x
PVector size_vis_inputs = new PVector(200,40);
color c_throttle=color(255,150,50);
vis_throttle = new BarH_cmd((int)pos_vis_inputs.x,(int)pos_vis_inputs.y,(int)size_vis_inputs.x,(int)size_vis_inputs.y,0,1000);
vis_throttle.setcmain(c_throttle);
vis_throttle.setTitle("Throttle");
color c_brake=color(200,200,50);
vis_brake = new BarH_cmd((int)(pos_vis_inputs.x-size_vis_inputs.x),(int)pos_vis_inputs.y,(int)size_vis_inputs.x,(int)size_vis_inputs.y,-1000,0);
vis_brake.setcmain(c_brake);
vis_brake.setTitle("Brake");
//Speed mean
2021-03-21 17:58:51 +00:00
vis_c_speed_mean = new Tacho(width/2-150,height-150,100,-100,600);
2021-03-21 17:21:05 +00:00
vis_c_speed_mean.setTitle("Speed");
2021-03-21 17:58:51 +00:00
vis_c_speed_mean.setValueUnit("rpm");
2021-03-21 17:21:05 +00:00
//Current
2021-03-21 17:58:51 +00:00
vis_currentAll = new Tacho(width/2+150,height-150,100,-1,10);
2021-03-21 17:21:05 +00:00
vis_currentAll.setTitle("minCurrent");
2021-03-21 17:58:51 +00:00
vis_currentAll.setValueUnit("A");
//Graph
vis_graph_speed_mean = new GraphRoll_minimal(700, 250, 400,200,-100,600,1);
vis_graph_speed_mean.setcborder(c_speed);
vis_graph_speed_mean.setcmain(c_speed);
vis_graph_speed_mean.setValueUnit("rpm");
vis_graph_currentAll = new GraphRoll_minimal(700, 250, 400,200,-1,10,1);
vis_graph_currentAll.setcborder(c_current);
vis_graph_currentAll.setcmain(c_current);
vis_graph_currentAll.setValueUnit("A");
2021-03-21 17:21:05 +00:00
2021-03-21 14:31:05 +00:00
}
void draw() {
2021-03-21 16:30:07 +00:00
long loopmillis=millis()+25000;
2021-03-25 18:25:31 +00:00
if (useSerial) {
while (serial.available() > 0) {
serialString = serial.readStringUntil(serial_endchar);
2021-03-26 16:02:23 +00:00
//println("read:"+serialString);
2021-03-25 18:25:31 +00:00
if (serialString != null) {
println(serialString);
String[] list = split(serialString, ',');
2021-03-26 16:02:23 +00:00
if (list.length==20) {
lastTimeData=nextTime;
nextTime=int(parseFloat(list[0])*1000);
cmd_FrontL=parseInt(list[1]);
cmd_FrontR=parseInt(list[2]);
cmd_RearL=parseInt(list[3]);
cmd_RearR=parseInt(list[4]);
current_FrontL=parseFloat(list[5]);
current_FrontR=parseFloat(list[6]);
current_RearL=parseFloat(list[7]);
current_RearR=parseFloat(list[8]);
speed_FrontL=parseInt(list[9]);
speed_FrontR=parseInt(list[10]);
speed_RearL=parseInt(list[11]);
speed_RearR=parseInt(list[12]);
temp_Front=parseFloat(list[13]);
temp_Rear=parseFloat(list[14]);
vbat_Front=parseFloat(list[15]);
vbat_Rear=parseFloat(list[16]);
currentAll=parseFloat(list[17]);
throttle=parseInt(list[18]);
brake=parseInt(list[19]);
}else{ //data missing or too much
dataErrorCount++;
}
2021-03-25 18:25:31 +00:00
}
}
}else{
if (loopmillis>=nextTime){ //New Data
TableRow row = logdata.getRow(nextID);
lastTimeData=nextTime;
nextTime=(long)(row.getFloat("time")*1000); //get time and convert from seconds to ms
cmd_FrontL=row.getInt("cmd_FrontL");
cmd_FrontR=row.getInt("cmd_FrontR");
cmd_RearL=row.getInt("cmd_RearL");
cmd_RearR=row.getInt("cmd_RearR");
current_FrontL=row.getFloat("current_FrontL");
current_FrontR=row.getFloat("current_FrontR");
current_RearL=row.getFloat("current_RearL");
current_RearR=row.getFloat("current_RearR");
speed_FrontL=row.getInt("speed_FrontL");
speed_FrontR=row.getInt("speed_FrontR");
speed_RearL=row.getInt("speed_RearL");
speed_RearR=row.getInt("speed_RearR");
temp_Front=row.getFloat("temp_Front");
temp_Rear=row.getFloat("temp_Rear");
vbat_Front=row.getFloat("vbat_Front");
vbat_Rear=row.getFloat("vbat_Rear");
currentAll=row.getFloat("currentAll");
throttle=row.getInt("throttle");
brake=row.getInt("brake");
2021-03-25 18:59:12 +00:00
if (loopmillis-nextTime>1000 && nextTime>lastTimeData) {//too much behind
2021-03-25 18:25:31 +00:00
long _timestep=nextTime-lastTimeData; //approximated time step
nextID+=(loopmillis-nextTime)/_timestep* 0.9; //fast forward estimated time steps
}
nextID++;
nextID=nextID%logdata.getRowCount();
2021-03-21 16:30:07 +00:00
}
2021-03-21 15:12:51 +00:00
}
2021-03-21 14:31:05 +00:00
2021-03-21 17:58:51 +00:00
background(bg);
2021-03-21 15:12:51 +00:00
2021-03-21 16:30:07 +00:00
vis_cmd_FrontL.setValue(cmd_FrontL); vis_cmd_FrontL.drawVis();
vis_cmd_FrontR.setValue(cmd_FrontR); vis_cmd_FrontR.drawVis();
vis_cmd_RearL.setValue(cmd_RearL); vis_cmd_RearL.drawVis();
vis_cmd_RearR.setValue(cmd_RearR); vis_cmd_RearR.drawVis();
2021-03-21 14:31:05 +00:00
2021-03-21 16:30:07 +00:00
vis_speed_FrontL.setValue(speed_FrontL); vis_speed_FrontL.drawVis();
vis_speed_FrontR.setValue(speed_FrontR); vis_speed_FrontR.drawVis();
vis_speed_RearL.setValue(speed_RearL); vis_speed_RearL.drawVis();
vis_speed_RearR.setValue(speed_RearR); vis_speed_RearR.drawVis();
2021-03-26 16:02:23 +00:00
vis_current_FrontL.setValue(current_FrontL); vis_current_FrontL.drawVis();
vis_current_FrontR.setValue(current_FrontR); vis_current_FrontR.drawVis();
vis_current_RearL.setValue(current_RearL); vis_current_RearL.drawVis();
vis_current_RearR.setValue(current_RearR); vis_current_RearR.drawVis();
2021-03-21 17:21:05 +00:00
vis_throttle.setValue(throttle); vis_throttle.drawVis();
vis_brake.setValue(-brake); vis_brake.drawVis();
2021-03-21 17:58:51 +00:00
int speed_mean=int((speed_FrontL+speed_FrontR+speed_RearL+speed_RearR)/4.0);
2021-03-21 17:21:05 +00:00
vis_c_speed_mean.setValue(speed_mean); vis_c_speed_mean.drawVis();
2021-03-26 16:02:23 +00:00
vis_currentAll.setValue(currentAll); vis_currentAll.drawVis();
2021-03-21 16:30:07 +00:00
2021-03-21 17:58:51 +00:00
vis_graph_speed_mean.setValue(speed_mean); vis_graph_speed_mean.drawVis();
2021-03-26 16:02:23 +00:00
vis_graph_currentAll.setValue(currentAll); vis_graph_currentAll.drawVis();
2021-03-21 17:58:51 +00:00
2021-03-21 16:30:07 +00:00
2021-03-21 17:21:05 +00:00
//Temperature
PVector pos_temperature = new PVector(220,12);
colorMode(HSB, 360, 100, 100);
fill(color(map(temp_Front,16,50,180,360),50,100));
text("temp_Front="+(temp_Front)+"°C", pos_temperature.x,pos_temperature.y);
fill(color(map(temp_Rear,16,50,180,360),50,100));
text("temp_Rear="+(temp_Rear)+"°C", pos_temperature.x,pos_temperature.y+12);
//Voltage
PVector pos_voltage = new PVector(pos_temperature.x+150,12);
colorMode(HSB, 360, 100, 100);
fill(color(map(vbat_Front,12*3,12*4.2,0,120),50,100));
2021-03-26 16:02:23 +00:00
text("vbat_Front="+(vbat_Front)+"V", pos_voltage.x,pos_voltage.y);
2021-03-21 17:21:05 +00:00
fill(color(map(vbat_Rear,12*3,12*4.2,0,120),50,100));
2021-03-26 16:02:23 +00:00
text("vbat_Rear="+(vbat_Rear)+"V", pos_voltage.x,pos_voltage.y+12);
2021-03-21 16:30:07 +00:00
2021-03-21 17:21:05 +00:00
colorMode(RGB, 255, 255, 255);
2021-03-21 16:30:07 +00:00
fill(color(200,200,200));
textAlign(LEFT);
2021-03-26 16:02:23 +00:00
textSize(vis_textsize);
2021-03-21 16:30:07 +00:00
text("d="+(nextTime-lastTimeData)+"ms", 5+70,12);
2021-03-25 18:25:31 +00:00
if (!useSerial && loopmillis-lastTimeData>(nextTime-lastTimeData)*10) { //deviation too high when reading from file
2021-03-21 17:21:05 +00:00
text("ff="+(loopmillis-lastTimeData)+"ms", 5+70+70,12); //show warning
2021-03-21 16:30:07 +00:00
}
2021-03-21 14:31:05 +00:00
2021-03-21 16:30:07 +00:00
text("t="+(loopmillis/1000.0)+"s", 5,12);
2021-03-26 16:02:23 +00:00
text(""+(dataErrorCount)+" errors", 5,12+vis_textsize);
2021-03-21 14:31:05 +00:00
}