Skip to content

Commit 531a724

Browse files
committed
Fly baby, fly
1 parent d46243b commit 531a724

File tree

13 files changed

+333
-352
lines changed

13 files changed

+333
-352
lines changed

Diff for: cmd/godrone/main.go

+104-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,122 @@
11
package main
22

33
import (
4-
"github.com/felixge/godrone"
4+
"github.com/felixge/godrone/attitude"
5+
"github.com/felixge/godrone/control"
6+
"github.com/felixge/godrone/drivers/motorboard"
7+
"github.com/felixge/godrone/drivers/navboard"
58
"github.com/felixge/godrone/http"
9+
"github.com/felixge/log"
610
gohttp "net/http"
711
"os"
812
"os/signal"
913
"syscall"
14+
"time"
1015
)
1116

17+
type Config struct {
18+
NavboardTTY string
19+
MotorboardTTY string
20+
RollPID [3]float64
21+
PitchPID [3]float64
22+
YawPID [3]float64
23+
HttpAddr string
24+
}
25+
26+
var (
27+
defaultRollPitchPID = [3]float64{0.02, 0, 0}
28+
29+
green = motorboard.Leds(motorboard.LedGreen)
30+
orange = motorboard.Leds(motorboard.LedOrange)
31+
red = motorboard.Leds(motorboard.LedRed)
32+
)
33+
34+
var DefaultConfig = Config{
35+
NavboardTTY: "/dev/ttyO1",
36+
MotorboardTTY: "/dev/ttyO0",
37+
RollPID: defaultRollPitchPID,
38+
PitchPID: defaultRollPitchPID,
39+
YawPID: [3]float64{1, 0, 0},
40+
HttpAddr: ":80",
41+
}
42+
43+
type Instances struct {
44+
log *log.Logger
45+
navboard *navboard.Navboard
46+
motorboard *motorboard.Motorboard
47+
attitude *attitude.Complementary
48+
control *control.Control
49+
http gohttp.Handler
50+
}
51+
1252
func main() {
13-
log := godrone.DefaultConfig.Log
14-
firmware := godrone.DefaultFirmware
15-
if err := firmware.Start(); err != nil {
53+
config := DefaultConfig
54+
i, err := NewInstances(config)
55+
if err != nil {
1656
panic(err)
1757
}
18-
defer firmware.Stop()
58+
i.log.Info("Starting godrone")
59+
defer i.motorboard.Close()
1960

20-
h := http.NewHandler(firmware, log)
21-
go gohttp.ListenAndServe(":80", h)
61+
i.motorboard.SetLeds(green)
62+
time.Sleep(500 * time.Millisecond)
63+
i.motorboard.SetLeds(red)
64+
65+
i.log.Info("Calibrating sensors")
66+
for {
67+
if err := i.navboard.Calibrate(); err == nil {
68+
break
69+
}
70+
}
71+
i.motorboard.SetLeds(green)
72+
73+
navDataCh := make(chan navboard.Data)
74+
go readNavData(i.navboard, navDataCh)
2275

2376
sigCh := make(chan os.Signal, 1)
2477
signal.Notify(sigCh, syscall.SIGINT)
25-
sig := <-sigCh
26-
log.Info("Received signal=%s, shutting down", sig)
78+
79+
go gohttp.ListenAndServe(config.HttpAddr, i.http)
80+
81+
i.log.Info("Entering main loop")
82+
mainLoop:
83+
for {
84+
select {
85+
case navData := <-navDataCh:
86+
attitudeData := i.attitude.Update(navData.Data)
87+
motorSpeeds := i.control.Update(attitudeData)
88+
if err := i.motorboard.SetSpeeds(motorSpeeds); err != nil {
89+
i.log.Error("Could not set motor speeds. err=%s", err)
90+
}
91+
case sig := <-sigCh:
92+
i.log.Info("Received signal=%s, shutting down", sig)
93+
break mainLoop
94+
}
95+
}
96+
}
97+
98+
func readNavData(board *navboard.Navboard, ch chan<- navboard.Data) {
99+
for {
100+
navData, err := board.NextData()
101+
if err != nil {
102+
continue
103+
}
104+
select {
105+
case ch <- navData:
106+
default:
107+
}
108+
}
109+
}
110+
111+
func NewInstances(c Config) (i Instances, err error) {
112+
i.log = log.DefaultLogger
113+
i.navboard = navboard.NewNavboard(c.NavboardTTY, i.log)
114+
i.motorboard, err = motorboard.NewMotorboard(c.MotorboardTTY)
115+
if err != nil {
116+
return
117+
}
118+
i.attitude = attitude.NewComplementary()
119+
i.control = control.NewControl(c.RollPID, c.PitchPID, c.YawPID)
120+
i.http = http.NewHandler(i.control, i.log)
121+
return
27122
}

Diff for: control/control.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package control
2+
3+
import (
4+
"github.com/felixge/godrone/attitude"
5+
"github.com/felixge/pidctrl"
6+
"math"
7+
"sync"
8+
)
9+
10+
const (
11+
throttleMax = 0.85
12+
adjMax = 1 - throttleMax
13+
)
14+
15+
func NewControl(rollPID, pitchPID, yawPID [3]float64) *Control {
16+
return &Control{
17+
roll: pidctrl.NewPIDController(rollPID[0], rollPID[1], rollPID[2]),
18+
pitch: pidctrl.NewPIDController(pitchPID[0], pitchPID[1], pitchPID[2]),
19+
yaw: pidctrl.NewPIDController(yawPID[0], yawPID[1], yawPID[2]),
20+
}
21+
}
22+
23+
type Control struct {
24+
l sync.Mutex
25+
throttle float64
26+
roll, pitch, yaw *pidctrl.PIDController
27+
}
28+
29+
func (c *Control) Set(s attitude.Data, throttle float64) {
30+
c.l.Lock()
31+
defer c.l.Unlock()
32+
33+
c.roll.Set(s.Roll)
34+
c.pitch.Set(s.Pitch)
35+
c.yaw.Set(s.Yaw)
36+
c.throttle = throttle
37+
}
38+
39+
func (c *Control) Update(a attitude.Data) (speeds [4]float64) {
40+
c.l.Lock()
41+
defer c.l.Unlock()
42+
43+
var (
44+
adjRoll = c.roll.Update(a.Roll)
45+
adjPitch = c.pitch.Update(a.Pitch)
46+
//adjYaw = c.yaw.Update(a.Yaw)
47+
adjYaw = 0.0
48+
throttle = math.Min(c.throttle, 1) * throttleMax
49+
)
50+
51+
if throttle <= 0 {
52+
return
53+
}
54+
55+
speeds = [4]float64{
56+
throttle + math.Max(math.Min(+adjRoll+adjPitch+adjYaw, adjMax), -adjMax),
57+
throttle + math.Max(math.Min(-adjRoll+adjPitch-adjYaw, adjMax), -adjMax),
58+
throttle + math.Max(math.Min(-adjRoll-adjPitch+adjYaw, adjMax), -adjMax),
59+
throttle + math.Max(math.Min(+adjRoll-adjPitch-adjYaw, adjMax), -adjMax),
60+
}
61+
return
62+
}

Diff for: defaults.go

-19
This file was deleted.

Diff for: drivers/motorboard/leds.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package motorboard
2+
3+
type LedColor int
4+
5+
const (
6+
LedOff LedColor = iota
7+
LedRed = 1
8+
LedGreen = 2
9+
LedOrange = 3
10+
)
11+
12+
func Leds(c LedColor) [4]LedColor {
13+
return [4]LedColor{c, c, c, c}
14+
}

0 commit comments

Comments
 (0)