This commit is contained in:
Andrey Egorov 2024-07-24 14:29:00 +03:00
commit 031d784c53
8 changed files with 149 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
out/
Makefile
go.sum
.git/
*.swp

10
Dockerfile Normal file
View File

@ -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"]

17
docker-compose.yml Normal file
View File

@ -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

5
go.mod Normal file
View File

@ -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

30
main.go Normal file
View File

@ -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
}

13
mpu6050/i2c.go Normal file
View File

@ -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
}

68
mpu6050/mpu.go Normal file
View File

@ -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
}

1
readme.md Normal file
View File

@ -0,0 +1 @@
mpu6050 test project