1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-06 19:29:15 +08:00

bebop: add ValidatePitch helper function for Parrot Bebop to package

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-02-07 11:31:51 +01:00
parent 47a13159ef
commit 9d73a4bf6e
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package bebop
import "math"
// ValidatePitch helps validate pitch values such as those created by
// a joystick to values between 0-100 that are required as
// params to Parrot Bebop PCMDs
func ValidatePitch(data float64, offset float64) int {
value := math.Abs(data) / offset
if value >= 0.1 {
if value <= 1.0 {
return int((float64(int(value*100)) / 100) * 100)
}
return 100
}
return 0
}

View File

@ -0,0 +1,19 @@
package bebop
import (
"testing"
"gobot.io/x/gobot/gobottest"
)
func TestBebopValidatePitchWhenEqualOffset(t *testing.T) {
gobottest.Assert(t, ValidatePitch(32767.0, 32767.0), 100)
}
func TestBebopValidatePitchWhenTiny(t *testing.T) {
gobottest.Assert(t, ValidatePitch(1.1, 32767.0), 0)
}
func TestBebopValidatePitchWhenCentered(t *testing.T) {
gobottest.Assert(t, ValidatePitch(16383.5, 32767.0), 50)
}