Added mpu module. Some refactoring and cleaning.

This commit is contained in:
Andrey Egorov 2024-07-28 20:58:39 +03:00
parent 9f9899d2cc
commit 7fbd64168c
4 changed files with 92 additions and 7 deletions

View File

@ -4,7 +4,7 @@ import (
"log" "log"
"time" "time"
"github.com/CGSG-2021-AE4/mpu-test/mpu6050" "github.com/CGSG-2021-AE4/mpu-test/pkg/mpu6050"
) )
func main() { func main() {
@ -26,7 +26,7 @@ func mainE() error {
for { for {
// temp, err := mpu.GetTemp() // temp, err := mpu.GetTemp()
// if err != nil { // if err != nil {
// return err // return err
// } // }
accel, err := mpu.GetAccel(false) accel, err := mpu.GetAccel(false)
if err != nil { if err != nil {

74
mpu/mpu.go Normal file
View File

@ -0,0 +1,74 @@
package mpu
import (
"fmt"
"io"
"github.com/CGSG-2021-AE4/mpu-test/pkg/mpu6050"
)
type MpuData struct {
TempData float32 `json:"Temperature"`
AccelData [3]float32 `json:"Accelerometer"`
GyroData [3]float32 `json:"Gyroscope"`
}
type mpu struct {
device mpu6050.Device
data MpuData
}
type Mpu interface {
Init() error
Update() error
Validate() bool
GetData() MpuData
io.Closer
}
func New() Mpu {
return &mpu{
device: nil,
}
}
func (m *mpu) Init() error {
d, err := mpu6050.Open()
if err != nil {
return fmt.Errorf("open mpu connection: %w", err)
}
m.device = d
return nil
}
func (m *mpu) Update() (err error) {
m.data.TempData, err = m.device.GetTemp()
if err != nil {
return fmt.Errorf("get temp data: %w", err)
}
m.data.AccelData, err = m.device.GetAccel(false)
if err != nil {
return fmt.Errorf("get accel data: %w", err)
}
m.data.GyroData, err = m.device.GetGyro()
if err != nil {
return fmt.Errorf("get gyro data: %w", err)
}
return nil
}
func (m *mpu) Validate() bool {
return m.device != nil
}
func (m *mpu) GetData() MpuData {
return m.data
}
func (m *mpu) Close() error {
defer func() {
m.device = nil
}()
return m.device.Close()
}

View File

@ -2,7 +2,7 @@ package mpu6050
import ( import (
"fmt" "fmt"
"log" "io"
"golang.org/x/exp/io/i2c" "golang.org/x/exp/io/i2c"
) )
@ -70,10 +70,14 @@ type device struct {
type Device interface { type Device interface {
GetTemp() (float32, error) GetTemp() (float32, error)
SetAccelRange(r byte) error SetAccelRange(r byte) error
GetAccel(inG bool) ([3]float32, error) GetAccel(inG bool) ([3]float32, error)
SetGyroRange(r byte) error SetGyroRange(r byte) error
GetGyro() ([3]float32, error) GetGyro() ([3]float32, error)
io.Closer
} }
func Open() (Device, error) { func Open() (Device, error) {
@ -140,8 +144,8 @@ func (d *device) GetAccel(inG bool) ([3]float32, error) {
scale = AccelScaleModifier2G scale = AccelScaleModifier2G
case AccelRange16G: case AccelRange16G:
scale = AccelScaleModifier2G scale = AccelScaleModifier2G
default: //default:
log.Println("not foudn accel range:", accelRange) // log.Println("not foudn accel range:", accelRange)
} }
scale = 1 / scale scale = 1 / scale
@ -201,8 +205,8 @@ func (d *device) GetGyro() ([3]float32, error) {
scale = GyroScaleModifier1000DEG scale = GyroScaleModifier1000DEG
case GyroRange2000DEG: case GyroRange2000DEG:
scale = GyroScaleModifier2000DEG scale = GyroScaleModifier2000DEG
default: // default:
log.Println("not found gyro range:", gyroRange) // log.Println("not found gyro range:", gyroRange)
} }
return [3]float32{ return [3]float32{
float32(x) / scale, float32(x) / scale,
@ -210,3 +214,10 @@ func (d *device) GetGyro() ([3]float32, error) {
float32(z) / scale, float32(z) / scale,
}, nil }, nil
} }
func (d *device) Close() error {
defer func() {
d.device = nil
}()
return d.device.Close()
}