commit 031d784c535ec59be23b271cc1fa5d21c9a027ff Author: Andrey Egorov Date: Wed Jul 24 14:29:00 2024 +0300 Initial. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f8ced0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +out/ +Makefile +go.sum +.git/ +*.swp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0ebf38e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:1.22.5 + +WORKDIR /app +COPY ./ ./ +RUN go mod download + + +RUN CGO_ENABLED=0 GOOS=linux go build -o /modem-test + +CMD ["/modem-test"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..05b0285 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: "3.4" + +services: + modem-test: + container_name: mpu-test + privileged: true + build: + context: ./ + dockerfile: ./Dockerfile + network_mode: host + volumes: + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro + devices: + - /dev/i2c-1 + - /dev/ttyS0 + restart: unless-stopped \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dcbfd52 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/CGSG-2021-AE4/mpu-test + +go 1.22.5 + +require golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect diff --git a/main.go b/main.go new file mode 100644 index 0000000..2093f5a --- /dev/null +++ b/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "log" + + "github.com/CGSG-2021-AE4/mpu-test/mpu6050" +) + +func main() { + log.Println("CGSG forever!!!") + if err := mainE(); err != nil { + log.Println("MAIN finished with error:", err.Error()) + } + log.Println("END") +} + +func mainE() error { + log.Println("OPEN") + mpu, err := mpu6050.Open() + if err != nil { + return err + } + log.Println("GET") + temp, err := mpu.GetTemp() + if err != nil { + return err + } + log.Println(temp) + return nil +} diff --git a/mpu6050/i2c.go b/mpu6050/i2c.go new file mode 100644 index 0000000..a24cf38 --- /dev/null +++ b/mpu6050/i2c.go @@ -0,0 +1,13 @@ +package mpu6050 + +import ( + "encoding/binary" +) + +func (d device) readWord(register byte) (int16, error) { + bytes := make([]byte, 2) + if err := d.device.ReadReg(register, bytes); err != nil { + return 0, err + } + return int16(binary.BigEndian.Uint16(bytes)), nil // !!! CHECK uint->int convertsion +} diff --git a/mpu6050/mpu.go b/mpu6050/mpu.go new file mode 100644 index 0000000..0506267 --- /dev/null +++ b/mpu6050/mpu.go @@ -0,0 +1,68 @@ +package mpu6050 + +import ( + "fmt" + + "golang.org/x/exp/io/i2c" +) + +const ( + mpuAddr = 0x68 + + // MPU registers + + // Power management + // powerMgmt1 = 0x6B + // powerMgmt2 = 0x6C + // Acceleration + // accelXOut0 = 0x3B + // accelYOut0 = 0x3D + // accelZOut0 = 0x3F + // Temperature + tempOut0 = 0x41 + +// Gyro +// gyroXOut0 = 0x43 +// gyroYOut0 = 0x45 +// gyroZOut0 = 0x47 +// Configs +// accelConfig = 0x1C +// gyroConfig = 0x1B +// mpuConfig = 0x1A +) + +type device struct { + device *i2c.Device +} + +type Device interface { + GetTemp() (float32, error) + GetAccel() ([3]float32, error) + GetGyro() ([3]float32, error) +} + +func Open() (Device, error) { + d, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, mpuAddr) + if err != nil { + return nil, fmt.Errorf("open i2c: %w", err) + } + return &device{ + device: d, + }, nil +} + +func (d *device) GetTemp() (float32, error) { + rawTemp, err := d.readWord(tempOut0) + if err != nil { + return 0, err + } + return (float32(rawTemp) / 340.0) + 36.53, nil +} + +func (d *device) GetAccel() ([3]float32, error) { + return [3]float32{}, nil +} + +func (d *device) GetGyro() ([3]float32, error) { + return [3]float32{}, nil +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e883563 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +mpu6050 test project \ No newline at end of file