ChatGPT osa 2

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 PID Control with PETSCII Trend Prediction
20 PRINT "{clr}Geothermal PID Control System Starting..."
30 REM Default PID Values
40 KP = 2
50 KI = 0.1
60 KD = 1
70 SETPOINT = 20
80 TEMP = 0
90 ERROR = 0
100 LAST_ERROR = 0
110 INTEGRAL = 0
120 AUTO_TUNE = 1
130 HEATING = 0
140 DIM TEMP_HISTORY(32) REM Stores 32 temperature readings

150 PRINT "{cyan}Choose Configuration:"
160 PRINT "{white}Press {green}1{white} for Auto-Tuning (System Adjusts PID Automatically)"
170 PRINT "Press {yellow}2{white} for Manual PID Tuning (Enter Your Own Values)"
180 GOSUB 9200 REM Play Selection Sound

190 REM User Selection
200 GET A$
210 IF A$ = "" THEN GOTO 200
220 IF A$ = "1" THEN GOTO 300
230 IF A$ = "2" THEN GOTO 400
240 GOTO 200

300 REM Auto-Tuning
310 PRINT "{cyan}Auto-Tuning Selected. Measuring System Response..."
320 GOSUB 2000
330 PRINT "{green}Auto-Tuning Complete! Using Calculated PID Values."
340 GOTO 500

400 REM Manual Tuning
410 PRINT "{yellow}Manual Tuning Selected."
420 PRINT "Enter Setpoint ({green}RETURN to keep current{white}): ";: INPUT A$
430 IF A$ <> "" THEN SETPOINT = VAL(A$)
440 PRINT "Enter KP value: ";: INPUT A$: IF A$ <> "" THEN KP = VAL(A$)
450 PRINT "Enter KI value: ";: INPUT A$: IF A$ <> "" THEN KI = VAL(A$)
460 PRINT "Enter KD value: ";: INPUT A$: IF A$ <> "" THEN KD = VAL(A$)
470 PRINT "{green}Manual tuning complete.{white}"
480 GOTO 500

500 REM Start Graphical Display
510 OPEN 1,8,15,"U0:9600,N,8,1"

520 REM Main Loop
530 INPUT #1,TEMP$
540 TEMP = VAL(TEMP$)

550 REM Shift Temperature History
560 FOR I = 0 TO 30
570 TEMP_HISTORY(I) = TEMP_HISTORY(I + 1)
580 NEXT I
590 TEMP_HISTORY(31) = TEMP REM Store latest temperature reading

600 PRINT "{home}{cyan}Geothermal Control System"
610 PRINT "{white}Setpoint: {yellow}"; SETPOINT; "°C {white}Current: {cyan}"; TEMP; "°C"

620 REM Draw Temperature Trend Graph
630 PRINT "{white}Temperature Trend:"

640 FOR Y = 30 TO 0 STEP -3
650 PRINT " "; Y; "° |";
660 FOR X = 0 TO 31
670 IF INT(TEMP_HISTORY(X)) = Y THEN PRINT "{red}*";: GOTO 690
680 PRINT "{cyan}-";
690 NEXT X
700 PRINT "{white}|"

710 REM Draw Setpoint Line
720 PRINT "Target |";
730 FOR X = 0 TO 31
740 IF INT(SETPOINT) = INT(TEMP_HISTORY(X)) THEN PRINT "{yellow}|"; ELSE PRINT " ";
750 NEXT X
760 PRINT "|"

770 REM Predict Future Trend Using Last 5 Data Points
780 SUM_X = 0: SUM_Y = 0: SUM_XY = 0: SUM_X2 = 0
790 N = 5
800 FOR I = 1 TO N
810 SUM_X = SUM_X + I
820 SUM_Y = SUM_Y + TEMP_HISTORY(31 - (N - I))
830 SUM_XY = SUM_XY + (I * TEMP_HISTORY(31 - (N - I)))
840 SUM_X2 = SUM_X2 + (I * I)
850 NEXT I

860 SLOPE = ((N * SUM_XY) - (SUM_X * SUM_Y)) / ((N * SUM_X2) - (SUM_X * SUM_X))
870 TREND_TEMP = TEMP + (SLOPE * 5) REM Predict Next 5 Steps

880 PRINT "{cyan}Predicted Temperature (Next 5 Steps): "; TREND_TEMP; "°C"

890 REM Show Predicted Trend on Graph
900 PRINT "Future  |";
910 FOR X = 0 TO 5
920 IF INT(TEMP + (SLOPE * X)) = INT(TEMP) THEN PRINT "{green}/"; ELSE PRINT " ";
930 NEXT X
940 PRINT "|"

950 REM PID Calculations
960 ERROR = SETPOINT - TEMP
970 INTEGRAL = INTEGRAL + ERROR
980 DERIVATIVE = ERROR - LAST_ERROR
990 PID_OUTPUT = (KP * ERROR) + (KI * INTEGRAL) + (KD * DERIVATIVE)

1000 PRINT "{white}PID Output: {cyan}"; PID_OUTPUT
1010 PRINT "KP: {yellow}"; KP; "{white} KI: {yellow}"; KI; "{white} KD: {yellow}"; KD

1020 REM Heating Control
1030 IF PID_OUTPUT > 10 THEN HEATING = 1: GOSUB 7000
1040 IF PID_OUTPUT < -10 THEN HEATING = 0: GOSUB 8000

1050 PRINT "Heating: ";
1060 IF HEATING = 1 THEN PRINT "{green}ON {reverse on}*{reverse off}" ELSE PRINT "{red}OFF {reverse on} {reverse off}"

1070 REM Update Error for Next Loop
1080 LAST_ERROR = ERROR

1090 REM Refresh Every 1 Second
1100 FOR I = 1 TO 1000: NEXT I
1110 GOTO 530

7000 REM Activate Heating Relay
7010 PRINT "{green}Activating Heating..."
7020 POKE 53280, 255
7030 RETURN

8000 REM Deactivate Heating Relay
8010 PRINT "{red}Deactivating Heating..."
8020 POKE 53280, 0
8030 RETURN

9000 REM High Temperature Sound
9010 POKE 54296, 255
9020 POKE 54277, 15
9030 POKE 54279, 15
9040 FOR I = 1 TO 1000: NEXT I
9050 POKE 54279, 0
9060 RETURN

9100 REM Low Temperature Sound
9110 POKE 54296, 100
9120 POKE 54277, 15
9130 POKE 54279, 15
9140 FOR I = 1 TO 1000: NEXT I
9150 POKE 54279, 0
9160 RETURN

9200 REM Selection Sound
9210 POKE 54296, 150
9220 POKE 54277, 15
9230 POKE 54279, 15
9240 FOR I = 1 TO 500: NEXT I
9250 POKE 54279, 0
9260 RETURN

2000 REM Auto-Tuning Routine
2010 PRINT "{cyan}Auto-Tuning Started..."
2020 POKE 53280, 255
2030 FOR I = 1 TO 5000: NEXT I
2040 POKE 53280, 0
2050 PRINT "{white}Measuring response..."
2060 INPUT #1,TEMP$
2070 TEMP = VAL(TEMP$)
2080 T1 = TIMER
2090 REPEAT:
2100 INPUT #1, TEMP$
2110 TEMP = VAL(TEMP$)
2120 UNTIL TEMP > SETPOINT + 2
2130 T2 = TIMER
2140 RESPONSE_TIME = T2 - T1
2150 KP = 0.6 * (SETPOINT / RESPONSE_TIME)
2160 KI = 2 * KP / RESPONSE_TIME
2170 KD = KP * RESPONSE_TIME / 8
2180 PRINT "{green}Auto-Tuning Complete!"
2190 RETURN

REFERENSSIT

[1] ChatGPT keskustelu Venäjän romahduksesta

[2] ChatGPT: Mitä on kvanttifysiikka?