## 4 Electrical System
### Real-Time Controller
The choice to integrate the motor control and the sensor acquisition system was made to reduce the number of peripherals, as well as the power consumption. The SeaLeon controller was implemented using a 32-bit SAMD20 microcontroller from Microchip that features an ARM Cortex-MO processor, and can easily interface to peripherals through multiple USART and I2C compatible pins. The software implemented on the microcontroller is modular and has a hierarchical design with three layers: an input/output layer, a task layer, and a control layer. A hierarchical design was chosen because of its adaptability, due to the separation of hardware specific drivers from high-level logic. Specifically, this architecture allows the easy integration of new devices, since the same control layer can be used, and only the input/output layer should be updated. The input/output layer includes device drivers for all of the peripherals used for the SeaLeon. These include the USART, the I2C bus, the analog-to-digital converter (ADC), and the pulse width modulation (PWM) generator. The task layer contains the high-level modules that control the peripherals. The control layer is responsible for managing the state of the sailboat and performing tasks at periodic intervals. To handle the multiple tasks, a clock interrupt handler expires every 200 milliseconds. When the handler is triggered, custom coded tasks are called, each at a unique period, as summarized in Table 2. This allows the controller to read the sensors, log the data, and control the motors at a pre-configured rate. In a future software design, the tasks running on the controller can be defined using a real-time operating system such as Free RTOS that is compatible with the ARM processor chosen. This can identify different levels of priorities for the tasks, and can be programmed to sleep to further reduce the energy consumption of the microcontroller. The sailboat has two data logging modes: _test_ and _deploy_. In test mode, the data are sent using an Xbee Series 1 radio module. The data can then be visualized in real time to a host computer for processing. The system logs sensor data including GPS coordinates, wind direction, and compass readings. It also logs controller data such as the next waypoint, the course selected by the navigation algorithm, and the rudder and sail positions. In deploy mode, data are sent less frequently via an XEOS Onyx device that communicates over the Iridium network. Log messages from the XEOS Onyx device are logged to a remote server. The data can then be viewed through a web client provided by XEOS. To enter the waypoints, a graphical user interface on a host computer is used to define the waypoint characteristics and send them to the EEPROM. For this purpose, the microcontroller regular navigation mode is interrupted and changed to a waypoint load mode. In this mode, a waypoint entry can be sent to the sailboat which indicates the waypoint index, the GPS coordinates of the waypoint, the waypoints threshold radius, and the index of the next waypoint in the path. One of the most critical functions of the controller is the navigation algorithm, shown in Figure 2(a). This algorithm uses information from all of the sailboat's peripherals, as well as the waypoints from the EEPROM. The goal of the algorithm is to determine how to update the rudder and sail to make the sailboat reach the waypoint. Each waypoint has a threshold radius that is indicated when uploading the waypoint to the EEPROM. When the distance between the boat's position and the waypoint is within the threshold radius, a validation counter is incremented. After five consecutive readings within the waypoint threshold radius, the algorithm advances to the next waypoint. The course selection is chosen to be the bearing between the boat's current position and the target waypoint. However, there are situations when the direction of the wind may make it impossible for the sailboat to go in that direction. The sailboat cannot travel in the direction directly opposite to the wind.