From a52d6b832504b79f772d2b08d91ad26e27da06a2 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Mon, 30 Aug 2021 19:16:50 +0200 Subject: [PATCH] Revision and new visualization system. --- code/arduino/coilDriver/coilDriver.ino | 40 ++++++----- plotting-go/.gitignore | 1 + plotting-go/go.mod | 8 +++ plotting-go/go.sum | 4 ++ plotting-go/index.html | 93 ++++++++++++++++++++++++++ plotting-go/main.go | 71 ++++++++++++++++++++ 6 files changed, 200 insertions(+), 17 deletions(-) create mode 100644 plotting-go/.gitignore create mode 100644 plotting-go/go.mod create mode 100644 plotting-go/go.sum create mode 100644 plotting-go/index.html create mode 100644 plotting-go/main.go diff --git a/code/arduino/coilDriver/coilDriver.ino b/code/arduino/coilDriver/coilDriver.ino index 9f83d57..f6aaf1c 100644 --- a/code/arduino/coilDriver/coilDriver.ino +++ b/code/arduino/coilDriver/coilDriver.ino @@ -14,6 +14,10 @@ int impulsDelay = 1; int impulsLength = 5; int potValue = 1; +int individualAfterDelay = 10; +unsigned long i1wait = 0; +unsigned long i2wait = 0; +unsigned long i3wait = 0; unsigned long previouscoil2trigger = 0; unsigned long lastcoil2trigger = 0; @@ -48,23 +52,14 @@ void setup() { Serial.begin(9600); Serial.flush(); - mySerial.begin(4800); - mySerial.flush(); } void loop() { - unsigned long currentMillis = millis(); // getting the pulse length from the pot - potValue = analogRead(A0); - impulsLength = map(potValue, 0, 1023, 0, 100); + //potValue = analogRead(A0); + //impulsLength = map(potValue, 0, 1023, 0, 100); // 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 if (digitalRead(sensor1pin)) { impuls1(); @@ -76,30 +71,41 @@ void loop() { } void impuls1() { + if (i1wait > millis()) { + return; + } delay(impulsDelay); digitalWrite(coil1pin, LOW); delay(impulsLength); digitalWrite(coil1pin, HIGH); + Serial.print("1 "); Serial.println(millis()); - delay(50); + i1wait = millis()+individualAfterDelay; } void impuls2() { + if (i2wait > millis()) { + return; + } delay(impulsDelay); digitalWrite(coil2pin, LOW); delay(impulsLength); digitalWrite(coil2pin, HIGH); + Serial.print("2 "); Serial.println(millis()); - delay(50); - previouscoil2trigger = lastcoil2trigger; - lastcoil2trigger = millis(); + i2wait = millis()+individualAfterDelay; + } void impuls3() { + if (i3wait > millis()) { + return; + } delay(impulsDelay); digitalWrite(coil3pin, LOW); delay(impulsLength); digitalWrite(coil3pin, HIGH); + Serial.print("3 "); Serial.println(millis()); - delay(50); + i3wait = millis()+individualAfterDelay; } diff --git a/plotting-go/.gitignore b/plotting-go/.gitignore new file mode 100644 index 0000000..2033008 --- /dev/null +++ b/plotting-go/.gitignore @@ -0,0 +1 @@ +data.csv diff --git a/plotting-go/go.mod b/plotting-go/go.mod new file mode 100644 index 0000000..6a9b93f --- /dev/null +++ b/plotting-go/go.mod @@ -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 +) diff --git a/plotting-go/go.sum b/plotting-go/go.sum new file mode 100644 index 0000000..4f66252 --- /dev/null +++ b/plotting-go/go.sum @@ -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= diff --git a/plotting-go/index.html b/plotting-go/index.html new file mode 100644 index 0000000..66e75c6 --- /dev/null +++ b/plotting-go/index.html @@ -0,0 +1,93 @@ + + + + + Graf rychlosti kuličky v urychlovači + + + +
+ Current speed: + + + + m·s⁻¹ +
+ + + + diff --git a/plotting-go/main.go b/plotting-go/main.go new file mode 100644 index 0000000..1cc22dd --- /dev/null +++ b/plotting-go/main.go @@ -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 +}