# Program Control

:::{topic} Learning goals
- evaluate a solution for an industrial control problem and explain the relevance of programmable systems for these problems
- visualize a program using flowchart
- apply program control syntax (to implement program)
:::

## Problem
:::{commons-figure} https://commons.wikimedia.org/wiki/File:Astronaut_Charles_Conrad_-_Cockpit_-_Lunar_Landing_Training_Vehicle_-_Ellington_AFB_(EAFB),_TX_(S69-56058).jpg
:height: 700px
25 Oct. 1969 - Commander of the Apollo 12 lunar landing mission, sits in the cockpit of a Lunar Landing Training Vehicle (LLTV) during a lunar simulation flight at Ellington Air Force Base.
:::

## Various control systems

### Mechanical ⚙️

<!--
[Centrigual governor controlling the speed of an engine](https://www.reddit.com/r/mechanical_gifs/comments/ak45bx/a_centrifugal_governor_is_a_specific_type_of)
-->
:::{commons-figure} https://commons.wikimedia.org/wiki/File:Steam_engine_in_action_%28two-thirds_speed%29.gif
Steam engine using a centrifugal governor
:::

### Electronic 💻

:::{commons-figure} https://commons.wikimedia.org/wiki/File:Arduino_ftdi_chip-1.jpg
:width: 600
A chip on an Arduino board
:::

### Manual 🤹🏼

:::{commons-figure} https://commons.wikimedia.org/wiki/File:Hands_on_wheel.jpg
:::

:::{list-table} Comparison of three control methods
:header-rows: 1
:width: 400px

* - Type
  - Pro 👍
  - Con 👎
* - Mechanical ⚙️
  - simple, reliable
  - bulky, complex systems not feasible
* - Electronic 💻
  - flexibility through programmability (computers), small
  - complex, reliability
* - Manual 🤹
  - very flexible, can cope with an unknown environment
  - costly, unreliable for repetitive tasks
:::

Regardless of the method, we use electronics to connect all systems together

:::{exercise}
As an engineer in the R&D department of the space agency you are designing the following subsystems of a lunar lander:

1. a sensor for pointing the antenna to the world 📡
1. subsystem for traveling from space to moon's proximity 
1. subsystem for landing on the moon 🛬

For these scenarios:
1. *pick* one control method
2. *explain* with an example how electronics (and computers) can help in this scenario

Steps:
1. ~1 min individually
1. ~2 min compare with your partner
1. Deliverable: Write down your result and keywords for explanation
:::

Assumption: We use a programmable electronic system to solve our problem.


## Mission: Safe landing

:::{figure} https://gymnasium.farama.org/_images/lunar_lander.gif
Lunar lander trying to land
:::

Problems:
- limited fuel 🌡️
- crash if high speed 💥

How do we control ❓

## Flowcharts

:::{plantuml}
:align: center

@startuml
start

:Look at the clock;

if (is time >= 12:00 and time < 12:30?) then (yes)
  #palegreen:Eat;
elseif (is time >= 19:00 and time < 20:00?) then (yes)
  #palegreen:Eat;
else (no)
  #pink:Do not eat;
endif

:Work for 20 to 30 minutes;

stop
@enduml
:::

:::{warning}
We are repetitively running the program, i.e., if we are at the bottom, we come back to the beginning.
:::

:::{exercise}
:label: flowchart

Draw a flowchart for the following program
- if the altitude is higher than 100
  - the thruster must be off.
- if the altitude is lower than or equal 100
  - turn on the thruster
- if the altitude lower than or equal 0 (landing)
  - the thruster must be off.

Steps:
1. ~2 min individually
1. ~1 min compare with your partner
:::

## Program control syntax

General:

```c
if (question1) {
  action1;
} else if (question2) {
  action2;
} else {
  action3;
}
```

Question examples:
- *is the time 1?*
  - `time == 1`
- *is the temperature lower or equal 20?*
  - `temperature <= 20`

Action examples:
- *set the speed to 10*
  - `speed = 10`

::::{grid} 2
:::{grid-item-card}
In words:

- if `temperature` is greater than 30 then activate `air_conditioner`
- else if `temperature` greater than 25 activate `ventilator`
- else turn everything off.
:::
:::{grid-item-card}
In code:
```c
if (temperature > 30) {
  air_conditioner = 1;
} else if (temperature > 25) {
  ventilator = 1;
} else {
  air_conditioner = 0;
  ventilator = 0;
}
```
:::
::::


::::{exercise}
Write code for landing using the following flowchart

:::{plantuml}
:align: center

@startuml
start

if (is altitude > 100?) then (yes)
  #pink:thruster off;
elseif (is altitude > 0?) then (yes)
  #palegreen:thruster on;
else (no)
  #pink:thruster off;
endif

stop
@enduml
:::

using the following *independent* lines:

```
} else {
} else if (altitude > 0) {
if (altitude > 0) {
thruster = 1;
thruster = 0;
thruster = 0;
}
```

<iframe width="1200px" height="500px" src="https://godbolt.org/e#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:5,endLineNumber:8,positionColumn:5,positionLineNumber:8,selectionStartColumn:5,selectionStartLineNumber:8,startColumn:5,startLineNumber:8),source:'%23include+%3Cstdio.h%3E%0A%23include+%3Cstdbool.h%3E%0A%0Aint+control(int+altitude)+%7B%0A++++int+thruster+%3D+0%3B%0A%0A++++///+YOUR+CODE+HERE%0A++++%0A++++%0A++++return+thruster%3B%0A%7D%0A%0Aint+test(int+altitude)+%7B%0A++++int+thruster+%3D+control(altitude)%3B%0A++++printf(%22For+altitude+%25i,+your+thruster+is+%25i%5Cn%22,+altitude,+thruster)%3B%0A++++if+(altitude+%3E+100+%26%26+thruster+%3D%3D+0)+return+true%3B%0A++++if+(altitude+%3C%3D+100+%26%26+altitude+%3E+0+%26%26+thruster+%3D%3D+1)+return+true%3B%0A++++if+(altitude+%3C%3D+0+%26%26+thruster+%3D%3D+0)+return+true%3B%0A++++return+false%3B++//+Means+error%0A%7D%0Aint+main()%7B%0A++++test(150)%3B%0A++++test(100)%3B%0A++++test(50)%3B%0A++++test(0)%3B%0A++++test(-1)%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:executor,i:(argsPanelShown:'1',compilationPanelShown:'0',compiler:cg151,compilerName:'',compilerOutShown:'0',execArgs:'',execStdin:'',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'',overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:'1',wrap:'1'),l:'5',n:'0',o:'Executor+x86-64+gcc+15.1+(C,+Editor+%231)',t:'0')),header:(),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4"></iframe>

Steps:
1. Work ~2 min individually. The program automatically tests your code.
2. Compare your result with your neighbor and help them.
::::


## Appendix

In a programmable computer, program is programmed into the memory and executed by the computer.
:::{plantuml}
:align: center
@startuml
package "Programmable computer" {
  
  [processor] as cpu
  [memory\n- ADD x1, x2\n- LOAD x3 ... (program)] as mem

  mem --> cpu : read
  cpu --> mem : write
}

@enduml
:::


<!--
different 
what could they reach in the discussion
what is the lower 
plenum discussion + menti feedback
high-order double check
what happens if you do it wrong

fat 

instead use explain
determine  
too long
dramatic what happens if not

using different activities is less boring

higher-order: there is not only one answer

too long
dramatic what happens if not

maybe some background needed
train, mechanical control
feed the coal

what were the difficulties
discussion about the solving

coming back to the first question again, then they have some background
-->
