Revision and new visualization system.
This commit is contained in:
parent
fbfd670f66
commit
a52d6b8325
@ -14,6 +14,10 @@
|
|||||||
int impulsDelay = 1;
|
int impulsDelay = 1;
|
||||||
int impulsLength = 5;
|
int impulsLength = 5;
|
||||||
int potValue = 1;
|
int potValue = 1;
|
||||||
|
int individualAfterDelay = 10;
|
||||||
|
unsigned long i1wait = 0;
|
||||||
|
unsigned long i2wait = 0;
|
||||||
|
unsigned long i3wait = 0;
|
||||||
|
|
||||||
unsigned long previouscoil2trigger = 0;
|
unsigned long previouscoil2trigger = 0;
|
||||||
unsigned long lastcoil2trigger = 0;
|
unsigned long lastcoil2trigger = 0;
|
||||||
@ -48,23 +52,14 @@ void setup() {
|
|||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
mySerial.begin(4800);
|
|
||||||
mySerial.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long currentMillis = millis();
|
|
||||||
// getting the pulse length from the pot
|
// getting the pulse length from the pot
|
||||||
potValue = analogRead(A0);
|
//potValue = analogRead(A0);
|
||||||
impulsLength = map(potValue, 0, 1023, 0, 100);
|
//impulsLength = map(potValue, 0, 1023, 0, 100);
|
||||||
// sending data to the displayDriver every 1s
|
// sending data to the displayDriver every 1s
|
||||||
if (currentMillis - lastdisplaywrite >= 1000) {
|
|
||||||
lastdisplaywrite = currentMillis;
|
|
||||||
mySerial.print(impulsLength);
|
|
||||||
mySerial.print(",");
|
|
||||||
mySerial.print(lastcoil2trigger-previouscoil2trigger);
|
|
||||||
mySerial.print('\n');
|
|
||||||
}
|
|
||||||
// checking if ball detected, if yes then fire the pulse
|
// checking if ball detected, if yes then fire the pulse
|
||||||
if (digitalRead(sensor1pin)) {
|
if (digitalRead(sensor1pin)) {
|
||||||
impuls1();
|
impuls1();
|
||||||
@ -76,30 +71,41 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void impuls1() {
|
void impuls1() {
|
||||||
|
if (i1wait > millis()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
delay(impulsDelay);
|
delay(impulsDelay);
|
||||||
digitalWrite(coil1pin, LOW);
|
digitalWrite(coil1pin, LOW);
|
||||||
delay(impulsLength);
|
delay(impulsLength);
|
||||||
digitalWrite(coil1pin, HIGH);
|
digitalWrite(coil1pin, HIGH);
|
||||||
|
Serial.print("1 ");
|
||||||
Serial.println(millis());
|
Serial.println(millis());
|
||||||
delay(50);
|
i1wait = millis()+individualAfterDelay;
|
||||||
}
|
}
|
||||||
|
|
||||||
void impuls2() {
|
void impuls2() {
|
||||||
|
if (i2wait > millis()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
delay(impulsDelay);
|
delay(impulsDelay);
|
||||||
digitalWrite(coil2pin, LOW);
|
digitalWrite(coil2pin, LOW);
|
||||||
delay(impulsLength);
|
delay(impulsLength);
|
||||||
digitalWrite(coil2pin, HIGH);
|
digitalWrite(coil2pin, HIGH);
|
||||||
|
Serial.print("2 ");
|
||||||
Serial.println(millis());
|
Serial.println(millis());
|
||||||
delay(50);
|
i2wait = millis()+individualAfterDelay;
|
||||||
previouscoil2trigger = lastcoil2trigger;
|
|
||||||
lastcoil2trigger = millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void impuls3() {
|
void impuls3() {
|
||||||
|
if (i3wait > millis()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
delay(impulsDelay);
|
delay(impulsDelay);
|
||||||
digitalWrite(coil3pin, LOW);
|
digitalWrite(coil3pin, LOW);
|
||||||
delay(impulsLength);
|
delay(impulsLength);
|
||||||
digitalWrite(coil3pin, HIGH);
|
digitalWrite(coil3pin, HIGH);
|
||||||
|
Serial.print("3 ");
|
||||||
Serial.println(millis());
|
Serial.println(millis());
|
||||||
delay(50);
|
i3wait = millis()+individualAfterDelay;
|
||||||
}
|
}
|
||||||
|
1
plotting-go/.gitignore
vendored
Normal file
1
plotting-go/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
data.csv
|
8
plotting-go/go.mod
Normal file
8
plotting-go/go.mod
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module gitlab.com/patek-devs/2019-mfp/particle-accelerator/plotting-go
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
|
||||||
|
)
|
4
plotting-go/go.sum
Normal file
4
plotting-go/go.sum
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
|
||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
|
||||||
|
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
|
||||||
|
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
93
plotting-go/index.html
Normal file
93
plotting-go/index.html
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="cs">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Graf rychlosti kuličky v urychlovači</title>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js" integrity="sha256-R4pqcOYV8lt7snxMQO/HSbVCFRPMdrhAFMH+vr9giYI=" crossorigin="anonymous"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="currentSpeed" style="font-size: 4em;">
|
||||||
|
Current speed:
|
||||||
|
<span>
|
||||||
|
|
||||||
|
</span>
|
||||||
|
m·s⁻¹
|
||||||
|
</div>
|
||||||
|
<canvas id="myChart" style="height: 59vh; width: 100vw;"></canvas>
|
||||||
|
<script>
|
||||||
|
let myChart;
|
||||||
|
|
||||||
|
function fetchData() {
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
fetch("./data.csv").then(r => r.text()).then(text => {
|
||||||
|
data = text.split("\n").map(e => [parseInt(e.split(", ")[0]),parseFloat(e.split(", ")[1])])
|
||||||
|
dataAvg3 = []
|
||||||
|
for (let i = 0; i < data.length-3; i++) {
|
||||||
|
dataAvg3[i] = [data[i][0],(data[i][1]+data[i+1][1]+data[i+2][1])/3]
|
||||||
|
}
|
||||||
|
document.querySelector("#currentSpeed span").innerText = dataAvg3[dataAvg3.length-1][1]
|
||||||
|
resolve([data, dataAvg3])
|
||||||
|
});
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let ctx = document.getElementById('myChart').getContext('2d');
|
||||||
|
hueRed = 0;
|
||||||
|
hueBlue = 240;
|
||||||
|
myChart = new Chart(ctx, {
|
||||||
|
type: 'line',
|
||||||
|
data: {
|
||||||
|
labels: [],
|
||||||
|
datasets: [{
|
||||||
|
label: "Raw speed data",
|
||||||
|
data: [],
|
||||||
|
pointRadius: 0,
|
||||||
|
pointHoverRadius: 0,
|
||||||
|
color: "rgba(255,0,0,1)",
|
||||||
|
backgroundColor: "rgba(0,0,0,0)",
|
||||||
|
hoverBackgroundColor: "rgba(0,0,0,0)",
|
||||||
|
borderColor: "hsla(" + hueRed + ",100%,50%,.3)",
|
||||||
|
hoverBorderColor: "hsla(" + hueRed + ",100%,50%,1)",
|
||||||
|
},{
|
||||||
|
label: "Average speed for 3 measurements",
|
||||||
|
data: [],
|
||||||
|
pointRadius: 0,
|
||||||
|
pointHoverRadius: 0,
|
||||||
|
color: "rgba(255,0,0,1)",
|
||||||
|
backgroundColor: "rgba(0,0,0,0)",
|
||||||
|
hoverBackgroundColor: "rgba(0,0,0,0)",
|
||||||
|
borderColor: "hsla(" + hueBlue + ",100%,50%,.7)",
|
||||||
|
hoverBorderColor: "hsla(" + hueBlue + ",100%,50%,1)",
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
maintainAspectRatio: false,
|
||||||
|
legend: {
|
||||||
|
display: window.innerWidth > window.innerHeight
|
||||||
|
},
|
||||||
|
hover: {
|
||||||
|
mode: "dataset"
|
||||||
|
},
|
||||||
|
animation: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
fetchData().then(datasets => {
|
||||||
|
myChart.data.labels = datasets[0].map(e => e[0])
|
||||||
|
for (let i = 0; i < datasets.length; i++) {
|
||||||
|
myChart.data.datasets[i].data = datasets[i].map(e => e[1])
|
||||||
|
}
|
||||||
|
myChart.update()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
setInterval(fetchData, 1000)
|
||||||
|
setInterval(update, 5000)
|
||||||
|
update()
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
71
plotting-go/main.go
Normal file
71
plotting-go/main.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "github.com/tarm/serial"
|
||||||
|
import "bufio"
|
||||||
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
|
import "time"
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
const loopLength = 2.05 // In meters
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
config := &serial.Config{
|
||||||
|
Name: "/dev/ttyUSB0",
|
||||||
|
Baud: 9600,
|
||||||
|
}
|
||||||
|
|
||||||
|
stream, err := serial.OpenPort(config)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
reader := bufio.NewReader(stream)
|
||||||
|
|
||||||
|
lastCoil := 0;
|
||||||
|
lastMillis := 0;
|
||||||
|
rAvg := 0.0;
|
||||||
|
rAvgCount := 0;
|
||||||
|
|
||||||
|
datafile, err := os.OpenFile("data.csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer datafile.Close()
|
||||||
|
|
||||||
|
|
||||||
|
for {
|
||||||
|
lineb, _, _ := reader.ReadLine()
|
||||||
|
line := string(lineb)
|
||||||
|
coilNum,_ := strconv.Atoi(strings.Split(line, " ")[0])
|
||||||
|
coilNum = coilNum % 3
|
||||||
|
millis,_ := strconv.Atoi(strings.Split(line, " ")[1])
|
||||||
|
d := float64(coilDistance(lastCoil, coilNum)) * (loopLength/3)
|
||||||
|
lastCoil = coilNum
|
||||||
|
t := float64(millis - lastMillis)/1000
|
||||||
|
lastMillis = millis
|
||||||
|
s := d/t
|
||||||
|
if (s < 0.1 || s > 4) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
a := (rAvg*float64(rAvgCount) + s)/float64(rAvgCount+1)
|
||||||
|
rAvg = a
|
||||||
|
rAvgCount++
|
||||||
|
fmt.Printf("u: %d, d: %fm, t: %fs, s: %fm/s, a: %fm/s\n", time.Now().UnixNano()/1000000,d,t,s,a)
|
||||||
|
fmt.Fprintf(datafile, "%d, %f\n", time.Now().UnixNano()/1000000, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func coilDistance(previous int, now int) int {
|
||||||
|
d := (previous-now+3)%3
|
||||||
|
if d == 0 {
|
||||||
|
d = 3
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user