Class 2 · Gondola and two belts¶
You have a frame and motors that move a measured distance. Today the gondola goes on the board, hanging from the top two belts, and your code turns a point in millimeters into the two belt lengths that put the pen there. This two-belt machine is a polargraph: gravity keeps both belts tight, so it works without the bottom motors.
By the end of class
You type a point like (300, 250) into your code, and the marker tip stops on a dot drawn at that point, within 3 mm.
New parts today¶
| Part | What it's for |
|---|---|
| Printed gondola (yours) with a marker | Holds the pen where the belts meet |
| GT2 belt, two lengths | Top-left and top-right motors to the gondola |
| Washers or nuts | Weight for the gondola and for the belt tails |
Build it¶
- Belts. Cut two lengths of belt, each long enough to reach from its top pulley to the far bottom corner of the board, plus 20 cm. Fix one end of each to the gondola (a printed clamp or a zip tie through a loop works).
- Run each belt over its pulley, teeth against the teeth. Hang a small weight (a few washers) on the free tail so the belt stays wrapped around the pulley. Keep it lighter than the gondola.
- Weight. Add washers to the gondola until both belts stay tight wherever you hold it. A light gondola lets the belts flop; too heavy and the motors skip.
- Pen. The marker tip should sit as close as you can get to the point where the two belts meet. That point is what your math calculates.
- Home mark. Pick a spot near the middle of the board, a bit below center. Put a small cross of tape there. Measure its position from the center of the top-left pulley: x to the right, y down, in millimeters. Write it in your build log.
Your coordinates for the whole project:
- The origin (0, 0) is the center of the top-left pulley.
- x grows to the right, y grows down, in millimeters.
- The top-right pulley is at (W, 0), where W is the distance you measured in Class 1.
The gondola drops when the motors let go
The motors only hold the gondola while the 24 V is on and the drivers are enabled (pin 8 LOW). Switching off the supply, or the Uno resetting during an upload, can let it fall. Keep a hand under it at those moments.
Every time you start: hold the gondola with the pen on the home cross, then reset or upload the Uno. Your code assumes it starts at home; it has no way to check.
Tool: Pythagoras in code (sqrt and float)
sqrt(n) returns the square root of n. For anything with decimals, use float variables. Here's a game that works out how far a player is from the goal on a screen:
const float GOAL_X = 600.0;
const float GOAL_Y = 80.0;
float distanceTo(float x, float y, float targetX, float targetY) {
float dx = targetX - x;
float dy = targetY - y;
return sqrt(dx * dx + dy * dy);
}
void setup() {
Serial.begin(115200);
float d = distanceTo(150.0, 400.0, GOAL_X, GOAL_Y);
Serial.println(d); // 552.18
}
void loop() {}
dx * dxis the same as dx²; it's faster thanpow(dx, 2).- Write
600.0, not600, when you mean a decimal.5 / 2is 2 in C++ (both are whole numbers);5.0 / 2is 2.5. - To turn a
floatinto a whole number of steps, round it:long steps = lround(mm * 80.0);
Why does the math work?
The belt from the top-left pulley to the pen is the hypotenuse of a right triangle. One side is the horizontal distance x, the other is the vertical distance y. So its length is √(x² + y²). For the top-right pulley, the horizontal side is W − x instead of x.
The motors don't care about the whole belt length, only about changes in length. At home, each motor's step count is 0. To go to a new point, each motor needs (new length − home length) × steps/mm steps. That's why the home mark matters: it's the only point where your code knows the true lengths.
Treating each belt as if it starts at the center of its pulley is a small simplification. On a board this size the error is a millimeter or two.
Mission 1 · Positive means longer
Make the top-left and top-right motors each move +800 steps (with the gondola hanging). For both motors, a positive move must let the belt out: the gondola goes down. If one goes the wrong way, fix it in code with setPinsInverted.
Done when
A positive move on either motor lowers that side of the gondola, and a negative move raises it.
Tool: MultiStepper (and a first look at arrays)
If two motors run one after the other, the pen draws an L instead of a slant. MultiStepper moves several motors so they all arrive at the same moment. It takes the targets as an array: a numbered row of values of one type.
This example is a pan-and-tilt camera head with a stepper for each axis:
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper pan(AccelStepper::DRIVER, 2, 5);
AccelStepper tilt(AccelStepper::DRIVER, 3, 6);
MultiStepper head;
void lookAt(long panSteps, long tiltSteps) {
long targets[2];
targets[0] = panSteps;
targets[1] = tiltSteps;
head.moveTo(targets);
head.runSpeedToPosition(); // waits until both arrive
}
void setup() {
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
pan.setMaxSpeed(1000);
tilt.setMaxSpeed(1000);
head.addStepper(pan); // pan is targets[0]
head.addStepper(tilt); // tilt is targets[1]
}
void loop() {
lookAt(1600, 400);
delay(1000);
lookAt(0, 0);
delay(1000);
}
- The order you call
addStepperis the order of the array. MultiSteppermoves at constant speed; it ignoressetAcceleration. KeepsetMaxSpeedmodest so the motors don't stall when they start.- Each motor's target is an absolute step count, like
moveTo.
Mission 2 · Go to a point
Write a function goTo(float x, float y) that moves the pen to (x, y) in millimeters. It works out both belt lengths at the new point, subtracts the lengths at home, turns the differences into steps with your measured steps/mm, and moves both motors with MultiStepper. Keep W, the home point and steps/mm as const values at the top of your file.
Done when
From home, goTo the point 100 mm right of home, then back to home. The pen comes back to the home cross within 3 mm.
Mission 3 · Hit the dots
Draw four small dots on the board at points you measure with a tape measure (spread them out over the board). Put their coordinates in your code and visit them one after another, pausing at each.
Done when
The marker tip stops within 3 mm of every dot. Take a short video for the build log.
If you finish early¶
- Find the edges of where it works. Near the top corners the belts get nearly flat and the motors struggle; near the sides one belt goes slack. Mark the safe drawing area on the board with tape and write down its corners.
- Use
constrain()insidegoToso a typo can't send the pen outside your safe area. - Raise the speed until one motor skips, then back off by a third.
- Print the servo mount for the gondola, if it isn't on yet.
Sign-off with Dr. Alvear
- Gondola hangs on two belts; home cross measured from the top-left pulley
- Positive steps let the belt out on both motors
-
goTo(x, y)written by your group, using Pythagoras and your steps/mm - Pen hits four measured dots within 3 mm (video)
- Build log updated (photo + two sentences)