create-rulecad 0.3.26 → 0.3.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +1 -1
- package/machines/arm/projects/arm/STREAMER.md +108 -35
- package/machines/arm/projects/arm/firmware/arm/arm.ino +76 -35
- package/machines/arm/projects/arm/send.mjs +51 -28
- package/machines/leg/projects/leg/STREAMER.md +209 -0
- package/machines/leg/projects/leg/firmware/leg/leg.ino +256 -0
- package/machines/leg/projects/leg/send.mjs +157 -0
- package/machines/leg/rules/leg.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,25 @@ print (a hole's diameter, a seat's depth, a part's outline). Each release lists
|
|
|
5
5
|
it makes, with its reason. A **fasten** is an entry in an assembly's `"fasten"` list: one screwed joint, naming the
|
|
6
6
|
screw by thread and head, the parts it passes through and the part it ends in.
|
|
7
7
|
|
|
8
|
+
|
|
9
|
+
## 0.3.27: the leg's streamer and sender, eight axes on a Mega (#199)
|
|
10
|
+
- The sender refuses a step file whose axes are not the machine's, by name and in order, so a file written for one machine never drives another's motors.
|
|
11
|
+
- STREAMER.md: set each driver breakout's switches to 1/8 (M0 and M1 on, M2 off) before streaming; they come set to 1/32.
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- `--machine leg` makes the leg with the streamer (`projects/leg/firmware/leg/leg.ino`), its sender (`send.mjs`) and
|
|
15
|
+
their write-up (`STREAMER.md`), the arm's own programs.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- The streamer (both machines) is built for an Uno under a CNC Shield V3 (three axes) or a Mega 2560 (eight), the
|
|
20
|
+
board it is compiled for picking its pins. It greets with `streamer ready, <n> axes` (it said `arm ready`). Each
|
|
21
|
+
axis's figures are worked out at a block's start and only the moving axes are stepped: on a simulated Uno the
|
|
22
|
+
slowest three-axis ceiling rose from 12428 to 14082 steps/s.
|
|
23
|
+
- `send.mjs` writes its lines for the machine description's controller (`controller.board`), refuses a file with more
|
|
24
|
+
axes than that board's streamer, and refuses to send to a streamer that says it was built for the other board.
|
|
25
|
+
- `leg.robot_mass`'s rule names the Mega and its drivers; its value is unchanged. No printed size moved.
|
|
26
|
+
|
|
8
27
|
## 0.3.26: the agents' guide follows cad 0.7.78
|
|
9
28
|
### Changed
|
|
10
29
|
|
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ npx cad plot arm/arm projects/arm/drawings/test.svg
|
|
|
26
26
|
|
|
27
27
|
`--machine arm` also makes a two-link arm that draws: sized for a landscape A6, A5 or A4 sheet (`--sheet`, default A5) and an
|
|
28
28
|
accuracy at the pen (`--accuracy` in mm, default 0.3), with its task, bought items, wires, plates, a test drawing fitted to the
|
|
29
|
-
sheet, and the streamer for an Uno. `--machine leg` makes a leg on a test stand, for a robot of `--robot` kg (default 1.5). In a
|
|
29
|
+
sheet, and the streamer for an Uno. `--machine leg` makes a leg on a test stand, for a robot of `--robot` kg (default 1.5), with the same streamer and sender. In a
|
|
30
30
|
terminal with neither given it asks; given a flag, or with no terminal, it uses the defaults without asking. The arm and the leg
|
|
31
31
|
are explained part by part in EXAMPLES chapters 12 and 14, and [the machines guide](https://jonibach.github.io/rulecad/docs/machines/)
|
|
32
32
|
walks the arm from task to drawing.
|
|
@@ -1,20 +1,33 @@
|
|
|
1
|
-
# The
|
|
1
|
+
# The streamer and its sender
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
A machine in the kit moves with two programs. The streamer runs on an Arduino: it takes blocks of synchronised steps
|
|
4
|
+
over the USB serial line and moves the motors. `send.mjs` runs on your computer: it reads a step file that `cad plot`
|
|
5
|
+
wrote, homes the machine where it has switches and sends the file to the board a line at a time. The board knows
|
|
6
|
+
nothing of the machine's shape. All the maths happens in `cad plot`.
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
The streamer is one sketch, the same in every project: `firmware/arm/arm.ino` in the arm's project and
|
|
9
|
+
`firmware/leg/leg.ino` in the leg's. The board it is compiled for picks its pins:
|
|
10
|
+
|
|
11
|
+
| Board | Axes | For | Pins |
|
|
12
|
+
|---|---|---|---|
|
|
13
|
+
| Arduino Uno under a CNC Shield V3 | 3 | the arm, the leg's stand | the shield's: STEP on D2 to D4, DIR on D5 to D7, home switches on D9, D10, D12, enable D8 |
|
|
14
|
+
| Arduino Mega 2560, a step driver for each axis on its own breakout | 8 | the quadruped | STEP on D22 to D29, DIR on D37 down to D30, home switches on A8 to A15, enable D38 (the `arduino-mega-2560` rule set) |
|
|
15
|
+
|
|
16
|
+
The machine description says which board a machine has: `cad machine` reads it from the bought board in the assembly
|
|
17
|
+
(`cnc-shield-v3` or `arduino-mega-2560`) and writes it as `controller.board`, and `send.mjs` writes its lines for that
|
|
18
|
+
board's axes.
|
|
19
|
+
|
|
20
|
+
**Status.** The sketch is compiled for the Uno and for the Mega, and run on a simulated Uno and a simulated Mega, on
|
|
21
|
+
every push to the kit (the `firmware` job of its tests). The simulation homes the arm and streams the worked arm's
|
|
22
|
+
step file, the stand's gait and the quadruped's trot through it. Moving real hardware is your test: each machine's
|
|
23
|
+
measured milestone.
|
|
11
24
|
|
|
12
25
|
## What you need
|
|
13
26
|
|
|
14
27
|
| | |
|
|
15
28
|
|---|---|
|
|
16
|
-
| **Board** | Arduino Uno (16 MHz), CNC Shield V3 on top, a step driver (DRV8825 or A4988) in each slot used |
|
|
17
|
-
| **On your computer** | Node 20 or later; the Arduino IDE or `arduino-cli` with the `arduino:avr` core and the `Servo` library, to flash the sketch |
|
|
29
|
+
| **Board** | Arduino Uno (16 MHz), CNC Shield V3 on top, a step driver (DRV8825 or A4988) in each slot used; or an Arduino Mega 2560 (16 MHz) with a DRV8825 on a driver breakout for each axis |
|
|
30
|
+
| **On your computer** | Node 20 or later; the Arduino IDE or `arduino-cli` with the `arduino:avr` core and the `Servo` library, to flash the sketch (`--fqbn arduino:avr:uno`, or `arduino:avr:mega` for the Mega) |
|
|
18
31
|
| **Wiring** | The shoulder on the shield's X slot, the elbow on Y, a third axis (if any) on Z; the home switches on the X and Y limit pins (D9 and D10), a third axis's on D12; the pen servo's signal on D11 (the Z limit pin on a V3.00 shield). `cad machine` writes the wiring sheet |
|
|
19
32
|
|
|
20
33
|
## Using it
|
|
@@ -29,12 +42,12 @@ hardware is your test: the arm's measured milestone.
|
|
|
29
42
|
The streamer knows only axes and steps, so a leg runs on it as the arm does. The leg's stand (`projects/leg`, with the
|
|
30
43
|
hip's motor on the shield's X slot and the knee's on Y) takes the same sketch, the same drivers and the same sender:
|
|
31
44
|
|
|
32
|
-
1. Flash
|
|
45
|
+
1. Flash the streamer to the stand's Uno, as in step 1 above (`firmware/leg` in the leg's project).
|
|
33
46
|
2. Write the machine and the gait's step file: `npx cad machine leg/stand` writes `exports/leg/stand.machine.json`, and
|
|
34
47
|
`npx cad plot leg/stand` writes `exports/leg/stand-gait.steps.json`: three cycles of the gait, from the pose the
|
|
35
48
|
assembly shows (`standing`) and back to it. `--cycles 5` plays five, `--pose <name>` starts and ends at another pose,
|
|
36
49
|
and `--period 4` plays a cycle in 4 s. `.preview/leg/stand-gait.svg` draws each axis through the run.
|
|
37
|
-
3. Dry run: `node projects/
|
|
50
|
+
3. Dry run: `node projects/leg/send.mjs exports/leg/stand-gait.steps.json --machine exports/leg/stand.machine.json --dry-run`.
|
|
38
51
|
4. Stand the leg in its pose, then send the file with `--port <port>`. The stand has no home switches, so the streamer
|
|
39
52
|
counts from where the motors are when it starts: the file's first block takes the leg from its pose to the cycle, and
|
|
40
53
|
its last brings it back. At the end the drivers go off and the carriage settles on its stop collars.
|
|
@@ -48,16 +61,63 @@ How the gait becomes blocks:
|
|
|
48
61
|
| **A joint turning back** | Where a foot lifts off or sets down, a joint reverses. The run slows there so that no axis changes speed by more than its jump at once: an eighth of its `maxRate`, the rate `cad machine` homes it at from rest. It speeds up and slows down at no more than 4 × `maxRate` steps/s², full speed in 0.25 s. Both are estimates: no motor's start-stop rate under load has been measured yet |
|
|
49
62
|
| **From the pose and back** | A move from rest to rest at half the axes' fastest |
|
|
50
63
|
|
|
51
|
-
|
|
52
|
-
|
|
64
|
+
## Eight axes on a Mega 2560
|
|
65
|
+
|
|
66
|
+
The quadruped has eight motors, more than the Uno's three. It carries an Arduino Mega 2560, which runs the same
|
|
67
|
+
streamer built for eight axes, and a DRV8825 for each motor on its own driver breakout. One board steps every leg in
|
|
68
|
+
the same blocks; two Unos kept in step would need a clock shared between them, and each would start its blocks when
|
|
69
|
+
its own line arrived. Each motor's wire goes to its breakout's terminals; each breakout's EN, STEP, DIR and GND go to
|
|
70
|
+
one of the Mega's slots, `S0` to `S7`, the streamer's axes 0 to 7. `cad machine leg/quadruped` lists them:
|
|
71
|
+
|
|
72
|
+
| Slot | Axis | STEP | DIR | Home switch |
|
|
73
|
+
|---|---|---|---|---|
|
|
74
|
+
| S0 | fl_hd, the front left hip | D22 | D37 | A8 |
|
|
75
|
+
| S1 | fl_kd, its knee | D23 | D36 | A9 |
|
|
76
|
+
| S2 | fr_hd | D24 | D35 | A10 |
|
|
77
|
+
| S3 | fr_kd | D25 | D34 | A11 |
|
|
78
|
+
| S4 | bl_hd | D26 | D33 | A12 |
|
|
79
|
+
| S5 | bl_kd | D27 | D32 | A13 |
|
|
80
|
+
| S6 | br_hd | D28 | D31 | A14 |
|
|
81
|
+
| S7 | br_kd | D29 | D30 | A15 |
|
|
82
|
+
|
|
83
|
+
The drivers' EN pins all go to D38. STEP on one port (port A) lets the streamer step every axis due on a tick in one
|
|
84
|
+
write, and DIR on another (port C) sets them all in one write at a block's start.
|
|
85
|
+
|
|
86
|
+
1. Set each breakout's microstep switches to 1/8, as `cad machine` counts them (1600 steps a turn): M0 and M1 on,
|
|
87
|
+
M2 off (the DRV8825's mode table: MODE0 and MODE1 high is 1/8). The breakouts come with every switch on, which is
|
|
88
|
+
1/32: left like that, every joint moves a quarter as far as the file asks. Set each driver's current too, as on the
|
|
89
|
+
stand (its trimmer, Vref = current / 2 at 0.1 ohm).
|
|
90
|
+
2. Wire a 12 V supply's 12 V and ground to each breakout's motor terminals. Eight motors take about 6 A, four times
|
|
91
|
+
the stand's 1.5 A for two (an estimate, until measured), past the bench supply: the quadruped lists a Mean Well
|
|
92
|
+
LRS-100-12 (8.5 A) to buy.
|
|
93
|
+
3. Flash the streamer for the Mega: `arduino-cli compile --fqbn arduino:avr:mega firmware/leg` then
|
|
94
|
+
`arduino-cli upload -p <port> --fqbn arduino:avr:mega firmware/leg`. It says `streamer ready, 8 axes`.
|
|
95
|
+
4. `npx cad machine leg/quadruped` and `npx cad plot leg/quadruped`: three cycles of the trot from the pose `standing`.
|
|
96
|
+
5. Dry run: `node projects/leg/send.mjs exports/leg/quadruped-gait.steps.json --machine exports/leg/quadruped.machine.json --dry-run`.
|
|
97
|
+
Without `--machine` the sender writes for the Uno's three axes and refuses the file.
|
|
98
|
+
6. Stand the robot in its pose, then send with `--port <port>`. A sender whose lines are for one board refuses to send
|
|
99
|
+
to a streamer that says it was built for the other.
|
|
100
|
+
|
|
101
|
+
## Homing
|
|
102
|
+
|
|
103
|
+
The arm homes on switches: each axis steps towards its switch until it closes (`h`). The legs have no switches yet,
|
|
104
|
+
so the streamer counts from where the motors stand when it starts, and each gait file starts and ends at a pose you
|
|
105
|
+
set by hand. Switches are the way to home here: the DRV8825's current limit is set with its trimmer ("the trimmer
|
|
106
|
+
potentiometer on the board can be used to set the current limit", Pololu's DRV8825 carrier page, product 2133), so the
|
|
107
|
+
streamer cannot turn the current down to home against a stop, and the driver cannot tell a stall (homing without
|
|
108
|
+
switches, as Klipper does it, needs "a stallGuard capable TMC stepper driver", Klipper's TMC drivers page). Against a
|
|
109
|
+
stop at the stand's 0.5 A, a 17HS4401 holds about 0.4 x 0.5 / 1.5 = 0.13 N m, through the 38:1 gearbox at 47%
|
|
110
|
+
2.4 N m on the printed joint at every homing, five times the 0.48 N m the trot asks of a hip. The streamer and the
|
|
111
|
+
sender already home any axis with a switch (`home` on its joint, as the arm's), on the Mega's A8 to A15 as on the
|
|
112
|
+
shield's limit pins; the leg's printed parts do not yet carry a switch's post and a flag to press it.
|
|
53
113
|
|
|
54
114
|
## The line protocol
|
|
55
115
|
|
|
56
116
|
115200 baud, 8 data bits, no parity, 1 stop bit. One command a line, ended by a newline. The streamer answers every
|
|
57
117
|
line once: `ok` when it has taken the line, or `error: <what>` when it cannot. The sender waits for that answer before
|
|
58
118
|
it sends the next line. That is the flow control: a line that finds the queue full is held until a block finishes,
|
|
59
|
-
and only then answered, so the 12-block queue never overruns. A line is at most 95 characters
|
|
60
|
-
number.
|
|
119
|
+
and only then answered, so the 12-block queue never overruns. A line is at most 95 characters on the Uno and 127 on the
|
|
120
|
+
Mega. Every number is a whole number.
|
|
61
121
|
|
|
62
122
|
The streamer reads a line a character at a time as it arrives, between steps, so a line costs it no more than its bytes
|
|
63
123
|
take on the wire. Because the sender waits for each answer, a block must last at least as long as its own line, the
|
|
@@ -70,17 +130,18 @@ block, naming its line, before it sends anything (see Timing). `cad plot` plans
|
|
|
70
130
|
|---|---|---|---|
|
|
71
131
|
| `e` | | Drivers on: the motors hold | `ok` |
|
|
72
132
|
| `d` | | Drivers off: the motors go slack | `ok` |
|
|
73
|
-
| `h` | axis, direction, rate, back | Home one axis (0, 1 or 2): step towards direction (1 or -1) at rate steps/s until its switch closes, then step back off it by back steps, and call that position 0. It waits for the queue to empty first | `ok`; `error: endstop` if the switch has not closed after 60 s; `error: home` for a bad axis or rate |
|
|
133
|
+
| `h` | axis, direction, rate, back | Home one axis (0, 1 or 2 on the Uno, 0 to 7 on the Mega): step towards direction (1 or -1) at rate steps/s until its switch closes, then step back off it by back steps, and call that position 0. It waits for the queue to empty first | `ok`; `error: endstop` if the switch has not closed after 60 s; `error: home` for a bad axis or rate |
|
|
74
134
|
| `p` | degrees, milliseconds | Queue the pen servo to degrees, then a wait of milliseconds before the next block | `ok` |
|
|
75
135
|
| `w` | milliseconds | Queue a wait | `ok` |
|
|
76
|
-
| `m` | a, b, c, entry, peak, exit, accel, up, down | Queue a block: signed steps on axes 0, 1 and 2; the major axis's rate at entry, at its peak and at exit (steps/s); its acceleration (steps/s²); the major axis's steps spent speeding up, and slowing down. The major axis is the one with the most steps; the others step in proportion | `ok`; `error: block` if it moves nothing or its peak or acceleration is 0; `error: rate` if a rate is past MAX_RATE |
|
|
77
|
-
| `?` | | Report | `q <pos0> <pos1> <pos2> <queued
|
|
136
|
+
| `m` | a, b, c, entry, peak, exit, accel, up, down | Queue a block: signed steps on each axis (axes 0, 1 and 2 on the Uno; eight numbers, axes 0 to 7, on the Mega); the major axis's rate at entry, at its peak and at exit (steps/s); its acceleration (steps/s²); the major axis's steps spent speeding up, and slowing down. The major axis is the one with the most steps; the others step in proportion | `ok`; `error: block` if it moves nothing or its peak or acceleration is 0; `error: rate` if a rate is past MAX_RATE |
|
|
137
|
+
| `?` | | Report | `q <pos0> <pos1> <pos2> <queued>` (eight positions on the Mega): positions in steps as of the last step taken, and the blocks waiting |
|
|
78
138
|
| `!` | | Stop at once and empty the queue; the drivers stay on | `ok` |
|
|
79
|
-
| | | A line over 95 characters, or a number in it that is not a whole number | `error: line` |
|
|
139
|
+
| | | A line over 95 characters (127 on the Mega), or a number in it that is not a whole number | `error: line` |
|
|
80
140
|
| | | Anything else | `error: command` |
|
|
81
141
|
|
|
82
|
-
On power-up, and each time the serial port opens (which resets the
|
|
83
|
-
waits 2.5 s for it
|
|
142
|
+
On power-up, and each time the serial port opens (which resets the board), the streamer prints
|
|
143
|
+
`streamer ready, 3 axes` (`8 axes` on the Mega). The sender waits 2.5 s for it, and refuses to send lines written for
|
|
144
|
+
the other board.
|
|
84
145
|
|
|
85
146
|
## Timing
|
|
86
147
|
|
|
@@ -88,9 +149,13 @@ waits 2.5 s for it.
|
|
|
88
149
|
|---|---|---|
|
|
89
150
|
| MAX_RATE, the fastest block the streamer takes | 8000 steps/s on the major axis | `RATE_LIMIT` in `arm.ino`, and `MAX_RATE` in `send.mjs` (a test holds them equal) |
|
|
90
151
|
| What the worked arm asks | up to 8000 steps/s an axis (`maxRate`: 300 rpm of a 200-step motor at 1/8 microstepping is 300 × 1600 / 60), 7799 steps/s in the test drawing | `cad machine arm/arm` and `cad plot` |
|
|
91
|
-
| The loop's own ceiling while speeding up, one, two and three axes stepping at once |
|
|
92
|
-
| The loop's own ceiling while slowing down, one, two and three axes |
|
|
93
|
-
| The loop's own ceiling at a block's cruise, one, two and three axes |
|
|
152
|
+
| The loop's own ceiling while speeding up, one, two and three axes stepping at once | 16547, 15288 and 14208 steps/s | measured on a simulated 16 MHz Uno (avr8js), the sketch built with RATE_LIMIT raised: the mean over a long block's middle steps |
|
|
153
|
+
| The loop's own ceiling while slowing down, one, two and three axes | 16376, 15142 and 14082 steps/s | the same |
|
|
154
|
+
| The loop's own ceiling at a block's cruise, one, two and three axes | 35777, 30370 and 26384 steps/s | the same |
|
|
155
|
+
| On the Mega, one to eight axes, speeding up | 15726, 14587, 13601, 12741, 11982, 11309, 10708 and 10167 steps/s | the same, on a simulated 16 MHz Mega 2560 |
|
|
156
|
+
| On the Mega, slowing down | 15504, 14395, 13435, 12594, 11853, 11194, 10604 and 10074 steps/s | the same |
|
|
157
|
+
| On the Mega, at the cruise | 33135, 28452, 24929, 22183, 19982, 18177, 16672 and 15397 steps/s | the same |
|
|
158
|
+
| What the quadruped asks | up to 5333 steps/s an axis (200 rpm at 1/8: 200 x 1600 / 60) | `cad machine leg/quadruped` |
|
|
94
159
|
| A byte on the serial line | 86.8 µs | 115200 baud, 10 bits a byte (start, 8 data, stop) |
|
|
95
160
|
| A line's cost to the streamer | its bytes, its newline and the 4-byte answer on the wire, and nothing more measurable | measured on the simulated Uno: a 36-character block line and its answer take 3.55 ms there, against 3.56 ms on the wire (before the streamer parsed as it read, 3.87 ms) |
|
|
96
161
|
| The USB turnaround | 5.1 ms, an estimate | the Uno's USB chip (an ATmega16U2 running Arduino-usbserial) passes the answer on when its timer 0 overflows, 256 × 256 / 16 MHz = 4.1 ms (`Arduino-usbserial.c` in ArduinoCore-avr, read 2026-11-23); then up to a 1 ms USB frame for the next line. Copies with a CH340 USB chip differ |
|
|
@@ -99,17 +164,22 @@ waits 2.5 s for it.
|
|
|
99
164
|
| STEP pulse width | 3.06 µs high | measured on the simulated Uno; the DRV8825 needs 1.9 µs, the A4988 1 µs (Pololu's DRV8825 carrier page, product 2133, from TI's datasheet) |
|
|
100
165
|
| Uno clock | 16 MHz | Arduino Uno Rev3 |
|
|
101
166
|
|
|
102
|
-
The slowest of the nine ceilings, three axes slowing down at
|
|
167
|
+
The slowest of the Uno's nine ceilings, three axes slowing down at 14082 steps/s, is 1.76 times MAX_RATE; the slowest
|
|
168
|
+
of the Mega's twenty-four, eight axes slowing down at 10074 steps/s, is 1.26 times MAX_RATE and 1.89 times the 5333 the
|
|
169
|
+
quadruped asks. Each axis's figures are worked out at a block's start (its steps without their sign, the way it steps,
|
|
170
|
+
its pin), and only the axes that move are stepped: the Uno's three-axis ceilings rose 13% with that (12428 to 14082
|
|
171
|
+
slowing down), and the Mega's eight-axis one from 8115 to 10074. A step that changes
|
|
103
172
|
speed costs a float multiply and a division (gap = 1000000 / rate), which is why speeding up and slowing down are slower
|
|
104
|
-
than the cruise (
|
|
173
|
+
than the cruise (3.30 times MAX_RATE on the Uno), where the gap stays as it is. Past 5000 steps/s the speed is worked out every
|
|
105
174
|
other step, over both steps' time: done on every step, three axes slowing down reached only 8904 steps/s, and with lines
|
|
106
|
-
arriving the streamer fell 8% behind on short blocks. The simulation fails if any of
|
|
175
|
+
arriving the streamer fell 8% behind on short blocks. The simulation fails if any of them falls under MAX_RATE.
|
|
107
176
|
|
|
108
177
|
The ceilings are measured with one long block and no serial traffic, so they do not show what a stream of short blocks
|
|
109
178
|
costs. The simulation streams those too: the shortest blocks the sender passes, at MAX_RATE on three axes, cruising
|
|
110
179
|
(29 steps, 3.63 ms) and slowing down on every step (32 steps, 4.0 ms), with no USB turnaround since the simulation has no
|
|
111
180
|
USB link. Both keep to their planned time (0.2500 s of 0.2500 s, and 0.2519 s of 0.2519 s); the simulation fails if
|
|
112
|
-
either is more than 1% behind.
|
|
181
|
+
either is more than 1% behind. On the Mega the same, on eight axes: 39 steps (4.88 ms) cruising and 42 (5.25 ms)
|
|
182
|
+
slowing down, 0.2534 s of 0.2534 s and 0.2519 s of 0.2519 s. Before the streamer parsed as it read, the same streams took 8% and 16% longer, and
|
|
113
183
|
3-axis blocks of 20 steps at MAX_RATE took 55% longer: such blocks are now refused by the sender.
|
|
114
184
|
|
|
115
185
|
A step that an interrupt (the serial line, the clock, the servo) or a block's start delays is caught up at once, so one
|
|
@@ -119,10 +189,13 @@ board has not yet been timed: your bench test does that, and the USB turnaround
|
|
|
119
189
|
|
|
120
190
|
## How the sketch is checked
|
|
121
191
|
|
|
122
|
-
`node scripts/firmware.mjs` (in the kit's repository) compiles every sketch for `arduino:avr:uno
|
|
123
|
-
error. With `--simulate <steps file> --machine <machine file>` it also
|
|
124
|
-
|
|
125
|
-
|
|
192
|
+
`node scripts/firmware.mjs` (in the kit's repository) compiles every sketch for `arduino:avr:uno`, and the streamer for
|
|
193
|
+
`arduino:avr:mega` too, and fails on a compile error. With `--simulate <steps file> --machine <machine file>` it also
|
|
194
|
+
runs this streamer on a simulated board, the Uno or the Mega as the machine's controller says, and fails when any of
|
|
195
|
+
these does not hold. The two flags can be given again for another step file: the kit's tests stream the arm's drawing,
|
|
196
|
+
the stand's gait and the quadruped's trot, each with its own machine, and measure the loop's own ceilings once on each
|
|
197
|
+
board. On the simulated Mega the trot's 148 lines stream in 14.43 s against 14.52 s planned, every one of its
|
|
198
|
+
178596 steps counted and the streamer's own report back at 0 on all eight axes.
|
|
126
199
|
|
|
127
200
|
| Check | Held to |
|
|
128
201
|
|---|---|
|
|
@@ -130,7 +203,7 @@ drawing and then the leg's gait, each with its own machine, and measure the loop
|
|
|
130
203
|
| Homing | each axis stops at its switch (placed 300 steps away) and backs off by `back` steps |
|
|
131
204
|
| Steps | each axis's step count and its net position equal the step file's sums, and the streamer's own `?` report agrees |
|
|
132
205
|
| Keeping up | the moves take no more than 5% longer than `cad plot` planned |
|
|
133
|
-
| Short blocks | the shortest blocks the sender passes, streamed at MAX_RATE on three
|
|
206
|
+
| Short blocks | the shortest blocks the sender passes, streamed at MAX_RATE on every axis (three, or eight), cruising and slowing down on every step, take no more than 1% longer than planned |
|
|
134
207
|
| MAX_RATE | a block 1 step/s past it is answered `error: rate`, a block at it `ok` |
|
|
135
|
-
| Ceiling | the loop steps one
|
|
208
|
+
| Ceiling | the loop steps one to all its axes faster than MAX_RATE at the cruise, speeding up and slowing down (nine figures on the Uno, twenty-four on the Mega), and no axis of the machine asks more than MAX_RATE |
|
|
136
209
|
| STEP pulse | at least 1.9 µs high |
|
|
@@ -1,57 +1,82 @@
|
|
|
1
|
-
// A streamer for
|
|
2
|
-
//
|
|
1
|
+
// A streamer for a machine's stepper axes: it takes synchronised step blocks over serial and moves. The same program runs
|
|
2
|
+
// an arm or a leg's stand on an Arduino Uno with a CNC Shield V3 (three axes), and a quadruped's eight axes on an Arduino
|
|
3
|
+
// Mega 2560 with a step driver for each on a breakout; the board it is built for picks its pins (see BOARD below).
|
|
4
|
+
// Not a general firmware. It knows nothing of the machine: cad plot does the maths and send.mjs sends the result.
|
|
3
5
|
// 115200 baud, one command a line. Every line is answered "ok" once it is taken, or "error: ..." if it can't be; send the
|
|
4
6
|
// next line only after the answer: that is the flow control, and the buffer can never overrun.
|
|
5
7
|
//
|
|
6
8
|
// e drivers on (the motors hold)
|
|
7
9
|
// d drivers off
|
|
8
|
-
// h <axis> <dir> <rate> <back> home axis 0
|
|
9
|
-
// closes, then come back off it by back steps. Answers "ok" when done, "error: endstop" if none in 60 s
|
|
10
|
+
// h <axis> <dir> <rate> <back> home an axis (0 to AXES - 1): step towards dir (1 or -1) at rate steps/s until its
|
|
11
|
+
// endstop closes, then come back off it by back steps. Answers "ok" when done, "error: endstop" if none in 60 s
|
|
10
12
|
// p <deg> <ms> the pen servo to deg, then wait ms before anything after it
|
|
11
13
|
// w <ms> wait
|
|
12
14
|
// m <a> <b> <c> <entry> <peak> <exit> <accel> <up> <down>
|
|
13
|
-
// a block: signed steps
|
|
15
|
+
// a block: signed steps for each of the AXES axes (three on the Uno, eight on the Mega), then
|
|
16
|
+
// the major axis's step rate at entry, peak and exit
|
|
14
17
|
// (steps/s), its acceleration (steps/s²), and the major steps spent speeding up and slowing down.
|
|
15
18
|
// "error: rate" if its peak is past MAX_RATE, "error: block" if it moves nothing
|
|
16
19
|
// (a block must last as long as its line and answer take to cross the serial line: send.mjs
|
|
17
20
|
// refuses a shorter one, and cad plot plans none shorter than the task's blockTime)
|
|
18
|
-
// ? prints "q <pos0> <pos1>
|
|
21
|
+
// ? prints "q <pos0> <pos1> ... <blocks queued>", a position for each axis (in steps, as of the last step)
|
|
19
22
|
// ! stop at once, empty the queue, drivers stay on: answers "ok"
|
|
20
23
|
//
|
|
21
|
-
// "error: line" if a line is over
|
|
24
|
+
// "error: line" if a line is over LINE_MAX characters (95 on the Uno, 127 on the Mega) or a number in it is not a whole number.
|
|
22
25
|
//
|
|
23
26
|
// Timing: steps are timed in the main loop from micros(), with serial read between them, so the servo library keeps its
|
|
24
27
|
// timer. A line is parsed a character at a time as it arrives, so no step waits for a whole line to be read, and a block
|
|
25
|
-
// that follows another keeps its time: its first step is one gap after the last block's last. The step
|
|
26
|
-
//
|
|
28
|
+
// that follows another keeps its time: its first step is one gap after the last block's last. The step pins are all on one
|
|
29
|
+
// port (the Uno's port D, D2 to D4; the Mega's port A, D22 to D29), so the axes due on a tick are stepped in one write, and
|
|
30
|
+
// so are the direction pins (the Uno's D5 to D7, also port D; the Mega's port C, D37 down to D30). A step that changes
|
|
27
31
|
// speed costs a division; past 5000 steps/s that is done every other step, over both steps' time. MAX_RATE is the
|
|
28
|
-
// fastest block it takes: on a simulated 16 MHz Uno (scripts/firmware.mjs --simulate, in CI) the loop keeps up
|
|
29
|
-
// than that with all
|
|
30
|
-
// shortest blocks the sender passes streaming in.
|
|
31
|
-
// Compiled for the Uno and run on
|
|
32
|
+
// fastest block it takes: on a simulated 16 MHz Uno and Mega (scripts/firmware.mjs --simulate, in CI) the loop keeps up
|
|
33
|
+
// with more than that with all its axes stepping at once, speeding up and slowing down as well as at the cruise, and with
|
|
34
|
+
// the shortest blocks the sender passes streaming in.
|
|
35
|
+
// Compiled for the Uno and the Mega and run on simulated ones in CI; moving hardware is a bench test (the machine's measured
|
|
36
|
+
// milestone).
|
|
32
37
|
#include <Servo.h>
|
|
33
38
|
|
|
39
|
+
// BOARD. The pins come from the board the sketch is compiled for. Uno: the CNC Shield V3's (its user guide). Mega 2560:
|
|
40
|
+
// chosen so that each set is one port (ArduinoCore-avr's variants/mega/pins_arduino.h): STEP on D22 to D29 (PA0 to PA7),
|
|
41
|
+
// DIR on D37 to D30 (PC0 to PC7), the home switches on A8 to A15 (PK0 to PK7), the drivers' shared enable on D38.
|
|
42
|
+
#if defined(__AVR_ATmega2560__)
|
|
43
|
+
#define AXES 8
|
|
44
|
+
#define STEP_PORT PORTA
|
|
45
|
+
#define DIR_PORT PORTC
|
|
46
|
+
const uint8_t STEP[AXES] = { 22, 23, 24, 25, 26, 27, 28, 29 }, DIR[AXES] = { 37, 36, 35, 34, 33, 32, 31, 30 };
|
|
47
|
+
const uint8_t LIMIT[AXES] = { A8, A9, A10, A11, A12, A13, A14, A15 }, ENABLE_PIN = 38, PEN_PIN = 11;
|
|
48
|
+
const uint8_t STEP_MASK[AXES] = { _BV(PA0), _BV(PA1), _BV(PA2), _BV(PA3), _BV(PA4), _BV(PA5), _BV(PA6), _BV(PA7) }; // the STEP pins' bits in PORTA
|
|
49
|
+
const uint8_t DIR_MASK[AXES] = { _BV(PC0), _BV(PC1), _BV(PC2), _BV(PC3), _BV(PC4), _BV(PC5), _BV(PC6), _BV(PC7) }; // the DIR pins' bits in PORTC
|
|
50
|
+
const uint8_t LINE_MAX = 127;
|
|
51
|
+
#elif defined(__AVR_ATmega328P__)
|
|
52
|
+
#define AXES 3
|
|
53
|
+
#define STEP_PORT PORTD
|
|
54
|
+
#define DIR_PORT PORTD
|
|
55
|
+
const uint8_t STEP[AXES] = { 2, 3, 4 }, DIR[AXES] = { 5, 6, 7 }, LIMIT[AXES] = { 9, 10, 12 }, ENABLE_PIN = 8, PEN_PIN = 11;
|
|
56
|
+
const uint8_t STEP_MASK[AXES] = { _BV(PD2), _BV(PD3), _BV(PD4) }; // the STEP pins' bits in PORTD
|
|
57
|
+
const uint8_t DIR_MASK[AXES] = { _BV(PD5), _BV(PD6), _BV(PD7) }; // the DIR pins' bits in PORTD
|
|
58
|
+
const uint8_t LINE_MAX = 95;
|
|
59
|
+
#else
|
|
60
|
+
#error "the streamer is written for an Arduino Uno (ATmega328P) under a CNC Shield V3, or an Arduino Mega 2560"
|
|
61
|
+
#endif
|
|
62
|
+
|
|
34
63
|
#ifndef RATE_LIMIT
|
|
35
64
|
#define RATE_LIMIT 8000
|
|
36
65
|
#endif
|
|
37
66
|
const long MAX_RATE = RATE_LIMIT; // steps/s on a block's major axis; the simulation builds with -DRATE_LIMIT raised to find the loop's own limit
|
|
38
|
-
const uint8_t STEP[3] = { 2, 3, 4 }, DIR[3] = { 5, 6, 7 }, LIMIT[3] = { 9, 10, 12 }, ENABLE_PIN = 8, PEN_PIN = 11;
|
|
39
|
-
const uint8_t STEP_MASK[3] = { _BV(PD2), _BV(PD3), _BV(PD4) }; // the STEP pins' bits in PORTD
|
|
40
|
-
const uint8_t DIR_MASK[3] = { _BV(PD5), _BV(PD6), _BV(PD7) }; // the DIR pins' bits in PORTD
|
|
41
67
|
const uint8_t QUEUE = 12;
|
|
42
68
|
const long MIN_RATE = 80; // steps/s: slower than this a block would take seconds to start
|
|
43
69
|
|
|
44
70
|
enum Kind : uint8_t { MOVE, PEN, WAIT };
|
|
45
|
-
struct Block { Kind kind; long steps[
|
|
71
|
+
struct Block { Kind kind; long steps[AXES]; long major, entry, peak, exit_, accel, up, down; };
|
|
46
72
|
Block queue[QUEUE];
|
|
47
73
|
uint8_t head = 0, tail = 0;
|
|
48
74
|
inline uint8_t count() { return head >= tail ? head - tail : head + QUEUE - tail; }
|
|
49
75
|
|
|
50
76
|
Servo pen;
|
|
51
|
-
long position[3] = { 0, 0, 0 };
|
|
52
77
|
// The line being read, taken a character at a time as it arrives, between steps: its command letter, then up to ARGS
|
|
53
78
|
// whole numbers, each built up digit by digit. Parsing a whole line at its end (strtol) held the steps up 520 to 600 µs.
|
|
54
|
-
const uint8_t ARGS =
|
|
79
|
+
const uint8_t ARGS = AXES + 6;
|
|
55
80
|
char cmd = 0;
|
|
56
81
|
long arg[ARGS];
|
|
57
82
|
uint8_t len = 0, nargs = 0;
|
|
@@ -61,7 +86,15 @@ bool pending = false; // a whole line is waiting for room in the queue
|
|
|
61
86
|
// The block being run
|
|
62
87
|
bool running = false;
|
|
63
88
|
Block cur;
|
|
64
|
-
long done = 0
|
|
89
|
+
long done = 0;
|
|
90
|
+
// Each axis as the loop steps it, in one record so a step reaches all of it from one address: its position (steps, as of
|
|
91
|
+
// its last step), its Bresenham count, its steps in the block without their sign and the step each takes on the position
|
|
92
|
+
// (1 or -1), and its STEP pin's bit. A block's start lists the axes that move, so the Mega's eight in a block that moves
|
|
93
|
+
// two cost the loop no more than two.
|
|
94
|
+
struct Axis { long position, acc, dist; int8_t sign; uint8_t mask; };
|
|
95
|
+
Axis ax[AXES];
|
|
96
|
+
Axis *moving[AXES];
|
|
97
|
+
uint8_t nMoving = 0;
|
|
65
98
|
unsigned long nextAt = 0, waitUntil = 0;
|
|
66
99
|
bool waiting = false;
|
|
67
100
|
bool chained = false; // the last block ended on a step with the next due one gap after it: the next block keeps that time
|
|
@@ -78,16 +111,16 @@ bool skipped = false; // the last step in a ramp left its speed change to this o
|
|
|
78
111
|
|
|
79
112
|
void setup() {
|
|
80
113
|
Serial.begin(115200);
|
|
81
|
-
for (uint8_t i = 0; i <
|
|
114
|
+
for (uint8_t i = 0; i < AXES; i++) { pinMode(STEP[i], OUTPUT); pinMode(DIR[i], OUTPUT); pinMode(LIMIT[i], INPUT_PULLUP); }
|
|
82
115
|
pinMode(ENABLE_PIN, OUTPUT);
|
|
83
116
|
digitalWrite(ENABLE_PIN, HIGH); // off until asked: the shield's enable is active low
|
|
84
117
|
pen.attach(PEN_PIN);
|
|
85
|
-
Serial.
|
|
118
|
+
Serial.print(F("streamer ready, ")); Serial.print(AXES); Serial.println(F(" axes"));
|
|
86
119
|
}
|
|
87
120
|
|
|
88
121
|
// A STEP pulse on the axes in mask, 3 µs high (delayMicroseconds(3) gave 2.06 µs on the simulated Uno, too near the
|
|
89
122
|
// DRV8825's 1.9 µs minimum; the A4988's is 1 µs)
|
|
90
|
-
void pulse(uint8_t mask) {
|
|
123
|
+
void pulse(uint8_t mask) { STEP_PORT |= mask; delayMicroseconds(4); STEP_PORT &= ~mask; }
|
|
91
124
|
|
|
92
125
|
bool home(uint8_t axis, int dir, long hz, long back) {
|
|
93
126
|
digitalWrite(DIR[axis], dir > 0 ? HIGH : LOW);
|
|
@@ -98,7 +131,7 @@ bool home(uint8_t axis, int dir, long hz, long back) {
|
|
|
98
131
|
}
|
|
99
132
|
digitalWrite(DIR[axis], dir > 0 ? LOW : HIGH);
|
|
100
133
|
for (long i = 0; i < back; i++) { pulse(STEP_MASK[axis]); delayMicroseconds(gap); }
|
|
101
|
-
|
|
134
|
+
ax[axis].position = 0;
|
|
102
135
|
return true;
|
|
103
136
|
}
|
|
104
137
|
|
|
@@ -125,11 +158,11 @@ bool obey() {
|
|
|
125
158
|
if (c == 'e') { digitalWrite(ENABLE_PIN, LOW); Serial.println(F("ok")); return true; }
|
|
126
159
|
if (c == 'd') { digitalWrite(ENABLE_PIN, HIGH); Serial.println(F("ok")); return true; }
|
|
127
160
|
if (c == '!') { head = tail = 0; running = false; waiting = false; chained = false; Serial.println(F("ok")); return true; }
|
|
128
|
-
if (c == '?') { Serial.print(F("q ")); for (uint8_t i = 0; i <
|
|
161
|
+
if (c == '?') { Serial.print(F("q ")); for (uint8_t i = 0; i < AXES; i++) { Serial.print(ax[i].position); Serial.print(' '); } Serial.println(count()); return true; }
|
|
129
162
|
if (c == 'h') {
|
|
130
163
|
if (running || count()) return false; // home only when everything before it is finished
|
|
131
164
|
long axis = a(0), dir = a(1), hz = a(2), back = a(3);
|
|
132
|
-
if (axis < 0 || axis
|
|
165
|
+
if (axis < 0 || axis >= AXES || hz <= 0) { Serial.println(F("error: home")); return true; }
|
|
133
166
|
Serial.println(home(axis, dir, hz, back) ? F("ok") : F("error: endstop"));
|
|
134
167
|
return true;
|
|
135
168
|
}
|
|
@@ -138,9 +171,9 @@ bool obey() {
|
|
|
138
171
|
Block &b = queue[head];
|
|
139
172
|
if (c == 'm') {
|
|
140
173
|
b.kind = MOVE;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
b.
|
|
174
|
+
b.major = 0;
|
|
175
|
+
for (uint8_t i = 0; i < AXES; i++) { b.steps[i] = a(i); if (labs(b.steps[i]) > b.major) b.major = labs(b.steps[i]); }
|
|
176
|
+
b.entry = a(AXES); b.peak = a(AXES + 1); b.exit_ = a(AXES + 2); b.accel = a(AXES + 3); b.up = a(AXES + 4); b.down = a(AXES + 5);
|
|
144
177
|
if (b.major == 0 || b.peak <= 0 || b.accel <= 0) { Serial.println(F("error: block")); return true; }
|
|
145
178
|
if (b.peak > MAX_RATE || b.entry > MAX_RATE || b.exit_ > MAX_RATE) { Serial.println(F("error: rate")); return true; }
|
|
146
179
|
} else if (c == 'p') { b.kind = PEN; b.major = a(0); b.up = a(1); }
|
|
@@ -172,9 +205,15 @@ void start() {
|
|
|
172
205
|
if (cur.kind == WAIT) { waitUntil = millis() + cur.up; waiting = true; return; }
|
|
173
206
|
// the direction pins in one write, and each axis's Bresenham count from half the block (a shift: a long division here
|
|
174
207
|
// cost about 600 cycles an axis between one block's last step and the next block's first)
|
|
175
|
-
uint8_t dirs = 0;
|
|
176
|
-
|
|
177
|
-
|
|
208
|
+
uint8_t dirs = 0, all = 0;
|
|
209
|
+
nMoving = 0;
|
|
210
|
+
for (uint8_t i = 0; i < AXES; i++) {
|
|
211
|
+
all |= DIR_MASK[i]; if (cur.steps[i] >= 0) dirs |= DIR_MASK[i];
|
|
212
|
+
Axis &x = ax[i];
|
|
213
|
+
x.acc = cur.major >> 1; x.dist = labs(cur.steps[i]); x.sign = cur.steps[i] < 0 ? -1 : 1; x.mask = STEP_MASK[i];
|
|
214
|
+
if (x.dist) moving[nMoving++] = &x;
|
|
215
|
+
}
|
|
216
|
+
DIR_PORT = (DIR_PORT & ~all) | dirs;
|
|
178
217
|
rate = max(cur.entry, MIN_RATE); gap = 1000000.0 / rate;
|
|
179
218
|
accelUs = cur.accel * 1e-6f; peakF = cur.peak; floorF = max(cur.exit_, MIN_RATE); downFrom = cur.major - cur.down; skipped = false;
|
|
180
219
|
done = 0; running = true;
|
|
@@ -185,9 +224,11 @@ void start() {
|
|
|
185
224
|
void tick() {
|
|
186
225
|
if ((long)(micros() - nextAt) < 0) return;
|
|
187
226
|
uint8_t mask = 0;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
227
|
+
const long major = cur.major;
|
|
228
|
+
for (uint8_t j = 0; j < nMoving; j++) {
|
|
229
|
+
Axis *x = moving[j];
|
|
230
|
+
x->acc += x->dist;
|
|
231
|
+
if (x->acc >= major) { x->acc -= major; mask |= x->mask; x->position += x->sign; }
|
|
191
232
|
}
|
|
192
233
|
pulse(mask);
|
|
193
234
|
done++;
|