start implementing live serial display

This commit is contained in:
interfisch 2021-03-25 19:25:31 +01:00
parent 817ad11569
commit 6b335e1b8b
2 changed files with 104 additions and 32 deletions

View File

@ -1,6 +1,12 @@
import processing.serial.*;
int vis_textsize=12; //copy from Visualization class int vis_textsize=12; //copy from Visualization class
boolean useSerial=false; //false=read from csv log, true=read from serial port
Serial serial;
String serialString=""; //last read string
int serial_endchar=10; //10=ASCII Linefeed
Visualization vis_cmd_FrontL; Visualization vis_cmd_FrontL;
Visualization vis_cmd_FrontR; Visualization vis_cmd_FrontR;
Visualization vis_cmd_RearL; Visualization vis_cmd_RearL;
@ -62,9 +68,20 @@ void setup() {
size(1920, 1080); size(1920, 1080);
frameRate(100); frameRate(100);
logdata = loadTable("LOG00008_rumfahren_neu.TXT", "header, csv"); if (useSerial) {
printArray(Serial.list());
// Open the port you are using at the rate you want:
serial = new Serial(this, Serial.list()[0], 9600);
serial.clear();
// Throw out the first reading, in case we started reading
// in the middle of a string from the sender.
serialString = serial.readStringUntil(serial_endchar);
serialString = null;
}else{
logdata = loadTable("LOG00008_rumfahren_neu.TXT", "header, csv");
println("loaded "+logdata.getRowCount()+" lines. Times: "+logdata.getRow(0).getFloat("time")+"s to "+logdata.getRow(logdata.getRowCount()-1).getFloat("time")+"s"); println("loaded "+logdata.getRowCount()+" lines. Times: "+logdata.getRow(0).getFloat("time")+"s to "+logdata.getRow(logdata.getRowCount()-1).getFloat("time")+"s");
}
PVector pos_vis_cmd = new PVector(100,150); PVector pos_vis_cmd = new PVector(100,150);
PVector size_vis_cmd = new PVector(10,100); PVector size_vis_cmd = new PVector(10,100);
@ -166,37 +183,72 @@ void setup() {
void draw() { void draw() {
long loopmillis=millis()+25000; long loopmillis=millis()+25000;
if (loopmillis>=nextTime){
TableRow row = logdata.getRow(nextID); if (useSerial) {
lastTimeData=nextTime; while (serial.available() > 0) {
nextTime=(long)(row.getFloat("time")*1000); //get time and convert from seconds to ms serialString = serial.readStringUntil(serial_endchar);
if (serialString != null) {
cmd_FrontL=row.getInt("cmd_FrontL"); println(serialString);
cmd_FrontR=row.getInt("cmd_FrontR"); lastTimeData=nextTime;
cmd_RearL=row.getInt("cmd_RearL"); nextTime=loopmillis;
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");
if (loopmillis-nextTime>1000) {//too much behind
long _timestep=nextTime-lastTimeData; //approximated time step String[] list = split(serialString, ',');
nextID+=(loopmillis-nextTime)/_timestep* 0.9; //fast forward estimated time steps cmd_FrontL=parseInt(list[0]);
cmd_FrontR=parseInt(list[1]);
cmd_RearL=parseInt(list[2]);
cmd_RearR=parseInt(list[3]);
current_FrontL=parseFloat(list[4]);
current_FrontR=parseFloat(list[5]);
current_RearL=parseFloat(list[6]);
current_RearR=parseFloat(list[7]);
speed_FrontL=parseInt(list[8]);
speed_FrontR=parseInt(list[9]);
speed_RearL=parseInt(list[10]);
speed_RearR=parseInt(list[11]);
temp_Front=parseFloat(list[12]);
temp_Rear=parseFloat(list[13]);
vbat_Front=parseFloat(list[14]);
vbat_Rear=parseFloat(list[15]);
currentAll=parseFloat(list[16]);
throttle=parseInt(list[17]);
brake=parseInt(list[18]);
}
}
}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");
if (loopmillis-nextTime>1000) {//too much behind
long _timestep=nextTime-lastTimeData; //approximated time step
nextID+=(loopmillis-nextTime)/_timestep* 0.9; //fast forward estimated time steps
}
nextID++;
nextID=nextID%logdata.getRowCount();
} }
nextID++;
nextID=nextID%logdata.getRowCount();
} }
@ -253,7 +305,7 @@ void draw() {
textAlign(LEFT); textAlign(LEFT);
textSize(12); textSize(12);
text("d="+(nextTime-lastTimeData)+"ms", 5+70,12); text("d="+(nextTime-lastTimeData)+"ms", 5+70,12);
if (loopmillis-lastTimeData>(nextTime-lastTimeData)*10) { //deviation too high if (!useSerial && loopmillis-lastTimeData>(nextTime-lastTimeData)*10) { //deviation too high when reading from file
text("ff="+(loopmillis-lastTimeData)+"ms", 5+70+70,12); //show warning text("ff="+(loopmillis-lastTimeData)+"ms", 5+70+70,12); //show warning
} }

View File

@ -0,0 +1,20 @@
// Example by Tom Igoe
import processing.serial.*;
// The serial port:
Serial serial;
void setup() {
// List all the available serial ports:
printArray(Serial.list());
// Open the port you are using at the rate you want:
serial = new Serial(this, Serial.list()[0], 115200);
}
void loop() {
serial.write("asdf");
}