Klausj
Published © GPL3+

Ship Swing Simulation

Did you enjoy jour last ride on a ship swing at the fair? And did you manage to flip it over the top? You can easily have it in simulation.

IntermediateFull instructions provided1 hour103
Ship Swing Simulation

Things used in this project

Story

Read more

Code

Ship Swing Simulation, R3 and R4

Arduino
/*
  Schiffschaukel ohne Daempfung
  Vergleich der Rechenzeit pro Schritt:
  R3: 370 - 390 us
  R4: 175 - 182 us
  Fussnote:
  drawCircle(mx,my,r1,color) erzeugt
  keinen Kreis. Grund unbekannt.
*/

/*
  diese Standard-Library laeuft
  nicht auf dem R4:
  TFT tft = TFT(cs, dc, rst);
*/

#include <TFT18_R4.h>
TFT18_R4 tft;
int w, h, mx, my, r1, r2;
const double m = 1; // irrelevant
const double g = 9.81;
int x, y, counter;
double Wkin, Wpot, Wges, dt, ds,
       phi, dPhi, yF, v, vx, vy;
void setup() {
  Serial.begin(9600);
  Serial.println(__FILE__);
  // Graphik:
  tft.begin();
  tft.setRotation(0);
  w = tft.width();
  h = tft.height();
  mx = w / 2;
  my = h / 2;
  r2 = 5;
  r1 = mx - r2;
  dt = 0.1;
  //tft.background(0, 0, 0);
  tft.fillScreen(ST7735_BLUE);
  tft.fillCircle(mx, my, r1 + r2, 0);
  tft.stroke(255, 255, 255);
  tft.text("Rechenzeit", mx - 1, 0);
  tft.text("pro Schritt", mx - 1, 9);
  // Anfangswerte:
  // Naehe oberer Totpunkt
  Wges = g * r1 * 1.001;
  phi = HALF_PI + 0.001;
}

void loop() {
  int xAlt = x;
  int yAlt = y;
  //==========================
  // berechne den naechsten Schritt:
  long t1 = micros();
  x = r1 * cos(phi);
  yF = r1 * sin(phi); // muss genau sein
  y = yF;
  Wpot = m * g * yF;
  Wkin = Wges - Wpot;
  v = sqrt(2 * Wkin / m);
  ds = v * dt;
  dPhi = ds / r1;
  phi = phi - dPhi;
  long t2 = micros();
  //=========================
  Serial.print(Wpot);
  Serial.print(" ");
  Serial.println(Wkin);
  if (Wkin < 0) {
    Serial.println("Error");
    tft.text("Wkin < 0", 0, 10);
    while (true);
  }
  // Nulldurchgang erfassen und anzeigen:
  if ( (xAlt < 0) && (x >= 0) ) {
    counter++;
    tft.setCursor(0, 0);
    tft.setTextSize(3);
    tft.writeInt(counter);
    tft.setTextSize(1);
  }
  // altes Bild nur loeschen wenn noetig:
  if ((x != xAlt) || (y != yAlt)) {
    tft.drawLine(mx, my, mx + xAlt, my - yAlt, 0);
    tft.fillCircle(mx + xAlt, my - yAlt, r2, 0);
  }
  // neue Graphik zeichnen:
  tft.fillCircle(mx + x, my - y, r2, 0xFF00);
  tft.drawLine(mx, my, mx + x, my - y, 0xFFFF);
  tft.fillCircle(mx, my, r2, ST7735_CYAN);
  // reine Rechenzeit fuer diesen Schritt:
  tft.setCursor(96, 18);
  tft.writeLong(t2 - t1);
  tft.text("us", 116, 18);
}

Library for using 1.8" TFT with UNO R4

Arduino
No preview (download only).

Credits

Klausj
83 projects • 7 followers
Contact

Comments

Please log in or sign up to comment.