\section{Using an acoustic modem to deploy an underwater vehicle}
\label{sec_deploy_minipacket}

This section is done on limited modem hardware (up to two teams of two at one time). Please find time to do this section when hardware is free (even if you haven't completed Section \ref{sec_acomms_example} yet).

\subsection{The WHOI Micro-Modem}

The WHOI Micro-Modem is one of only a few commercially available acoustic modems. It uses two types of modulation (Frequency-Hopping Frequency Shift Keying (FH-FSK) and Phase Shift Keying (PSK)) to achieve a range of packet sizes from 32 bytes / packet to 2048 bytes / packet. Higher rates are more susceptible to bit errors and thus packet loss. Realistically, a single packet can be sent about once every 10-15 seconds after accounting for signal length (3.5 seconds), time of flight (100 to 10000 meters range over 1500 m/s nominal speed of sound), and potentially an acoustic acknowledgment. Thus, realistic data rates range from 20 bps to to 200 bps \textit{before} accounting for packet loss (which can be easily greater than fifty percent). This is why we spend so much time in the rest of this lab focusing on data compression for very small packets.

\subsubsection{Serial Data Protocol}

The WHOI Micro-Modem connects over a serial line (RS-232) at a default 19200 baud rate. It uses a data protocol roughly based on the NMEA 0183 standard, which is widely used in marine electronics (e.g. GPS units)\footnote{The entire protocol is posted at \url{http://acomms.whoi.edu/umodem/documentation.html} under ``Micro-Modem Software Interface Guide''}.
 Two primary ``talker'' ids are employed: \$CC for messages from the computer and \$CA for messages from the modem. A full PSK transaction including acknowledgment from modem 1 to modem 2 looks like this and is rather complicated (the actual data sent over the acoustic link is only 0x31313131313131313131313131313131... (64 copies of 0x31) and 32323232323232323232323232323232... (64 copies of 0x32)). The rest of the messages are protocol between the autonomy computer and the modem locally over the RS-232 serial link:

\begin{footnotesize}
\begin{verbatim}
{goby::acomms::modemdriver::out::1}: $CCCYC,0,1,2,2,1,3*5A
{goby::acomms::modemdriver::out::1}: ^ Network Cycle Initialization Command
{goby::acomms::modemdriver::in::1}: $CACYC,0,1,2,2,1,3*58
{goby::acomms::modemdriver::in::1}: ^ Echo of Network Cycle Initialization command
{goby::acomms::modemdriver::in::1}: $CADRQ,025728,1,2,1,64,1*4E
{goby::acomms::modemdriver::in::1}: ^ Data request message, modem to host
{goby::acomms::modemdriver::out::1}: $CCTXD,1,2,1,31313131313131313131313131313131...
{goby::acomms::modemdriver::out::1}: ^ Transmit binary data message, host to modem
{goby::acomms::modemdriver::in::1}: $CATXD,1,2,1,64*7A
{goby::acomms::modemdriver::in::1}: ^ Echo back of transmit binary data message
{goby::acomms::modemdriver::in::1}: $CADRQ,025729,1,2,1,64,2*4C
{goby::acomms::modemdriver::in::1}: ^ Data request message, modem to host
{goby::acomms::modemdriver::out::1}: $CCTXD,1,2,1,32323232323232323232323232323232...
{goby::acomms::modemdriver::out::1}: ^ Transmit binary data message, host to modem
{goby::acomms::modemdriver::in::1}: $CATXD,1,2,1,64*7A
{goby::acomms::modemdriver::in::1}: ^ Echo back of transmit binary data message
{goby::acomms::modemdriver::in::1}: $CADRQ,025729,1,2,1,64,3*4D
{goby::acomms::modemdriver::in::1}: ^ Data request message, modem to host
{goby::acomms::modemdriver::out::1}: $CCTXD,1,2,1,*7A
{goby::acomms::modemdriver::out::1}: ^ Transmit binary data message, host to modem
{goby::acomms::modemdriver::in::1}: $CATXD,1,2,1,0*48
{goby::acomms::modemdriver::in::1}: ^ Echo back of transmit binary data message
{goby::acomms::modemdriver::in::1}: $CATXP,128*49
{goby::acomms::modemdriver::in::1}: ^ Start of packet transmission, modem to host
{goby::acomms::modemdriver::in::2}: $CARXP,1*45
{goby::acomms::modemdriver::in::2}: ^ Incoming packet detected, modem to host
{goby::acomms::modemdriver::in::1}: $CATXF,128*5F
{goby::acomms::modemdriver::in::1}: ^ End of packet transmission, modem to host
{goby::acomms::modemdriver::in::1}: $CAREV,025733,AUV,0.94.0.00*0E
{goby::acomms::modemdriver::in::1}: ^ Software revision message, modem to host
{goby::acomms::modemdriver::in::1}: $CAREV,025733,COPROC,0.10.0.46*40
{goby::acomms::modemdriver::in::1}: ^ Software revision message, modem to host
{goby::acomms::modemdriver::in::2}: $CACYC,1,1,2,2,1,3*59
{goby::acomms::modemdriver::in::2}: ^ Echo of Network Cycle Initialization command
{goby::acomms::modemdriver::in::2}: $CARXD,1,2,1,1,3131313131313131313131313131313...
{goby::acomms::modemdriver::in::2}: ^ Received binary message, modem to host
{goby::acomms::modemdriver::in::2}: $CARXD,1,2,1,2,3232323232323232323232323232323...
{goby::acomms::modemdriver::in::2}: ^ Received binary message, modem to host
{goby::acomms::modemdriver::in::2}: $CARXD,1,2,1,3,*61
{goby::acomms::modemdriver::in::2}: ^ Received binary message, modem to host
{goby::acomms::modemdriver::in::2}: $CACST,0,025729.0000,1,1903,20,0410,0167,00,01,...
{goby::acomms::modemdriver::in::2}: ^ Communication cycle receive statistics
{goby::acomms::modemdriver::in::2}: $CAREV,025733,AUV,0.94.0.00*0E
{goby::acomms::modemdriver::in::2}: ^ Software revision message, modem to host
{goby::acomms::modemdriver::in::2}: $CAREV,025733,COPROC,0.10.0.46*40
{goby::acomms::modemdriver::in::2}: ^ Software revision message, modem to host
{goby::acomms::modemdriver::in::1}: $CARXP,1*45
{goby::acomms::modemdriver::in::1}: ^ Incoming packet detected, modem to host
{goby::acomms::modemdriver::in::2}: $CATXP,0*42
{goby::acomms::modemdriver::in::2}: ^ Start of packet transmission, modem to host
{goby::acomms::modemdriver::in::1}: $CAACK,2,1,1,1*48
{goby::acomms::modemdriver::in::1}: ^ Acknowledgment of a transmitted packet
{goby::acomms::modemdriver::in::1}: $CAACK,2,1,2,1*4B
{goby::acomms::modemdriver::in::1}: ^ Acknowledgment of a transmitted packet
{goby::acomms::modemdriver::in::1}: $CAACK,2,1,3,1*4A
{goby::acomms::modemdriver::in::1}: ^ Acknowledgment of a transmitted packet
{goby::acomms::modemdriver::in::1}: $CACST,0,025734.0000,1,1608,20,0295,0165,00,01,01,...
\end{verbatim}
\end{footnotesize}

The Micro-Modem also has several other features, one of which is the handy ability to send 13 bits in the mini-packet (normally used for control messages such as the acknowledgment). This is pretty much the acoustic modem equivalent of the SMS. While very small in size, the packet is received with high reliability, and the transaction is simpler (see also Fig. \ref{fig:goby-ccmuc}) (sending hexadecimal data \verb|0x1234|):

\begin{footnotesize}
\begin{verbatim}
{goby::acomms::modemdriver::out::1}: $CCMUC,1,2,1234
{goby::acomms::modemdriver::in::1}: $CAMUC,1,2,1234*72
{goby::acomms::modemdriver::in::1}: $CATXP,0*42
{goby::acomms::modemdriver::in::1}: $CATXF,0*54
{goby::acomms::modemdriver::in::2}: $CARXP,1*45
{goby::acomms::modemdriver::in::2}: $CAMUA,1,2,1234*70
\end{verbatim}
\end{footnotesize}

\begin{figure}
\begin{center}
	\includegraphics[width=\textwidth]{goby-acomms-mmdriver-muc.pdf}
	\captionof{figure}{WHOI Micro-Modem User mini-packet 13 bit data transmission using Goby. You should see something like the far right ModemTransmission message if you open uXMS on \texttt{ACOMMS\_MODEM\_RECEIVE} on the receiving end (vehicle). The actual acoustic transmission occurs where it says ``FSK-Mini'' in the center.}
	\label{fig:goby-ccmuc}
\end{center}
\end{figure}


You should try this now:

\begin{enumerate}
\item Connect one modem to each of you and your lab partner's machines. Both of you should identify the serial port created by the connection. Use \verb|ls /dev/tty*| to see what device is added when you plug in the USB-serial dongle. On Linux it's probably \verb|/dev/ttyUSB0|; on Mac OS X it's probably \verb|/dev/tty.usbserial| or \verb|/dev/tty.PL2303...|). Replace all usages of \verb|/dev/ttyUSB0| below with the correct device name for your machine. If you're using a Mac (OS X 10.7 and 10.8 have this problem) and you don't see the device when you plug it in, you'll need to install the PL2303 driver as explained on this blog post \url{http://xbsd.nl/2011/07/pl2303-serial-usb-on-osx-lion.html} Use "sudo bash" or "sudo tcsh" to get a root shell before following their instructions.
\item Open picocom
\begin{verbatim}
picocom /dev/ttyUSB0 -b 19200
\end{verbatim}
and examine the output
\begin{verbatim}
$CAREV,025733,AUV,0.94.0.00*0E
$CAREV,025733,COPROC,0.10.0.46*40
\end{verbatim}
\verb|$CAREV| is the software revision number and serves as a heartbeat.
\item Configure one modem to be modem id 1 (the modem id is called SRC on the WHOI Micro-Modem)
\begin{verbatim}
printf '$CCCFG,SRC,1\r\n' > /dev/ttyUSB0
\end{verbatim}
and the other 2
\begin{verbatim}
printf '$CCCFG,SRC,2\r\n' > /dev/ttyUSB0
\end{verbatim}
Make sure you get the reply 
\begin{verbatim}
$CACFG,SRC,N*XX
\end{verbatim}
from the modem where $N$ is the modem id you send in the \verb|$CCCFG| and \verb|*XX| is the NMEA-0183 checksum. If you get an error, try sending it again. If you still get an error, ask me for help.
\item Try sending each other minipackets. For the person connected to modem 1:
\begin{verbatim}
printf '$CCMUC,1,2,1234\r\n' > /dev/ttyUSB0
\end{verbatim}
or for the person on modem 2:
\begin{verbatim}
printf '$CCMUC,2,1,1234\r\n' > /dev/ttyUSB0
\end{verbatim}
On the sending end you will see the modem echo back the message you sent it:
\begin{verbatim}
$CAMUC,1,2,1234*72
\end{verbatim}
And on the the receiving end you will see the received message:
\begin{verbatim}
$CAMUA,1,2,1234*72
\end{verbatim}

Feel free to replace \verb|1234| with any 13-bit hex number (\verb|0000| to \verb|1fff|). 
\item Close picocom (CTRL-A CTRL-X) on both machines in preparation for the next section.
\end{enumerate}

\subsection{Sending a Mini-Packet command using pAcommsHandler}

One of you will act the role of the shoreside, the other the vehicle. It doesn't matter which person was id 1 last time, pAcommsHandler will change it to the correct value.

\begin{enumerate}
\item Find the mission named ``acomms2'':
\begin{verbatim}
cd moos-ivp-extend-acomms/missions/acomms2
\end{verbatim}
\item If you are using a Mac OS X machine, change the last line in \verb|goby_liaison.pb.cfg| to read
\begin{verbatim}
load_shared_library: "../../lib/liblab12messages.dylib"
\end{verbatim}

If you are using Linux, set it to
\begin{verbatim}
load_shared_library: "../../lib/liblab12messages.so"
\end{verbatim}
\item Change the \verb|MICROMODEM_PORT| definition in \verb|meta_shoreside.moos| to correspond to the correct serial port and launch the shoreside on one machine:
\begin{verbatim}
./launch_shoreside.sh
\end{verbatim}
\item Similarly, change the \verb|MICROMODEM_PORT| definition in \verb|meta_vehicle.moos| and launch the vehicle on the other machine:
\begin{verbatim}
./launch_vehicle.sh
\end{verbatim}
\item Open a web browser of your choice and point it to \url{http://localhost:50001/#/mooscommander}. Choose the \verb|MiniCommand| message and fill in the fields (e.g. destination: 2, state: DEPLOY, manual\_override: false, speed: 3). Click ``Send'' and the vehicle will receive the message and publish the variables specified under the \verb|translator_entry { publish {} }| in \verb|plug_pAcommsHandler.moos|. Once you have this working properly, kill the vehicle community and open picocom again
\begin{verbatim}
picocom /dev/ttyUSB0 -b 19200
\end{verbatim}
\item Examine the raw modem feed when you send the MiniCommand message. Make a note of the \verb|$CAMUA| when the shoreside pushes ``Send'' on Goby Liaison Commander (Question 1 below).
\item Now, quit picocom and restart the vehicle. Examine the MOOS variables \verb|ACOMMS_RAW_INCOMING| and \verb|ACOMMS_MODEM_RECEIVE|. In addition to the \verb|src|, \verb|dest|, \verb|type|, \verb|frame| fields you saw when you looked at \verb|ACOMMS_MODEM_RECEIVE| in section \ref{sec:MOOSAPI}, you'll also notice a large amount of diagnostic information about the transmission. This is extracted from the \verb|$CACST| message from the WHOI Micro-Modem.
\end{enumerate}

The interaction with the Goby ModemDriver and the rest of Goby (and MOOS via pAcommsHandler) is diagrammed in Fig. \ref{fig:goby-ccmuc}.




\subsection{Questions}

Please hand these in with the questions from the other sections by the end of lab Thursday.
\begin{enumerate}
\item What is the raw mini-packet NMEA-0183 message received by the ``vehicle'' computer when you send the message below?
\begin{verbatim}
destination: 2
state: DEPLOY
manual_override: false
speed: 2
\end{verbatim}

\ifx\answer\undefined
\else
{\color{red}
\verb|$CAMUA,1,2,1e8a|

Note that the full message is the NMEA-0183, whereas the data payload (DCCL encoded) is the hexadecimal value 0x1eca (binary 1111011001010). Only these 13 bits (and the addressing overhead) are actually sent on the acoustic link. The rest of the message is overhead used only on the local serial connection.
}
\fi

\item Use the oscilloscope to measure the approximate length (in seconds) of the mini-packet. How long is the mini-packet (rounded to the nearest second is fine)?

\item Besides sending small commands from a shoreside computer as we did in this section, what is another possible use for a 13-bit packet?

\ifx\answer\undefined
\else
{\color{red}
13 bits is a very small amount, but this mini-packet is transmitted with high reliability. Pick your favorite from a few possibilities (this list is hardly exhaustive):
\begin{itemize}
\item Acknowledgment of up to 13 previously received data frames.
\item Multivehicle collaboration handshaking (e.g. ``start mission'', ``collecting data'', ``done collecting data'', ``come closer so we can communicate at high rates'').
\item Time synchronization
\item Basic vehicle health report.
\end{itemize}
}
\fi

\item Fig. \ref{fig:mini-packet-spectrogram} shows a partial spectrogram (which is a series of short-time power spectral densities) of the acoustic signal generated by the WHOI Micro-Modem frequency-hopping frequency shift keying (FH-FSK) mini-packet (\verb|$CCMUC|). This plot was measured directly from the output of the modem, so no acoustic channel effects such as Doppler or multipath are present. White regions on the spectrogram represent high signal strength, and black areas are low. There are seven hops, with each hop (bin) containing a binary symbol (0 or 1). I have marked binary ``0'' as a dashed line, and ``1'' as a solid line on this plot. 


\begin{center}
	\includegraphics[width=0.8\textwidth]{fh-fsk.pdf}
	\captionof{figure}{Partial spectrogram of Micro-Modem FH-FSK User minipacket.}
	\label{fig:mini-packet-spectrogram}
\end{center}

Using Fig. \ref{fig:mini-packet-spectrogram}, please answer the following questions (you may want to wait until after Thursday's lecture):
\begin{enumerate}
\item What is the hopping sequence used? (where hop 0 is the lowest frequency bin).

\ifx\answer\undefined
\else
{\color{red}
0 1 2 3 4 5 6
}
\fi

\item What is the binary symbol sequence represented by the portion of the message shown in Fig. \ref{fig:mini-packet-spectrogram}?

\ifx\answer\undefined
\else
{\color{red}
1111100 1000101 1100110
}
\fi

\item The FH-FSK modulation on the Micro-Modem uses a rate 1/2 convolutional channel code, which means that each 1 bit of input means 2 binary symbols are sent acoustically. This redundancy allows for some errors to be corrected upon receipt (using a Viterbi decoder). Keeping this in mind, how long (in seconds) is the 13-bit payload portion of the mini-packet you sent earlier? The modem adds additional overhead (synchronization, source/destination addressing and a cyclic redundancy check), which is why the full message is longer.

\ifx\answer\undefined
\else

{\color{red}
13 bits of input is 26 symbols using a rate 1/2 code. The rate is 160 symbols per second (which can be at least roughly measured off the graph), so 26 symbols would take 0.1625 seconds (an answer from 0.14 to 0.19 is fine, based on inaccuracy reading off the graph).
}
\fi

\item How much bandwidth (in Hertz) is used by this modulation scheme? How much bandwidth would be used if no hopping was performed (normal binary FSK)? Briefly, what is the purpose of the hopping?

\ifx\answer\undefined
\else

{\color{red}
About 4.5 kHz of bandwidth, .6 kHz if no hopping is used. The hopping allows the channel to clear from multipath echos at a given frequency band. This clearing time is 0.0375 seconds which is not always enough for highly reverberant environments. 
}
\fi

\end{enumerate}

%\item [Extra credit] Once you are able to command the vehicle successfully, disconnect both the coaxial cables from either the top or bottom modem. Now try to send a RETURN command to the vehicle. What happens? Why is this happening?
%
%{\color{red}
%The obvious thing would be that the message does not get received since you unplugged the connection, which is what you get some of the time. Other times, however, the RETURN command is \textit{still} received by the vehicle. This suggests that the signal is being transmitted between modems via some path that is not the coaxial cables. My guess is that the signal is showing up (weakly) on the shared power or ground lines of the modems and is getting amplified (30 dB is the default setting) by the receiving modem so that it can still be correctly received. Remember, a large amount of work is done at the signal processing level of the modem to receive weak and damaged signals from the water. It is thus not surprising some transients are picked up when both modems share a housing. Note this effect goes away when the receiving modem's analog gain (\verb|$CCCFG,AGN,0|) is set to 0 dB. 
%}


\end{enumerate}


