Skip to content

Commit

Permalink
Merge pull request hybridgroup#647 from samkass/feature/leap-api-v6
Browse files Browse the repository at this point in the history
Update gobot leap platform to support Leap Motion API v6
  • Loading branch information
trevrosen authored Feb 13, 2019
2 parents 9ddd6c0 + b843702 commit 554dd04
Show file tree
Hide file tree
Showing 5 changed files with 760 additions and 197 deletions.
2 changes: 1 addition & 1 deletion platforms/leap/leap_motion_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewAdaptor(port string) *Adaptor {
name: gobot.DefaultName("LeapMotion"),
port: port,
connect: func(host string) (io.ReadWriteCloser, error) {
return websocket.Dial("ws://"+host+"/v3.json", "", "http://"+host)
return websocket.Dial("ws://"+host+"/v6.json", "", "http://"+host)
},
}
}
Expand Down
29 changes: 21 additions & 8 deletions platforms/leap/leap_motion_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Driver struct {
// Adds the following events:
// "message" - Gets triggered when receiving a message from leap motion
// "hand" - Gets triggered per-message when leap motion detects a hand
// "gesture" - Gets triggered per-message when leap motion detects a hand
// "gesture" - Gets triggered per-message when leap motion detects a gesture
func NewDriver(a *Adaptor) *Driver {
l := &Driver{
name: gobot.DefaultName("LeapMotion"),
Expand Down Expand Up @@ -61,6 +61,20 @@ func (l *Driver) adaptor() *Adaptor {
return l.Connection().(*Adaptor)
}

func enableFeature(l *Driver, feature string) (err error) {
command := map[string]bool{feature: true}
b, e := json.Marshal(command)
if e != nil {
return e
}
_, e = l.adaptor().ws.Write(b)
if e != nil {
return e
}

return nil
}

// Start inits leap motion driver by enabling gestures
// and listening from incoming messages.
//
Expand All @@ -69,14 +83,13 @@ func (l *Driver) adaptor() *Adaptor {
// "hand" - Emits Hand when detected in message from Leap.
// "gesture" - Emits Gesture when detected in message from Leap.
func (l *Driver) Start() (err error) {
enableGestures := map[string]bool{"enableGestures": true}
b, e := json.Marshal(enableGestures)
if e != nil {
return e
err = enableFeature(l,"enableGestures")
if err != nil {
return err
}
_, e = l.adaptor().ws.Write(b)
if e != nil {
return e
err = enableFeature(l,"background")
if err != nil {
return err
}

go func() {
Expand Down
18 changes: 14 additions & 4 deletions platforms/leap/leap_motion_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,18 @@ func TestLeapMotionDriverParser(t *testing.T) {
t.Errorf("ParseFrame incorrectly parsed frame")
}

gobottest.Assert(t, parsedFrame.Timestamp, uint64(4729292670))
gobottest.Assert(t, parsedFrame.Hands[0].X(), 117.546)
gobottest.Assert(t, parsedFrame.Hands[0].Y(), 236.007)
gobottest.Assert(t, parsedFrame.Hands[0].Z(), 76.3394)
gobottest.Assert(t, parsedFrame.Timestamp, uint64(134211791358))
gobottest.Assert(t, parsedFrame.Hands[0].X(), 247.410)
gobottest.Assert(t, parsedFrame.Hands[0].Y(), 275.868)
gobottest.Assert(t, parsedFrame.Hands[0].Z(), 132.843)

gobottest.Assert(t, parsedFrame.Pointables[0].BTipPosition[0], 214.293)
gobottest.Assert(t, parsedFrame.Pointables[0].BTipPosition[1], 213.865)
gobottest.Assert(t, parsedFrame.Pointables[0].BTipPosition[2], 95.0224)

gobottest.Assert(t, parsedFrame.Pointables[0].Bases[0][0][0], -0.468069)
gobottest.Assert(t, parsedFrame.Pointables[0].Bases[0][0][1], 0.807844)
gobottest.Assert(t, parsedFrame.Pointables[0].Bases[0][0][2], -0.358190)

gobottest.Assert(t, parsedFrame.Pointables[0].Width, 19.7871)
}
55 changes: 38 additions & 17 deletions platforms/leap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,75 @@ import (
"encoding/json"
)

// Gesture is a Leap Motion gesture tht has been detected
// Gesture is a Leap Motion gesture that has been detected
type Gesture struct {
Center []float64 `json:"center"`
Direction []float64 `json:"direction"`
Duration int `json:"duration"`
Hands []Hand `json:"hands"`
HandIDs []int `json:"handIds"`
ID int `json:"id"`
Pointables []Pointable `json:"pointables"`
Normal []float64 `json:"normal"`
PointableIDs []int `json:"pointableIds"`
Position []float64 `json:"position"`
Progress float64 `json:"progress"`
Radius float64 `json:"radius"`
Speed float64 `json:"speed"`
StartPosition []float64 `json:"StartPosition"`
State string `json:"state"`
Type string `json:"type"`
}

// Hand is a Leap Motion hand tht has been detected
// Hand is a Leap Motion hand that has been detected
type Hand struct {
ArmBasis [][]float64 `json:"armBasis"`
ArmWidth float64 `json:"armWidth"`
Confidence float64 `json:"confidence"`
Direction []float64 `json:"direction"`
Elbow []float64 `json:"elbow"`
GrabStrength float64 `json:"grabStrength"`
ID int `json:"id"`
PalmNormal []float64 `json:"palmNormal"`
PalmPosition []float64 `json:"PalmPosition"`
PalmVelocity []float64 `json:"PalmVelocity"`
PinchStrength float64 `json:"PinchStrength"`
R [][]float64 `json:"r"`
S float64 `json:"s"`
SphereCenter []float64 `json:"sphereCenter"`
SphereRadius float64 `json:"sphereRadius"`
StabilizedPalmPosition []float64 `json:"stabilizedPalmPosition"`
T []float64 `json:"t"`
TimeVisible float64 `json:"TimeVisible"`
Type string `json:"type"`
Wrist []float64 `json:"wrist"`
}

// Pointable is a Leap Motion pointing motion tht has been detected
// Pointable is a Leap Motion pointing motion that has been detected
type Pointable struct {
Direction []float64 `json:"direction"`
HandID int `json:"handId"`
ID int `json:"id"`
Length float64 `json:"length"`
StabilizedTipPosition []float64 `json:"stabilizedTipPosition"`
TimeVisible float64 `json:"timeVisible"`
TipPosition []float64 `json:"tipPosition"`
TipVelocity []float64 `json:"tipVelocity"`
Tool bool `json:"tool"`
TouchDistance float64 `json:"touchDistance"`
TouchZone string `json:"touchZone"`
Bases [][][]float64 `json:"bases"`
BTipPosition []float64 `json:"btipPosition"`
CarpPosition []float64 `json:"carpPosition"`
DipPosition []float64 `json:"dipPosition"`
Direction []float64 `json:"direction"`
Extended bool `json:"extended"`
HandID int `json:"handId"`
ID int `json:"id"`
Length float64 `json:"length"`
MCPPosition []float64 `json:"mcpPosition"`
PIPPosition []float64 `json:"pipPosition"`
StabilizedTipPosition []float64 `json:"stabilizedTipPosition"`
TimeVisible float64 `json:"timeVisible"`
TipPosition []float64 `json:"tipPosition"`
TipVelocity []float64 `json:"tipVelocity"`
Tool bool `json:"tool"`
TouchDistance float64 `json:"touchDistance"`
TouchZone string `json:"touchZone"`
Type int `json:"type"`
Width float64 `json:"width"`
}

// InteractionBox is the area within which the gestural interaction has been detected
type InteractionBox struct {
Center []int `json:"center"`
Center []float64 `json:"center"`
Size []float64 `json:"size"`
}

Expand Down
Loading

0 comments on commit 554dd04

Please sign in to comment.