Skip to content

Commit

Permalink
holystone: some needed changes for proper driver
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 19, 2017
1 parent 6bd8280 commit 54fad37
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 18 deletions.
7 changes: 7 additions & 0 deletions platforms/holystone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Holystone

This package contains the Gobot drivers for the various Holystone (http://www.holystone.com/) drones.

This package currently supports the following drones:
- [Holystone HS200](http://www.holystone.com/product/Holy_Stone_HS200W_FPV_Drone_with_720P_HD_Live_Video_Wifi_Camera_2_4GHz_4CH_6_Axis_Gyro_RC_Quadcopter_with_Altitude_Hold,_Gravity_Sensor_and_Headless_Mode_Function_RTF,_Color_Red-39.html)

6 changes: 6 additions & 0 deletions platforms/holystone/holystone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Package holystone contains the Gobot drivers for the Holystone drones.
// Currently only have support for the HS200 drone.
// For more information, go to:
// http://www.holystone.com/product/Holy_Stone_HS200W_FPV_Drone_with_720P_HD_Live_Video_Wifi_Camera_2_4GHz_4CH_6_Axis_Gyro_RC_Quadcopter_with_Altitude_Hold,_Gravity_Sensor_and_Headless_Mode_Function_RTF,_Color_Red-39.html
//
package holystone // import "gobot.io/x/gobot/platforms/holystone"
11 changes: 10 additions & 1 deletion platforms/holystone/hs200/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Holystone HS200

## How to Install

```
go get -d -u gobot.io/x/gobot/...
```

## How to Use
- Connect to the drone's Wi-Fi network and identify the drone/gateway IP address.
- Use that IP address when you create a new driver.
- Some drones appear to use a different TCP port (8080 vs. 8888?). If the example doesn't work scan the drone for open ports or modify the driver not to use TCP.

Here is a sample of how you initialize and use the driver:

```go
package main

Expand Down Expand Up @@ -59,4 +68,4 @@ https://github.com/lancecaraccioli/holystone-hs110w
`26 e1 07 00 00 07 00 00 00 10 00 00 00 00 00 00 00 14 00 00 00 0e 00 00 00 03 00 00 00`
- The doesn't seem to be any telemetry coming out of the drone besides the video feed.
- The drone can sometimes be a little flaky. Ensure you've got a fully charged battery, minimal Wi-Fi interference, various connectors on the drone all well seated.
- It's not clear whether the drone's remote uses Wi-Fi or not, possibly Wi-Fi is only for the mobile app.
- It's not clear whether the drone's remote uses Wi-Fi or not, possibly Wi-Fi is only for the mobile app.
2 changes: 2 additions & 0 deletions platforms/holystone/hs200/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package hs200 is the Gobot driver for the Holystone HS200 drone.
package hs200 // import "gobot.io/x/gobot/platforms/holystone/hs200"
68 changes: 51 additions & 17 deletions platforms/holystone/hs200/hs200-driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@ import (
"net"
"sync"
"time"

"gobot.io/x/gobot"
)

// Driver reperesents the control information for the hs200 drone
type Driver struct {
mutex sync.RWMutex // Protect the command from concurrent access
stopc chan struct{} // Stop the flight loop goroutine
cmd []byte // the UDP command packet we keep sending the drone
enabled bool // Are we in an enabled state
udpconn net.Conn // UDP connection to the drone
tcpconn net.Conn // TCP connection to the drone
name string
mutex *sync.RWMutex // Protect the command from concurrent access
stopc chan struct{} // Stop the flight loop goroutine
cmd []byte // the UDP command packet we keep sending the drone
enabled bool // Are we in an enabled state
tcpaddress string
udpaddress string
udpconn net.Conn // UDP connection to the drone
tcpconn net.Conn // TCP connection to the drone
}

// NewDriver creates a driver for the HolyStone hs200
func NewDriver(tcpaddress string, udpaddress string) (*Driver, error) {
tc, terr := net.Dial("tcp", tcpaddress)
if terr != nil {
return nil, terr
}
uc, uerr := net.Dial("udp4", udpaddress)
if uerr != nil {
return nil, uerr
}

func NewDriver(tcpaddress string, udpaddress string) *Driver {
command := []byte{
0xff, // 2 byte header
0x04,
Expand All @@ -49,7 +45,40 @@ func NewDriver(tcpaddress string, udpaddress string) (*Driver, error) {
}
command[10] = checksum(command)

return &Driver{stopc: make(chan struct{}), cmd: command, udpconn: uc, tcpconn: tc}, nil
return &Driver{name: gobot.DefaultName("HS200"), stopc: make(chan struct{}),
tcpaddress: tcpaddress, udpaddress: udpaddress, cmd: command, mutex: &sync.RWMutex{}}
}

// Name returns the name of the device.
func (d *Driver) Name() string { return d.name }

// SetName sets the name of the device.
func (d *Driver) SetName(n string) { d.name = n }

// Connection returns the Connection of the device.
func (d *Driver) Connection() gobot.Connection { return nil }

// Start starts the driver.
func (d *Driver) Start() (err error) {
tc, terr := net.Dial("tcp", d.tcpaddress)
if terr != nil {
return terr
}
uc, uerr := net.Dial("udp4", d.udpaddress)
if uerr != nil {
return uerr
}

d.udpconn = uc
d.tcpconn = tc

return
}

// Halt stops the driver.
func (d *Driver) Halt() (err error) {
d.stop()
return
}

func (d *Driver) stop() {
Expand Down Expand Up @@ -83,12 +112,14 @@ func checksum(c []byte) byte {
}
return 255 - sum
}

func (d *Driver) sendUDP() {
d.mutex.RLock()
defer d.mutex.RUnlock()
d.udpconn.Write(d.cmd)
}

// Enable enables the drone to start flying.
func (d Driver) Enable() {
d.mutex.Lock()
defer d.mutex.Unlock()
Expand All @@ -98,6 +129,7 @@ func (d Driver) Enable() {
}
}

// Disable disables the drone from flying.
func (d Driver) Disable() {
d.mutex.Lock()
defer d.mutex.Unlock()
Expand All @@ -106,6 +138,7 @@ func (d Driver) Disable() {
}
}

// TakeOff tells drones to liftoff and start flying.
func (d Driver) TakeOff() {
d.mutex.Lock()
d.cmd[9] = 0x40
Expand All @@ -118,6 +151,7 @@ func (d Driver) TakeOff() {
d.mutex.Unlock()
}

// Land tells drone to come in for landing.
func (d Driver) Land() {
d.mutex.Lock()
d.cmd[9] = 0x80
Expand Down
17 changes: 17 additions & 0 deletions platforms/holystone/hs200/hs200_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hs200

import (
"testing"

"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)

var _ gobot.Driver = (*Driver)(nil)

func TestHS200Driver(t *testing.T) {
d := NewDriver("127.0.0.1:8080", "127.0.0.1:9090")

gobottest.Assert(t, d.tcpaddress, "127.0.0.1:8080")
gobottest.Assert(t, d.udpaddress, "127.0.0.1:9090")
}

0 comments on commit 54fad37

Please sign in to comment.