Esempio n. 1
0
/**
* Read the raw gyro values and display every 100th one
**/
func main() {
	var (
		err        error
		errCnt     int
		pause      time.Duration
		i          int
		x, y, z    int16
		xx, yy, zz int
		px, py, pz int
		gyroscope  *sensors.L3GD20 = nil
	)
	gyroscope, err = sensors.NewL3GD20()
	if err != nil {
		fmt.Printf("Error: getting device L3GD20, err=%v\n", err)
	} else {
		pause = time.Millisecond * 100
		for {
			x, y, z, err = gyroscope.ReadRaw()
			if err != nil {
				errCnt++
			} else {
				i++
				xx += int(x)
				yy += int(y)
				zz += int(z)
				if i == 10 {
					xx = xx / i
					yy = yy / i
					zz = zz / i
					fmt.Printf("%4d, %4d, %4d\n", xx-px, yy-py, zz-pz)
					i = 0
					px = xx
					py = yy
					pz = zz
					xx = 0
					yy = 0
					zz = 0
				}
			}
			time.Sleep(pause)
		}
	}
}
Esempio n. 2
0
/**
* Setup the sensors
**/
func setup() (gyroscope *sensors.L3GD20, accelerometer *sensors.LSM303ACCEL, magnetometer *sensors.LSM303MAG, err error) {
	fmt.Printf("Setup...\n")
	gyroscope, err = sensors.NewL3GD20()
	if err != nil {
		fmt.Printf("Error: getting device L3GD20, err=%v\n", err)
	} else {
		accelerometer, err = sensors.NewLSM303ACCEL()
		if err != nil {
			fmt.Printf("Error: getting device LSM303ACCEL, err=%v\n", err)
		} else {
			magnetometer, err = sensors.NewLSM303MAG()
			if err != nil {
				fmt.Printf("Error: getting device LSM303MAG, err=%v\n", err)
				//} else {
				//	err = calibrate(gyroscope, accelerometer)
				//	if err != nil {
				//		fmt.Printf("Error: %v\n", err)
				//	}
			}
		}
	}
	return
}
Esempio n. 3
0
/**
* This test program reads data from the three sensors
* gyroscope, accelerometer, and magnetometer, runs it
* through the filter, and prints the computed
* Yaw, Pitch, and Roll every 100 iterations.
* Each iteration takes approximately 3 milliseconds
* on a RaspberryPi model B.
**/
func main() {
	const (
		d2r = math.Pi / 180.0 // Used to convert degrees to radians
		r2d = 180.0 / math.Pi // Used to convert radians to degrees
	)
	var (
		gyroscope     *sensors.L3GD20      = nil
		accelerometer *sensors.LSM303ACCEL = nil
		magnetometer  *sensors.LSM303MAG   = nil
		imu           *imus.ImuMayhony     = nil

		err              error
		dbgPrint         bool
		gx, gy, gz       float32 // Gyroscope rate in degrees
		ax, ay, az       float32 // Accelerometer data
		mx, my, mz       float32 // Magnetometer data
		yaw, pitch, roll float32 // IMU results in radians
	)

	gyroscope, err = sensors.NewL3GD20()
	if err != nil {
		fmt.Printf("Error: getting device L3GD20, err=%v\n", err)
		return
	}

	accelerometer, err = sensors.NewLSM303ACCEL()
	if err != nil {
		fmt.Printf("Error: getting device LSM303ACCEL, err=%v\n", err)
		return
	}

	magnetometer, err = sensors.NewLSM303MAG()
	if err != nil {
		fmt.Printf("Error: getting device LSM303MAG, err=%v\n", err)
		return
	}

	imu = imus.NewImuMayhony()

	maxIterations := 10000
	for {
		startTime := time.Now().UnixNano()
		for i := 0; i < maxIterations; i++ {
			dbgPrint = (i % 100) == 0
			now := time.Now().UnixNano()
			gx, gy, gz, err = gyroscope.ReadXYZ()
			if err == nil {
				ax, ay, az, err = accelerometer.ReadXYZ()
				if err == nil {
					mx, my, mz, err = magnetometer.ReadXYZ()
					if err == nil {
						yaw, pitch, roll = imu.Update(now, gx*d2r, gy*d2r, gz*d2r, ax, ay, az, mx, my, mz)
						if dbgPrint {
							fmt.Printf("  YPR(%10.5f, %10.5f, %10.5f)\n", yaw*r2d, pitch*r2d, roll*r2d)
						}
					} else {
						fmt.Printf("Error: reading magnetometer, err=%v\n", err)
					}
				} else {
					fmt.Printf("Error: reading accelerometer, err=%v\n", err)
				}
			} else {
				fmt.Printf("Error: reading gyroscope, err=%v\n", err)
			}
		}
		finishTime := time.Now().UnixNano()
		delta := finishTime - startTime
		fmt.Printf("%4.2f milliseconds per iteration\n", (float64(delta)/float64(maxIterations))/1000000.0)
	}
}