Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxSpeedScaled() and AverageSpeedScaled() both return Nan #395

Closed
Oupsman opened this issue Sep 2, 2024 · 3 comments
Closed

MaxSpeedScaled() and AverageSpeedScaled() both return Nan #395

Oupsman opened this issue Sep 2, 2024 · 3 comments

Comments

@Oupsman
Copy link

Oupsman commented Sep 2, 2024

Hi guys,

I'm working on an activity tracker and I have an issue with this module. I've tried with files coming from differents bikes computers and from a Fenix 7X and I have the issue every time.

Here is my code :

package main

import (
	"fmt"
	"github.com/muktihari/fit/decoder"
	"github.com/muktihari/fit/profile/filedef"
	"os"
)

func main() {
	// filePath := "FIT/Karoo-Morning_Ride-Sep-06-2022-061544.fit"
	filePath := "2024-04-29-18-28-29.fit"
	f, err := os.Open(filePath)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	dec := decoder.New(f)
	// Read the fit file

	for dec.Next() {
		fit, err := dec.Decode()
		if err != nil {
			panic(err)
		}
		activity := filedef.NewActivity(fit.Messages...)

		fmt.Printf("File Type: %s\n", activity.FileId.Type)
		fmt.Printf("Average Speed: %f\n", activity.Sessions[0].AvgSpeedScaled())
		fmt.Printf("Max Speed: %f\n", activity.Sessions[0].MaxSpeedScaled())

	}

}

and the result :

oupsman@Surface:~/IdeaProjects/mysportwebgo/prototypes$ /usr/local/go/bin/go run read_fit.go 
File Type: activity
Average Speed: NaN
Max Speed: NaN

what am I doing wrong ?

thanks for you answer.

@muktihari
Copy link
Owner

Hi @Oupsman,

A message may contains only few fields and the rest of the fields will be set to its corresponding invalid value. It's up to the creator of the FIT file what's to include in the message. When the field is invalid, in this case AvgSpeed, AvgSpeedScaled() will return NaN value to indicate that the field AvgSpeed is invalid. The best practice is to check the validity of the value first before processing the field:

if activity.Sessions[0].AvgSpeed != basetype.Uint16Invalid {}
// or
if !math.IsNaN(activity.Sessions[0].AvgSpeedScaled()) {}

For AvgSpeed and MaxSpeed, some manufactures may prefer using EnhancedAvgSpeed and EnhancedMaxSpeed fields. We need to check those value as well.

@muktihari
Copy link
Owner

Another suggestion, you can use fitconv or fitprint CLI to help you debug what's in the file since you might be working with FIT files created from different manufacturers. Some manufacturers may prefer using some fields over the others.

@Oupsman
Copy link
Author

Oupsman commented Sep 3, 2024

Thanks, I did not thought of checking if Enhanced values were available.

The worst part is that I already check in my code if I have to use SpeedScaled() or EnhancedSpeedScaled() to get the speed in a record. My bad.

@Oupsman Oupsman closed this as completed Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants