Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ jobs:
working-directory: site
- name: Validate Hawk app contract
run: node scripts/deploy-check.mjs --contract >/dev/null
- name: Test server progress store
run: node --test server/test_progress.mjs
- name: Test Python runner
run: python3 -m unittest discover -t . -s runner -p "test_*.py" -v
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

__pycache__/
*.pyc
.data/
3 changes: 2 additions & 1 deletion .hawk/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"port": 3000,
"health": "/health",
"database": null,
"data_dir": null,
"data_dir": "/data",
"required_env": [
"APP_URL"
],
"optional_env": [
"PORT",
"HOST",
"DATA_DIR",
"RUNNER_URL",
"HAWK_SUITE_ISSUER",
"HAWK_SUITE_TRUST_PROXY",
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ ENV APP_REVISION=${APP_REVISION}
LABEL org.opencontainers.image.revision=${APP_REVISION}
ENV NODE_ENV=production PORT=3000 HOST=0.0.0.0
RUN groupadd --system --gid 1001 nodejs \
&& useradd --system --uid 1001 --gid nodejs hawklearn
&& useradd --system --uid 1001 --gid nodejs hawklearn \
&& mkdir -p /data \
&& chown hawklearn:nodejs /data
COPY --from=builder --chown=hawklearn:nodejs /app/site/dist ./site/dist
COPY --chown=hawklearn:nodejs server.mjs ./server.mjs
COPY --chown=hawklearn:nodejs server ./server
COPY --chown=hawklearn:nodejs .hawk ./.hawk
COPY --chown=hawklearn:nodejs scripts/deploy-check.mjs ./scripts/deploy-check.mjs
USER hawklearn
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ services:
- "3000:3000"
environment:
APP_URL: http://localhost:3000
DATA_DIR: /data
RUNNER_URL: http://hawk-learn-runner:4000
volumes:
- hawk_learn_data:/data
networks:
- default
- learn_runner
Expand All @@ -34,3 +37,6 @@ services:
networks:
learn_runner:
internal: true

volumes:
hawk_learn_data:
Binary file added docs/screenshots/pr-30/after-index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/after-lesson1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/after-lesson2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/after-lesson3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/after-lesson4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/before-index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/before-lesson1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/before-lesson2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/before-lesson3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/pr-30/before-lesson4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 51 additions & 55 deletions lessons/01-flowcharts/README.md
Original file line number Diff line number Diff line change
@@ -1,85 +1,81 @@
---
title: "Programs are step-by-step"
goal: "See a program as a flowchart of blocks and watch the computer evaluate it one step at a time."
goal: "Follow a robot flowchart from START to END and watch the computer run one instruction at a time."
order: 10
section: "Programming with Blocks"
---

# One step at a time

Before we write a single line of code, let's get the big idea out of the way:
**a program is a list of steps, and the computer does them one at a time, in
order.** It never does two things at once by accident, and it never skips
ahead. It finishes one step, then moves to the next.
Before we write a single line of Python, let's get the big idea out of the way:
**a program is a list of instructions, and the computer runs them one at a time,
in order.** It never skips ahead, and it never runs two steps at once by
accident. It finishes one box, updates its memory, and follows the arrow to the
next box.

A good way to picture this is a **flowchart** — boxes joined by arrows. Each box
does one small thing, and the arrows show what feeds into what. The computer
follows the arrows.
A **flowchart** shows that exact journey from **START** to **END**:

The block editor here is exactly that: a little flowchart that does
math. Read it from top to bottom, the same way you read code:
- Each **box** is one instruction — like storing a number or doing math.
- Each **arrow** shows which step runs next.
- **Robot Memory** at the bottom shows the named values the robot is remembering
right now.

- The **number** blocks hold values.
- The **+** and **×** blocks each take *two* inputs (the arrows coming in from
the blocks above them) and produce *one* output.
- The **result** block at the bottom shows the final answer.
The flowchart here calculates an FRC robot's **autonomous score**: `3` coral at
`4` points each, plus a `3`-point bonus for leaving the starting zone.

Press **▶ Run step by step** and watch. The computer can't multiply until it
knows what `3 + 4` is, so it works out the inner block first, then feeds that
answer forward. That order — inputs before the things that use them — is the
whole game.
Press **▶ Run** (or click **⏭ Step** to advance one instruction at a time) and
watch **Robot Memory** update as each box lights up. You can also click
**Peek at Python** to see the exact same steps written as real Python code.

```blocks
preset: sequence
```

# Blocks carry values
# Order matters because steps share memory

So this flowchart computes `(3 + 4) × 2`. Notice the computer had to figure out
`3 + 4 = 7` *before* it could do `7 × 2 = 14`. It literally cannot do the
multiply first — one of its inputs doesn't exist yet. **Every block has to wait
for its inputs to be ready.**
Notice how `points` changed twice during the run:

That waiting is why order matters, and it's the same reason the lines of a real
program run top to bottom: later steps often depend on what earlier steps
produced.
1. First, `points = coral × 4` stored `12` in Robot Memory.
2. Next, `points = points + 3` read that `12`, added `3`, and updated `points`
to `15`.

Now make it your own. The number blocks in the editor are **editable** this
time — click into one and change its value. Before you press Run, try to predict
the new answer in your head. Then run it and check whether the value flowing
into the result block matches what you expected.
If you tried to add the bonus *before* calculating `coral × 4`, `points`
wouldn't even exist yet! **Later steps depend on what earlier steps stored in
memory** — that's why top-to-bottom order is the foundation of every program.

Now experiment. The `coral` and `leave_bonus` boxes are **editable** — click a
number to change it, predict the new score in your head, then **Step** through
to check your prediction.

Some things to try:
- Set the two top numbers so their sum is `10`. What does the result become?
- Can you make the result equal `0`? (Hint: what times anything is zero?)
- Make the result a **negative** number.
- Score `5` coral with a `3`-point leave bonus. What does `points` reach?
- What if the robot didn't leave the starting zone (`leave_bonus = 0`)?
- What if `coral = 0` and `leave_bonus = 0`?

```blocks
preset: edit-values
```

# Build your own math machine

Time to build a flowchart from scratch. This canvas starts with two number
blocks and a result block, and a toolbar for adding more.

**How to work the editor:**

- Click a toolbar button — **number**, **+**, **−**, or **×** — to drop a new
block onto the canvas. Drag blocks around to arrange them.
- **Connect blocks by dragging** from the little dot on the bottom of one block
to a dot on the top of another. An operation block has *two* dots on top
because it needs two inputs; the result block has one.
- Press **▶ Run step by step** to watch your machine evaluate. **Reset** clears
the shown values so you can run it again.

A block shows `?` when it doesn't have all of its inputs wired up yet — that's
the computer telling you a step is missing.

Your challenge: build a machine that computes **`(8 − 3) × 4`** and check that
the result block lands on `20`. You'll need a subtract block feeding into a
multiply block, just like the very first example — except now *you* decide the
order the computer works in.
# Build your own scoring flowchart

Time to build a flowchart yourself. Suppose our robot took `8` shots
(`attempts`) and dropped `3` of them (`dropped`), and each scored coral is worth
`4` points.

**How to build:**
- **Drag a block** from the palette into the drop slot (or click its **`+`**
button to insert it).
- Use the **↑ / ↓** arrows on any block you added to change the order, or the
**trash icon** to remove it.
- Press **▶ Run** or **⏭ Step** to trace your program. **Clear run** resets only
the animation (your blocks stay safe); **Start over** resets the whole canvas.

Your challenge: assemble the steps so the robot computes
**`(attempts − dropped) × 4`** and outputs `score`. Try putting the multiply
block *before* the subtract block once just to see what happens when a step
reads a variable that hasn't been calculated yet — then put them in the right
order and click **✓ Check** to test your flowchart against three match
scenarios!

```blocks
preset: build
Expand Down
98 changes: 42 additions & 56 deletions lessons/02-conditionals/README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,72 @@
---
title: "Making decisions"
goal: "Use a condition to make a program choose between two paths."
goal: "Use a condition diamond to make a program choose one path and skip the other."
order: 20
section: "Programming with Blocks"
---

# When programs choose
# When a robot has to choose

So far our flowcharts always did the exact same steps in the exact same order.
Real programs need to **make decisions**: do one thing when the weather is cold,
another when it's warm; show "pass" when a score is high enough, "fail" when it
isn't.
So far our flowcharts ran every single box from **START** to **END**. Real
robots have to **make decisions** based on sensors: slow down when the battery
is low, shoot when the target is in range, or stop when an arm hits its limit
switch.

In a flowchart, a decision is a **diamond**. It takes two numbers, `a` and `b`,
and asks a yes/no question about them — `a > b?` here (the question can also
use `<`, `=`, or `≠`). Then the program follows **exactly one of the two
arrows leaving it**: the `true` arrow when the answer is yes, the `false`
arrow when it's no. Never both, never neither.
In a flowchart, a decision is a **diamond** (`IF ... ?`). It asks a yes/no
question about the robot's memory — here, `battery < 10 ?` — and then follows
**exactly one** of its two outgoing arrows:

Each arrow leads to an **answer** block — the step that runs on that path. An
answer block has two inputs: `when` (wired from one of the diamond's arrows)
and `value` (what to produce if its path is the one taken). The branch the
program doesn't take simply **never runs**.
- Follow **YES** when the condition is **True**.
- Follow **NO** when the condition is **False**.

The flowchart in the editor finds **the larger of two numbers**. The diamond
asks `a > b?` — that's `true` here (`7 > 4`) — so the program follows the
`true` arrow down to the answer wired to `a`, and `7` flows on to the result.
The other branch **grays out and is completely skipped** — none of the steps on
that side ever run. After the taken branch finishes, both paths come back
together and continue to **OUTPUT** and **END**.

Press **▶ Run step by step**. Watch the diamond answer `true` and the program
head down the `true` arrow — while the other branch **grays out and gets
skipped**. Those blocks never run. This fork is exactly what Python writes as
`if / else`, and you'll type it yourself in [lesson {n}](#/lesson/09-if-statements).
Press **▶ Run** or **⏭ Step** on this **Battery Brownout Limiter**. With
`battery = 12`, watch the robot check `12 < 10` (False), follow the **NO**
arrow to set `mode = "FULL POWER"` and `max_speed = 100`, and skip the entire
**YES** branch. Click **Peek at Python** to see how this maps directly to
`if / else` (which you'll write yourself in
[lesson {n}](#/lesson/09-if-statements)).

```blocks
preset: cond-demo
```

# The road not taken
# Testing the boundary

The key idea: a decision **takes exactly one path, every time.** The other
branch isn't "a value you don't use" — it's code that **never runs**. Watch it
gray out during a run: the computer doesn't even look at it.
A decision **takes exactly one path, every time.** Every step inside the untaken
branch is skipped — the computer doesn't even touch it.

The numbers are editable now. Try these and run each time:
Now both the `battery` voltage and the `IF` condition are editable. Try these
and **Step** through each one:

- Make `a` **smaller** than `b`. Which arrow does the program follow now?
`a > b` is `false`, so it takes the `false` branch — and the answer wired to
`b` is the one that runs while the other side gets skipped.
- Make `a` and `b` **equal**. Is `a > b` true or false when they're the same?
(This is a classic bug source — "greater than" is not the same as "greater
than or equal to.")

The result always ends up being whichever number is bigger, no matter which one
you put where. That's the whole point: the program adapts to its inputs instead
of doing one fixed thing.
- Set `battery` to **`9`**. Now `9 < 10` is **YES (True)** — watch the **YES**
branch run (`mode = "LOW POWER"`, `max_speed = 40`) while the **NO** branch
grays out.
- Set `battery` to **`10`** — right on the boundary! Is `10 < 10` True or False?
(This is one of the most common bugs in robotics code: `<` means *strictly
less than*, while `≤` includes the boundary itself.)
- Try switching `<` to `≤` in the diamond and run `battery = 10` again.

```blocks
preset: cond-edit
```

# Build a decision

Your turn. Build a flowchart that outputs **the smaller of two numbers** and
check it against a few inputs.

**How:** you have a toolbar with number blocks, four decision diamonds
(`if >`, `if <`, `if =`, `if ≠`), and an **answer** block. Drag from a block's
output dot to an input dot on another block to wire things up.

A diamond needs `a` and `b` wired into its top. Each answer block needs two
wires: `when` (from one of the diamond's `true`/`false` arrows) and `value`
(the number that path should produce). Wire both answers into the result.
# Build a shooter range check

Two ways to get the *smaller* number — see if you can find both:
Your turn. Our robot has a distance sensor measuring how many meters away the
target is (`distance`).

- Use `if <`: ask `a < b`, wire the `true` arrow's answer to `a` and the
`false` arrow's answer to `b`.
- Or use `if >` and swap which number each answer is wired to.
**Your challenge:**
- If `distance < 3`, the robot should set `action = "SHOOT"`.
- Otherwise (`NO`), the robot should set `action = "DRIVE CLOSER"`.

Both work, because a decision is really just "which path do I take?" — and
each path carries its own answer. Run it with different numbers to convince
yourself it always picks the smaller one.
**How:** drag a block from the palette into the **YES** drop slot and another
into the **NO** drop slot (or click **`+`**). Then click **✓ Check** — it will
automatically test your flowchart with `distance = 2`, the boundary `distance = 3`,
and `distance = 5`!

```blocks
preset: cond-build
Expand Down
Loading
Loading