Skip to content

Commit

Permalink
docs: fix hybridgroup#363 by using atomic.Value to protect current va…
Browse files Browse the repository at this point in the history
…lues used by mutiple goroutines

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 7, 2017
1 parent 4b54045 commit 8730233
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 192 deletions.
108 changes: 60 additions & 48 deletions examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"math"
"sync/atomic"
"time"

"gobot.io/x/gobot"
Expand All @@ -14,6 +14,10 @@ type pair struct {
y float64
}

var leftX, leftY, rightX, rightY atomic.Value

const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
Expand All @@ -23,79 +27,84 @@ func main() {
ardroneAdaptor := ardrone.NewAdaptor()
drone := ardrone.NewDriver(ardroneAdaptor)

work := func() {
offset := 32767.0
rightStick := pair{x: 0, y: 0}
leftStick := pair{x: 0, y: 0}
leftX.Store(float64(0.0))
leftY.Store(float64(0.0))
rightX.Store(float64(0.0))
rightY.Store(float64(0.0))

work := func() {
stick.On(joystick.SquarePress, func(data interface{}) {
drone.TakeOff()
})

stick.On(joystick.TrianglePress, func(data interface{}) {
drone.Hover()
})

stick.On(joystick.XPress, func(data interface{}) {
drone.Land()
})

stick.On(joystick.LeftX, func(data interface{}) {
val := float64(data.(int16))
if leftStick.x != val {
leftStick.x = val
}
leftX.Store(val)
})

stick.On(joystick.LeftY, func(data interface{}) {
val := float64(data.(int16))
if leftStick.y != val {
leftStick.y = val
}
leftY.Store(val)
})

stick.On(joystick.RightX, func(data interface{}) {
val := float64(data.(int16))
if rightStick.x != val {
rightStick.x = val
}
rightX.Store(val)
})

stick.On(joystick.RightY, func(data interface{}) {
val := float64(data.(int16))
if rightStick.y != val {
rightStick.y = val
}
rightY.Store(val)
})

gobot.Every(10*time.Millisecond, func() {
pair := leftStick
if pair.y < -10 {
drone.Forward(validatePitch(pair.y, offset))
} else if pair.y > 10 {
drone.Backward(validatePitch(pair.y, offset))
} else {
leftStick := getLeftStick()

switch {
case leftStick.y < -10:
drone.Forward(ardrone.ValidatePitch(leftStick.y, offset))
case leftStick.y > 10:
drone.Backward(ardrone.ValidatePitch(leftStick.y, offset))
default:
drone.Forward(0)
}

if pair.x > 10 {
drone.Right(validatePitch(pair.x, offset))
} else if pair.x < -10 {
drone.Left(validatePitch(pair.x, offset))
} else {
switch {
case leftStick.x > 10:
drone.Right(ardrone.ValidatePitch(leftStick.x, offset))
case leftStick.x < -10:
drone.Left(ardrone.ValidatePitch(leftStick.x, offset))
default:
drone.Right(0)
}
})

gobot.Every(10*time.Millisecond, func() {
pair := rightStick
if pair.y < -10 {
drone.Up(validatePitch(pair.y, offset))
} else if pair.y > 10 {
drone.Down(validatePitch(pair.y, offset))
} else {
rightStick := getRightStick()

switch {
case rightStick.y < -10:
drone.Up(ardrone.ValidatePitch(rightStick.y, offset))
case rightStick.y > 10:
drone.Down(ardrone.ValidatePitch(rightStick.y, offset))
default:
drone.Up(0)
}

if pair.x > 20 {
drone.Clockwise(validatePitch(pair.x, offset))
} else if pair.x < -20 {
drone.CounterClockwise(validatePitch(pair.x, offset))
} else {
switch {
case rightStick.x > 20:
drone.Clockwise(ardrone.ValidatePitch(rightStick.x, offset))
case rightStick.x < -20:
drone.CounterClockwise(ardrone.ValidatePitch(rightStick.x, offset))
default:
drone.Clockwise(0)
}
})
Expand All @@ -110,13 +119,16 @@ func main() {
robot.Start()
}

func validatePitch(data float64, offset float64) float64 {
value := math.Abs(data) / offset
if value >= 0.1 {
if value <= 1.0 {
return float64(int(value*100)) / 100
}
return 1.0
}
return 0.0
func getLeftStick() pair {
s := pair{x: 0, y: 0}
s.x = leftX.Load().(float64)
s.y = leftY.Load().(float64)
return s
}

func getRightStick() pair {
s := pair{x: 0, y: 0}
s.x = rightX.Load().(float64)
s.y = rightY.Load().(float64)
return s
}
105 changes: 58 additions & 47 deletions examples/bebop_ps3.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"math"
"sync/atomic"
"time"

"gobot.io/x/gobot"
Expand All @@ -14,6 +14,10 @@ type pair struct {
y float64
}

var leftX, leftY, rightX, rightY atomic.Value

const offset = 32767.0

func main() {
joystickAdaptor := joystick.NewAdaptor()
stick := joystick.NewDriver(joystickAdaptor,
Expand All @@ -24,9 +28,10 @@ func main() {
drone := bebop.NewDriver(bebopAdaptor)

work := func() {
offset := 32767.0
rightStick := pair{x: 0, y: 0}
leftStick := pair{x: 0, y: 0}
leftX.Store(float64(0.0))
leftY.Store(float64(0.0))
rightX.Store(float64(0.0))
rightY.Store(float64(0.0))

recording := false

Expand All @@ -43,71 +48,74 @@ func main() {
drone.HullProtection(true)
drone.TakeOff()
})

stick.On(joystick.TrianglePress, func(data interface{}) {
drone.Stop()
})

stick.On(joystick.XPress, func(data interface{}) {
drone.Land()
})

stick.On(joystick.LeftX, func(data interface{}) {
val := float64(data.(int16))
if leftStick.x != val {
leftStick.x = val
}
leftX.Store(val)
})

stick.On(joystick.LeftY, func(data interface{}) {
val := float64(data.(int16))
if leftStick.y != val {
leftStick.y = val
}
leftY.Store(val)
})

stick.On(joystick.RightX, func(data interface{}) {
val := float64(data.(int16))
if rightStick.x != val {
rightStick.x = val
}
rightX.Store(val)
})

stick.On(joystick.RightY, func(data interface{}) {
val := float64(data.(int16))
if rightStick.y != val {
rightStick.y = val
}
rightY.Store(val)
})

gobot.Every(10*time.Millisecond, func() {
pair := leftStick
if pair.y < -10 {
drone.Forward(validatePitch(pair.y, offset))
} else if pair.y > 10 {
drone.Backward(validatePitch(pair.y, offset))
} else {
leftStick := getLeftStick()

switch {
case leftStick.y < -10:
drone.Forward(bebop.ValidatePitch(leftStick.y, offset))
case leftStick.y > 10:
drone.Backward(bebop.ValidatePitch(leftStick.y, offset))
default:
drone.Forward(0)
}

if pair.x > 10 {
drone.Right(validatePitch(pair.x, offset))
} else if pair.x < -10 {
drone.Left(validatePitch(pair.x, offset))
} else {
switch {
case leftStick.x > 10:
drone.Right(bebop.ValidatePitch(leftStick.x, offset))
case leftStick.x < -10:
drone.Left(bebop.ValidatePitch(leftStick.x, offset))
default:
drone.Right(0)
}
})

gobot.Every(10*time.Millisecond, func() {
pair := rightStick
if pair.y < -10 {
drone.Up(validatePitch(pair.y, offset))
} else if pair.y > 10 {
drone.Down(validatePitch(pair.y, offset))
} else {
rightStick := getRightStick()
switch {
case rightStick.y < -10:
drone.Up(bebop.ValidatePitch(rightStick.y, offset))
case rightStick.y > 10:
drone.Down(bebop.ValidatePitch(rightStick.y, offset))
default:
drone.Up(0)
}

if pair.x > 20 {
drone.Clockwise(validatePitch(pair.x, offset))
} else if pair.x < -20 {
drone.CounterClockwise(validatePitch(pair.x, offset))
} else {
switch {
case rightStick.x > 20:
drone.Clockwise(bebop.ValidatePitch(rightStick.x, offset))
case rightStick.x < -20:
drone.CounterClockwise(bebop.ValidatePitch(rightStick.x, offset))
default:
drone.Clockwise(0)
}
})
Expand All @@ -122,13 +130,16 @@ func main() {
robot.Start()
}

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
func getLeftStick() pair {
s := pair{x: 0, y: 0}
s.x = leftX.Load().(float64)
s.y = leftY.Load().(float64)
return s
}

func getRightStick() pair {
s := pair{x: 0, y: 0}
s.x = rightX.Load().(float64)
s.y = rightY.Load().(float64)
return s
}
Loading

0 comments on commit 8730233

Please sign in to comment.