tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/lsm9ds1/main.go (about)

     1  // LSM9DS1, 9 axis Inertial Measurement Unit (IMU)
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  	"machine"
     7  	"time"
     8  
     9  	"tinygo.org/x/drivers/lsm9ds1"
    10  )
    11  
    12  const (
    13  	PLOTTER             = false
    14  	SHOW_ACCELERATION   = true
    15  	SHOW_ROTATION       = true
    16  	SHOW_MAGNETIC_FIELD = true
    17  	SHOW_TEMPERATURE    = true
    18  )
    19  
    20  func main() {
    21  
    22  	// I2C configure
    23  	machine.I2C0.Configure(machine.I2CConfig{})
    24  
    25  	// LSM9DS1 setup
    26  	device := lsm9ds1.New(machine.I2C0)
    27  	err := device.Configure(lsm9ds1.Configuration{
    28  		AccelRange:      lsm9ds1.ACCEL_2G,
    29  		AccelSampleRate: lsm9ds1.ACCEL_SR_119,
    30  		GyroRange:       lsm9ds1.GYRO_250DPS,
    31  		GyroSampleRate:  lsm9ds1.GYRO_SR_119,
    32  		MagRange:        lsm9ds1.MAG_4G,
    33  		MagSampleRate:   lsm9ds1.MAG_SR_40,
    34  	})
    35  	if err != nil {
    36  		for {
    37  			println("Failed to configure", err.Error())
    38  			time.Sleep(time.Second)
    39  		}
    40  	}
    41  
    42  	for {
    43  
    44  		if !device.Connected() {
    45  			println("LSM9DS1 not connected")
    46  			time.Sleep(time.Second)
    47  			continue
    48  		}
    49  
    50  		ax, ay, az, _ := device.ReadAcceleration()
    51  		gx, gy, gz, _ := device.ReadRotation()
    52  		mx, my, mz, _ := device.ReadMagneticField()
    53  		t, _ := device.ReadTemperature()
    54  
    55  		if PLOTTER {
    56  			printPlotter(ax, ay, az, gx, gy, gz, mx, my, mz, t)
    57  			time.Sleep(time.Millisecond * 100)
    58  		} else {
    59  			printMonitor(ax, ay, az, gx, gy, gz, mx, my, mz, t)
    60  			time.Sleep(time.Millisecond * 1000)
    61  		}
    62  
    63  	}
    64  
    65  }
    66  
    67  // Arduino IDE's Serial Plotter
    68  func printPlotter(ax, ay, az, gx, gy, gz, mx, my, mz, t int32) {
    69  	if SHOW_ACCELERATION {
    70  		fmt.Printf("AX:%f, AY:%f, AZ:%f,", axis(ax), axis(ay), axis(az))
    71  	}
    72  	if SHOW_ROTATION {
    73  		fmt.Printf("GX:%f, GY:%f, GZ:%f,", axis(gx), axis(gy), axis(gz))
    74  	}
    75  	if SHOW_MAGNETIC_FIELD {
    76  		fmt.Printf("MX:%d, MY:%d, MZ:%d,", mx, my, mz)
    77  	}
    78  	if SHOW_TEMPERATURE {
    79  		fmt.Printf("T:%f", float32(t)/1000)
    80  	}
    81  	println()
    82  }
    83  
    84  // Any Serial Monitor
    85  func printMonitor(ax, ay, az, gx, gy, gz, mx, my, mz, t int32) {
    86  	if SHOW_ACCELERATION {
    87  		fmt.Printf("Acceleration (g): %f, %f, %f\r\n", axis(ax), axis(ay), axis(az))
    88  	}
    89  	if SHOW_ROTATION {
    90  		fmt.Printf("Rotation (dps): %f, %f, %f\r\n", axis(gx), axis(gy), axis(gz))
    91  	}
    92  	if SHOW_MAGNETIC_FIELD {
    93  		fmt.Printf("Magnetic field (nT): %d, %d, %d\r\n", mx, my, mz)
    94  	}
    95  	if SHOW_TEMPERATURE {
    96  		fmt.Printf("Temperature C: %f\r\n", float32(t)/1000)
    97  	}
    98  	println()
    99  }
   100  
   101  func axis(raw int32) float32 {
   102  	return float32(raw) / 1000000
   103  }