<commit_before>// +build arm

package main

import (
	"encoding/json"
	"github.com/westphae/goflying/mpu9250"
	"log"
	"time"
	"fmt"
)

type MPU9250Listener struct {
	r	*room
	mpu 	*mpu9250.MPU9250
	data	*AHRSData
}

func (ml *MPU9250Listener) SetRoom(r *room) {
	ml.r = r
}

const (
	gyroRange = 250
	accelRange = 4
	updateFreq = 100
	MPURETRIES = 10
)

func (ml *MPU9250Listener) Init() {
	var err error

	for i:=0; i<MPURETRIES; i++ {
		mpu, err := mpu9250.NewMPU9250(gyroRange, accelRange, updateFreq, false)
		if err != nil {
			fmt.Printf("Error initializing MPU9250, attempt %d of %d\n", i, MPURETRIES)
			time.Sleep(5 * time.Second)
		} else {
			ml.mpu = mpu
			break
		}
	}

	if err != nil {
		fmt.Println("Error: couldn't initialize MPU9250")
		return
	}

	if err := ml.mpu.CalibrateGyro(1); err != nil {
		fmt.Println(err.Error())
		return
	}

	ml.data = new(AHRSData)
	time.Sleep(100 * time.Millisecond)
}

func (ml *MPU9250Listener) Close() {
	ml.mpu.CloseMPU()
}

func (ml *MPU9250Listener) GetData() *AHRSData {
	return ml.data
}

func (ml *MPU9250Listener) update() {
	Ts, Gx, Gy, Gz, Ax, Ay, Az, Mx, My, Mz, gaError, magError := ml.mpu.Read()

	if gaError == nil {
		ml.data.Ts = Ts
		ml.data.Gx, ml.data.Gy, ml.data.Gz = Gx, Gy, Gz
		ml.data.Ax, ml.data.Ay, ml.data.Az = Ax, Ay, Az

		// Quick and dirty calcs
		ml.data.Pitch += 0.1 * ml.data.Gy
		ml.data.Roll += 0.1 * ml.data.Gx
		ml.data.Heading += 0.1 * ml.data.Gz

		ml.data.X_accel = ml.data.Ax
		ml.data.Y_accel = ml.data.Ay
		ml.data.Z_accel = ml.data.Az
	}

	if magError == nil {
		ml.data.Tsm = Ts
		ml.data.Mx, ml.data.My, ml.data.Mz = Mx, My, Mz

		ml.data.X_mag = ml.data.Mx
		ml.data.Y_mag = ml.data.My
		ml.data.Z_mag = ml.data.Mz
	}
}

func (ml *MPU9250Listener) Run() {
	clock := time.NewTicker(100 * time.Millisecond)

	for {
		<-clock.C

		ml.update()

		data := ml.GetData()
		msg, err := json.Marshal(data)
		if err != nil {
			log.Println("Error marshalling json data: ", err.Error())
			log.Println(data)
			continue
		}

		log.Println("Sending IMUData to room")
		ml.r.forward <- msg
	}
}<commit_msg>ahrsweb mpu9250 listener works with channels<commit_after>// +build arm

package main

import (
	"encoding/json"
	"github.com/westphae/goflying/mpu9250"
	"log"
	"time"
	"fmt"
	"os"
)

type MPU9250Listener struct {
	r	*room
	mpu 	*mpu9250.MPU9250
	data	*AHRSData
}

func (ml *MPU9250Listener) SetRoom(r *room) {
	ml.r = r
}

const (
	gyroRange = 250
	accelRange = 4
	updateFreq = 100
	MPURETRIES = 10
)

func (ml *MPU9250Listener) Init() {
	var err error

	for i:=0; i<MPURETRIES; i++ {
		mpu, err := mpu9250.NewMPU9250(gyroRange, accelRange, updateFreq, true, false)
		if err != nil {
			fmt.Printf("Error initializing MPU9250, attempt %d of %d\n", i, MPURETRIES)
			time.Sleep(5 * time.Second)
		} else {
			mpu.CCal <- 2
			err = <-mpu.CCalResult
			if err != nil {
				fmt.Printf("Error calibrating try %d of %d\n", i, MPURETRIES)
				time.Sleep(100 * time.Millisecond)
			} else {
				ml.mpu = mpu
				break
			}
		}
	}

	if err != nil {
		fmt.Println("Error: couldn't initialize MPU9250")
		os.Exit(1)
	}

	for i:=0; i<MPURETRIES; i++ {
	}

	if err != nil {
		fmt.Println("Error: couldn't calibrate MPU9250")
		os.Exit(2)
	}

	ml.data = new(AHRSData)
	time.Sleep(250 * time.Millisecond)
}

func (ml *MPU9250Listener) Close() {
	ml.mpu.CloseMPU()
}

func (ml *MPU9250Listener) GetData() *AHRSData {
	return ml.data
}

func (ml *MPU9250Listener) update() {
	data := <-ml.mpu.CAvg

	if data.GAError == nil && data.N > 0 {
		ml.data.Ts = data.T.UnixNano()
		ml.data.Gx, ml.data.Gy, ml.data.Gz = data.G1, data.G2, data.G3
		ml.data.Ax, ml.data.Ay, ml.data.Az = data.A1, data.A2, data.A3

		// Quick and dirty calcs
		ml.data.Pitch += 0.1 * ml.data.Gx
		ml.data.Roll += 0.1 * ml.data.Gy
		ml.data.Heading -= 0.1 * ml.data.Gz

		ml.data.X_accel = ml.data.Ax
		ml.data.Y_accel = ml.data.Ay
		ml.data.Z_accel = ml.data.Az
	}

	if data.MagError == nil && data.NM > 0 {
		ml.data.Tsm = data.TM.UnixNano()
		ml.data.Mx, ml.data.My, ml.data.Mz = data.M1, data.M2, data.M3

		ml.data.X_mag = ml.data.Mx
		ml.data.Y_mag = ml.data.My
		ml.data.Z_mag = ml.data.Mz
	}
	if ml.data.Az < 0.2 {
		os.Exit(0)
	}
}

func (ml *MPU9250Listener) Run() {
	clock := time.NewTicker(100 * time.Millisecond)

	for {
		<-clock.C

		ml.update()

		data := ml.GetData()
		msg, err := json.Marshal(data)
		if err != nil {
			log.Println("Error marshalling json data: ", err.Error())
			log.Println(data)
			continue
		}

		log.Println("Sending IMUData to room")
		ml.r.forward <- msg
	}
}
{'fraction_non_alphanumeric': 0.16245328643657947, 'fraction_numerical': 0.04132776434381183, 'mean_word_length': 2.9393939393939394, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5262643', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2045, 'n_words': 433}