mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-08 19:29:16 +08:00
bebop: add ValidatePitch helper function for Parrot Bebop to package
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
47a13159ef
commit
9d73a4bf6e
17
platforms/parrot/bebop/pitch.go
Normal file
17
platforms/parrot/bebop/pitch.go
Normal 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
|
||||||
|
}
|
19
platforms/parrot/bebop/pitch_test.go
Normal file
19
platforms/parrot/bebop/pitch_test.go
Normal 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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user