MATLAB Conditions, Loops, and Command Routines
English translation
Learning objectives
Execute the same movement plan repeatedly, and write rules to stop on invalid inputs or battery conditions.
Execution conditions
Use MATLAB or MATLAB Online. The basic activities run in MATLAB alone and do not require a drone connection.
Execute a list in order
Starting conditions
Use the example's four movements, starting at (0,0).
Procedure
- Use for to accumulate movements and save the position at each step.
- Check final position and total travel distance separately.
Results to retain
- Path plot and position at each step.
moves_m=[.5,0;0,.5;-.5,0;0,-.5];
path_m=[0,0;cumsum(moves_m,1)];
plot(path_m(:,1),path_m(:,2),'o-');axis equal;grid on;
% TODO: Compare positions at each step with the for version.
Record results and self-check
Step | |
Movement x/y [m] | |
Cumulative position [m] |
Points to check
- Have you avoided reporting total travel distance as 0 just because the path returns to the origin?
- Did you include the starting point in the first row of the position array?
Add proceed/stop conditions
Starting conditions
The battery readings before movement are [80,78,15,14]%; proceed only at 20% or higher.
Procedure
- Check the battery before movement and break if it is below the threshold.
- Record the stop step, reason, and final position.
- Check how an input of 20%, equal to the threshold, is handled.
Results to retain
- Stopped paths and boundary checks.
battery_pct=[80,78,15,14];position_m=[0,0];
for k=1:size(moves_m,1)
if battery_pct(k)<20
fprintf('Stopped before step %d\n',k);break;
end
position_m=position_m+moves_m(k,:);
end
disp(position_m);
% TODO: Check the 20% input and the stopping path.
Record results and self-check
Step | |
Battery [%] | |
Proceed? | |
Position/stop reason |
Points to check
- Did you check the battery before moving?
- Did execution end without continually repeating the last command?
Apply the same rules to another plan
Starting conditions
Use the triangular path [.3,0;0,.4;−.3,−.4] as input.
Procedure
- Write a function taking the plan array, battery readings before each movement, and battery threshold as arguments. Use the preceding activity's movement accumulation and battery checking code inside the function, and return stepwise positions and the stop reason.
- Run the triangular plan as valid input. Then run a copy with just one movement changed to exceed the allowed length. First record the allowed length, the changed movement's index, and its old and new values; check to stop before that movement.
- Record the completed movement count, final position, and stop reason for both runs side by side. Identify coordinate units and stop rules that should remain when changing aircraft.
Results to retain
- A function or function contract, and results for valid and invalid plans.
triangle_m=[.3,0;0,.4;-.3,-.4];
assert(norm(sum(triangle_m,1))<1e-12);
% TODO: Separate responsibilities into validatePlan and simulatePlan.
Record results and self-check
Plan | |
Check result | |
Final position | |
State to return |
Points to check
- Have you kept command transmission functions separate from plan validation?
- Can you reproduce normal and stopped paths without an actual aircraft?