Skip to content

Science/sysid_backseat.tl

  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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#   Copyright (c) 2024 MBARI
#   MBARI Proprietary Information. Confidential. All Rights Reserved
#   Unauthorized copying or distribution of this file via any medium is strictly
#   prohibited.
#
#   WARNING - This file contains information whose export is restricted by the
#   Export Administration Act of 1979 (Title 50, U.S.C., App. 2401 et seq.), as
#   amended. Violations of these export laws are subject to severe civil and/or
#   criminal penalties.

mission sysid_backseat {
  """
  Universal system identification mission driven by a backseat application.

  The backseat publishes time-varying actuator commands and hold flags via
  customUri variables on the slate. This mission routes those commands to
  the appropriate Guidance behaviors and enforces a safety envelope.

  Supports all sysid test types: thrust model, elevator/rudder effectiveness,
  mass shifter/bladder trim, free response, and coupled MIMO excitation.
  """

  arguments {
    MissionTimeout = 4 hour
      """
      Maximum duration of mission.
      """

    BackseatTimeout = 10 second
      """
      Max time allowed between backseat sysidActive updates.
      If exceeded, vehicle enters safe mode.
      """

    MaxDepth = 200 meter
      """
      Hard maximum depth limit (mission envelope).
      """

    MinAltitude = 7 meter
      """
      Hard minimum altitude limit (mission envelope).
      """

    MaxPitch = 30 degree
      """
      Hard maximum pitch limit (mission envelope).
      """

    MaxRoll = 30 degree
      """
      Hard maximum roll limit (mission envelope).
      """

    InitialDepth = 3 meter
      """
      Depth target for the initial dive before the backseat test starts.
      """

    InitialSpeed = 1.0 meter_per_second
      """
      Speed target for the initial dive.
      """

    DiveSettleTime = 2 second
      """
      Time to hold at InitialDepth before triggering the backseat test.
      Allows vehicle to stabilize at depth.
      """

    TestName = "sysid_01_thrust_model"
      """
      Name of the test plan YAML to run (without .yaml extension).
      Written to the slate to trigger the backseat application.
      """

    ElevatorDefault = 0 degree
      """
      Default elevator angle when not being commanded.
      """

    RudderDefault = 0 degree
      """
      Default rudder angle when not being commanded.
      """

    MassDefault = Control:VerticalControl.massDefault
      """
      Default mass position.
      """

    BuoyancyDefault = Control:VerticalControl.buoyancyNeutral
      """
      Default buoyancy position.
      """
  }

  output {
    Speed = InitialSpeed
      """
      Commanded vehicle speed (m/s).
      """

    Heading = 0 degree
      """
      Commanded vehicle heading.
      """

    ElevatorAngle = ElevatorDefault
      """
      Commanded elevator angle.
      """

    RudderAngle = RudderDefault
      """
      Commanded rudder angle.
      """

    MassPosition = MassDefault
      """
      Commanded mass position.
      """

    BuoyancyPosition = BuoyancyDefault
      """
      Commanded buoyancy position.
      """

    DepthTarget = InitialDepth
      """
      Commanded depth target.
      """

    HoldDepth = 1 count
      """
      Whether depth hold is active (1) or not (0).
      """

    HoldHeading = 1 count
      """
      Whether heading hold is active (1) or not (0).
      """

    HoldPitch = 1 count
      """
      Whether pitch hold is active (1) or not (0).
      """

    HoldSpeed = 1 count
      """
      Whether speed hold is active (1) or not (0).
      """

    SysidActive = 0 count
      """
      Whether backseat is actively commanding (1) or idle (0).
      """

    ElapsedSinceMissionStarted = 0 minute
  }

  # Mission timeout.

  timeout duration=MissionTimeout

  # Init Heading at current vehicle heading.

  assign in sequence Heading = Universal:platform_orientation

  behavior Guidance:Execute {
    run in sequence

    set command = "set _.sysidTestCmd string \"init\""
  }

  # === BACKSEAT UPDATE AGGREGATES ===
  # Poll each customUri; update output when backseat value is newer.

  aggregate BackseatTimeout {
    run when (
      SysidActive == 1 count
      and ( elapsed ( customUri "_.sysidActive" ) >= BackseatTimeout )
    )

    assign in sequence SysidActive = 0 count
    assign in sequence Speed = 0 meter_per_second

    syslog fault "Backseat timeout! Setting safe mode."
  }

  aggregate UpdateSysidActive {
    run when (
      elapsed ( customUri "_.sysidActive" ) < ( elapsed ( SysidActive )
        and SysidActive != customUri "_.sysidActive"
      )
    )

    assign in sequence SysidActive = customUri "_.sysidActive"
  }

  aggregate UpdateSpeed {
    run when (
      elapsed ( customUri "_.speedCmd" ) < ( elapsed ( Speed )
        and ( Speed != customUri "_.speedCmd" )
      )
    )

    assign in sequence Speed = customUri "_.speedCmd"
  }

  aggregate UpdateHeading {
    run when (
      elapsed ( customUri "_.headingCmd" ) < ( elapsed ( Heading )
        and (
          customUri "_.headingCmd" == Heading
          or (
            customUri "_.headingCmd" == 0 degree
            and Heading == 0 degree
          )
        )
      )
    )

    assign in sequence Heading = customUri "_.headingCmd"
  }

  aggregate UpdateElevator {
    run when (
      elapsed ( customUri "_.elevatorAngleCmd" ) < ( elapsed ( ElevatorAngle )
        and ( ElevatorAngle != customUri "_.elevatorAngleCmd" )
      )
    )

    assign in sequence ElevatorAngle = customUri "_.elevatorAngleCmd"
  }

  aggregate UpdateRudder {
    run when (
      elapsed ( customUri "_.rudderAngleCmd" ) < ( elapsed ( RudderAngle )
        and ( RudderAngle != customUri "_.rudderAngleCmd" )
      )
    )

    assign in sequence RudderAngle = customUri "_.rudderAngleCmd"
  }

  aggregate UpdateMassPosition {
    run when (
      elapsed ( customUri "_.massPositionCmd" ) < ( elapsed ( MassPosition )
        and ( MassPosition != customUri "_.massPositionCmd" )
      )
    )

    assign in sequence MassPosition = customUri "_.massPositionCmd"
  }

  aggregate UpdateBuoyancyPosition {
    run when (
      elapsed ( customUri "_.buoyancyPositionCmd" ) < ( elapsed ( BuoyancyPosition )
        and ( BuoyancyPosition != customUri "_.buoyancyPositionCmd" )
      )
    )

    assign in sequence BuoyancyPosition = customUri "_.buoyancyPositionCmd"
  }

  aggregate UpdateDepthTarget {
    run when (
      elapsed ( customUri "_.depthCmd" ) < ( elapsed ( DepthTarget )
        and ( DepthTarget != customUri "_.depthCmd" )
      )
    )

    assign in sequence DepthTarget = customUri "_.depthCmd"
  }

  aggregate UpdateHoldDepth {
    run when (
      elapsed ( customUri "_.holdDepth" ) < ( elapsed ( HoldDepth )
        and ( HoldDepth != customUri "_.holdDepth" )
      )
    )

    assign in sequence HoldDepth = customUri "_.holdDepth"
  }

  aggregate UpdateHoldHeading {
    run when (
      elapsed ( customUri "_.holdHeading" ) < ( elapsed ( HoldHeading )
        and ( HoldHeading != customUri "_.holdHeading" )
      )
    )

    assign in sequence HoldHeading = customUri "_.holdHeading"
  }

  aggregate UpdateHoldPitch {
    run when (
      elapsed ( customUri "_.holdPitch" ) < ( elapsed ( HoldPitch )
        and ( HoldPitch != customUri "_.holdPitch" )
      )
    )

    assign in sequence HoldPitch = customUri "_.holdPitch"
  }

  aggregate UpdateHoldSpeed {
    run when (
      elapsed ( customUri "_.holdSpeed" ) < ( elapsed ( HoldSpeed )
        and ( HoldSpeed != customUri "_.holdSpeed" )
      )
    )

    assign in sequence HoldSpeed = customUri "_.holdSpeed"
  }

  # === BACKSEAT POWER ===

  insert Insert/BackseatDriver.tl

  assign in sequence BackseatDriver:EnableBackseat = true

  insert Insert/PowerOnly.tl

  # === SAFETY ENVELOPES ===

  insert Insert/StandardEnvelopes.tl

  assign in sequence StandardEnvelopes:MinAltitude = MinAltitude
  assign in sequence StandardEnvelopes:MaxDepth = MaxDepth

  # TODO limit pitch and roll
  # assign in sequence StandardEnvelopes:MaxPitch = MaxPitch
  # assign in sequence StandardEnvelopes:MaxRoll = MaxRoll

  # === INITIAL DIVE ===
  # Dive to InitialDepth under mission control before handing off to backseat.

  aggregate InitialDive {
    run in sequence

    syslog important "Beginning initial dive to " + InitialDepth~meter + " at "
         + InitialSpeed~meter_per_second

    behavior Guidance:SetSpeed {
      run in parallel

      set speed = InitialSpeed
    }

    behavior Guidance:Point {
      run in parallel

      set heading = Heading
    }

    behavior Guidance:Pitch {
      run in sequence

      set depth = InitialDepth
    }

    # Settle at depth
    behavior Guidance:Wait {
      run in sequence

      set duration = DiveSettleTime
    }

    syslog important "Initial dive complete. Triggering backseat test: " + TestName~none_str
  }

  # Trigger the backseat test by writing the test name to the slate.
  # Use Guidance:Execute with `set ... string ...` rather than
  # `assign in sequence customUri ... = TestName` because the assign form
  # pre-types the customUri slot as numeric at mission load (TethysL can't
  # infer string type from an arg without a unit declaration), which then
  # blocks any later string write. The Execute path defers slot creation
  # to runtime with an explicit `string` type. Pattern from
  # lrauv-backseat-marl/Missions/marl4.tl:285-288.
  behavior Guidance:Execute {
    run in sequence

    #set command = "set _.sysidTestCmd string \"sysid_01_thrust_model\""
    set command = "set _.sysidTestCmd string \"" + TestName + "\""
  }

  # === GUIDANCE BEHAVIORS ===
  # Each behavior is conditionally active based on the hold flags.
  # When a hold is ON, the closed-loop controller manages that axis.
  # When a hold is OFF, direct actuator commands from the backseat are used.

  aggregate SysidControl {
    run in sequence

    # --- Speed: always active (no direct RPM control) ---
    behavior Guidance:SetSpeed {
      run in parallel

      set speed = Speed
    }

    # --- Buoyancy: always active ---
    behavior Guidance:Buoyancy {
      run in parallel

      set position = BuoyancyPosition
    }

    # --- Mass: always active ---
    behavior Guidance:Mass {
      run in parallel

      set position = MassPosition
    }

    # --- Depth hold: closed-loop depth control ---
    aggregate DepthHoldActive {
      run while ( HoldDepth == 1 count )

      behavior Guidance:Pitch {
        run in parallel

        set depth = DepthTarget
      }
    }

    # --- Pitch hold: closed-loop pitch control ---
    aggregate PitchHoldActive {
      run while (
        HoldPitch == 1 count
        and HoldDepth == 0 count
      )

      behavior Guidance:Pitch {
        run in parallel

        set pitch = 0 degree
      }
    }

    # --- Direct elevator: when depth and pitch holds are OFF ---
    aggregate DirectElevator {
      run while (
        HoldDepth == 0 count
        and HoldPitch == 0 count
      )

      behavior Guidance:Pitch {
        run in parallel

        set elevatorAngle = ElevatorAngle
      }
    }

    # --- Heading hold: closed-loop heading control ---
    aggregate HeadingHoldActive {
      run while ( HoldHeading == 1 count )

      behavior Guidance:Point {
        run in parallel

        set forceUpdate = true
        set heading = Heading
      }
    }

    # --- Direct rudder: when heading hold is OFF ---
    aggregate DirectRudder {
      run while ( HoldHeading == 0 count )

      behavior Guidance:Point {
        run in parallel

        set forceUpdate = true
        set rudderAngle = RudderAngle
      }
    }

    # --- Periodic check-in for comms ---
    aggregate CheckIn {
      run in sequence repeat=576

      behavior Guidance:Wait {
        run in sequence

        set duration = MissionTimeout
      }
    }
  }
}