\documentclass[onecolumn,letterpaper,11pt]{article}

%===================================================================
% Include common packages, environments, counters etc
\input{./common_latex/common_latex_packages}
\input{./common_latex/common_latex_lab_format}
\input{./common_latex/common_latex_definitions}
%===================================================================
\begin{document}
\wikidoc{file=MCA.Lab09CollabPursuit}
\wikidoc{auth=mikerb@mit.edu}

%===================================================================
% Build the Cover Page in an environment style easy for wiki parsing
\begin{labcover}
\labtitle{Lab 9 - Collaborative Pursuit}
\vspace{0.1in}
%\labcourse{2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications}
\vspace{0.1in}
\labpng{0.75}{figs_common/header.png}
\labdate{June 2026}
\vspace{0.1in}
\labauth{Michael Benjamin, mikerb@mit.edu}
\labauth{Department of Mechanical Engineering}
\labauth{MIT, Cambridge MA 02139}
\labtoc{footnotesize}
\end{labcover}

%===================================================================
% Add a blank page so first body page is on the right when doubleside
\blankpage

%===================================================================
% Begin the body of the text
%===================================================================
\section{Overview and Objectives}
\label{sec_overview}
%===================================================================

This lab presents a short challenge problem to bring together a few
topics introduced in the course thus far. It begins with a working
baseline mission, followed by a sequence of modifications, each
adding more capability and complexity to the autonomous vehicles in
the mission. It will touch upon the following topics: 


\begin{labinfo}
\begin{packed_itemize}[2]
\item Multi-vehicle simulations
\item Use of a developer-provided MOOS app in a mission
\item Intervehicle message passing
\item Coordination strategy based on message passing
\end{packed_itemize}
\end{labinfo}


\subsection{Preliminaries}
\label{prelims}

In this lab, the baseline mission is provided as a stand-alone mission
folder downloadable from public Github repository. To complete the lab
you only need an Internet connection to get this repo, and no additional
tools other than those you have been using in earlier labs. 

\vspace{0.1in}
\noindent
In the baseline mission you will launch a three-vehicle mission, with one
vehicle patrollnig a race-track pattern, and two vehicles trailing the
lead vehicle at two distinct relative angles. In the baseline mission,
all three vehicles will endlessly do this task, with the user able to
deploy, station-keep or return the vehicles at any time.

\vspace{0.1in}
\noindent
We ask you to make the following modifications: 

\begin{packed_itemize}
\item \var{Exercise 1}: Modify the two trailing vehicles to use your
  \app{pOdometry} app, and return home after 300 meters of distance
  travelled. The \app{pMarineViewer} app should add a button to send
  an odometry reset and re-deploy message to a returned vehicle.

\item \var{Exercise 2:} Modify the mission to automatically reset the
  odometry distance when the vehicle returns home, and immediately re-deploy. 
  
\item \var{Exercise 3}: Pick one of the following vehicles to be a
  primary vehicle. When the primary trailing vehicle is trailing 
  the other vehicle should not. Use a message between the two vehicles
  to accomplish this.
\end{packed_itemize}


%======================================================================
\newpage
\section{Exercises}
%======================================================================

%======================================================================
\subsection{Exercise 0: Obtain and Run the Baseline Mission}
\label{sec_s8}

Obtain the baseline mission folder and put it in the same directory as
the other missions you have created:

\vspace{0.1in}
\begin{consoleverb}
 $ git clone https://github.com/mit2680/double_trail.git
\end{consoleverb}
\vspace{0.1in}

\noindent
This is a public readable folder on the Github website. When you have
downloaded, it should look like:

\vspace{0.1in}
\begin{consoleverb}
$ cd double_trail
$ ls 
clean.sh*             launch_vehicle.sh*   meta_vehicle.bhv     plugs_chaser.bhv
init_field.sh*        launch.sh*           meta_vehicle.moos    plugs_surveyor.bhv
launch_shoreside.sh*  meta_shoreside.moos  plugs_all.bhv        plugs.moos
\end{consoleverb}
\vspace{0.1in}

\noindent
To launch this mission:

\vspace{0.1in}
\begin{consoleverb}
$ cd double_trail
$ ./launch.sh 10
\end{consoleverb}
\vspace{0.1in}

\noindent
Upon launch, three vehicles will be positioned as shown below. The green vehicle,
abe, is the "survey" vehicle, and the two blue vehicles, ben, and cal, are the
"trailing" vehicles. Clicking the deploy button will result in the continous
pattern shown below:

\begin{figure}[H]
  \centering 
  \includegraphics[width=0.9\textwidth]{figures/baseline.png}
  \caption{The baseline mission is comprised of a single {\em survey} vehicle
    followed by two {\em trailing} vehicles. After the \var{DEPLOY} button is
    hit, the trailing vehicles will follow endlessly.}
  \label{fig_basesline}
  \gif{figures/baseline.gif}
\end{figure}

\noindent
Note in this baseline mission, you can:

\begin{packed_itemize}
\item Deploy all vehicles with the \var{DEPLOY\_ALL} buttons. Same with return.
\item Return or re-deploy any particular vehicle.
\item Station-keep any vehicle by left-clicking on or near it.
\item Un-Station-keep any vehicle by right-clicking on or near it.
\end{packed_itemize}


%======================================================================
\newpage
\subsection{Exercise 1: Use your pOdometry App to Auto-Return}

In this exercise, include your \app{pOdometry} app from the previous lab, to
auto-return the trailing vehicles after they have traversed 300 meters.

\vspace{0.1in}
\noindent
You will need to:

\begin{packed_itemize}
\item Make sure your \app{pOdometry} app is working to the specs of the
  previous lab.
\item Make sure it is in your shell path.
\item Add it to the \app{pAntler} block in \var{meta\_vehicle.moos}
\item Add a condition to the behavior modes for the trailing vehicle so that
  it returns after 300 meters.
\item Augment the \var{DEPLOY} button in \app{pMarineViewer} to reset the
  odometry distance when clicked. This should allow you to re-deploy the vehicle
  after it has returned home.
\end{packed_itemize}

\vspace{0.1in}
\noindent
Note that the \var{.moos} mission files for \pvar{ben} and \pvar{cal} are
\var{targ\_ben.moos} and \var{targ\_cal.moos} respectively. They are created
at launch time from \var{meta\_vehicle.bhv} which includes \var{plugs\_chaser.bhv}
and \var{plugs\_all.bhv}. You will find the MODE definitions for the chaser
vehicle in \var{plugs\_chaser.bhv}.


\vspace{0.1in}
\noindent
The end result when properly working should look like below. Note the
vehicles return automatically, enter station-keeping mode automatically
upon return, and wait for the user to redeploy before resuming trailing
the survey vehicle.

\begin{figure}[H]
  \centering 
  \includegraphics[width=0.9\textwidth]{figures/double_pursuit_return_odo.png}
  \caption{After the chaser vehicles have pursued the survey vehicle for 300
    meters, they automatically return to their launch point and enter the
    station-keeping mode. The deploy button in \app{pMarineViewer} can be used
    to re-deploy the vehicles after the odometry has been reset to zero.}
  \label{fig_double_pursuit_return_odo}
  \gif{figures/double_pursuit_return_odo.gif}
\end{figure}

%======================================================================
\newpage
\subsection{Exercise 2: Automatic Re-deploy after Odometry Reset}

This exercise is a modest modification from the previous one. The primary differences
are:

\begin{packed_itemize}
\item When a vehicle returns home, the odometry is automatically reset to zero
\item The user does not need to be involved to re-deploy the vehicle. It will
  re-deploy automatically upon return and odometry reset.
\end{packed_itemize}

\vspace{0.1in}
\noindent
The end result when properly working should look like below. Note the
the user does not use any buttons to re-deploy the vehicle.

\begin{figure}[H]
  \centering 
  \includegraphics[width=0.9\textwidth]{figures/double_pursuit_auto_redeploy.png}
  \caption{After the chaser vehicles have pursued the survey vehicle for 300
    meters, they automatically return to their launch point. Upon returning
    home the automatically reset their odometry distance and re-deploy themselves
    to pursue the survey vehicle. No human intervention involved.}
  \label{fig_double_pursuit_auto_redeploy}
  \gif{figures/double_pursuit_auto_redeploy.gif}
\end{figure}

%======================================================================
\newpage
\subsection{Exercise 3: Coordinated Pursuit}

In this exercise, the additional challenge is to coordinate between the two
vehicles so that when one is trailing, the other is not. A few hints and
assumptions:

\begin{packed_itemize}
\item The \app{uFldMessageHandler} needs to be added to apps running on vehicles.
  Even though only the trailing vehicles need to receive messages, don't worry
  if the survey vehicle also does.
\item Pick one of the two trail vehicles as a primary.  The primary
  trails whenever his odometry allows him. The other trail vehicle
  trails when the primary is not.
\item You can use the \bvar{idleflag} and \bvar{runflag} parameters, available
  on all behaviors. This can be used inside the Trail behavior to 
\end{packed_itemize}

\vspace{0.1in}
\noindent
The end result when properly working should look like below. 

\begin{figure}[H]
  \centering 
  \includegraphics[width=0.9\textwidth]{figures/handoff_pursuit.png}
  \caption{The \pvar{cal} vehicle pursues the survey (green)
    vehicle. As before, \pvar{cal} returns home after reaching an
    odometry limit. During this return home phase, \pvar{ben} takes
    over pursuit untl \pvar{resumes} pursuit.}
  \label{fig_handoff_pursuit}
  \gif{figures/handoff_pursuit.gif}
\end{figure}


\vspace{0.1in}
\noindent
Recall that an inter-vehicle message is of the form:

\vspace{0.1in}
\begin{consoleverb}
  NODE_MESSAGE_LOCAL=src_node=vname1,dest_node=vname2,var_name=MOOSVAR,string_val=string
\end{consoleverb}
\vspace{0.1in}

\vspace{0.1in}
\noindent
Recall also that to make life simpler, you can just send the message to all vehicles,
as below. 

\vspace{0.1in}
\begin{consoleverb}
  NODE_MESSAGE_LOCAL=src_node=vname1,dest_node=all,var_name=MOOSVAR,string_val=string
\end{consoleverb}
\vspace{0.1in}

\vspace{0.1in}
\noindent
The node message can be published as part of a behavior flag. For example:

\vspace{0.1in}
\begin{consoleverb}
  runflag = NODE_MESSAGE_LOCAL=src_node=vname1,dest_node=all,var_name=MOOSVAR,string_val=string
\end{consoleverb}
\vspace{0.1in}

\vspace{0.1in}
\noindent
Consider generating your inter-vehicle message from within the Trail
behavior, configured in \var{plugs\_chaser.bhv}, using the
\var{runflag} and \var{idleflag} parameters.

\vspace{0.1in}
\noindent
Behavior flags support certain "macros". For example the \var{OWNSHIP} macro
expands to \pvar{ben} when \pvar{ben} is posting it. For example: 

\vspace{0.1in}
\begin{consoleverb}
  runflag = NODE_MESSAGE_LOCAL=src_node=$[OWNSHIP], ....
\end{consoleverb}
\vspace{0.1in}


%======================================================================
\subsection{A Solution to the Exercise}
\label{solution}

Below is one solution to the exercises. Keep in mind that there is always
more than one way accomplish a mission. This represents one way. 

\vspace{0.1in}
\begin{consoleverb}
 $ git clone https://github.com/mit2680/double_trail_solution.git
\end{consoleverb}
\vspace{0.1in}


\end{document}
