/
pilot.go
333 lines (289 loc) · 9.04 KB
/
pilot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package glider
import (
"github.com/nsf/termbox-go"
"github.com/stianeikeland/go-rpio/v4"
"io"
"math"
"time"
)
type PilotState uint8
// These are defined in reverse order, so that blinking the status makes
// more sense. Fewer blink patterns indicate we're done initializing.
const (
flying PilotState = iota + 1
waitingForLaunch
waitingForButton
initializing
landed
testMode
)
func (ps PilotState) String() string {
return []string{
"(unused-0-state)",
"flying",
"waitingForLaunch",
"waitingForButton",
"initializing",
"landed",
"testMode",
}[ps]
}
type Pilot struct {
state PilotState
telemetry *Telemetry
control *Control
statusIndicator *LedStatusIndicator
buttonPin *rpio.Pin
buttonPressTime time.Time
zeroSpeedTime *time.Time
waypoints *Waypoints
previousUpdateRoll_r Radians
previousUpdatePitch_r Radians
previousAxes Axes
axesIdleTime time.Time
}
func NewPilot() (*Pilot, error) {
telemetry, err := NewTelemetry()
if err != nil {
return nil, err
}
response := rpio.Pin(configuration.ButtonPin)
buttonPin := &response
buttonPin.Input()
buttonPin.PullUp()
return &Pilot{
// TODO
//state: initializing,
state: testMode,
control: NewControl(),
telemetry: telemetry,
statusIndicator: NewLedStatusIndicator(uint8(initializing)),
buttonPin: buttonPin,
buttonPressTime: time.Now(),
zeroSpeedTime: nil,
waypoints: NewWaypoints(),
}, nil
}
// Run the local glide test, e.g. when throwing the plane down a hill
func (pilot *Pilot) RunGlideTestForever() {
previousState := pilot.state
Logger.Infof("Starting RunGlideTestForever in state %s", pilot.state)
eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
for {
if previousState != pilot.state {
Logger.Infof("RunGlideTestForever new state %s", pilot.state)
previousState = pilot.state
}
pilot.statusIndicator.BlinkState(uint8(pilot.state))
// Parse all queued messages
for {
parsed, err := pilot.telemetry.ParseQueuedMessage()
if err != nil && err != io.EOF {
Logger.Errorf("Unable to parse GPS message: %v", err)
break
}
if !parsed {
break
}
}
Logger.Debug("Running step")
switch pilot.state {
case initializing:
pilot.runInitializing()
case waitingForButton:
pilot.runWaitingForButton()
case waitingForLaunch:
pilot.runWaitForLaunch()
case flying:
pilot.runFlying()
case landed:
pilot.runLanded()
case testMode:
pilot.runGlideDirection()
}
select {
case event := <-eventQueue:
// Check for any key presses
if event.Type == termbox.EventKey {
return
}
default:
updateDashboard(pilot.telemetry, pilot)
}
// TODO: Maybe we want to figure out how long one iteration
// took, then sleep an appropriate amount of time, so we can get
// close to however many cycles per second
time.Sleep(configuration.IterationSleepTime)
}
}
func (pilot *Pilot) runInitializing() {
if pilot.telemetry.HasGpsLock {
pilot.state = waitingForButton
Logger.Info("Got GPS lock, waiting for button")
}
}
func (pilot *Pilot) runWaitingForButton() {
buttonState := pilot.buttonPin.Read()
if buttonState == rpio.Low {
Logger.Info("Button pressed, waiting for launch")
pilot.state = waitingForLaunch
pilot.buttonPressTime = time.Now()
}
}
func (pilot *Pilot) runWaitForLaunch() {
// Just adjust the ailerons to keep the plane level
if time.Since(pilot.buttonPressTime) < configuration.LaunchGlideDuration {
pilot.runGlideLevel()
} else {
pilot.state = flying
}
}
func (pilot *Pilot) runFlying() {
// Fly in a direction
position := pilot.telemetry.GetPosition()
if pilot.waypoints.Reached(position) {
pilot.waypoints.Next()
}
waypoint := pilot.waypoints.GetWaypoint()
axes, err := pilot.telemetry.GetAxes()
if err != nil {
// I guess just log it?
Logger.Errorf("runFlying unable to get axes: %v", err)
time.Sleep(configuration.ErrorSleepDuration)
return
}
if pilot.hasLanded(axes) {
pilot.state = landed
return
}
targetRoll_r := getTargetRollPosition(axes.Yaw, position, waypoint)
pilot.adjustAileronsToRollPitch(targetRoll_r, configuration.TargetPitch, axes)
}
func (pilot *Pilot) runLanded() {
// Move the servos to center
pilot.control.SetLeft(90)
pilot.control.SetRight(90)
// When the button is pressed, start over
buttonState := pilot.buttonPin.Read()
if buttonState == rpio.Low {
pilot.state = waitingForLaunch
Logger.Info("Waiting for launch")
}
}
// Just adjust the ailerons to fly in a direction.
func (pilot *Pilot) runGlideDirection() {
axes, err := pilot.telemetry.GetAxes()
if err != nil {
// I guess just log it?
Logger.Errorf("runGlideDirection unable to get axes: %v", err)
time.Sleep(configuration.ErrorSleepDuration)
return
}
// If we've landed, stop adjusting the ailerons
if pilot.hasLanded(axes) {
pilot.state = landed
return
}
targetRoll_r := getTargetRollHeading(axes.Yaw, configuration.FlyDirection)
Logger.Debugf("targetRoll:%0.1f", ToDegrees(targetRoll_r))
// Now adjust the ailerons to fly that direction
pilot.adjustAileronsToRollPitch(targetRoll_r, configuration.TargetPitch, axes)
}
// Just adjust the ailerons to fly roll level. Good for testing or
// immediately after launch.
func (pilot *Pilot) runGlideLevel() {
axes, err := pilot.telemetry.GetAxes()
if err != nil {
// I guess just log it?
Logger.Errorf("runGlideLevel unable to get axes: %v", err)
time.Sleep(configuration.ErrorSleepDuration)
return
}
// If we've landed, stop adjusting the ailerons
if pilot.hasLanded(axes) {
pilot.state = landed
return
}
// Now adjust the ailerons to fly straight
pilot.adjustAileronsToRollPitch(0.0, configuration.TargetPitch, axes)
}
func (pilot *Pilot) hasLanded(axes Axes) bool {
// TODO: When we launch the balloon, check that the altitude is
// below configuration.LandingPointAltitude + configuration.LandingPointAltitudeOffset
var returnValue bool
if math.Abs(pilot.previousAxes.Roll-axes.Roll) > ToRadians(Degrees(1.0)) {
pilot.axesIdleTime = time.Now()
returnValue = false
} else if time.Since(pilot.axesIdleTime) > configuration.LandNoMoveDuration {
returnValue = true
}
if pilot.telemetry.GetSpeed() > 0.1 {
pilot.zeroSpeedTime = nil
returnValue = false
} else if pilot.zeroSpeedTime == nil {
returnValue = false
} else if time.Since(*pilot.zeroSpeedTime) > configuration.LandNoMoveDuration {
returnValue = returnValue && true
}
pilot.previousAxes = axes
return returnValue
}
// Adjust the ailerons to match some pitch and roll
func (pilot *Pilot) adjustAileronsToRollPitch(targetRoll_r, targetPitch_r Radians, axes Axes) {
// Just use a P loop for now?
rollDifference := axes.Roll - targetRoll_r
leftAngle_r := rollDifference * configuration.ProportionalRollMultiplier
rightAngle_r := leftAngle_r
adjustment := (targetPitch_r - axes.Pitch) * configuration.ProportionalPitchMultiplier
adjustment = clamp(adjustment, -configuration.MaxServoPitchAdjustment, configuration.MaxServoPitchAdjustment)
leftAngle_r -= adjustment
rightAngle_r += adjustment
leftAngle_r = clamp(leftAngle_r, -configuration.MaxServoAngleOffset, configuration.MaxServoAngleOffset)
rightAngle_r = clamp(rightAngle_r, -configuration.MaxServoAngleOffset, configuration.MaxServoAngleOffset)
// Let's only move the servo when it's changed a little so that the
// servo isn't freaking out due to noisy sensors
difference_r := math.Abs(pilot.previousUpdateRoll_r - axes.Roll)
difference_r += math.Abs(pilot.previousUpdatePitch_r - axes.Pitch)
Logger.Debugf("roll:%0.1f targetRoll:%0.1f", ToDegrees(axes.Roll), ToDegrees(targetRoll_r))
Logger.Debugf("pitch:%0.1f targetPitch:%0.1f", ToDegrees(axes.Pitch), ToDegrees(targetPitch_r))
Logger.Debugf("leftAngle:%0.1f rightAngle:%0.1f", ToDegrees(leftAngle_r), ToDegrees(rightAngle_r))
if difference_r < ToRadians(4) {
Logger.Debugf("difference %0.1f is too low", ToDegrees(difference_r))
return
}
pilot.previousUpdateRoll_r = axes.Roll
pilot.previousUpdatePitch_r = axes.Pitch
Logger.Debugf("setting leftAngle:%0.1f rightAngle:%0.1f", ToDegrees(leftAngle_r), ToDegrees(rightAngle_r))
pilot.control.SetLeft(ToRadians(90) + leftAngle_r)
pilot.control.SetRight(ToRadians(90) + rightAngle_r)
}
func getTargetRollPosition(yaw_r Radians, position, waypoint Point) Radians {
goalHeading_r := Course(position, waypoint)
return getTargetRollHeading(yaw_r, goalHeading_r)
}
func getTargetRollHeading(yaw_r, goalHeading_r Radians) Radians {
adjustHeading_r := GetAngleTo(yaw_r, goalHeading_r)
targetRoll_r := adjustHeading_r * configuration.ProportionalTargetRollMultiplier
targetRoll_r = clamp(targetRoll_r, -configuration.MaxTargetRoll, configuration.MaxTargetRoll)
return targetRoll_r
}
func clamp(value, minimum, maximum float64) float64 {
if minimum > maximum {
temp := minimum
minimum = maximum
maximum = temp
Logger.Errorf("clamp minimum and maximum were reversed: %v %v", minimum, maximum)
}
if value < minimum {
return minimum
}
if value > maximum {
return maximum
}
return value
}