From 6f900a8dc2d0b6ff7b4ace86f8600c7041d1b4a6 Mon Sep 17 00:00:00 2001 From: Sam Kass Date: Wed, 26 Dec 2018 00:09:09 +0100 Subject: [PATCH] Update gobot leap platform to support Leap Motion API v6 --- platforms/leap/leap_motion_adaptor.go | 2 +- platforms/leap/leap_motion_driver.go | 29 +- platforms/leap/leap_motion_driver_test.go | 8 +- platforms/leap/parser.go | 55 +- .../leap/test/support/example_frame.json | 853 ++++++++++++++---- 5 files changed, 750 insertions(+), 197 deletions(-) diff --git a/platforms/leap/leap_motion_adaptor.go b/platforms/leap/leap_motion_adaptor.go index 37882a10b..98f561f67 100644 --- a/platforms/leap/leap_motion_adaptor.go +++ b/platforms/leap/leap_motion_adaptor.go @@ -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) }, } } diff --git a/platforms/leap/leap_motion_driver.go b/platforms/leap/leap_motion_driver.go index f3cc14fa7..ce4f1958b 100644 --- a/platforms/leap/leap_motion_driver.go +++ b/platforms/leap/leap_motion_driver.go @@ -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"), @@ -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. // @@ -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() { diff --git a/platforms/leap/leap_motion_driver_test.go b/platforms/leap/leap_motion_driver_test.go index 8d444c033..2ac83609e 100644 --- a/platforms/leap/leap_motion_driver_test.go +++ b/platforms/leap/leap_motion_driver_test.go @@ -89,8 +89,8 @@ 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) } diff --git a/platforms/leap/parser.go b/platforms/leap/parser.go index 296da7b29..56f8cc53e 100644 --- a/platforms/leap/parser.go +++ b/platforms/leap/parser.go @@ -4,27 +4,37 @@ 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"` @@ -32,26 +42,37 @@ type Hand struct { 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"` } diff --git a/platforms/leap/test/support/example_frame.json b/platforms/leap/test/support/example_frame.json index 1509d5693..6ef648faa 100644 --- a/platforms/leap/test/support/example_frame.json +++ b/platforms/leap/test/support/example_frame.json @@ -1,277 +1,796 @@ { - "currentFrameRate": 115.473, + "currentFrameRate": 110.583, + "devices": [], "gestures": [ { "direction": [ - -0.647384, - 0.750476, - -0.132964 + 0.721478, + -0.510801, + 0.467495 ], - "duration": 0, + "duration": 108349, "handIds": [ - 57 + 129 ], - "id": 72, + "id": 1, "pointableIds": [ - 14 + 1290 ], "position": [ - 117.665, - 313.471, - 27.2095 - ], - "speed": 1050.66, - "startPosition": [ - 195.438, - 223.313, - 43.183 - ], - "state": "start", - "type": "swipe" + 228.679, + 214.101, + 101.574 + ], + "progress": 1.00000, + "state": "stop", + "type": "keyTap" } ], "hands": [ { + "armBasis": [ + [ + 0.642307, + 0.418027, + 0.642414 + ], + [ + -0.760890, + 0.448533, + 0.468898 + ], + [ + -0.0921319, + -0.789982, + 0.606168 + ] + ], + "armWidth": 62.2635, + "confidence": 0.152371, "direction": [ - 0.772435, - 0.520335, - -0.364136 + 0.221251, + -0.0617914, + -0.973257 ], - "id": 57, + "elbow": [ + 212.001, + 79.2408, + 343.710 + ], + "grabStrength": 0.00000, + "id": 129, "palmNormal": [ - -0.0100593, - -0.563263, - -0.826217 + 0.813532, + -0.538649, + 0.219139 ], "palmPosition": [ - 117.546, - 236.007, - 76.3394 + 247.410, + 275.868, + 132.843 ], "palmVelocity": [ - -866.196, - -100.749, - 275.692 + -0.713996, + -23.6569, + -13.8691 ], + "palmWidth": 88.9102, + "pinchStrength": 0.00000, "r": [ [ - 0.999844, - 0.0142022, - 0.0105289 + 0.192921, + 0.960078, + -0.202565 ], [ - -0.0141201, - 0.99987, - -0.00783186 + -0.745728, + 0.00929180, + -0.666186 ], [ - -0.0106388, - 0.00768197, - 0.999914 + -0.637708, + 0.279579, + 0.717750 ] ], - "s": 0.992511, + "s": 1.30419, "sphereCenter": [ - 156.775, - 227.378, - 48.3453 + 287.160, + 297.142, + 100.094 ], - "sphereRadius": 75.3216, + "sphereRadius": 67.0665, "stabilizedPalmPosition": [ - 119.009, - 236.071, - 75.951 + 247.480, + 276.296, + 133.123 ], "t": [ - -38.0468, - 28.2341, - -21.3291 - ], - "timeVisible": 0.051952 + 61.7977, + 116.515, + 56.4642 + ], + "timeVisible": 1.87085, + "type": "right", + "wrist": [ + 236.308, + 287.660, + 183.786 + ] } ], - "id": 99943, + "id": 641123, "interactionBox": { "center": [ - 0, - 200, - 0 + 0.00000, + 189.367, + 0.00000 ], "size": [ - 221.418, - 221.418, - 154.742 + 222.740, + 222.740, + 139.896 ] }, "pointables": [ { + "bases": [ + [ + [ + -0.468069, + 0.807844, + -0.358190 + ], + [ + -0.882976, + -0.411231, + 0.226369 + ], + [ + 0.0355723, + 0.422230, + 0.905791 + ] + ], + [ + [ + -0.466451, + 0.822774, + -0.324757 + ], + [ + -0.870335, + -0.361355, + 0.334573 + ], + [ + 0.157925, + 0.438709, + 0.884643 + ] + ], + [ + [ + -0.466451, + 0.822774, + -0.324757 + ], + [ + -0.884402, + -0.440445, + 0.154403 + ], + [ + -0.0159988, + 0.359238, + 0.933109 + ] + ], + [ + [ + -0.466451, + 0.822774, + -0.324757 + ], + [ + -0.817784, + -0.261199, + 0.512840 + ], + [ + 0.337125, + 0.504796, + 0.794687 + ] + ] + ], + "btipPosition": [ + 214.293, + 213.865, + 95.0224 + ], + "carpPosition": [ + 229.172, + 258.776, + 187.433 + ], + "dipPosition": [ + 222.002, + 225.409, + 113.196 + ], "direction": [ - 0.54044, - 0.174084, - -0.823176 + 0.0159988, + -0.359238, + -0.933109 + ], + "extended": true, + "handId": 129, + "id": 1290, + "length": 50.9251, + "mcpPosition": [ + 229.172, + 258.776, + 187.433 + ], + "pipPosition": [ + 221.469, + 237.377, + 144.284 ], - "handId": 57, - "id": 1, - "length": 48.393, "stabilizedTipPosition": [ - 194.714, - 291.812, - 20.6219 + 220.428, + 215.841, + 99.4813 ], - "timeVisible": 0.13873, + "timeVisible": 1.87085, "tipPosition": [ - 194.714, - 291.812, - 20.6219 + 212.427, + 218.656, + 99.7341 ], "tipVelocity": [ - -716.414, - 686.468, - -427.914 + -324.525, + 55.5317, + -41.1663 ], "tool": false, - "touchDistance": 0.333333, - "touchZone": "hovering" + "touchDistance": 0.341002, + "touchZone": "hovering", + "type": 0, + "width": 19.7871 }, { + "bases": [ + [ + [ + 0.654255, + 0.750454, + 0.0936476 + ], + [ + -0.683587, + 0.639792, + -0.351248 + ], + [ + -0.323510, + 0.165789, + 0.931588 + ] + ], + [ + [ + 0.641810, + 0.756085, + 0.128125 + ], + [ + -0.742571, + 0.654463, + -0.142363 + ], + [ + -0.191492, + -0.00377221, + 0.981487 + ] + ], + [ + [ + 0.641810, + 0.756085, + 0.128125 + ], + [ + -0.726482, + 0.652969, + -0.214139 + ], + [ + -0.245569, + 0.0443555, + 0.968364 + ] + ], + [ + [ + 0.641810, + 0.756085, + 0.128125 + ], + [ + -0.709564, + 0.648876, + -0.274734 + ], + [ + -0.290860, + 0.0854138, + 0.952946 + ] + ] + ], + "btipPosition": [ + 269.006, + 271.529, + 17.2009 + ], + "carpPosition": [ + 225.768, + 285.455, + 171.343 + ], + "dipPosition": [ + 263.665, + 273.097, + 34.6993 + ], "direction": [ - 0.655715, - 0.423222, - -0.625237 + 0.245569, + -0.0443555, + -0.968364 + ], + "extended": true, + "handId": 129, + "id": 1292, + "length": 65.4748, + "mcpPosition": [ + 247.823, + 274.152, + 107.833 + ], + "pipPosition": [ + 256.842, + 274.330, + 61.6066 ], - "handId": 57, - "id": 69, - "length": 43.4594, "stabilizedTipPosition": [ - 166.231, - 308.631, - 21.1697 + 268.409, + 272.318, + 21.5696 ], - "timeVisible": 0.156012, + "timeVisible": 1.87085, "tipPosition": [ - 166.231, - 308.631, - 21.1697 + 268.718, + 271.794, + 21.5136 ], "tipVelocity": [ - -817.678, - 511.988, - -496.456 + -13.9732, + 6.53920, + -19.5770 ], "tool": false, - "touchDistance": 0.333333, - "touchZone": "hovering" + "touchDistance": 0.332816, + "touchZone": "hovering", + "type": 2, + "width": 18.5630 }, { + "bases": [ + [ + [ + 0.649174, + 0.716291, + 0.255928 + ], + [ + -0.645061, + 0.696732, + -0.313783 + ], + [ + -0.403074, + 0.0386109, + 0.914353 + ] + ], + [ + [ + 0.610655, + 0.716881, + 0.336426 + ], + [ + -0.767227, + 0.640801, + 0.0271437 + ], + [ + -0.196123, + -0.274690, + 0.941319 + ] + ], + [ + [ + 0.610655, + 0.716881, + 0.336426 + ], + [ + -0.728617, + 0.675044, + -0.115904 + ], + [ + -0.310191, + -0.174348, + 0.934550 + ] + ], + [ + [ + 0.610655, + 0.716881, + 0.336426 + ], + [ + -0.679596, + 0.692496, + -0.242072 + ], + [ + -0.406510, + -0.0808113, + 0.910065 + ] + ] + ], + "btipPosition": [ + 282.293, + 310.673, + 32.4311 + ], + "carpPosition": [ + 233.241, + 294.849, + 171.406 + ], + "dipPosition": [ + 274.872, + 309.198, + 49.0461 + ], "direction": [ - 0.593251, - 0.566682, - -0.571773 + 0.310191, + 0.174348, + -0.934550 + ], + "extended": true, + "handId": 129, + "id": 1293, + "length": 62.9558, + "mcpPosition": [ + 257.913, + 292.486, + 115.440 + ], + "pipPosition": [ + 266.475, + 304.478, + 74.3433 ], - "handId": 57, - "id": 14, - "length": 47.3576, "stabilizedTipPosition": [ - 119.328, - 312.537, - 27.7416 + 281.546, + 310.434, + 36.7157 ], - "timeVisible": 0.164757, + "timeVisible": 1.87085, "tipPosition": [ - 117.665, - 313.471, - 27.2095 + 281.024, + 309.986, + 36.2968 ], "tipVelocity": [ - -779.297, - 651.055, - -269.665 + -49.1356, + 23.1718, + -14.1464 ], "tool": false, - "touchDistance": 0.333333, - "touchZone": "hovering" + "touchDistance": 0.332567, + "touchZone": "hovering", + "type": 3, + "width": 17.6638 }, { + "bases": [ + [ + [ + 0.695759, + 0.590913, + 0.408339 + ], + [ + -0.569298, + 0.800316, + -0.188135 + ], + [ + -0.437971, + -0.101569, + 0.893233 + ] + ], + [ + [ + 0.555629, + 0.543209, + 0.629445 + ], + [ + -0.762608, + 0.634559, + 0.125553 + ], + [ + -0.331219, + -0.549780, + 0.766835 + ] + ], + [ + [ + 0.555629, + 0.543209, + 0.629445 + ], + [ + -0.707544, + 0.706513, + 0.0148494 + ], + [ + -0.436645, + -0.453610, + 0.776903 + ] + ], + [ + [ + 0.555629, + 0.543209, + 0.629445 + ], + [ + -0.645129, + 0.759246, + -0.0857538 + ], + [ + -0.524486, + -0.358426, + 0.772299 + ] + ] + ], + "btipPosition": [ + 298.360, + 340.270, + 71.0701 + ], + "carpPosition": [ + 244.922, + 300.813, + 176.031 + ], + "dipPosition": [ + 289.526, + 334.233, + 84.0778 + ], "direction": [ - 0.292649, - -0.45091, - -0.84323 + 0.436645, + 0.453610, + -0.776904 + ], + "extended": true, + "handId": 129, + "id": 1294, + "length": 49.3562, + "mcpPosition": [ + 269.737, + 306.568, + 125.421 + ], + "pipPosition": [ + 281.181, + 325.564, + 98.9257 ], - "handId": 57, - "id": 29, - "length": 57.4251, "stabilizedTipPosition": [ - 223.535, - 239.849, - 37.7847 + 297.824, + 337.184, + 73.7178 ], - "timeVisible": 0.086657, + "timeVisible": 1.87085, "tipPosition": [ - 223.535, - 239.849, - 37.7847 + 295.463, + 338.639, + 73.5436 ], "tipVelocity": [ - -929.251, - 390.064, - -506.855 + -67.9258, + 44.5899, + -9.70583 ], "tool": false, - "touchDistance": 0.333333, - "touchZone": "hovering" + "touchDistance": 0.332281, + "touchZone": "hovering", + "type": 4, + "width": 15.6904 }, { + "bases": [ + [ + [ + 0.567299, + 0.817958, + -0.0954824 + ], + [ + -0.785572, + 0.502726, + -0.360755 + ], + [ + -0.247081, + 0.279664, + 0.927760 + ] + ], + [ + [ + 0.556741, + 0.828691, + -0.0575341 + ], + [ + -0.461401, + 0.250903, + -0.850974 + ], + [ + -0.690760, + 0.500319, + 0.522046 + ] + ], + [ + [ + 0.556741, + 0.828691, + -0.0575341 + ], + [ + 0.184936, + -0.191172, + -0.963977 + ], + [ + -0.809838, + 0.526046, + -0.259688 + ] + ], + [ + [ + 0.556741, + 0.828691, + -0.0575341 + ], + [ + 0.450238, + -0.359238, + -0.817456 + ], + [ + -0.698087, + 0.429207, + -0.573111 + ] + ] + ], + "btipPosition": [ + 297.722, + 214.346, + 100.704 + ], + "carpPosition": [ + 220.180, + 275.044, + 173.613 + ], + "dipPosition": [ + 286.067, + 221.511, + 91.1361 + ], "direction": [ - 0.887561, - 0.160925, - 0.43167 + 0.809838, + -0.526046, + 0.259688 + ], + "extended": false, + "handId": 129, + "id": 1291, + "length": 57.4633, + "mcpPosition": [ + 237.942, + 254.939, + 106.918 + ], + "pipPosition": [ + 266.941, + 233.936, + 85.0028 ], - "handId": -1, - "id": 83, - "length": 64.4149, "stabilizedTipPosition": [ - 1.8725087977383e-321, - 2.1433800000305e-314, - 2.1681499999593e-314 + 294.484, + 215.800, + 98.6516 ], - "timeVisible": 0.034634, + "timeVisible": 1.87085, "tipPosition": [ - 196.31, - 166.58, - 102.577 + 294.960, + 216.076, + 98.9796 ], "tipVelocity": [ - -478.099, - 493.551, - -417.63 + 77.7159, + 38.2101, + -21.6539 ], "tool": false, - "touchDistance": 0.0166667, - "touchZone": "none" + "touchDistance": 0.340420, + "touchZone": "none", + "type": 1, + "width": 18.9007 } ], "r": [ [ - -0.973344, - 0.0737047, - 0.217182 + 0.192921, + 0.960078, + -0.202565 ], [ - -0.0241526, - 0.908749, - -0.416645 + -0.745728, + 0.00929180, + -0.666186 ], [ - -0.228073, - -0.410784, - -0.882745 + -0.637708, + 0.279579, + 0.717750 ] ], - "s": -34.3848, + "s": 1.30419, "t": [ - -10091.8, - -24629.7, - 1011.04 + 61.7977, + 116.515, + 56.4642 ], - "timestamp": 4729292670 -} + "timestamp": 134211791358 +} \ No newline at end of file