Aiemmin sanoin ChatGPT:n olevan vain puppugeneraattori. Tässä viikon aikana tullut enemmän tutustuttua, niin en enää usko niin. [1, 2]
PID säädin automaattisella konfiguraatiolla Commodore 64 -koneella
Arduino C++
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Pin connected to the DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int relayPin = 3; // Pin connected to the relay
void setup() {
Serial.begin(9600);
sensors.begin();
pinMode(relayPin, OUTPUT); // Set relay pin as output
}
void loop() {
sensors.requestTemperatures(); // Request temperature reading
float temp = sensors.getTempCByIndex(0); // Get temperature of the first sensor
Serial.println(temp); // Send the temperature to C64
delay(1000); // Wait for 1 second before sending the next reading
}
C64 BASIC
10 REM Geothermal System PID Control with Auto-Tuning
20 PRINT "Geothermal PID Control System Starting..."
30 REM Initial Default PID Values
40 KP = 2
50 KI = 0.1
60 KD = 1
70 SETPOINT = 20 REM Initial Desired Temperature (°C)
80 TEMP = 0 REM Current Temperature
90 ERROR = 0 REM Error (SETPOINT - TEMP)
100 LAST_ERROR = 0 REM Last Error
110 INTEGRAL = 0 REM Integral Term
120 AUTO_TUNE = 1 REM 1 = Enable Auto-Tuning, 0 = Use Manual Values
130 PRINT "Press any key to start auto-tuning or press RETURN to use manual values."
140 REM Wait for key press before starting auto-tuning
150 IF PEEK(198) = 0 THEN GOTO 150 REM Wait for key press
160 REM Check if user pressed RETURN (no auto-tune)
170 IF PEEK(198) = 13 THEN AUTO_TUNE = 0
180 REM If Auto-Tuning is Enabled, Perform Automatic PID Calibration
190 IF AUTO_TUNE = 1 THEN GOSUB 2000
200 PRINT "Final PID Values: KP="; KP; " KI="; KI; " KD="; KD
210 PRINT "Press any key to continue..."
220 REM Open Serial Port (assuming C64 serial is at 9600 baud)
230 OPEN 1,8,15,"U0:9600,N,8,1" REM C64 opens serial port to listen
240 REM Loop to check temperature and control relays
250 REM Read Temperature from Serial Port
260 INPUT #1,TEMP$ REM Read temperature string from serial port
270 TEMP = VAL(TEMP$) REM Convert to numeric value
280 PRINT "Current Temperature: "; TEMP
290 REM Calculate PID terms
300 ERROR = SETPOINT - TEMP
310 INTEGRAL = INTEGRAL + ERROR
320 DERIVATIVE = ERROR - LAST_ERROR
330 REM Calculate PID Output
340 PID_OUTPUT = (KP * ERROR) + (KI * INTEGRAL) + (KD * DERIVATIVE)
350 PRINT "PID Output: "; PID_OUTPUT
360 REM Set Relay based on PID Output
370 IF PID_OUTPUT > 10 THEN GOSUB 5000 REM Activate heating if output > 10
380 IF PID_OUTPUT < -10 THEN GOSUB 6000 REM Deactivate heating if output < -10
390 REM Save the current error for the next loop
400 LAST_ERROR = ERROR
410 REM Add SID Sound for High and Low Temperatures
420 IF TEMP > 20 THEN GOSUB 7000 REM High temperature sound (too warm)
430 IF TEMP < 10 THEN GOSUB 8000 REM Low temperature sound (too cold)
440 REM Repeat every 1 second
450 FOR I = 1 TO 1000: NEXT I
460 GOTO 250
5000 REM Activate Heating Relay
5010 PRINT "Activating Heating..."
5020 POKE 53280, 255 REM Set user port to turn on heating relay
5030 RETURN
6000 REM Deactivate Heating Relay
6010 PRINT "Deactivating Heating..."
6020 POKE 53280, 0 REM Set user port to turn off heating relay
6030 RETURN
7000 REM High Temperature Sound (Too Warm)
7010 POKE 54296, 255 REM Set voice 1 frequency high (too warm)
7020 POKE 54277, 15 REM Set volume (loud)
7030 POKE 54279, 15 REM Enable sound
7040 FOR I = 1 TO 1000: NEXT I
7050 POKE 54279, 0 REM Disable sound
7060 RETURN
8000 REM Low Temperature Sound (Too Cold)
8010 POKE 54296, 100 REM Set voice 1 frequency low (too cold)
8020 POKE 54277, 15 REM Set volume (loud)
8030 POKE 54279, 15 REM Enable sound
8040 FOR I = 1 TO 1000: NEXT I
8050 POKE 54279, 0 REM Disable sound
8060 RETURN
2000 REM Auto-Tuning Routine
2010 PRINT "Auto-Tuning Started..."
2020 PRINT "Step 1: Applying Heat Step Input..."
2030 REM Apply Heat Pulse (Force System to React)
2040 POKE 53280, 255 REM Turn ON heating relay
2050 FOR I = 1 TO 5000: NEXT I REM Wait for system response (simulate heating)
2060 POKE 53280, 0 REM Turn OFF heating relay
2070 PRINT "Step 2: Measuring Response..."
2080 REM Measure Temperature Rise and Oscillation
2090 INPUT #1, TEMP$
2100 TEMP = VAL(TEMP$)
2110 T1 = TIMER REM Start time measurement
2120 REM Wait Until Temperature Peaks
2130 REPEAT:
2140 INPUT #1, TEMP$
2150 TEMP = VAL(TEMP$)
2160 UNTIL TEMP > SETPOINT + 2 REM Wait until temperature overshoots by 2°C
2170 T2 = TIMER REM Save time when peak is reached
2180 REM Calculate Approximate System Response Time
2190 RESPONSE_TIME = T2 - T1
2200 PRINT "Response Time Detected: "; RESPONSE_TIME; " Cycles"
2210 REM Assign Approximate PID Values Based on Response
2220 KP = 0.6 * (SETPOINT / RESPONSE_TIME)
2230 KI = 2 * KP / RESPONSE_TIME
2240 KD = KP * RESPONSE_TIME / 8
2250 PRINT "Auto-Tuning Complete!"
2260 RETURN