Vector Display

Schematic of Vector Display
// Five ternary acceleration digits: X, Y, pitch, yaw and depth.
// The 8-bit command is held on D2-D9. D10 clocks the 16 ms motion step.
unsigned long frameAt = 0;
unsigned long highAt = 0;
int phase = 0;
bool heartbeat = true;
int lastCommand = 0;
int tickIndex = 0;
int spinRate[2] = {64, 116};
int spinAccel[2];
int spinDemand[2];
int depthPosition = 0;
int depthVelocity = 0;
int depthForce = 0;
int depthDemand = 0;
bool movingAway = true;
int approach(int current, int target) {
  if (current < target) return current + 1;
  if (current > target) return current - 1;
  return current;
}

int limited(int value, int lo, int hi) {
  if (value < lo) return lo;
  if (value > hi) return hi;
  return value;
}

int digitForRate(int current, int target) {
  if (current < target - 6) return 1;
  if (current > target + 6) return 2;
  return 0;
}

int accelerationFor(int digit) {
  if (digit == 1) return 4;
  if (digit == 2) return -4;
  return 0;
}

int depthDigit() {
  if (movingAway && depthPosition >= 9000) movingAway = false;
  else if (!movingAway && depthPosition <= 480) movingAway = true;
  int remaining = movingAway ? 9000 - depthPosition : depthPosition - 480;
  int rate = 64;
  if (remaining < 1200) rate = 36;
  if (remaining < 450) rate = 16;
  if (!movingAway) rate = -rate;
  return digitForRate(depthVelocity, rate);
}

int planCommand() {
  int period = tickIndex / 32;
  int x = 0;
  int y = 0;
  if (period == 0 || period == 5) x = 1;
  if (period == 2 || period == 7) x = 2;
  if (period == 1 || period == 6) y = 1;
  if (period == 3 || period == 4) y = 2;
  int pitchTarget = 52;
  if ((period & 1) != 0) pitchTarget = 92;
  int yawTarget = 78;
  if ((period & 2) != 0) yawTarget = 142;
  int pitch = digitForRate(spinRate[0], pitchTarget);
  int yaw = digitForRate(spinRate[1], yawTarget);
  spinDemand[0] = accelerationFor(pitch);
  spinDemand[1] = accelerationFor(yaw);
  int depth = depthDigit();
  depthDemand = accelerationFor(depth);
  return x + 3 * y + 9 * pitch + 27 * yaw + 81 * depth;
}

void sendCommand(int cmd) {
  int changed = cmd ^ lastCommand;
  int value = cmd;
  for (int bit = 0; bit < 8 && changed != 0; bit++) {
    if ((changed & 1) != 0) digitalWrite(2 + bit, value & 1);
    changed = changed >> 1;
    value = value >> 1;
  }
  lastCommand = cmd;
}

void advanceRateModel() {
  for (int i = 0; i < 2; i++) spinAccel[i] = approach(spinAccel[i], spinDemand[i]);
  spinRate[0] = limited(spinRate[0] + spinAccel[0], 40, 100);
  spinRate[1] = limited(spinRate[1] + spinAccel[1], 60, 156);
  depthForce = approach(depthForce, depthDemand);
  int oldDepthSpeed = depthVelocity;
  depthVelocity = limited(oldDepthSpeed + depthForce, -72, 72);
  depthPosition = depthPosition + (oldDepthSpeed + depthVelocity) / 2;
  if (depthPosition < 0) {
    depthPosition = 0;
    depthVelocity = 0;
  } else if (depthPosition > 9600) {
    depthPosition = 9600;
    depthVelocity = 0;
  }
  tickIndex = (tickIndex + 1) & 255;
}

void setup() {
  for (int pin = 2; pin <= 10; pin++) {
    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);
  }
  pinMode(11, INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}

void loop() {
  unsigned long now = millis();
  bool beat = now % 1500UL < 1000UL;
  if (beat != heartbeat) {
    heartbeat = beat;
    digitalWrite(13, beat ? HIGH : LOW);
  }
  bool ack = digitalRead(11) != 0;
  if (phase == 0 && !ack && now - frameAt >= 16UL) {
    frameAt = now;
    if ((tickIndex & 7) == 0) {
      int cmd = planCommand();
      if (cmd != lastCommand) {
        sendCommand(cmd);
        phase = 1;
        return;
      }
    }
    digitalWrite(10, HIGH);
    highAt = now;
    phase = 2;
  } else if (phase == 1 && now - frameAt >= 2UL) {
    digitalWrite(10, HIGH);
    highAt = now;
    phase = 2;
  } else if (phase == 2 && (ack || now - highAt >= 32UL)) {
    if (ack) advanceRateModel();
    digitalWrite(10, LOW);
    phase = 0;
  }
}

// Live second-order cube. Center uses 256 units per grid pixel.
int controlPins[8] = {10,9,8,7,6,5,4,3};
int demand[5];
int force[5];
int lastCommand = 0;
int depthPosition = 0;
int depthVelocity = 0;
int faceCutoff = 12;
int quarterSine[65] = {0, 3, 6, 9, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58, 60, 63, 66, 68, 71, 74, 76, 79, 81, 84, 86, 88, 91, 93, 95, 97, 99, 101, 103, 105, 106, 108, 110, 111, 113, 114, 116, 117, 118, 119, 121, 122, 122, 123, 124, 125, 126, 126, 127, 127, 127, 128, 128, 128, 128};
int edgeStart[12] = {0,2,4,6,0,1,4,5,0,1,2,3};
int edgeEnd[12] = {1,3,5,7,2,3,6,7,4,5,6,7};
int px[8];
int py[8];
// Angular phases use 64 units per sine-table step.
int pitchPhase = 2368;
int yawPhase = 3392;
int spinRate[2] = {64, 116};
int centerX = 12544;
int centerY = 13824;
int velocityX = 128;
int velocityY = 96;
int visible = 0;
bool ackHigh = false;
bool tickPending = false;

int sine128(int turn) {
  turn = turn & 255;
  if (turn <= 64) return quarterSine[turn];
  if (turn <= 128) return quarterSine[128 - turn];
  if (turn <= 192) return -quarterSine[turn - 128];
  return -quarterSine[256 - turn];
}

int fractionalSine(int phase) {
  int step = (phase >> 6) & 255;
  int value = sine128(step);
  return value + (sine128(step + 1) - value) * (phase & 63) / 64;
}

bool frontFace(int corner, int axis, int normal) {
  if (normal >= -faceCutoff && normal <= faceCutoff) return false;
  return ((corner & axis) != 0) == (normal > 0);
}

int approach(int current, int target) {
  if (current < target) return current + 1;
  if (current > target) return current - 1;
  return current;
}

int limited(int value, int lo, int hi) {
  if (value < lo) return lo;
  if (value > hi) return hi;
  return value;
}

int wallSpeed(int v) {
  if (v < 0) v = -v;
  if (v < 64) v = 64;
  return v;
}

void stepTranslation() {
  int oldX = velocityX;
  int oldY = velocityY;
  velocityX = limited(oldX + force[0], -184, 184);
  velocityY = limited(oldY + force[1], -184, 184);
  centerX = centerX + (oldX + velocityX) / 2;
  centerY = centerY + (oldY + velocityY) / 2;
}

void readCommand() {
  int cmd = 0;
  for (int i = 7; i >= 0; i--) cmd = 2 * cmd + digitalRead(controlPins[i]);
  if (cmd > 242 || cmd == lastCommand) return;
  lastCommand = cmd;
  for (int i = 0; i < 5; i++) {
    int digit = cmd % 3;
    int target = 0;
    if (digit == 1) target = 4;
    if (digit == 2) target = -4;
    demand[i] = target;
    cmd = cmd / 3;
  }
}

void stepAngularMotion() {
  int oldPitch = spinRate[0];
  int oldYaw = spinRate[1];
  spinRate[0] = limited(oldPitch + force[2], 40, 100);
  spinRate[1] = limited(oldYaw + force[3], 60, 156);
  pitchPhase = (pitchPhase + (oldPitch + spinRate[0]) / 2) & 16383;
  yawPhase = (yawPhase + (oldYaw + spinRate[1]) / 2) & 16383;
}

void stepDepth() {
  int oldDepthSpeed = depthVelocity;
  depthVelocity = limited(oldDepthSpeed + force[4], -72, 72);
  depthPosition = depthPosition + (oldDepthSpeed + depthVelocity) / 2;
  if (depthPosition < 0) {
    depthPosition = 0;
    depthVelocity = 0;
  } else if (depthPosition > 9600) {
    depthPosition = 9600;
    depthVelocity = 0;
  }
}

// Expand the fixed-point coordinate without exceeding signed 16-bit range.
int canvasCoordinate(int coordinate) {
  return 9 * (coordinate / 256) + (9 * (coordinate % 256) + 128) / 256;
}

void updateCube(bool advance) {
  int sa = fractionalSine(pitchPhase);
  int ca = fractionalSine(pitchPhase + 4096);
  int sb = fractionalSine(yawPhase);
  int cb = fractionalSine(yawPhase + 4096);

  int xFromY = 18 * (sa * sb / 128);
  int xFromZ = 18 * (ca * sb / 128);
  int xFromX = 18 * cb;
  int yFromY = 18 * ca;
  int yFromZ = 18 * sa;
  int zFromY = sa * cb / 128;
  int zFromZ = ca * cb / 128;
  int dFromY = 18 * zFromY;
  int dFromZ = 18 * zFromZ;
  int dFromX = -18 * sb;
  int cameraDistance = 64 + depthPosition / 128;
  faceCutoff = 768 / cameraDistance;
  int minX = 1024, maxX = -1024;
  int minY = 1024, maxY = -1024;
  for (int v = 0; v < 8; v++) {
    int x = (v & 1) ? xFromX : -xFromX;
    int y = (v & 2) ? xFromY : -xFromY;
    int z = (v & 4) ? xFromZ : -xFromZ;
    int rawX = (x + y + z) / 16;
    int rawY = (((v & 4) ? yFromZ : -yFromZ) - ((v & 2) ? yFromY : -yFromY)) / 16;
    int dx = (v & 1) ? dFromX : -dFromX;
    int dy = (v & 2) ? dFromY : -dFromY;
    int dz = (v & 4) ? dFromZ : -dFromZ;
    int distance = cameraDistance - (dx + dy + dz) / 320;
    int xPos = 64 * rawX / distance;
    int yPos = 64 * rawY / distance;
    px[v] = xPos;
    py[v] = yPos;
    if (xPos < minX) minX = xPos;
    if (xPos > maxX) maxX = xPos;
    if (yPos < minY) minY = yPos;
    if (yPos > maxY) maxY = yPos;
  }

  if (advance) stepTranslation();

  int lowX = -32 * minX;
  int lowY = -32 * minY;
  int highX = 28928 - 32 * maxX;
  int highY = 28928 - 32 * maxY;
  if (centerX < lowX) {
    centerX = lowX;
    velocityX = wallSpeed(velocityX);
  } else if (centerX > highX) {
    centerX = highX;
    velocityX = -wallSpeed(velocityX);
  }
  if (centerY < lowY) {
    centerY = lowY;
    velocityY = wallSpeed(velocityY);
  } else if (centerY > highY) {
    centerY = highY;
    velocityY = -wallSpeed(velocityY);
  }



  for (int v = 0; v < 8; v++) {
    px[v] = canvasCoordinate(centerX + 32 * px[v]);
    py[v] = canvasCoordinate(centerY + 32 * py[v]);
  }

  visible = 0;
  int bit = 1;
  for (int e = 0; e < 12; e++) {
    int corner = edgeStart[e];
    if ((e >= 4 && frontFace(corner, 1, -sb)) ||
        ((e < 4 || e >= 8) && frontFace(corner, 2, zFromY)) ||
        (e < 8 && frontFace(corner, 4, zFromZ))) visible = visible + bit;
    bit = 2 * bit;
  }
}

void setup() {
  chipName("114 x 114 Motion Control Cube");
  logicFamily(CMOS);
  pin(1, "ACK", OUTPUT);
  pin(2, "TICK", INPUT);
  pin(3, "A7", INPUT);
  pin(4, "A6", INPUT);
  pin(5, "A5", INPUT);
  pin(6, "A4", INPUT);
  pin(7, "A3", INPUT);
  pin(8, "A2", INPUT);
  pin(9, "A1", INPUT);
  pin(10, "A0", INPUT);
  pin(19, "GND", GROUND);
  pin(20, "5V", POWER);
  digitalWrite(1, LOW);
  updateCube(false);
}

void loop() {
  bool strobe = digitalRead(2) != 0;
  if (strobe != ackHigh) {
    ackHigh = strobe;
    digitalWrite(1, strobe ? HIGH : LOW);
    if (strobe) {
      readCommand();
      tickPending = true;
    }
    return;
  }
  if (tickPending) {
    tickPending = false;
    for (int i = 0; i < 5; i++) force[i] = approach(force[i], demand[i]);
    stepAngularMotion();
    stepDepth();
    updateCube(true);
  }
}

void draw() {
  int width = chipWidth();
  int height = chipHeight();
  if (width < 1026 || height < 1026) return;
  int left = (width - 1026) / 2;
  int top = (height - 1026) / 2;
  setColor(255, 170, 34);
  fillRect(left, top, 1026, 1026);
  setColor(0, 0, 0);
  int mask = visible;
  for (int e = 0; e < 12; e++) {
    if ((mask & 1) != 0) {
      int a = edgeStart[e];
      int b = edgeEnd[e];
      if (px[a] != px[b] || py[a] != py[b]) {
        drawLine(left + px[a], top + py[a], left + px[b], top + py[b]);
      }
    }
    mask = mask >> 1;
  }
  setDefaultColor();
}