MiRo and Layered Control Architectures¶
This page will help you get started with the lab template code.
Overview¶

Main control loop¶
Make sure to toggle off all the checkboxes in the Program Start Block:
These settings are not saved
You'll need to repeat this procedure every time you refresh or close/open the MiRoCODE page.

The step variable keeps track of the main control loop iterations.
All the other helper functions are explained below.
Initialisation function¶
- The Move Neckblock puts the MiRo's head in a position where it can use both sonar to detect walls and edges, and vision to detect AprilTags.
- The boolean variables boost,tumbleandobstacle_aheadcan be useful for inter-layer communication.
- The tags_seenlist stores AprilTags, in the order they have been seen.
- The tumble rate should be adjusted to provide a rate of 0.2 tumble per second for your machine (details in the brief).
- The modifiedvariable is used to toggle between the base and the modified model.
All the rest of the variables here should be self-evident.
Sensing function¶
Here, all the data from the sensors is acquired and specific readings are stored for later use.
- Sonar range: readings are within a range of 0.03 to 1m. The neck position (given above) should make it possible to use this both for cliff and wall detection.
- Cliff sensors: list of two readings (left and right sensor) which can be set to return either True/False or float values. Float values allow for a more nuanced thresholding for cliff detection.
Print status function¶
This function outputs the state of the agent at each step.
Step: 1
Energy: 100.00%
Energy Flow: 0.00%
Brightness: 63.1%
Speed: 0.00m/s [0%]
Tumbles: 0
Tags seen: []
Formatted strings
Note the use of Python strings with format to print variables with a specified precision.
This way of formatting is agnostic to Python version (2 or 3) and is compatible with MiRo.  
Layer 1: Metabolism¶
This layer should compute all the energy incomings and outgoings.
At each step, it should add/subtract energy_flow to/from the energy and keep watch for the stopping condition (energy = 0).  
Layer 2: Safety¶
This layer should use the sonar and cliff_sensors to detect obstacles.
Different sonar thresholds can be used to detect both cliffs and walls.
Tip
Using two sensors is more reliable than one.
You can use the obstacle_ahead variable to subsume the higher Run layer.
Layer 3: Run¶
Makes MiRo move forward, with velocity as a function of overall brightness.
Layer 4: Tumble¶
How to do random tumbles?¶
Below is a toy example that should help you with writing the L4_Tumble layer. 

You should aim to have all your helper functions structured this way.
Layer 5: Acquisition¶
You should check if an AprilTag is detected, and if so, if it is at 1 meter or closer.
Only then you should add it to the tags_seen list.
You can use the boost variable to arrange inter-layer communication to L1.