• No results found

Finite State Machine

The core program flow is managed by a Finite State Machine (FSM). State diagram with transition conditions are illustrated in Figure 5.5. It is structured with five main states, INIT, IDLE, CONFIGURE, MISSION, and PICKUP, and three additional states: SLEEP, FAILURE, and LOWPOWER. The later mentioned serve critical sup-port roles for when the normal program flow is either encounter an exception, such as error handling or critically low battery, the system is not being operated and need to be put to sleep to save power. Below follows a brief description of each state:

RESET:Activated RESET button trigger a hard reset. The core modules and peripherals are initiated before the FSM is entered.

INIT: First state entered when system is reset by RESET button or a soft reset from system off mode, triggered by hall-effect button. The INIT state start a timer for sampling SAADC every 10 second to measure battery, and a

timer to updating FSM every 500 ms in order be responsive to change while it can be put to system-On sleep and respond when events and interrupts occur. In addition the various fsm struct flags are initialized to false. The SAADC sampled variables, battery and pressure is populated with default values, LOW_POWER_THRESHOLD+10 (+10) in order to avoid triggered LOWPOWER state before first measurements is ready while still maintain a predictable initial value. The pressure variable is initiated with the min-imum SAADC value that correspond to 0.5 V from pressure sensor, which is expected value when the sensor is not in the water. After initialization the FSM transitions directly to IDLE state.

IDLE: The IDLE state a serve as a middle state between operational and system-off mode. It is not advertising BLE, but provide by activating the hall-effect button it switches to CONFIGURE state which advertises BLE.

On the other hand it starts a one-shot timer that counts up to 2 minutes, and if not stopped (still in IDLE state) within 2 minutes the system enter system-off mode. The IDLE state also measure batter power and handles errors, so that if battery power is at threshold or below it will interrupt op-eration before a mission is configured or started. The same goes for errors.

The state is indicated by minimum birghtness on the RGB LED, shown as a dim white light.

CONFIGURE:The LED change color to pure blue to indicate the configure state. BLE is advertising and can be connected to by the dedicated desktop terminal application "BuoyancyApp", or by a serial terminal application on a phone/tablet. Default settings for two missions are pre-configured so that if no specific configuration is need the BLE application is not needed. In most situations custom configuration is necessary. Next state, MISSION, is triggered either by hall-effect button or by BLE-command "#4 Start Mis-sion". The BLE-command "#5 Go to Idle" will transition the state machine back to IDLE state and a new 2 minute timer is started to put it to sleep if inactive.

MISSION:When mission starts the BLE connection is terminated by server, the LED switches to green to be visible in the water, and mission is run ac-cording to Figure 5.8. The function prepareMission() calculate the number of configured mission, and create a new mission Log file to record the mis-sion. When final mission is finished or aborted (by hall-effect button) the new piston position is set to 0.0 to make sure the vehicle float to surface.

Transition to next state, PickUp state, is triggered when the final mission is finished, or by activating hall-effect button.

PICKUP:When piston reaches position 0.0, or bottom end limit switch and velocity is zero the LED change to yellow to signal PickUp state. BLE is

ad-vertising in order to re-configure the vehicle, transfer files, delete files or transition to another state. IDLE, CONFIGURE, and MISSION state can be entered through BLE-command. If hall-effect button is activated the FSM transitions to IDLE state. That way the hall effect button can be used to all the actively operate states without the need for BLE application. when IDLE state is entered the BLE conection is terminated and advertising is stopped.

SLEEP: The SLEEP state is entered when the timer in IDLE state reach 2 minutes. The SD card is unmounted and system-off mode is entere by call-ingsleep\_mode\_enter(), this function is called by the FSM module in order to access the function:sd_power_system_off()in main module. The system-off mode is the minimum power mode and wake-up require hard reset, (activ-ate RESET button) or activ(activ-ate the hall-effect button, which trigger a soft reset. All settings will then be initialized to default upon reset.

LOWPOWER:Battery power is measured ever 500 second during mission, and every 10 second otherwise. In the case that voltage fall below 12,8 V the LOWPOWER state is entered directly, without a regular state transition.

This is done because it is highly critical that motor has time to set piston to 0.0, corresponding to 0 meter, or float to surface. Otherwise the vehicle may be lost. The LED is changed to bright RED to signal LOWPOWER state, and SD card is unmounted. Then system is entering a while loop that that call __WFE() to put system in system-on sleep mode. Hard reset is required upon entering LOWPOWER state.

FAILURE: In the case of a detected fault in software the failure state is entered directly to avoid the fault to surface as unpredictable behavior dur-ing operation. Mission is aborted and vehicle is set to float to surface by setting piston position to 0.0. SD card in unmounted, LED is changed to PINK to signal FAILURE - fault handling - and the nordic SDK error handler takes care of the detected fault. The system require hard reset upon entering FAILURE state.

Figure 5.5: State diagram to illustrate the program flow. The state transitions are triggered by Hall effect button and/or BLE command input. In the case of detected error the FAILURE state is entered, and if low battery power is detected the LOWPOWER state is entered, both exception states require a hard reset (reset button activated).