예제 #1
0
/**
* Remotely control a quadcopter using a RaspberryPi as
* the on board computer, reading the sensors and computing
* how much power to apply to the motors. The current implementation
* uses three sensors (gyroscope, accelerometer, and magnetometer).
**/
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 (
		imu      *imus.ImuMayhony
		sData    io.SensorData
		cData    io.CmdData
		i        int // number of iterations in the for loop
		cnt      int // number of printf calls  (or ~seconds)
		now      int64
		lastTime int64 // last time a printf was called
		second   int64 // One second in nanoseconds
		yaw      float32
		pitch    float32
		roll     float32
	)

	imu = imus.NewImuMayhony()
	sensorChannel := make(chan io.SensorData)
	cmdChannel := make(chan io.CmdData)

	go io.ReadSensors(sensorChannel)

	go io.ReadCommands(cmdChannel)

	second = int64(time.Second)
	lastTime = time.Now().UnixNano()
	for {
		select {
		case sData = <-sensorChannel:
			yaw, pitch, roll = imu.Update(sData.When, sData.Gx*d2r, sData.Gy*d2r, sData.Gz*d2r, sData.Ax, sData.Ay, sData.Az, sData.Mx, sData.My, sData.Mz)
			i++
			now = time.Now().UnixNano()
			if (now - lastTime) >= second {
				cnt++
				fmt.Printf("%d YPR(%10.5f, %10.5f, %10.5f)\n", cnt, yaw*r2d, pitch*r2d, roll*r2d)
				lastTime = now
			}
		case cData = <-cmdChannel:
			fmt.Printf("%d CMD(%d, %d, %d, %d, %d, %d\n", cData.Yaw, cData.Pitch, cData.Roll, cData.Aux1, cData.Aux2)
		}
	}
}
예제 #2
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)
	}
}