Skip to content

Commit

Permalink
Fix ADC drift
Browse files Browse the repository at this point in the history
  • Loading branch information
bclswl0827 committed Sep 24, 2023
1 parent 9365d45 commit 7582838
Show file tree
Hide file tree
Showing 24 changed files with 85 additions and 72 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.1.4p
v2.1.5p
2 changes: 1 addition & 1 deletion build/assets/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"fullscale": 5.0
},
"serial_settings": {
"length": 100,
"packet": 2,
"baud": 19200,
"device": "/dev/ttyUSB0"
},
Expand Down
2 changes: 1 addition & 1 deletion config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type station struct {
type serial struct {
Device string `json:"device"`
Baud int `json:"baud"`
Length int `json:"length"`
Packet int `json:"packet"`
}

type adc struct {
Expand Down
8 changes: 4 additions & 4 deletions driver/serial/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"io"
)

func Filter(port io.ReadWriteCloser, signature []byte) error {
func Filter(port io.ReadWriteCloser, signature []byte, retry int) ([]byte, error) {
header := make([]byte, len(signature))

for i := 0; i < 64; i++ {
for i := 0; i < retry; i++ {
port.Read(header)

if bytes.Equal(header, signature) {
return nil
return nil, nil
}
}

return fmt.Errorf("failed to filter header")
return header, fmt.Errorf("failed to filter header")
}
2 changes: 1 addition & 1 deletion feature/geophone/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (a *Geophone) OnReady(options *feature.FeatureOptions, v ...any) {
)

// Appending packet data to buffer
for i := 0; i < options.Config.Serial.Length; i++ {
for i := 0; i < options.Config.Serial.Packet; i++ {
options.Status.Buffer.EHZ = append(options.Status.Buffer.EHZ, packet.EHZ[i])
options.Status.Buffer.EHE = append(options.Status.Buffer.EHE, packet.EHE[i])
options.Status.Buffer.EHN = append(options.Status.Buffer.EHN, packet.EHN[i])
Expand Down
28 changes: 28 additions & 0 deletions feature/geophone/counts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package geophone

import "sort"

func (g *Geophone) getCounts(data []int32) []int32 {
sortedData := make([]int32, len(data))
copy(sortedData, data)
sort.Slice(sortedData, func(i, j int) bool {
return sortedData[i] < sortedData[j]
})

var median int32
if len(sortedData)%2 == 1 {
middleIndex := len(sortedData) / 2
median = sortedData[middleIndex]
} else {
middleIndex1 := len(sortedData)/2 - 1
middleIndex2 := len(sortedData) / 2
median = (sortedData[middleIndex1] + sortedData[middleIndex2]) / 2
}

result := make([]int32, len(data))
for i, value := range data {
result[i] = value - median
}

return result
}
8 changes: 4 additions & 4 deletions feature/geophone/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

func (g *Geophone) Start(options *feature.FeatureOptions) {
var (
device = options.Config.Serial.Device
baud = options.Config.Serial.Baud
length = options.Config.Serial.Length
device = options.Config.Serial.Device
baud = options.Config.Serial.Baud
packetLen = options.Config.Serial.Packet
)

port, err := serial.Open(device, baud)
Expand All @@ -29,7 +29,7 @@ func (g *Geophone) Start(options *feature.FeatureOptions) {

lastRead := time.Now().UTC()
for {
err := g.Read(port, &packet, length)
err := g.Read(port, &packet, packetLen)
if err != nil {
serial.Close(port)
g.OnError(options, err)
Expand Down
23 changes: 14 additions & 9 deletions feature/geophone/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"github.com/bclswl0827/observer/driver/serial"
)

func (g *Geophone) Read(port io.ReadWriteCloser, packet *Packet, length int) error {
func (g *Geophone) Read(port io.ReadWriteCloser, packet *Packet, packetLen int) error {
// Filter frame header
err := serial.Filter(port, []byte{0xFC, 0x1B})
_, err := serial.Filter(port, SYNC_WORD[:], 16)
if err != nil {
return err
}

// checksumLength * (uint8 + int32 * length)
checksumLength := len(packet.Checksum)
packetSize := checksumLength * (1 + 4*length)
// checksumLen * (uint8 + int32 * packetLen) + uint8
checksumLen := len(packet.Checksum)
packetSize := checksumLen*(1+4*packetLen) + 1

// Read data frame
buf := make([]byte, packetSize)
Expand All @@ -28,9 +28,9 @@ func (g *Geophone) Read(port io.ReadWriteCloser, packet *Packet, length int) err
}

// Allocate memory for data frame
packet.EHZ = make([]int32, length)
packet.EHE = make([]int32, length)
packet.EHN = make([]int32, length)
packet.EHZ = make([]int32, packetLen)
packet.EHE = make([]int32, packetLen)
packet.EHN = make([]int32, packetLen)

// Create reader for data frame
reader := bytes.NewReader(buf[:n])
Expand All @@ -54,7 +54,7 @@ func (g *Geophone) Read(port io.ReadWriteCloser, packet *Packet, length int) err
}

// Parse checksum
for i := 0; i < checksumLength; i++ {
for i := 0; i < checksumLen; i++ {
err = binary.Read(reader, binary.LittleEndian, &packet.Checksum[i])
if err != nil {
return err
Expand All @@ -67,5 +67,10 @@ func (g *Geophone) Read(port io.ReadWriteCloser, packet *Packet, length int) err
return err
}

// Offset data to center around 0
packet.EHZ = g.getCounts(packet.EHZ)
packet.EHE = g.getCounts(packet.EHE)
packet.EHN = g.getCounts(packet.EHN)

return nil
}
4 changes: 2 additions & 2 deletions feature/geophone/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

func (g *Geophone) Reset(port io.ReadWriteCloser) error {
_, err := port.Write([]byte{0x61})
_, err := port.Write(RESET_WORD[:])
if err != nil {
return err
}

err = serial.Filter(port, []byte{0xFC, 0x2B})
_, err = serial.Filter(port, ACK_WORD[:], 64)
if err != nil {
return fmt.Errorf("failed to reset geophone")
}
Expand Down
9 changes: 9 additions & 0 deletions feature/geophone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const (
TIMEOUT_THRESHOLD time.Duration = 3 * time.Second
)

var (
// RESET_WORD resets geophone ADC module
RESET_WORD = [...]byte{0x61}
// SYNC_WORD indicates a data packet is following
SYNC_WORD = [...]byte{0xFC, 0x1B}
// ACK_WORD indicates a valid command is received
ACK_WORD = [...]byte{0xFC, 0x2B}
)

type Geophone struct{}

type Packet struct {
Expand Down
8 changes: 4 additions & 4 deletions frontend/dist/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"files": {
"main.css": "/static/css/main.6306a3a5.css",
"main.js": "/static/js/main.51d2d5e6.js",
"main.js": "/static/js/main.8eb19dec.js",
"static/css/10.525e2941.chunk.css": "/static/css/10.525e2941.chunk.css",
"static/js/10.acbbbcaf.chunk.js": "/static/js/10.acbbbcaf.chunk.js",
"static/js/311.c9e754c7.chunk.js": "/static/js/311.c9e754c7.chunk.js",
"static/js/923.7f5fcd6c.chunk.js": "/static/js/923.7f5fcd6c.chunk.js",
"static/js/311.a3ecb2f6.chunk.js": "/static/js/311.a3ecb2f6.chunk.js",
"static/js/923.3fab24c3.chunk.js": "/static/js/923.3fab24c3.chunk.js",
"static/js/419.50ce82ce.chunk.js": "/static/js/419.50ce82ce.chunk.js",
"static/js/377.309ea121.chunk.js": "/static/js/377.309ea121.chunk.js",
"static/js/493.11396ed4.chunk.js": "/static/js/493.11396ed4.chunk.js",
Expand Down Expand Up @@ -41,6 +41,6 @@
},
"entrypoints": [
"static/css/main.6306a3a5.css",
"static/js/main.51d2d5e6.js"
"static/js/main.8eb19dec.js"
]
}
2 changes: 1 addition & 1 deletion frontend/dist/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><link rel="icon" href="/favicon.ico"/><link rel="manifest" href="/manifest.json"/><script defer="defer" src="/static/js/main.51d2d5e6.js"></script><link href="/static/css/main.6306a3a5.css" rel="stylesheet"></head><body><div id="root"></div></body></html>
<!doctype html><html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><link rel="icon" href="/favicon.ico"/><link rel="manifest" href="/manifest.json"/><script defer="defer" src="/static/js/main.8eb19dec.js"></script><link href="/static/css/main.6306a3a5.css" rel="stylesheet"></head><body><div id="root"></div></body></html>
1 change: 1 addition & 0 deletions frontend/dist/static/js/311.a3ecb2f6.chunk.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7582838

Please sign in to comment.