Nov 16, 2026 | 1445 words | 14 min read
18.7. Milestone 3 - Iteration #1: Greenhouse Controller (Team)#
At a Glance
What you will do: Build your Milestone 3 design as a set of Python functions and test it against the acceptance test.
What you will submit: As a team, your student_controller/ package.
This task spans Milestones 3 and 4
You build your controller in two passes, which match the two iteration cycles in your report.
Milestone 3, Iteration #1: Complete Steps 1 to 2. Write your first draft from your flowchart and run it until the crop survives the mission. Submit the working first draft at TP3.
Milestone 4, Iteration #2: Complete Steps 3 to 4. Analyze the controller with your own plots and tune it until it is efficient and reliable. Submit the tuned final version at TP4.
IMPORTANT: ONLY COMPLETE STEPS 1-2 FOR MILESTONE 3.
18.7.1. How Your Controller Works#
Your controller is a small package, a folder named student_controller/ that
holds one function per actuator and a main.py that runs them.
The folder holds these files.
heater.py,leds.py,co2.py,pump.py: one control function each, for exampledef control_heater(readings, setpoints, dt, state). Each returns a command from 0 to 100 for its actuator.main.py: a givencontrol_all(readings, setpoints, dt, state)that calls your four functions and packages their outputs.You fill in the four actuator functions AND leave
main.pyand__init__.pyas they are.hardware.py: your team’s hardware selection, two lines that name the glazing and the heater (GLAZINGandHEATER_W). The simulator and the acceptance test both read it.
Some strategies must remember a value between ticks. PI remembers its integral, and
PID also remembers the previous error. These values are stored in a plain dictionary
named state that is passed to your functions on every tick. Your function reads the
old value and writes the new value back, so the new value is available on the next
call. The example below shows the mechanism with a running total.
def running_total(amount, state):
# state is a plain dict handed in from outside, so it survives between calls
state["total"] = state.get("total", 0.0) + amount
return state["total"]
state = {} # created once, outside the function
print(running_total(1, state)) # 1.0
print(running_total(1, state)) # 2.0 <- state kept the total from the first call
A value stored in state is kept from tick to tick. A local variable created inside
the function starts over on every call. Your PI integral needs only this mechanism.
All four actuator files share one state dictionary, so start every key with your
actuator’s name, such as state["heater_total"]. Two files that both write
state["total"] overwrite each other without any error message.
The template package already has the four function files in place, and you write the
control logic in them.
18.7.2. Step 1: Implement Your Design#
Do this work inside your unzipped sandbox folder, which is the same kit you used to
operate the greenhouse in Milestone 2. It contains the student_controller_template/
folder, the martian_greenhouse_sim/ package, and two practice acceptance checks:
practice_test_m3.py for Milestone 3 and practice_test_m4.py for Milestone 4.
Copy the
student_controller_template/folder to a folder namedstudent_controller/in that same place. You fill in the four actuator functions. Leavemain.pyand__init__.pyas they are.Assign
GLAZINGandHEATER_Winhardware.pyto the hardware your Milestone 3 design memo chose.Translate your Milestone 3 flowchart into your four control functions. Every box in the flowchart should be recognizable in the code. Your code must follow professional programming standards.
Start with the parameter values from your M3 parameter table.
Divide the work by actuator, as the roles table in section II of your report assigns it. Each of the four actuators (heater, LEDs, CO2 valve, pump) has its own file and a named owner. The owner is responsible for that function, its rows in the parameter table, and its tuning cycles. Mark the owner at the top of that file. The team jointly owns the integrated behavior in
main.pyand the CO2 sensor-trust rule. Integrate and review the code together.
Run a first mission from inside your sandbox folder:
# quick_run.py, saved next to your student_controller/ folder
from martian_greenhouse_sim.simulation import (
load_student_controller,
load_student_hardware,
run_simulation,
)
controller = load_student_controller(search_dirs=["."])
hardware = load_student_hardware(search_dirs=["."])
result = run_simulation(days=30, seed=42, controller_cls=controller, hardware=hardware)
print(result.summary())
result.summary() prints the mission report: sols survived, temperature
error, CO2 and humidity performance, any safety events, and your hardware’s
worst-sol energy and launch mass.
18.7.3. Step 2: Run the Practice Acceptance Check#
Your sandbox folder includes a practice copy of the client’s Milestone 3 acceptance
test, practice_test_m3.py. It scores the same criteria as the graded test on one
30-sol mission. With your student_controller/ package in place, run the practice check
from inside your sandbox folder.
python practice_test_m3.py
The practice check runs the following tests.
the hardware is declared and on the menu
the file loads
the mission completes
no CO2 toxic event
plants survive >= 28 of 30 sols
mean temperature error \(< 2.5\ ^\circ\text{C}\)
CO2 in band \(\geq 65\%\) of the time
relative humidity in \(40\) to \(60\%\) at least \(25\%\) of the time
deterministic repeat runs
launch mass (glazing, heater, and battery) \(\leq 600\ \text{kg}\)
The graded test flies several 30-sol missions that you cannot see. Each one has a dust storm and K30 freezes at different times, and every test must pass on every mission. The practice check flies the mission with seed 42. To fly a different mission, give the practice check another seed.
python practice_test_m3.py --seed 7
Important
The acceptance test runs your controller on the hardware declared in
student_controller/hardware.py. If the declaration is missing or names hardware that
is not on the menu, the hardware test fails and the mission runs on the as-built
hardware (twin-wall glazing, \(2000\ \text{W}\) heater) instead. You do not modify the
simulation files.
Warning
A controller that hard-codes an action sequence against the practice run will likely fail the graded test. Your program must respond to your sensor readings, and it must not depend on one specific set of circumstances.
Read these two notes before you tune:
Gap analysis (graded, in the tuning log): Before tuning, compare these acceptance criteria with the criteria for success your team wrote in Milestone 1 (report section III.b). Identify where they differ and which set is stricter. The gap analysis helps you understand the contract you are held to. It does not replace your numbers with the client’s. Compare what the acceptance test asks of humidity with the client’s original \(40\) to \(60\%\) requirement, and connect it to your M3 achievability assessment. Compare the worst-sol energy and launch mass that the practice check reports with your Milestone 2 estimate, and explain the difference.
Test on several missions: Run the practice check on several seeds of your own choosing before you submit. A controller tuned to one specific storm is not a viable solution, and none of the graded missions is the practice mission.
18.7.4. Step 3: Write Your Analysis Script#
A score reports whether the controller worked. The mission log shows why and where it
did not. Write analyze_mission.py to do the following.
Run the mission with
log_records=Trueand save the full log.result = run_simulation(days=30, seed=42, controller_cls=controller, log_records=True) with open("mission_log.csv", "w") as f: f.write(result.to_csv())
Load
mission_log.csvwith pandas and produce at least three plots with matplotlib. Save the plots as PNG files, formatted to the course’s professional standards, with titles, axis labels with units, and legends. Produce these three plots.Temperature tracking:
t_in_cvssol, with the day/night setpoints visible, the dust-storm window evident, and the \(\pm 2\ ^\circ\text{C}\) band shaded or marked.CO2 data:
co2_ppm(actual),co2_k30_reading, andco2_scd41_readingvssolon one chart. The chart shows how your sensor-trust logic performs during the freeze.Effort: at least one actuator trace (e.g.
heater_pctvssol). The log’selectrical_wcolumn is the heater and LED draw in watts. Summed over each sol, it gives the energy your battery is sized to.
Print the headline metrics (mean temp error, % CO2 in band, sols survived, etc.) so each tuning run produces a one-line scoreboard.
18.7.5. Step 4: Tune and Log at Least Two Cycles#
Record every tuning cycle in your tuning log. For each cycle, the log records these items.
Who ran the cycle
The parameter or parameters changed, and why (cite a plot or metric)
The expected effect on a named metric
The new values
The resulting metrics, next to what you expected
The verdict: keep, revert, or adjust further
Write down the expected effect before you run the cycle. At least one full cycle per team member is required, and every member runs and documents at least one.
Use the diagnostic table at the end of the primer to read your plots. Sawtooth, offset, overshoot just after sunrise, and noise-chasing each have a recognizable signature.
Optional stretch: Use mission logs from several seeds to calculate the mean and standard deviation of your temperature error. Then use the normal model to estimate the probability that a random minute stays within 2 C of setpoint. This value shows how reliable your controller is across missions.
18.7.6. Submission#
IMPORTANT: ONLY COMPLETE STEPS 1-2 FOR MILESTONE 3 AND SUBMIT.
Make one team submission to 18.7 Milestone 3 - Iteration #1: Greenhouse Controller (Team) in Gradescope, with all members added.
The submission contains these items:
Your
student_controller/package, includinghardware.py, which passes the acceptance test you ran in Step 2.
Submit before the deadline on the Course Schedule.
Learning Objectives
- PR01:
Programming Standards - Develop code that follows established programming standards and conventions.
- PR02:
Data Storage - Store and access data appropriately.
- PR04:
Modular Programming - Create and use modular code components, including scripts and user-defined functions.
- PR05:
Code Structures - Understand and write sequential and non-sequential code structures to achieve a programming objective.
- PR06:
Translate Program Descriptions - Translate written descriptions or flowcharts into working code.
- PR07:
Test Cases and Tracking - Test and track information through flowcharts and programs.
- PR08:
Debugging - Debug and refine code so that it achieves its objective without dynamic or semantic errors.
- EB05:
Iterative Improvement - Present findings from iterative testing or optimization efforts.
- DV01:
Table and Plot Formatting - Prepare tables and plots for technical presentation with proper formatting. Include descriptive titles, axis labels with units, appropriately scaled axes, and visible legends.
- AI05:
Communicate clearly and effectively about AI usage and the choices surrounding it in a given field.