\documentclass[12pt]{article}


\usepackage[pdftex,pdfpagelabels,bookmarks,hyperindex,hyperfigures]{hyperref}


\usepackage[margin=1in]{geometry}
\usepackage{float}
\usepackage{adjustbox}
\usepackage[table]{xcolor}
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{listings}

\setlength{\parskip}{1em} % Add spacing between paragraphs
\setlength{\parindent}{0em} % Remove indentation at the start of paragraphs


\pagestyle{fancy}
\fancyhf{} % Clear all headers/footers
\lhead{Bobot}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0pt}

% --- Title and TOC ---

\begin{document}

\title{4P78 Project Documentation}
\author{
    Parker TenBroeck 7376726\\
    pt21zs@brocku.ca
    \and
    Brett Terpstra 6920201\\
    bt19ex@brocku.ca
}
\date{\today}

\makeatletter
\begin{titlepage}
	\def \LOGOPATH {brock.jpg}
	\def \UNIVERSITY {Brock University}
	\def \FACULTY {Faculty of Mathematics \& Science}
	\def \DEPARTMENT {Department of Computer Science}
	\def \COURSETITLE {COSC 4P78: Robotics}
	\def \SUPERVISOR {Earl Foxwell}
	
	
	\vfill
	\begin{center}
		\includegraphics[width=0.6\textwidth]{brock.jpg}
		\fontsize{14pt}{14pt}\selectfont
		\vfill
		\UNIVERSITY \\
		\FACULTY \\
		\DEPARTMENT \\
		\vfill
		\fontsize{18pt}{18pt}\selectfont
		\textbf{\COURSETITLE} \\[0.5cm]
		\textbf{\@title}
		\vfill
		\fontsize{14pt}{14pt}\selectfont
		Prepared By: \\[0.5cm]
		
		\begin{tabular}[t]{c}
			\@author
		\end{tabular}\par
	
	    \vfill
		Instructor: \\
		\SUPERVISOR
		\vfill
		\@date
	\end{center}
\end{titlepage}
\makeatother

\newpage

\section{Introduction}
What if you wanted to make a robot to map out a room but you only had two wheels and two motors at your disposal? Well, we have the perfect solution for you! Introducing the Bobot, a two-wheeled robot which is more of a circus act than useful. \\
With only two wheels, it requires active stabilization, meaning it'll fall over due to its own stupidity if not tuned correctly - but it’s very cute while doing so! This little guy can whiz around your house at the speed of a snail and (poorly) map out a room to your heart’s content.

\section{Instructions}
\begin{itemize}
	\item Hold robot in a vertical upright position clear of obsticals
	\item Place battery in top compartment
	\item Plug battery in and wait for robot to initialize
	\item Once robot has booted let go and step away
	\item Connect computer to network \texttt{MEOW}
	\item launch vidualization and control software
	\item Pan with WASD and zoom with scroll or +/-
	\item Control by clicking on screen to set target position
	\item use buttons/dropdowns to set/zero/get values from the robot
\end{itemize}

\section{Problem Set}

\subsection{Balancing}
We have a vertical two-wheeled robot, and I hope it's fairly obvious when I say that it is only dynamically stable, meaning it requires control systems that constantly read from several sensors to keep it not only upright, but also stationary. This presents a challenge, because we need a system that can maintain balance across a variety of situations, respond dynamically to unpredictable changes, and still be able to move through an environment based on user commands.
\subsection{Odometry}
Because we intend to map an area with the robot we need to know where we are. This issue is made slightly more challenging due to the fact we are in nearly constant motion keeping ourself upright.
\subsection{Target Positions}
The problem of target positions like odometry is made more challenging due to the fact we are almost constantly in motion. We simply cannot "not move" when we are in the location we want to be as we'd fall over. This means the target position will need to be a "best effort" battle to keep in the same place as best as we can.
\subsection{Communication}
The only source of computation used is a single \texttt{ESP8266} microcontroller, this in combination with the strict timing requirements on the duration of the control loop means the communication between the robot and the control/mapping software needs to be quick and efficient.

\section{Approaches}


\subsection{Balancing}
The balancing is done by a single PID controller where the pitch of the robot is fed as the input to the controller and the output is $\pm1$ which represents motor speed forward/backwards

\subsection{Odometry}
The odometry system uses a simple arc length based system\\\\
\begin{math}
	b = \text{Distance between wheels}\\
	\triangle R = \text{Delta distance of right wheel}\\
	\triangle L = \text{Delta distance of left wheel}\\
	\triangle D = \frac{\triangle R + \triangle L}{2}\\
	\theta_n = \theta_{n-1} + \frac{\triangle R - \triangle L}{b}\\
	\theta' = \frac{\theta_n + \theta_{n-1}}2\\
	x_n = x_{n-1} + cos(\theta')\times \triangle D\\
	y_n = y_{n-1} + sin(\theta')\times \triangle D\\
\end{math}

\subsection{Target Positions}
The target position is achieved through the combination of two PID controllers. One is responsible for turn and simply adds motor speeds directly to the output of the balancing PID controller. One thing to note is the max turn speed is only 15\% of the max motor speed and decreases linearly as the angle goes to $\pm12^\circ$ to increase stability. The second PID controller is responsible for moving forward/backwards. It works by being the input to the balancing PID controller and setting the angle set point. It is bound to $\pm2^\circ$ and will bias the robot to move in one direction.\\
These work together by calculating the heading and displacement needed from the robots current position to the target position and setting those values as the input to the aforementioned PID controllers.
\subsection{Communication}
Communication is done through a stateless but sequenced and tagged UDP protocol. 


\lstdefinelanguage{RustLike}{
	sensitive,
	morekeywords={struct, u32, f32, String, union},
	morecomment=[l]{//},
	morecomment=[s]{/*}{*/},
%	
	showspaces=false,
	showtabs=false,
	showstringspaces=true,
%	   
	commentstyle=\color{codegreen},
	keywordstyle=\color[rgb]{0.75, 0, 0},
	stringstyle=\color{codepurple},
	backgroundcolor=\color[gray]{0.97},
	rulecolor=\color[gray]{0.90},
%	
	numberstyle=\tiny,
	basicstyle=\ttfamily\footnotesize,
	breakatwhitespace=false,         
	breaklines=true,               
	numbers=left,                    
	numbersep=5pt,                   
	tabsize=2
}

\begin{lstlisting}[language=RustLike]
struct{
	sequence: u32,
	id: u32,
	type: union{
		zero: ZeroPacket,
		get_data: GetDataPacket,
		set_target: SetTargetPacket,
		everything: EverythingPacket,
		get_data_plus: GetDataPlusPacket,
		get_pid: GetPIDPacket,
		set_pid: SetPIDPacket,
	}
}
\end{lstlisting}

Each type has a unique associated ID, and also potentially associated with the packet. Each packet is responsible for responding. The response follows a similar style, The sequence value is always the same, the ID is typically the same but in special cases like errors can be some other value, and the data responded with is upto the packet. Even if response packets don't contain any data they are still sent.

\section{Challenges}

\subsection{Balancing}
Balancing took the longest out of every task combined. It required research on not only PID control loops but also on different libraries and calibration requirements for our \texttt{MPU6050} gyroscope. These difficulties were compounded by the fact that we did not know which part/section would cause the robot to "randomly flail about and violently crash into the wall/floor". 

\subsection{Odometry}
We originally didn't have encoders to keep track of our position and instead attempted to use the accelerometer to calculate displacement/heading to save money on parts. This however, did not pan out. The first problem we ran into was our heading drifting. We knew it was going to be an issue after reading docs as the module does not have a magnetometer builtin. That aside we tried a good old fashion "how bad could it possibly be" and oh boy did it drift fast. The second issue was "double integration over a noisy inaccurate signal" lead to our calculated position quickly exiting the stratosphere. These issues very quickly lead us to just get encoders.

\subsection{Target Positions}
Using the bias towards a direction by setting the target angle was not the first approach we used. We originally directly added a movement speed to the motors like we do for turning. This however caused the robot to become unstable in certain situations. We eventually abandoned the approach in favor of the one used currently.

\subsection{Communication}
Since we have limited processing power/time per loop iteration we need to be smart in how we receive and transmit data to our mapping software. For this reason we designed a stateless UDP based network protocol over top the esp8266 Wifi \& UDP libraries. \cite{wifi_lib}\\

We originally used a TCP + Web server based library for this as it allowed us to use HTML + JS to display information but it continually had processing requirements the controller could not meet.

\section{Resources Used}
\begin{itemize}
	\item ESP8266 core libraries to note the Wifi and UDP libraries. \cite{wifi_lib}
	\item \texttt{PID\_V1} The PID library of choice. \cite{pid_lib}
	\item Adafruit VL53L0X was used the library used to interface with the time of flight sensor used for mapping. \cite{dist_lib}
	\item The AS5600 library was used to interface with the AS5600 magnetic encoders on each wheel. \cite{enc_lib}
	\item The MPU6050 library was used to interface and interpret the accelerometer and gyroscope data. \cite{gyro_lib}. It required modification to work with our hardware as it was a knockoff and the device ID was different than what it was expecting.
	\item A blog by the author of the PID library used was very helpful when tuning and configuring the PID controls in the robot. \cite{pid_help}
	\item A paper on odometry for robots with differential steering which we based our odometry system off of. \cite{odom_help}
\end{itemize}


\nocite{*}
\bibliographystyle{IEEEtran}
\bibliography{references}

\end{document}
