• No results found

At system boot the mission module is initialized by setting ’mission’ and ’pid’ struc-tures to 0, then populate them with relevant default data values, example default PID coefficients, depth and time for mission 1 and 2, while mission 3 and 4 is set to 0. Then the PID instance is created and configured with direction, output limits, sample time, and initially set to manual. Manual refer to disabled, while auto refer to enabled controller.

Figure 5.8 illustrate the mission program flow. Before mission starts the function

prepareMission() is run to set the latest PID tuning coefficients and calculate the number of missions to be run. Number of missions is calculated by condition that for a given missionNr time is different from NULL.

The mission log is initiated by setting the data structure to 0, then a new mis-sion log is created and made ready to write the mismis-sion log. Finally the 10 second battery measurement timer is stopped as battery will be measured along with pressure every 500 ms during mission.

Mission is started by calling runMission(). A timer to update the mission log once a second is started, and an initial timestamp is taken to mark the start of the mission execution at time 0.0 ms.

To fetch and execute the number of configured missions a for-loop with index, i, started at 0 and continue execution until it is equal to the number of missions. the for-loop encapsulate the entire mission execution and i is increased each time a mission is finished.

On the beginning of each iteration a missionId is given equal to the current number of finished missions. and the missionId variable is used to populate the current mission with target depth and time. This is shown is the listing below:

101 uint8_t missionId = mission.missionFinished;

102 mission.currentMission = mission.missionNr[missionId];

Before the actual mission can start the mission timer compare value is updated with with current mission time value, before it is started. In addition the PID and SAADC sampe timers are started.

The core mission code section is encapsulated within a while loop that execute as long as missionId is equal to the number og finished missions, as such:

while(missionId == mission.missionFinished){

Everything within the while loop is conditionally executed, as illustrated in Figure 5.8. The follow boolean flags are used for execution within the while-loop:

• sampleSAADC: Flag to signal SAADC need to sample battery and pressure

• SAADCdataReady: Flag to signal SAADC data is ready to be read

• calculatePID: Flag to signal new PID calculation.

• missionLogUpdated: Flag to signal mission log is updated

• TMP117dataReady: Flag to signal TMP117 data is ready to be read

• fsm.hallEffectButton Flag to signal hall-effect button activated

The sampleSAADC timer set the sampleSAADC flag to true. The if statement is then evaluated to true, and both SAADC and TMP117 sensor is sampled.

When SAADC has finished sampling it sets SAADCdataReady = true. The con-dition statement evaluating SAADCdataReady is then evaluated to true, and the pressure data is used to calculate pressure represented in voltage, psi, and pascal.

Psi pressure is then used in calculate depth in meters.

Current position is read from the motor controller to write it to mission log. Then PID is used to calculate new piston position based on measured depth in meters.

When PID has finished a timestamp is taken, before the new piston position is sent to motor controller, directly based on PID output value.

As it is critical that buoyancy vehicle have enough power to float to the sur-face before battery is completely empty the measured battery power is continu-ously compared to the threshold voltage: LOW_POWER_THRESHOLD = 12.8. In any case the measured voltage is less or equal to threshold all timers are stopped, mission log file is closed, and LOWPOWER state is entered.

__WFE() is called to enter system-On mode, which is a light sleep state, which it is waken up from at any event. Then if hall-effect button is triggered this sets the flag in fsm structure, and the conditional is evaluated to true, so that mis-sion is aborted. This is a handy functionality as sometimes the hall-effect butten is triggered by accident if a magnet comes too close the sensor. It would be a waste of time to have to wait for mission to finish in the case where a, or several, mission was configured.

When mission timer handler is triggered the missionFinished value is incremen-ted, which make the while loop evaluate to false. The mission relevant timers are then stopped to make ready for a updated timer value, before new mission is star-ted, if configured. Otherwise all missions are finished. The Motor is then set to float the vehicle to the surface, and a timer is set to measure motor velocity. This trigger the motorstop() function when velocity reach 0, in order to put motor to sleep and save power. The timer is need to add a delay in order for the motor start and gain velocity.

Finally the current mission log file is closed, and timer for measuring battery every 10 seconds is restarted to keep track of battery voltage.

Figure 5.8:Block diagram to illustrate mission program flow; A function is called to make initial calculations before the mission starts, the a for-loop is entered to run all the configured missions where each mission is running inside a while loop until a timer-based interrupt increment a variable that breaks the loop.