In der Simulation läuft es schon mal.
Datei mit der Endung .html erstellen, Skript einfügen und einfach mal testen.
Position 0% = Ost
Position 100% = West
Leider zählt die Variable [Position] den Wert weiter hoch obwohl der Motor steht aber das ist in der Simulation eigentlich egal.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Solar Nachführung Simulation</title>
<style>
body { font-family: sans-serif; text-align: center; margin-top: 20px; }
.box { display: inline-block; width: 100px; height: 50px; margin: 10px; line-height: 20px; color: white; font-weight: bold; border-radius: 10px; font-size: 12px; }
.inactive { background: #888; }
.active { background: #28a745; }
.moving { background: #ffc107; color: #000; }
.halt { background: #dc3545; }
#status { width: 100px; height: 20px; line-height: 20px; margin-top: 20px; }
#pulseDisplay { width: 600px; height: 20px; background: #eee; margin: 10px auto; border-radius: 10px; overflow: hidden; position: relative; }
#pulseBar { height: 100%; width: 0%; transition: width 0.1s, background 0.1s; }
table { margin: 20px auto; border-collapse: collapse; font-size: 14px; }
td, th { border: 1px solid #ccc; padding: 4px 8px; }
th { background: #eee; }
</style>
</head>
<body>
<h1>Solar Nachführung Simulation</h1>
<div id="ldrOst" class="box inactive">LDR OST</div>
<div id="ldrWest" class="box inactive">LDR WEST</div><br>
<div id="motorOst" class="box inactive">Motor OST</div>
<div id="motorWest" class="box inactive">Motor WEST</div><br>
<div id="status" class="box inactive">Status: OK</div>
<div id="homingPhase">Homing: -</div>
<div id="driveTime">Fahrtzeit OST->WEST: 50 Sekunden</div>
<h3>Pulsanzeige</h3>
<div id="pulseDisplay">
<div id="pulseBar"></div>
</div>
<h2>Live STATE Dashboard</h2>
<table id="dashboard">
<thead>
<tr><th>Variable</th><th>Wert</th></tr>
</thead>
<tbody></tbody>
</table>
<h3>Steuerung über Tasten:</h3>
<p>Drücke die Pfeiltasten: ↑ um LDR Ost zu erhöhen, ↓ um LDR West zu erhöhen</p>
<script>
// ================== CONFIG ==================
let CONFIG = {
ip_Uni_West: "192.168.178.61",
ip_1PM_Ost: "192.168.178.70",
ip_1PM_West: "192.168.178.71",
max_power_block: 45,
nominal_power: 25,
wear_threshold: 1.3,
full_drive_time: 50000, // Gesamtfahrzeit (30000ms = 30s)
updateInterval: 100, // Update Intervall von 1000ms auf 200ms reduzieren (macht es langsamer)
nextMoveDelay: 1500,
safetyTimeout: 500,
homingTimeMorning: "05:00",
homingTimeEvening: "18:00",
darknessThreshold: 10,
minLdrDifference: 7, // MINIMALER Unterschied zwischen LDR Ost und West
pulseSmall: 200,
pulseMedium: 500,
pulseLarge: 900
};
// ================== STATE ==================
let STATE = {
currentPos: 50,
isMoving: false,
lastDirection: null,
systemHalt: false,
eveningCheckDone: false,
ostReachedTime: 0,
powerSamples: [],
blockCounter: 0,
nextMoveAllowed: 0,
homingPhase: null,
homingStartTime: 0,
ldrOstValue: 50, // Startwert für LDR in der Mitte
ldrWestValue: 50, // Startwert für LDR in der Mitte
simulatedPower: 20,
lastPulse: 0,
lastPulseType: ''
};
// ================== HELPER ==================
function updateLDR() {
document.getElementById('ldrOst').className = 'box ' + (STATE.ldrOstValue > CONFIG.darknessThreshold ? 'active':'inactive');
document.getElementById('ldrOst').innerText = `LDR OST\n${Math.round(STATE.ldrOstValue)}%`;
document.getElementById('ldrWest').className = 'box ' + (STATE.ldrWestValue > CONFIG.darknessThreshold ? 'active':'inactive');
document.getElementById('ldrWest').innerText = `LDR WEST\n${Math.round(STATE.ldrWestValue)}%`;
}
function updatePulseDisplay(pulseDuration, pulseType) {
let bar = document.getElementById('pulseBar');
STATE.lastPulseType = pulseType;
// Farbe je Pulsart
let color = pulseType === 'small' ? '#ff9800' :
pulseType === 'medium' ? '#ffeb3b' :
'#f44336';
bar.style.background = color;
bar.style.width = (pulseDuration / CONFIG.full_drive_time * 100) + '%';
setTimeout(() => { bar.style.width = '0%'; }, pulseDuration);
}
function drive(direction, pulseDuration = null, pulseType = 'small') {
// Wenn Systemhalt oder schon eine Fahrt läuft, nichts tun
if (STATE.systemHalt
STATE.isMoving
Date.now() < STATE.nextMoveAllowed) return;
// Überprüfen, ob die Position die Grenzen erreicht hat
if (direction === 'OST' && STATE.currentPos <= 0) {
console.log("Position 0% erreicht, kann nicht weiter nach Osten fahren");
return; // Keine Bewegung nach Osten, wenn Position 0% erreicht
}
if (direction === 'WEST' && STATE.currentPos >= 100) {
console.log("Position 100% erreicht, kann nicht weiter nach Westen fahren");
return; // Keine Bewegung nach Westen, wenn Position 100% erreicht
}
STATE.isMoving = true;
STATE.lastDirection = direction;
// Zuerst alle Motoren auf OFF setzen
document.getElementById('motorOst').className = 'box inactive';
document.getElementById('motorWest').className = 'box inactive';
// Nur den aktuellen Motor auf ON setzen
if (direction === 'OST') document.getElementById('motorOst').className = 'box moving';
else document.getElementById('motorWest').className = 'box moving';
let duration = pulseDuration
CONFIG.full_drive_time;
STATE.lastPulse = duration;
updatePulseDisplay(duration, pulseType);
let startPos = STATE.currentPos;
let targetChange = direction === 'WEST' ? 100 - STATE.currentPos : 0 - STATE.currentPos;
let startTime = Date.now();
let interval = setInterval(() => {
let elapsed = Date.now() - startTime;
if (elapsed >= duration) elapsed = duration;
STATE.currentPos = startPos + targetChange * (elapsed / duration);
// Aktualisiere die Dashboard-Position
updateStateDashboard();
if (elapsed >= duration) {
clearInterval(interval);
STATE.isMoving = false;
STATE.nextMoveAllowed = Date.now() + CONFIG.nextMoveDelay;
// Nach Fahrt Motor wieder auf OFF
document.getElementById('motorOst').className = 'box inactive';
document.getElementById('motorWest').className = 'box inactive';
// Pulsanzeige zurücksetzen
document.getElementById('pulseBar').style.width = '0%';
// Position bleibt sofort an der aktuellen Stelle (wie ein sofort stopppender Motor)
STATE.currentPos = STATE.currentPos; // Position einfrieren
}
}, CONFIG.updateInterval);
}
// ================== SIMULATION LOOP ==================
function solveLogic() {
if (STATE.systemHalt) return;
updateLDR(); // LDR-Werte aktualisieren
updateStateDashboard(); // Live Dashboard aktualisieren
// Berechne den LDR-Unterschied
const ldrDifference = Math.abs(STATE.ldrOstValue - STATE.ldrWestValue);
// Wenn der Unterschied zu klein ist, stoppe den Motor
if (ldrDifference <= CONFIG.minLdrDifference) {
console.log("LDR Unterschied zu klein, stoppe Bewegung");
// Falls der Motor sich gerade bewegt, stoppen
if (STATE.isMoving) {
STATE.isMoving = false;
document.getElementById('motorOst').className = 'box inactive';
document.getElementById('motorWest').className = 'box inactive';
// Pulsanzeige zurücksetzen
document.getElementById('pulseBar').style.width = '0%';
// Position bleibt sofort an ihrem aktuellen Wert
STATE.currentPos = STATE.currentPos; // Position einfrieren
}
}
// Wenn der Unterschied größer als der Schwellenwert ist, fahre zur Sonne
else {
console.log("LDR Unterschied ausreichend, starte Bewegung");
if (STATE.ldrOstValue < STATE.ldrWestValue) {
console.log("Fahre nach Osten");
drive('OST', CONFIG.pulseLarge, 'large');
} else {
console.log("Fahre nach Westen");
drive('WEST', CONFIG.pulseLarge, 'large');
}
}
}
// Funktion, die das Dashboard regelmäßig aktualisiert
function updateStateDashboard() {
const tbody = document.querySelector('#dashboard tbody');
tbody.innerHTML = ''; // Vorherige Werte löschen
// Dashboard mit den aktuellen Werten füllen
const stateData = [
{ name: 'Position', value: `${STATE.currentPos.toFixed(2)} %` },
{ name: 'LDR Ost', value: `${Math.round(STATE.ldrOstValue)} %` },
{ name: 'LDR West', value: `${Math.round(STATE.ldrWestValue)} %` },
{ name: 'Motor Bewegung', value: STATE.isMoving ? 'Ja' : 'Nein' },
{ name: 'Letzte Richtung', value: STATE.lastDirection
'N/A' },
{ name: 'Homing Phase', value: STATE.homingPhase
'Nicht aktiv' },
];
stateData.forEach(item => {
const tr = document.createElement('tr');
const tdName = document.createElement('td');
const tdValue = document.createElement('td');
tdName.textContent = item.name;
tdValue.textContent = item.value;
tr.appendChild(tdName);
tr.appendChild(tdValue);
tbody.appendChild(tr);
});
}
// Steuerung mit den Pfeiltasten
document.addEventListener('keydown', (event) => {
if (event.key === "ArrowUp") {
if (STATE.ldrOstValue < 100) {
STATE.ldrOstValue += 1;
STATE.ldrWestValue = 100 - STATE.ldrOstValue;
}
}
if (event.key === "ArrowDown") {
if (STATE.ldrWestValue < 100) {
STATE.ldrWestValue += 1;
STATE.ldrOstValue = 100 - STATE.ldrWestValue;
}
}
});
// Starte die Simulation
setInterval(solveLogic, CONFIG.updateInterval);
</script>
</body>
</html>