Lab 4:
Weighting for Maglev

The questions below are due on Friday November 27, 2026; 10:00:00 AM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
You are viewing the PRELIMINARY 6.3100 Fall 2026 web site. Please note that information on this page may change throughout the semester, please check back frequently.

Quick Reference

Links

Sketch constants you will edit

LineConstantMeaning
11MODE0: open loop (command = input waveform), 1: PD feedback, 2: state space (later labs)
12–14SensA, SensB, SensCDistance-sensor fit, \Delta y = (a\,x + b)/(c\,x + 1)
15crntSignSign of the field-sensor reading (set in the assembly guide)
17PsmoothInput-step smoothing, 0.0 (none) to 0.999 (very smooth)

Serial plotter send-window indices

Type the index, a space and a value, then click Send (e.g., 3 4 sets K_p to 4).
IndexParameter
0Extra samples for the derivative estimate (ExtraB)
1Derivative gain, K_d
2Integral gain (leave at 0 in this lab)
3Proportional gain, K_p
4Plot spacing (Skips)
5Input frequency (Hz)
6Input amplitude: >0 square wave, <0 sine, 0 none
7Coil-current gain (state space, later labs)
8Reference scaling, K_r (state space, later labs)

Measuring time. The controller runs every 0.1 ms, and adjacent plotted points are (20 + \text{Skips}) \times 0.1 ms apart: 2 ms by default, or 0.5 ms after sending 4 -15.

Task Summary Part A

  1. Build and check the maglev by following the assembly guide (Checkoff 1).
  2. Calibrate the distance sensor, and find a proportional gain that makes the umbrella oscillate (Checkoff 2).
  3. Find a PD controller that floats the umbrella and tracks ±2 mm steps (Checkoff 3).

Introduction

In this lab and the next, you will float a "magnetic umbrella" about 1.5 cm below a three-coil electromagnet. The umbrella is a neodymium magnet in a 3-D printed holder; its top face is a diffuse infrared reflector, and its machine-screw stem keeps it from tilting, like the tail on a kite. An optical sensor measures the distance to the umbrella, a Hall-effect sensor measures the field the coils produce, and the Teensy adjusts the coil currents to keep the umbrella floating.

The video shows a previous term's system toggling the umbrella's position under a lead controller. This week you will build and calibrate the system, and find a PD controller that floats the umbrella. Next week you will model the system and use the model to design a better controller.

Strictly, this is not levitation: the umbrella hangs, it doesn't rise. But "magnetic hanging" doesn't sound nearly as cool.

Rare-earth magnets are dangerous. They are brittle, and when two of them slam together they can send sharp fragments flying. Keep the umbrella away from other magnets and from steel tools.

Build the Maglev

Follow the maglev assembly guide from start to finish. Along the way you will:

  • build the frame, electromagnet and cover bar, and wire both sensors;
  • check with a compass that all three coils point the same way;
  • set crntSign so that the field reading (Crnt) follows the command (Cmd);
  • check the distance sensor's readings at +15 mm and −10 mm.

Checkoff 1:
  1. Show your assembled maglev, and demonstrate with the compass that every coil's needle points north when Cmd is high.
  2. Show that Crnt follows Cmd for a 1 Hz square wave (MODE 0, 6 1, 5 1).
  3. Show your Sensor readings at +15 mm and −10 mm.

Calibrate the Distance Sensor

The optical sensor is a photo-transistor, so its reading x (the Sensor trace, between 0 and 1) grows with the infrared light reflected back from the umbrella. That light falls off steeply with distance, so x is strongly nonlinear in distance. The sketch converts x to the umbrella's displacement from its nominal position, \Delta y in mm, with the rational function

\Delta y = \frac{a\,x + b}{c\,x + 1}.
Positive \Delta y means the umbrella is closer to the electromagnet. You will find a, b and c from three readings, at -10, 0 and +10 mm.

Fitting a, b and c

Multiply both sides by (c\,x + 1), and the fit becomes linear in the three unknowns:

a\,x + b - c\,(x\,\Delta y) = \Delta y.
Three readings (x_i, \Delta y_i) give three equations, which you can write as a matrix equation:
\begin{bmatrix} x_1 & 1 & -x_1 \Delta y_1 \\ x_2 & 1 & -x_2 \Delta y_2 \\ x_3 & 1 & -x_3 \Delta y_3 \end{bmatrix} \begin{bmatrix} a \\ b \\ c \end{bmatrix} = \begin{bmatrix} \Delta y_1 \\ \Delta y_2 \\ \Delta y_3 \end{bmatrix}.
In Python:

import numpy as np
x  = np.array([0.134, 0.280, 0.560])   # readings at -10, 0, +10 mm
dy = np.array([-10.0, 0.0, 10.0])
A  = np.column_stack([x, np.ones(3), -x*dy])
a, b, c = np.linalg.solve(A, dy)

Try it first. With the example readings in the code (x = 0.134, 0.280 and 0.560), find a, b and c, and enter them as a Python list [a, b, c].

[a, b, c] =

Using that fit, what \Delta y does a reading of x = 0.847 give? (That is a typical +15 mm reading.)

\Delta y (mm) =

A fit through −10, 0 and +10 mm still predicts +15 mm well. A straight line through the same three points would not.

Taking your three readings

Set up as for the range test in the assembly guide: MODE 0, amplitude zero (6 0), only the Sensor trace plotted, the potentiometer turned so the electromagnet pulls the umbrella up, and the calibration attachment clipped on.

−10 mm: pulled up against the bottom of the attachment
0 mm: electromagnet off, umbrella resting on the middle ledge
+10 mm: pulled up against the highest ledge
  1. −10 mm: use your reading from the range test.
  2. +10 mm: with the electromagnet still pulling up, hold the umbrella against the attachment's highest ledge, and note the reading.
  3. 0 mm: unplug the high-current (driver) power supply so the electromagnet is off, rest the umbrella on the attachment's middle ledge, and note the reading. Plug the supply back in afterwards.
For each, read the middle of the noisy Sensor trace.

Entering and testing your fit

  1. Solve for your a, b and c, enter them as SensA, SensB and SensC on lines 12–14, and upload again.
  2. Test the fit: remove the attachment, let the electromagnet pull the umbrella up against the sensor bracket (+15 mm), and plot Y. It should read close to 15, even though 15 mm is outside the range you fit.

With the umbrella against the bracket, Y reads (mm): 

Proportional Feedback

With the sensor calibrated, try feedback for the first time.

  1. Set MODE to 1, upload, and set the proportional gain to 3 (send 3 3).
  2. Hold the umbrella at the 0 mm position, and turn the nominal-command potentiometer until the umbrella feels weightless: the electromagnet's pull should just cancel gravity.
  3. Loosen your grip. The umbrella should oscillate, at least briefly, as in the video below.
  • If the electromagnet pulls harder as the umbrella gets closer, the feedback sign is wrong. Swap the last two arguments in all three hbridgeBipolar calls (lines 208–210). Then also flip the sign of crntSign on line 15 (-1.0 to 1.0, or the reverse). Reversing the coil commands reverses the field the sensor sees, so without this change Crnt would no longer follow Cmd. Upload again, and recheck with the 1 Hz square wave from "Matching the Field Sensor to the Command" in the assembly guide.
  • If the push-back is correct but weak, increase the gain, but do not exceed 10. Higher gains saturate the coil drivers, and the system stops behaving like a linear feedback loop.
With proportional feedback alone, the umbrella oscillates about its set point (previous term's setup).

Checkoff 2:
  1. Show your three calibration readings, how you solved for a, b and c, and the +15 mm test. Why is the sensor reading nonlinear in distance?
  2. In MODE 1, demonstrate a proportional gain that makes the umbrella oscillate. What gain did you use?
  3. Why does proportional feedback alone make the umbrella oscillate instead of settle?

PD Control

Set MODE to 1, upload, and start from the proportional gain that made the umbrella oscillate. Now add derivative gain K_d (index 1) to damp the oscillation. Find a K_p and K_d that float the umbrella steadily, then test with ±2 mm square-wave steps: send 6 2 (amplitude 2 mm) and 5 0.5 (0.5 Hz). If tracking a square wave is hard, try setting Psmooth on line 17 to 0.95. Expect a noisy command and imperfect tracking; next week's model-based controller does much better.

PD control on a previous term's setup. Note how noisy the command is.

Checkoff 3:
  1. Demonstrate your PD controller floating the umbrella. What are your values of Kp and Kd?
  2. Show the umbrella tracking ±2 mm steps. Are the rising and falling responses the same?
  3. Is the command smooth or noisy? Where does the noise come from?

STOP HERE FOR WEEK A