Added mpu module. Some refactoring and cleaning.
This commit is contained in:
		
							
								
								
									
										2
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								main.go
									
									
									
									
									
								
							@@ -4,7 +4,7 @@ import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/CGSG-2021-AE4/mpu-test/mpu6050"
 | 
			
		||||
	"github.com/CGSG-2021-AE4/mpu-test/pkg/mpu6050"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										74
									
								
								mpu/mpu.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								mpu/mpu.go
									
									
									
									
									
										Normal 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()
 | 
			
		||||
}
 | 
			
		||||
@@ -2,7 +2,7 @@ package mpu6050
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"io"
 | 
			
		||||
 | 
			
		||||
	"golang.org/x/exp/io/i2c"
 | 
			
		||||
)
 | 
			
		||||
@@ -70,10 +70,14 @@ type device struct {
 | 
			
		||||
 | 
			
		||||
type Device interface {
 | 
			
		||||
	GetTemp() (float32, error)
 | 
			
		||||
 | 
			
		||||
	SetAccelRange(r byte) error
 | 
			
		||||
	GetAccel(inG bool) ([3]float32, error)
 | 
			
		||||
 | 
			
		||||
	SetGyroRange(r byte) error
 | 
			
		||||
	GetGyro() ([3]float32, error)
 | 
			
		||||
 | 
			
		||||
	io.Closer
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Open() (Device, error) {
 | 
			
		||||
@@ -140,8 +144,8 @@ func (d *device) GetAccel(inG bool) ([3]float32, error) {
 | 
			
		||||
		scale = AccelScaleModifier2G
 | 
			
		||||
	case AccelRange16G:
 | 
			
		||||
		scale = AccelScaleModifier2G
 | 
			
		||||
	default:
 | 
			
		||||
		log.Println("not foudn accel range:", accelRange)
 | 
			
		||||
		//default:
 | 
			
		||||
		// log.Println("not foudn accel range:", accelRange)
 | 
			
		||||
	}
 | 
			
		||||
	scale = 1 / scale
 | 
			
		||||
 | 
			
		||||
@@ -201,8 +205,8 @@ func (d *device) GetGyro() ([3]float32, error) {
 | 
			
		||||
		scale = GyroScaleModifier1000DEG
 | 
			
		||||
	case GyroRange2000DEG:
 | 
			
		||||
		scale = GyroScaleModifier2000DEG
 | 
			
		||||
	default:
 | 
			
		||||
		log.Println("not found gyro range:", gyroRange)
 | 
			
		||||
		// default:
 | 
			
		||||
		//   log.Println("not found gyro range:", gyroRange)
 | 
			
		||||
	}
 | 
			
		||||
	return [3]float32{
 | 
			
		||||
		float32(x) / scale,
 | 
			
		||||
@@ -210,3 +214,10 @@ func (d *device) GetGyro() ([3]float32, error) {
 | 
			
		||||
		float32(z) / scale,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *device) Close() error {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		d.device = nil
 | 
			
		||||
	}()
 | 
			
		||||
	return d.device.Close()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user